Comparing version 0.1.0 to 0.2.0
@@ -15,7 +15,17 @@ /* | ||
function Breed () {} | ||
function Breed () { | ||
this.instances = {}; | ||
} | ||
function extend (name, isFunction) { | ||
Breed.prototype[name.toUpperCase()] = name.toLowerCase(); | ||
Breed.prototype['is' + name] = isFunction; | ||
Breed.prototype['isnt' + name] = function (arg) { | ||
return !this['is' + name](arg); | ||
}; | ||
} | ||
Breed.prototype = { | ||
get: function (arg) { | ||
is: function (arg) { | ||
var i, type; | ||
@@ -30,34 +40,70 @@ if ((type = typeof arg) !== 'object') { return type; } | ||
return type; | ||
} | ||
}, | ||
}; | ||
register: function () { | ||
var _this = this, | ||
args = Array.prototype.slice.call(arguments); | ||
args.forEach(function (type) { | ||
if (_this.isFunction(type)) { | ||
_this.instances[type.name] = type; | ||
extend(type.name, function (arg) { | ||
return (arg || (arg = {})).constructor.name === type.name || (arg instanceof _this.instances[type.name]); | ||
}); | ||
} | ||
}); | ||
}, | ||
types.forEach(function (type) { | ||
isNaN: function (arg) { | ||
return isNaN(arg); | ||
}, | ||
Breed.prototype[type.toUpperCase()] = type.toLowerCase(); | ||
isntNaN: function (arg) { | ||
return !isNaN(arg); | ||
}, | ||
Breed.prototype['is' + type] = function (arg) { | ||
return {}.toString.call(arg) === '[object ' + type + ']'; | ||
}; | ||
isFinite: function (arg) { | ||
return isFinite(arg); | ||
}, | ||
Breed.prototype['isnt' + type] = function (arg) { | ||
return !this['is' + type](arg); | ||
}; | ||
isntFinite: function (arg) { | ||
return !isFinite(arg); | ||
}, | ||
}); | ||
isInfinite: function (arg) { | ||
return this.isntFinite(arg); | ||
}, | ||
errors.forEach(function (err) { | ||
isJSON: function (arg) { | ||
try { | ||
JSON.parse(arg); | ||
} catch(e) { | ||
return false; | ||
} | ||
return true; | ||
}, | ||
Breed.prototype[err.toUpperCase()] = err.toLowerCase(); | ||
isntJSON: function (arg) { | ||
return !this.isJSON(arg); | ||
}, | ||
Breed.prototype['is' + err] = function (arg) { | ||
return arg.name === err; | ||
}; | ||
JSON: 'json', | ||
Breed.prototype['isnt' + err] = function (arg) { | ||
return !this['is' + err](arg); | ||
}; | ||
NAN: 'nan', | ||
FINITE: 'finite' | ||
}; | ||
types.forEach(function (type) { | ||
extend(type, function (arg) { | ||
return {}.toString.call(arg) === '[object ' + type + ']'; | ||
}); | ||
}); | ||
errors.forEach(function (err) { | ||
extend(err, function (arg) { | ||
return (arg || {}).name === err; | ||
}); | ||
}); | ||
module.exports = exports = new Breed(); |
{ | ||
"name": "breed", | ||
"description": "Breed is a Helper Module which makes working with types and typeof easy as easy as it should be", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"homepage": "https://github.com/hereandnow/node-breed", | ||
@@ -36,3 +36,9 @@ "author": { | ||
}, | ||
"keywords": ["breed", "type", "typeof", "typecheck", "check"] | ||
} | ||
"keywords": [ | ||
"breed", | ||
"type", | ||
"typeof", | ||
"typecheck", | ||
"check" | ||
] | ||
} |
229
README.md
@@ -1,7 +0,48 @@ | ||
# breed | ||
# Breed | ||
Breed is a Helper Module which makes working with working with types and typeof easy as easy as it should be. | ||
Breed is a Helper Module which makes working with types and typeof easy as easy as it should be. | ||
Breed currently supports following types (see more detail in documentation below) | ||
You are even able to extend Breed with [registering your own Classes](#register-your-own-classes-1) and do checks like this: | ||
``` | ||
function Person () {} | ||
breed.register(Person); | ||
breed.isPerson(new Person()); // true | ||
``` | ||
##Table of Contents | ||
- [Getting Started](#getting-started) | ||
- [Documentation](#documentation) | ||
- [Basic Supported Types](#basic-supported-types) | ||
- [is… and isnt…](#is-and-isnt) | ||
- [is](#is) | ||
- [Constants](#constants) | ||
- [Register your own Classes](#register-your-own-classes) | ||
- [Examples](#examples) | ||
- [Basic Supported Types](#basic-supported-types-1) | ||
- [Register your own Classes](#register-your-own-classes-1) | ||
- [Contributing](#contributing) | ||
- [Release History](#release-history) | ||
- [License](#license) | ||
## Getting Started | ||
Install the module with: `npm install breed` | ||
``` | ||
var breed = require('breed'); | ||
breed.isArray([]); // true | ||
breed.isntArray([]); // false | ||
breed.is([]) // 'array' | ||
breed.ARRAY // 'array' | ||
``` | ||
## Documentation | ||
### Basic Supported Types | ||
* Null | ||
@@ -17,2 +58,5 @@ * Object | ||
* Undefined | ||
* JSON | ||
* Finite | ||
* NaN | ||
* Error | ||
@@ -27,134 +71,127 @@ * EvalError | ||
### is… and isnt... | ||
## Getting Started | ||
Install the module with: `npm install breed` | ||
Breed always has for every Type 2 Functions: | ||
``` | ||
var breed = require('breed'); | ||
breed.isArray([]); // true | ||
breed.isntArray([]); // false | ||
breed.get([]) // 'array' | ||
breed.ARRAY // 'array' | ||
breed.is... | ||
breed.isnt... | ||
``` | ||
for example: | ||
``` | ||
breed.isFunction() | ||
breed.isntFunction() | ||
## Documentation | ||
// or | ||
###General-purpose constructors | ||
breed.isUndefined() | ||
breed.isntUndefined() | ||
###### Array | ||
// or | ||
breed.isJSON() | ||
breed.isntJSON() | ||
// … and so on | ||
``` | ||
breed.isArray([]) // true | ||
breed.isntArray([]) // false | ||
breed.ARRAY // 'array' | ||
``` | ||
###### Boolean | ||
There is 1 exceptional Case where isInfinite is just a little bit nicer than isntFinite, so both Cases are supported: | ||
``` | ||
breed.isBoolean(true) // true | ||
breed.isntBoolean(true) // false | ||
breed.BOOLEAN // 'boolean' | ||
breed.isntFinite() === breed.isInfinite() | ||
``` | ||
###### Date | ||
### is | ||
``` | ||
breed.isDate(new Date()) // true | ||
breed.isntDate(new Date()) // false | ||
breed.DATE // 'date' | ||
``` | ||
###### Function | ||
You are also able to check via breed's is-Function which will always return the type as a String: | ||
``` | ||
breed.isFunction(function () {}) // true | ||
breed.isntFunction(function () {}) // false | ||
breed.FUNCTION // 'function' | ||
breed.is([]) // 'array' | ||
breed.is(1) // 'number' | ||
breed.is({}) // 'object' | ||
... | ||
``` | ||
###### Number | ||
### Constants | ||
Every type has its 'Constant' Variable (always uppercase) whos value is always lowercase: | ||
``` | ||
breed.isNumber(1) // true | ||
breed.isntNumber(1) // false | ||
breed.ARRAY // 'array' | ||
breed.NUMBER // 'number' | ||
breed.OBJECT // 'object' | ||
... | ||
``` | ||
###### Object | ||
So you can combine it with the is-Function: | ||
``` | ||
breed.isObject({}) // true | ||
breed.isntObject({}) // false | ||
breed.OBJECT // 'object' | ||
breed.is([]) === breed.ARRAY // true | ||
breed.is(1) === breed.NUMBER // true | ||
breed.is({}) === breed.OBJECT // true | ||
... | ||
``` | ||
###### RegExp | ||
``` | ||
breed.isRegExp(/1/) // true | ||
breed.isntRegExp(/1/) // false | ||
breed.REGEXP // 'regexp' | ||
``` | ||
###### String | ||
### Register your own Classes | ||
Just add as much Classes as you want: | ||
``` | ||
breed.isString('') // true | ||
breed.isntString('') // false | ||
breed.STRING // 'string' | ||
breed.register(*functions) | ||
``` | ||
###Error Constructors | ||
The best about that is, that breed.register even supports Inheritance (see the example: [Register your own Classes](#register-your-own-classes-1)) | ||
* Error | ||
* EvalError | ||
* RangeError | ||
* ReferenceError | ||
* SyntaxError | ||
* TypeError | ||
* URIError | ||
###### Examples | ||
## Examples | ||
### Basic Supported Types | ||
``` | ||
breed.isError(new Error()) // true | ||
breed.isError(new TypeError()) // true | ||
breed.isError(new EvalError()) // true | ||
// some Functions | ||
breed.isObject({}) // true | ||
breed.isFunction(function () {}) // true | ||
breed.isntDate(new Date()) // false | ||
breed.isntUndefined() // false | ||
// errors all have the super-constructor Error | ||
breed.isRangeError(new RangeError()) // true | ||
breed.isError(new RangeError()) // true | ||
breed.isntError(new Error()) // false | ||
breed.isRangeError(new RangeError()) // true | ||
// some constants | ||
breed.ERROR // 'error' | ||
breed.RANGEERROR // 'rangeerror' | ||
breed.TYPEERROR // 'typeerror' | ||
breed.DATE // 'date' | ||
breed.ARRAY // 'array' | ||
``` | ||
###Other | ||
###### Null | ||
### Register your own Classes | ||
``` | ||
breed.isNull(null) // true | ||
breed.isntNull(null) // false | ||
breed.NULL // 'null' | ||
``` | ||
###### Undefined | ||
var breed = require('breed'); | ||
var util = require('util'); | ||
``` | ||
breed.isUndefined() // true | ||
breed.isntUndefined() // false | ||
breed.UNDEFINED // 'undefined' | ||
``` | ||
// your code creates some Functions | ||
function Person () {} | ||
function Child () {} | ||
function Mom () {} | ||
util.inherits(Child, Person); | ||
util.inherits(Mom, Person); | ||
## Examples | ||
// register your classes | ||
breed.register(Person, Child, Mom); | ||
``` | ||
var breed = require('breed'); | ||
// 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 | ||
if (breed.isRegExp(/1/) ) { | ||
// do your Stuff | ||
} | ||
// or you could use the constants | ||
if (breed.get(/1/) === breed.REGEXP) { | ||
// do your Stuff | ||
} | ||
// constants | ||
breed.PERSON // 'person' | ||
breed.CHILD // 'child' | ||
breed.MOM // 'mom' | ||
``` | ||
@@ -165,12 +202,16 @@ | ||
``` | ||
$ grunt default | ||
``` | ||
- Available Tasks: | ||
- jshint | ||
- nodeunit | ||
- default (jshint + nodeunit) | ||
## TODOs | ||
* Support Typed array constructors | ||
* isJSON, isNaN | ||
## Release History | ||
- 0.2.0 Second Release | ||
- Implement JSOn, NaN, Finite | ||
- author breed.register() | ||
- rename breed.get() to breed.is() | ||
* 0.1.0 Initial Release | ||
@@ -177,0 +218,0 @@ |
32178
20
593
219