0.1.2 • Published 10 years ago
node-typed v0.1.2
node-typed
A simplistic node type checking library
A simple library I developed for the Evercam.io Node.js wrapper.
Installation
npm install node-typed
Usage
type(type_format, value, [default_value])
var type = require('node-typed')
// Single type
type('age:number', 23);
// Multiple options
type('ids:string,[string]', ['node']);
// Default values
type('favColor:string', undefined, 'green');
// Based on constructor.name
function Car() {};
var tesla = new Car;
type('manufacter:car', tesla);
// No name specfied
type('number', 1);
// Call on any object (null or undefined aren't objects)
var obj = "hello";
obj.type('string');
// Return boolean instead of throwing an exception, with '?'
"node".type('number?');
// Check for null or undefined values
type('undefined,null?', null);
// Throws TypeError on errors
test('i:number', 'string');
Test
npm test
Type format
In Extended Backus–Naur Form, where constructor is any valid JavaScript constructor name and name is any valid JavaScript variable name
format = [name, ":"], type, {",", type}, ["?"];
type = "[", constructor, "]" | constructor;
Todo list
Extend Object to eliminate thevalue
parameter- Add a method for testing types, without throwing exceptions
Changelog
0.0.0
- Initial release
0.0.1
Object.type
added- Type format name is not optional
0.1.0
- Extended type format to allow disabling throwing TypeErrors
0.1.1
- Object.type() now has
enumerable
set to false
0.1.2
- null and undefined are special cases and can be type checked