Skip to content

Instantly share code, notes, and snippets.

@deadkff01
Created November 15, 2019 21:44
Show Gist options
  • Save deadkff01/b26ce5063408904490da536399797ed9 to your computer and use it in GitHub Desktop.
Save deadkff01/b26ce5063408904490da536399797ed9 to your computer and use it in GitHub Desktop.
Using an object as a switch case with javascript
// obj literals with javascript
const actions = {
'sayHello': name => `hello my friend ${name}`,
'typeOf': value => typeof value,
'onlyString': 'simple string',
'default': null
}
const reducer = (action, payload) => {
if(!actions[action]) return actions['default']
if(typeof actions[action] === 'function')
return actions[action](payload)
return actions[action]
}
reducer('sayHello', 'deadkff01')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment