Skip to content

Instantly share code, notes, and snippets.

@Prinzhorn Prinzhorn/switch.js forked from substack/switch.js
Created Mar 28, 2012

Embed
What would you like to do?
regex fall-throughs using a switch(true), inspired by @AvianFlu
substack : ~ $ node switch.js bleep blah
blah
substack : ~ $ node switch.js for the lose
lose
substack : ~ $ node switch.js for the win
win
substack : ~ $ node switch.js for the fail
fail
substack : ~ $
var m, s = process.argv.slice(2).join(' ');
switch (false) {
case !(m = s.match(/fail/)) :
console.log('fail');
break;
case !(m = s.match(/for the (\S+)/)) :
console.log(m[1]);
break;
case !(m = s.match(/blah/)) :
console.log('blah');
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.