JSTypes is a small JavaScript library that does two things:
- Check if variables are of certain types (numbers, strings, undefined)
- Reliably create certain values (
undefined,NaN,Infinity)
Note that you probably shouldn't use JSTypes unless you need it -- other popular libraries like jQuery and Underscore already have many of these methods. If you aren't using these libraries, then JSTypes might be of help!
Examples:
var x = 1;
var y = new Number(22);
x instanceof Number; // false; not what we want!
JSTypes.isNumber(x); // true
JSTypes.isNumber(y); // truevar x;
JSTypes.isUndefined(x); // true
undefined = 10; // this is terrible code, but it might get written!
JSTypes.isUndefined(x); // still truevar x = 5;
undefined = 10;
x = undefined; // bad: x = 10
x = JSTypes.makeUndefined(); // good: x is now really undefined
JSTypes.isUndefined(x); // trueI recommend (and do) move this library into your code's namespace if you're writing a library. Just replace all occurrences of JSTypes with your library's namespace.
For licensing info, see LICENSE.txt.
Here's everything in the API.
JSTypes.isNumber(toCheck)returns true if something is a numberJSTypes.isInteger(toCheck)returns true if something is an integerJSTypes.isString(toCheck)returns true if something is a stringJSTypes.isBoolean(toCheck)returns true if something is a booleanJSTypes.isArray(toCheck)returns true if something is an arrayJSTypes.isDefined(toCheck)returns true if something is defined (will return false forundefined, ornull, orNaN)JSTypes.isUndefined(toCheck)returns true if something isundefinedJSTypes.isNAN(toCheck)returns true if something isNaNJSTypes.isInfinite(toCheck)returns true of something is positive or negative infinityJSTypes.makeUndefined()always returnsundefined, even if it's stupidly redefinedJSTypes.makeNaN()always returnsNaN, even if it's stupidly redefinedJSTypes.makeInfinity()always returnsInfinity, even if it's stupidly redefined