Skip to content

Instantly share code, notes, and snippets.

@constantology
Last active October 13, 2015 15:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save constantology/4217094 to your computer and use it in GitHub Desktop.
Save constantology/4217094 to your computer and use it in GitHub Desktop.
using functions to `case` match in `switch` statements

the main caveat is that each case function will be executed until a match is found, however, this would not be any different from doing the same thing using if/else.

note: i am not advocating this method, it's just something i've never seen done before and thought it was interesting enough to share.

// yes there are more key codes than this, it's just an example though, so chillax...
;!function() {
// 112 == F1, 123 == F12
function f_key( kc ) {
return ( kc >= 112 && kc <= 123 ) ? kc : false;
}
// 65 == 'A', 90 == 'Z', 32 == ' ' <= space character
function letter( kc ) {
return ( kc >= 65 && kc <= 90 ) || kc == 32 ? kc : false;
}
// pg up, pg down, end, home, left, up, right, down, tab
function navigation( kc ) {
return ( kc >= 33 && kc <= 40 ) || kc == 9 ? kc : false;
}
// 48 == 0, 57 == 9 | NUMBER PAD: 96 == 0, 105 == 9
function number( kc ) {
return ( kc >= 48 && kc <= 57 ) || ( kc >= 96 && kc <= 105 ) ? kc : false;
}
document.addEventListener( 'keydown', function( evt ) {
var kc = evt.keyCode, msg;
switch ( kc ) {
case letter( kc ) : msg = 'a letter'; break;
case number( kc ) : msg = 'a number'; break;
case navigation( kc ) : msg = 'a navigation'; break;
case f_key( kc ) : msg = 'a function key'; break;
default : msg = 'some other key'; break;
}
console.log( msg + ' was pressed, not telling which one though.' );
}, false );
}();
;!function() {
// it's important to return the actual value in the `switch` so we get the correct `case` match or the `switch` will only match `default`
function lt( val, bound ) { return val <= bound ? val : false; }
function within( val, lower_bound, upper_bound ) { return val >= lower_bound && val <= upper_bound ? val : false; }
var msg, val = 17;
switch ( val ) {
case lt( val, 5 ) : msg = ' is less than or equal to 5.'; break;
case lt( val, 10 ) : msg = ' is less than or equal to 10.'; break;
case lt( val, 15 ) : msg = ' is less than or equal to 15.'; break;
case lt( val, 20 ) : msg = ' is less than or equal to 20.'; break;
case lt( val, 25 ) : msg = ' is less than or equal to 25.'; break;
default : msg = ' is greater than 25.'; break;
}
console.log( val, msg );
switch ( val ) {
case within( val, 0, 5 ) : msg = ' is within the range of 0-5.'; break;
case within( val, 6, 10 ) : msg = ' is within the range of 6-10.'; break;
case within( val, 11, 15 ) : msg = ' is within the range of 11-15.'; break;
case within( val, 16, 20 ) : msg = ' is within the range of 16-20.'; break;
case within( val, 21, 25 ) : msg = ' is within the range of 21-25.'; break;
default : msg = ' is not in a valid range.'; break;
}
console.log( val, msg );
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment