Skip to content

Instantly share code, notes, and snippets.

@callmehiphop
Last active August 29, 2015 14:01
Show Gist options
  • Save callmehiphop/389a4fb0712976d0af0b to your computer and use it in GitHub Desktop.
Save callmehiphop/389a4fb0712976d0af0b to your computer and use it in GitHub Desktop.
death to all the switches!
function $switch (test, cases) {
function doit (thing) {
return (cases[thing] || cases['default'] || function () {})();
}
if (test && cases) {
return doit(test);
}
cases = test;
return doit;
}
var cb = $switch({
one: function () {
console.log('uno!');
},
default: function () {
console.log('wat');
}
});
cb('one'); // uno!
cb('two'); // wat
// OORORRORO
var unexpectedThing = 'one';
$switch(unexpectedThing, {
one: function () {
console.log('uno!');
},
default: function () {
console.log('wat');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment