Skip to content

Instantly share code, notes, and snippets.

@juntalis
Created September 26, 2012 15:00
Show Gist options
  • Save juntalis/3788547 to your computer and use it in GitHub Desktop.
Save juntalis/3788547 to your computer and use it in GitHub Desktop.
Utility one-liners for checking types, values, etc
/* Utility one-liners for checking types, values, etc */
var prims = ['string', 'number', 'boolean', 'function', 'array' ],
typof = function(x) { var s, t = typeof(x); return t === 'object' ? (s = Object.prototype.toString.call(x)).substring(8, s.length - 1).toLowerCase() : t; },
undef = function(x) { return typeof(x) === 'undefined' || x == undefined; },
defined = function(x) { return !undef(x); },
noVal = function(x) { return undef(x) || typof(x) === 'null' || x == null || isNaN(x); },
hasVal = function(x) { return !noVal(x); },
isStr = function(x) { return typof(x) === prims[0]; },
isNum = function(x) { return typof(x) === prims[1]; },
isBool = function(x) { return typof(x) === prims[2]; },
isFunc = function(x) { return typof(x) === prims[3]; },
isArr = function(x) { return typof(x) === prims[4]; },
isObj = function(x) { return prims.indexOf(typof(x)) === -1; },
empty = function(x) { return !isStr(x) || x.length === 0; },
istrcmp = function(x,y) { return !empty(x) && !empty(y) && (x.toLowerCase() === y.toLowerCase()); };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment