Breed is a Helper Module which makes working with types and typeof as easy as it should be.
Install the module with: npm install breed
And include it in your code
var breed = require('breed');
So you can use it (eg. do something with any error which is not a TypeError)
if(breed.isError(e) && breed.isntTypeError(e)) {
// your code here
}
Breed always has 2 Functions for every Type
breed.is...
breed.isnt...
breed.isObject({}) // true
breed.isFunction(function () {}) // true
breed.isntDate(new Date()) // false
breed.isntUndefined() // false
There is 1 exceptional Case where isInfinite is just a little bit nicer to read than isntFinite, so both Cases are supported
breed.isntFinite() === breed.isInfinite()
You are also able to check via Breed's is-Function which will always return the type as a String
breed.is([]) // 'array'
breed.is(1) // 'number'
breed.is({}) // 'object'
...
Every type has its 'Constant' Variable (always uppercase) whos value is always lowercase
breed.ARRAY // 'array'
breed.NUMBER // 'number'
breed.OBJECT // 'object'
breed.REGEXP // 'regexp'
// combined with the is-Function
breed.is([]) === breed.ARRAY // true
breed.is(1) === breed.NUMBER // true
breed.is({}) === breed.OBJECT // true
breed.is(/1/) === breed.REGEXP // true
Just add as much Classes as you want within 1 Register-Call
breed.register(*functions)
The best about that is, that breed.register even supports Inheritance!
var breed = require('breed');
var util = require('util');
// your code creates some Functions (including inheritance)
function Person () {}
function Child () {}
function Mom () {}
util.inherits(Child, Person);
util.inherits(Mom, Person);
// register your classes
breed.register(Person, Child, Mom);
// do typechecking
breed.isPerson(new Person()) // true
breed.isChild(new Child()) // true
breed.isPerson(new Child()) // true
breed.isMom(new Mom()) // true
breed.isPerson(new Mom()) // true
breed.isntChild(new Mom()) // true
// constants
breed.PERSON // 'person'
breed.CHILD // 'child'
breed.MOM // 'mom'
Copyright (c) 2013 Bastian Behrens Licensed under the MIT license.