Text script for US State abbreviations

Had a need to parse out full State names from their 2 character abbreviations. This is what I came up with and it appears to work well. The JSON array is from this GitHub repo.

In the form create a new Standard script (text type) and copy the code below into it. Change the record field name to match the one you need to expand into its full name.

//read in the state field from the record set
var StateAbbr = record.fields["State"];
//create JSON array of state abbreviations and names
var states = {
	"AL": "Alabama",
	"AK": "Alaska",
	"AS": "American Samoa",
	"AZ": "Arizona",
	"AR": "Arkansas",
	"CA": "California",
	"CO": "Colorado",
	"CT": "Connecticut",
	"DE": "Delaware",
	"DC": "District Of Columbia",
	"FM": "Federated States Of Micronesia",
	"FL": "Florida",
	"GA": "Georgia",
	"GU": "Guam",
	"HI": "Hawaii",
	"ID": "Idaho",
	"IL": "Illinois",
	"IN": "Indiana",
	"IA": "Iowa",
	"KS": "Kansas",
	"KY": "Kentucky",
	"LA": "Louisiana",
	"ME": "Maine",
	"MH": "Marshall Islands",
	"MD": "Maryland",
	"MA": "Massachusetts",
	"MI": "Michigan",
	"MN": "Minnesota",
	"MS": "Mississippi",
	"MO": "Missouri",
	"MT": "Montana",
	"NE": "Nebraska",
	"NV": "Nevada",
	"NH": "New Hampshire",
	"NJ": "New Jersey",
	"NM": "New Mexico",
	"NY": "New York",
	"NC": "North Carolina",
	"ND": "North Dakota",
	"MP": "Northern Mariana Islands",
	"OH": "Ohio",
	"OK": "Oklahoma",
	"OR": "Oregon",
	"PW": "Palau",
	"PA": "Pennsylvania",
	"PR": "Puerto Rico",
	"RI": "Rhode Island",
	"SC": "South Carolina",
	"SD": "South Dakota",
	"TN": "Tennessee",
	"TX": "Texas",
	"UT": "Utah",
	"VT": "Vermont",
	"VI": "Virgin Islands",
	"VA": "Virginia",
	"WA": "Washington",
	"WV": "West Virginia",
	"WI": "Wisconsin",
	"WY": "Wyoming"
};
//return full state name based on it's abbreviation
results.html(states[StateAbbr]);

If you’ve got a better way to approach this let me know!

A further tweak, if needed, would be to format the result to uppercase. To do that just replace the final line in the script with this one:

results.html(formatter.upperCase(states[StateAbbr]));

I like the approach but conceptually, I’d do this in the Data Mapper. By the time data reaches the Template, it should (“should” being purely my own opinion) be completely ready for display with minimal formatting or scripting.

@TDGreer: that’s a good point and one that I usually try to make with anyone using Connect. There are exceptions, however, such as when you want to dynamically translate strings of text on your template. In that case, you’re better off extracting a language-agnostic code such as a State abbreviation so that the template can then pick the proper translation (NC = North-Carolina | Caroline du nord).

But unless you have a specific need for that kind of stuff, you’re right that one should always try to make the data as template-ready as possible right from the start.

Thanks @TDGreer and @Phil for the information.

So if I were to tweak this to run on the Data Mapper side, I’d just add this code as a post function on the “State” field?

How would I pass the extract step results into the states array function? I see the following information on the post function, but I’m missing something about how I’d reference it for the array.

More of a learning exercise than anything, but I appreciate it!

  • Convert the above code to a function named getStateName that receives as its sole parameter a State abbreviation, and then save that code as a Javascript file somewhere on your drive
  • In your data mapping configuration’s Settings panel, add that JS file to the list of External JS Libraries
  • In your extraction step, change the field’s Based on option from Location to Javascript
  • The expression field will automatically be set to the proper extraction command. Something like
    data.extract('./RECORD[1]/State[1]')
  • Modify the code so that command gets passed as the parameter to your getStateName function. Something like getStateName( data.extract('./RECORD[1]/State[1]') );

Of course, the exact syntax of the extraction command will vary according to the type of data you are processing in the DataMapper, but the idea is simply to pass the state abbreviation as the parameter to the function you created in the external JS library.