Skip to content

Instantly share code, notes, and snippets.

@danielkellyio
Last active June 26, 2019 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielkellyio/b1d08ec9e0388199593b825edd33358c to your computer and use it in GitHub Desktop.
Save danielkellyio/b1d08ec9e0388199593b825edd33358c to your computer and use it in GitHub Desktop.
The best way I've found to to set a variable based on multiple conditions
//In helper file with global helper functions
function switchMap(before_value, map={}, default_value=false){
return Object.keys(map).includes(before_value) ? map[before_value] : default_value;
}
//In code where condition is needed
var friendly_timezone = switchMap(tz_timezone, {
'America/New_York': 'Eastern',
'America/Chicago': 'Central',
'America/Denver': 'Mountain',
'America/Los_Angeles': 'Pacific',
}, "No Timezone Specified");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment