Last active
February 18, 2019 00:23
-
-
Save azder/8432068 to your computer and use it in GitHub Desktop.
No more switch statements in JS. Run the code here: http://jsfiddle.net/azder/tD2B8/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var s, switcher; | |
switcher = (function () { | |
// ALWAYS | |
'use strict'; | |
var elvis = function (value, dfault) { | |
return (null === value || void 0 === value ? dfault : value); | |
}; | |
return function (dfault, map) { | |
map = elvis(map, {}); | |
return function (key) { | |
return elvis(map[key], dfault); | |
}; | |
}; | |
}()); | |
s = switcher('<unknown>', { | |
1: 'a', | |
2: 'b', | |
3: 'c' | |
}); | |
console.log(s(1), s(2), s(3), s(4), s()); | |
s = switcher(); | |
console.log(s(1), s(2), s(3), s(4), s()); | |
s = switcher('constant'); | |
console.log(s(1), s(2), s(3), s(4), s()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment