Skip to content

Instantly share code, notes, and snippets.

@miguelludert
Created June 18, 2014 15:58
Show Gist options
  • Save miguelludert/1d8107d7e3be62e92dfa to your computer and use it in GitHub Desktop.
Save miguelludert/1d8107d7e3be62e92dfa to your computer and use it in GitHub Desktop.
format address function
function formatAddress(obj, context) {
// this allows you to pass in a context and keys instead of values
if (arguments.length >= 2) {
if (context) {
obj = {
address: _.map(obj.address, function (item) {
return context[item];
}),
city: context[obj.city],
state: context[obj.state],
zip5: context[obj.zip5],
zip4: context[obj.zip4]
}
} else {
// if there is no context, but it's been passed in, then we have nothing to display.
return "";
}
}
var br = '<br/>';
var result = [];
if (_.isArray(obj.address)) {
result.push(_.compact(obj.address).join(br));
} else {
result.push(obj.address);
}
result.push([br,obj.city,', ',obj.state,' ',obj.zip5]);
if(obj.zip4){
result.push(['-', obj.zip4]);
}
return _.flatten(result).join('');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment