Skip to content

Instantly share code, notes, and snippets.

@matthewp
Last active December 28, 2015 15:19
Show Gist options
  • Save matthewp/7520865 to your computer and use it in GitHub Desktop.
Save matthewp/7520865 to your computer and use it in GitHub Desktop.
This uses the power of Underscore/Lodash to do a simple swap, assuming we have an array of objects each containing two fields, we want the other field.
var states = [
{'name':'Alabama', 'abbrev':'AL'},
{'name':'Alaska', 'abbrev':'AK'},
{'name':'Arizona', 'abbrev':'AZ'},
{'name':'Arkansas', 'abbrev':'AR'},
{'name':'California', 'abbrev':'CA'},
{'name':'Colorado', 'abbrev':'CO'},
{'name':'Connecticut', 'abbrev':'CT'},
{'name':'Delaware', 'abbrev':'DE'},
{'name':'Florida', 'abbrev':'FL'},
{'name':'Georgia', 'abbrev':'GA'},
{'name':'Hawaii', 'abbrev':'HI'},
{'name':'Idaho', 'abbrev':'ID'},
{'name':'Illinois', 'abbrev':'IL'},
{'name':'Indiana', 'abbrev':'IN'},
{'name':'Iowa', 'abbrev':'IA'},
{'name':'Kansas', 'abbrev':'KS'},
{'name':'Kentucky', 'abbrev':'KY'},
{'name':'Louisiana', 'abbrev':'LA'},
{'name':'Maine', 'abbrev':'ME'},
{'name':'Maryland', 'abbrev':'MD'},
{'name':'Massachusetts', 'abbrev':'MA'},
{'name':'Michigan', 'abbrev':'MI'},
{'name':'Minnesota', 'abbrev':'MN'},
{'name':'Mississippi', 'abbrev':'MS'},
{'name':'Missouri', 'abbrev':'MO'},
{'name':'Montana', 'abbrev':'MT'},
{'name':'Nebraska', 'abbrev':'NE'},
{'name':'Nevada', 'abbrev':'NV'},
{'name':'New Hampshire', 'abbrev':'NH'},
{'name':'New Jersey', 'abbrev':'NJ'},
{'name':'New Mexico', 'abbrev':'NM'},
{'name':'New York', 'abbrev':'NY'},
{'name':'North Carolina', 'abbrev':'NC'},
{'name':'North Dakota', 'abbrev':'ND'},
{'name':'Ohio', 'abbrev':'OH'},
{'name':'Oklahoma', 'abbrev':'OK'},
{'name':'Oregon', 'abbrev':'OR'},
{'name':'Pennsylvania', 'abbrev':'PA'},
{'name':'Rhode Island', 'abbrev':'RI'},
{'name':'South Carolina', 'abbrev':'SC'},
{'name':'South Dakota', 'abbrev':'SD'},
{'name':'Tennessee', 'abbrev':'TN'},
{'name':'Texas', 'abbrev':'TX'},
{'name':'Utah', 'abbrev':'UT'},
{'name':'Vermont', 'abbrev':'VT'},
{'name':'Virginia', 'abbrev':'VA'},
{'name':'Washington', 'abbrev':'WA'},
{'name':'West Virginia', 'abbrev':'WV'},
{'name':'Wisconsin', 'abbrev':'WI'},
{'name':'Wyoming', 'abbrev':'WY'}
];
/*
* The easy way, this simply finds the other field in the array
* that isn't to.
**/
function convertState(name, to) {
var from = _.without(['name', 'abbrev'], to)[0];
var exp = new RegExp('^' + name + '$', 'i');
var selected = _.find(states, function(st) {
return exp.test(st[from]);
});
return selected[to];
}
/*
* The better way! Since we have a pattern of swapping one field for
* another we can make it reusable. Whenever we have an array of objects
* containing two fields, we can use this function to create a new function
* which will swap them around for us.
**/
function makeSwappable(array, fields) {
return function(name, to) {
var from = _.without(array, to)[0];
var exp = new RegExp('^' + name + '$', 'i');
var selected = _.find(array, function(item) {
return exp.test(item[from]);
});
return selected[to];
};
}
var convertState = makeSwappable(states, ['name', 'abbrev']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment