Sensible / unsurprising JavaScript type detection and comparison using a 
combination of ({}).toString and constructors.

var Type = require('type-of-is');

Type.of(obj);              // returns constructor type of an object
Type.string(obj);          // provides type as String
Type.is(obj, type);        // tests whether obj is of type (constructor or 
String)
Type.instance(obj, type);  // wrapper of "obj instanceof type"

// following all evaluate to true
Type.is({ x : 2 }, Object);
Type.is(function () {}, Function);
Type.is([1, 2, 3], Array);
Type.is("barf", String);
Type.is(true, Boolean);
Type.is(10, Number);
Type.is(new Date(), Date);
Type.is(/abc/, RegExp);
Type.is(new Error("barf!"), Error);

function Person (name) {
  this.name = name;
}
var ralph = new Person('Ralph');

Type.of(ralph);                // [Function: Person]
Type.is(ralph, Person);        // true
Type.is(ralph, Object);        // false
Type.instance(ralph, Person);  // true
Type.instance(ralph, Object);  // true


https://github.com/stephenhandley/type-of-is

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to