javascript / K345.is()
Description
K345.is() compares a value with one or multiple type definitions and returns true if the value's type matches one definition.
The comparsion of K345.is() is strict and tries to return the best matching type result only. (for example, K345.is() returns false for a DOM node with cmptype "object", although a node is an object.
Requirements
K345.is() requires some JavaScript 1.6+ array methods:
- Array.prototype.map()
- Array.prototype.some()
- Array.prototype.every()
For old browsers (like IE8 and lower) a polyfill is needed.
Code & Downloads
- download source (minified)
- download source
- download array methods polyfill (minified)contains:
• Array.isArray
• Array.prototype.every
• Array.prototype.filter
• Array.prototype.forEach
• Array.prototype.indexOf
• Array.prototype.lastIndexOf
• Array.prototype.map
• Array.prototype.reduce
• Array.prototype.reduceRight
• Array.prototype.some - view source
Usage:
Boolean result = K345.is(Mixed testvalue, String cmptype …);
Boolean result = K345.is.call([String cmptype …], Mixed testvalue);
where each cmptype is one of these strings:
"string" | a string primitive | "number" | a number primitive |
---|---|---|---|
"object" | a [custom] object | "null" | value null |
"nan" | value NaN | "function" | a function |
"undefined" | value undefined | "boolean" | a boolean primitive |
"array" | an Array | "node" | a DOM node element |
"text" | a DOM textnode | "list" | a DOM HTMLCollection or NodeList |
"date" | a date object | "regexp" | a RexExp object |
"int" | an integer (implies number) | "float" | a float (implies number) |
"infinity" | value Infinity |
cmptype is case insensitive.
Some additional values for cmptype are accepted, e.g.
K345.is(Math, 'math'); // returns true if you're lucky
but it is not recommended to test host objects (built-ins) against single specific types.
Examples:
var abc = 'Hello!';
console.log(K345.is(abc, 'string')); // true
var abc = [1,2,3];
console.log(K345.is(abc, 'array')); // true
var abc = null;
console.log(K345.is(abc, 'object')); // false
It is possible to test multiple types:
if (K345.is(myVar, 'string', 'number', 'array')) {
/* … */
}
is similar to
if (typeof myVar === 'string' || typeof myVar === 'number' || Array.isArray(myVar)) {
/* … */
}
Shortcuts
Any kind of shortcut can be created using Function.prototype bind():
var isObject = K345.is.bind(['object']);
var d = {};
console.log(isObject(d)); // true
var isNullOrUndef = K345.is.bind(['undefined', 'null']);
var d;
console.log(isNullOrUndef(d)); // true
var e = null;
console.log(isNullOrUndef(e)); // true
First parameter of .bind() MUST be an array of cmptype strings
Tests:
dynamic test table
table with test results goes here. javascript is required.
There should be just one sign per row except some numeric values, which should be both number and either int or float (slightly darker blue).
Legend | ||
---|---|---|
Function returned true. | Okay | |
Function returned false. | Okay | |
Function returned true; should have been false. | Error | |
function returned false; should have been true. | Error |