node-breed

Breed is a Helper Module which makes working with types and typeof as easy as it should be.

Getting Started

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
}

Basic Supported Types

is… and isnt...

Breed always has 2 Functions for every Type

breed.is...
breed.isnt...

Examples

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()

is

You are also able to check via Breed's is-Function which will always return the type as a String

Examples

breed.is([]) // 'array'
breed.is(1) // 'number'
breed.is({}) // 'object'
...

Constants

Every type has its 'Constant' Variable (always uppercase) whos value is always lowercase

Examples

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

Register your own Classes

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!

Examples

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'

License

Copyright (c) 2013 Bastian Behrens Licensed under the MIT license.