Last active
August 29, 2015 14:01
-
-
Save callmehiphop/389a4fb0712976d0af0b to your computer and use it in GitHub Desktop.
death to all the switches!
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
function $switch (test, cases) { | |
function doit (thing) { | |
return (cases[thing] || cases['default'] || function () {})(); | |
} | |
if (test && cases) { | |
return doit(test); | |
} | |
cases = test; | |
return doit; | |
} |
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 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