I created this simple little javascript state matching library and I was amazed at how simple it turned out being, so I had to post it.

I’m leveraging a javascript object as a Key/Value Pair lookup.

stateMatcher.js


var stateMatcher = {
    Names: { "":"", "ALASKA": "AK", "ALABAMA": "AL", "ARKANSAS": "AR", "ARIZONA": "AZ", "CALIFORNIA": "CA", "COLORADO": "CO", "CONNECTICUT": "CT", "DISTRICT OF COLUMBIA": "DC", "DELAWARE": "DE", "FLORIDA": "FL", "GEORGIA": "GA", "GUAM": "GU", "HAWAII": "HI", "IOWA": "IA", "IDAHO": "ID", "ILLINOIS": "IL", "INDIANA": "IN", "KANSAS": "KS", "KENTUCKY": "KY", "LOUISIANA": "LA", "MASSACHUSETTS": "MA", "MARYLAND": "MD", "MAINE": "ME", "MICHIGAN": "MI", "MINNESOTA": "MN", "MISSOURI": "MO", "MISSISSIPPI": "MS", "MONTANA": "MT", "NORTH CAROLINA": "NC", "NORTH DAKOTA": "ND", "NEBRASKA": "NE", "NEW HAMPSHIRE": "NH", "NEW JERSEY": "NJ", "NEW MEXICO": "NM", "NEVADA": "NV", "NEW YORK": "NY", "OHIO": "OH", "OKLAHOMA": "OK", "OREGON": "OR", "PENNSYLVANIA": "PA", "PUERTO RICO": "PR", "RHODE ISLAND": "RI", "SOUTH CAROLINA": "SC", "SOUTH DAKOTA": "SD", "TENNESSEE": "TN", "TEXAS": "TX", "UTAH": "UT", "VIRGINIA": "VA", "VERMONT": "VT", "WASHINGTON": "WA", "WISCONSIN": "WI", "WEST VIRGINIA": "WV", "WYOMING": "WY" },
    Abbreviations: { "": "", "AK": "Alaska", "AL": "Alabama", "AR": "Arkansas", "AZ": "Arizona", "CA": "California", "CO": "Colorado", "CT": "Connecticut", "DC": "DistrictofColumbia", "DE": "Delaware", "FL": "Florida", "GA": "Georgia", "GU": "Guam", "HI": "Hawaii", "IA": "Iowa", "ID": "Idaho", "IL": "Illinois", "IN": "Indiana", "KS": "Kansas", "KY": "Kentucky", "LA": "Louisiana", "MA": "Massachusetts", "MD": "Maryland", "ME": "Maine", "MI": "Michigan", "MN": "Minnesota", "MO": "Missouri", "MS": "Mississippi", "MT": "Montana", "NC": "NorthCarolina", "ND": "NorthDakota", "NE": "Nebraska", "NH": "NewHampshire", "NJ": "NewJersey", "NM": "NewMexico", "NV": "Nevada", "NY": "NewYork", "OH": "Ohio", "OK": "Oklahoma", "OR": "Oregon", "PA": "Pennsylvania", "PR": "PuertoRico", "RI": "RhodeIsland", "SC": "SouthCarolina", "SD": "SouthDakota", "TN": "Tennessee", "TX": "Texas", "UT": "Utah", "VA": "Virginia", "VT": "Vermont", "WA": "Washington", "WI": "Wisconsin", "WV": "WestVirginia", "WY": "Wyoming" }
};

Usage


var longName = stateMatcher.Abbreviations["NY"];
// or
var abbr = stateMatcher.Names["New York".toUpperCase()];

I just love how simple it is. I feel like I’m calling a function, but I’m not.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>