typeutil

Typify your JavaScript functions.

npm install typeutil
40 downloads in the last week
67 downloads in the last month

typeutil Build Status Coverage Status Code Climate NPM version

Typify your JavaScript functions.

browser support

Installation

$ npm install typeutil --save

Usage

Node.js

var typeutil = require('typeutil');

Browser (RequireJS)

<script src='require.js'></script>

<script>
    require([
        'build/typeutil.min.js'
    ], function (typeutil) {
        // ...
    });
</script>

Browser (Global/Window)

<script src='build/typeutil.min.js'></script>

API

typeutil.typify(callback: Function, signature: string) => Function

Example: log(message: string) => void

var log;

log = typeutil.typify(function (message) {
    // ...
}, '(string)');

log('');            // passes arguments ['']

log();              // throws TypeError: (!>string)
log({});            // throws TypeError: (!>string)
log({}, '');        // throws TypeError: (!>string)
log({}, {});        // throws TypeError: (!>string)
log({}, null);      // throws TypeError: (!>string)
log({}, undefined); // throws TypeError: (!>string)
log(null);          // throws TypeError: (!>string)
log(undefined);     // throws TypeError: (!>string)

log('', '');        // throws TypeError: (string, !>...)
log('', {});        // throws TypeError: (string, !>...)
log('', null);      // throws TypeError: (string, !>...)
log('', undefined); // throws TypeError: (string, !>...)

Example: assert(expression?: any, message?: string) => void

var assert;

assert = typeutil.typify(function (expression, message) {
    // ...
}, '(any?, string?)');

assert();               // passes arguments [null, null]
assert('');             // passes arguments ['', null]
assert('', '');         // passes arguments ['', '']
assert('', null);       // passes arguments ['', null]
assert('', undefined);  // passes arguments ['', undefined]
assert({});             // passes arguments [{}, null]
assert(null);           // passes arguments [null, null]
assert(undefined);      // passes arguments [undefined, null]

assert('', {});         // throws TypeError: (any?, string?, !>...)

Handling of optional arguments without typeutil:

var readFile;

readFile = function (filename, options, callback) {
    if (typeof options === 'function') {
        callback = options;
        options = null;
    }

    // ...
};

and with typeutil:

readFile = typeutil.typify(function (filename, options, callback) {
    // ...
}, '(string, Object?, Function)');

readFile('', {}, function () {});   // passes arguments ['', {}, function () {}]
readFile('', function () {});       // passes arguments ['', null, function () {}]

typeutil.getTypeOf(value?: any) => string

typeutil.getTypeOf();               // returns 'undefined'
typeutil.getTypeOf(false);          // returns 'boolean'
typeutil.getTypeOf(null);           // returns 'null'
typeutil.getTypeOf(0);              // returns 'number'
typeutil.getTypeOf('');             // returns 'string'
typeutil.getTypeOf(undefined);      // returns 'undefined'

typeutil.getTypeOf(arguments);      // returns 'object'
typeutil.getTypeOf([]);             // returns 'object'
typeutil.getTypeOf(new Boolean());  // returns 'object'
typeutil.getTypeOf(new Date());     // returns 'object'
typeutil.getTypeOf(new Error());    // returns 'object'
typeutil.getTypeOf(function () {}); // returns 'object'
typeutil.getTypeOf(global);         // returns 'object'
typeutil.getTypeOf(JSON);           // returns 'object'
typeutil.getTypeOf(Math);           // returns 'object'
typeutil.getTypeOf(new Number());   // returns 'object'
typeutil.getTypeOf({});             // returns 'object'
typeutil.getTypeOf(new RegExp());   // returns 'object'
typeutil.getTypeOf(new String());   // returns 'object'

typeutil.getClassOf(object?: any) => string

typeutil.getClassOf(arguments);         // returns 'Arguments'
typeutil.getClassOf([]);                // returns 'Array'
typeutil.getClassOf(new Boolean());     // returns 'Boolean'
typeutil.getClassOf(new Date());        // returns 'Date'
typeutil.getClassOf(new Error());       // returns 'Error'
typeutil.getClassOf(function () {});    // returns 'Function'
typeutil.getClassOf(global);            // returns 'Global'
typeutil.getClassOf(JSON);              // returns 'JSON'
typeutil.getClassOf(Math);              // returns 'Math'
typeutil.getClassOf(new Number());      // returns 'Number'
typeutil.getClassOf({});                // returns 'Object'
typeutil.getClassOf(new RegExp());      // returns 'RegExp'
typeutil.getClassOf(new String());      // returns 'String'

typeutil.getClassOf();                  // returns null
typeutil.getClassOf(false);             // returns null
typeutil.getClassOf(null);              // returns null
typeutil.getClassOf(0);                 // returns null
typeutil.getClassOf('');                // returns null
typeutil.getClassOf(undefined);         // returns null

typeutil.is(value?: any) => boolean

typeutil.is();                      // returns false
typeutil.is(null);                  // returns false
typeutil.is(undefined);             // returns false

typeutil.is(false);                 // returns true
typeutil.is(true);                  // returns true
typeutil.is(0);                     // returns true
typeutil.is(1);                     // returns true
typeutil.is(Infinity);              // returns true
typeutil.is(NaN);                   // returns true
typeutil.is('');                    // returns true

typeutil.is(arguments);             // returns true
typeutil.is([]);                    // returns true
typeutil.is(new Boolean());         // returns true
typeutil.is(new Date());            // returns true
typeutil.is(new Error());           // returns true
typeutil.is(new EvalError());       // returns true
typeutil.is(new RangeError());      // returns true
typeutil.is(new ReferenceError());  // returns true
typeutil.is(new SyntaxError());     // returns true
typeutil.is(new TypeError());       // returns true
typeutil.is(new URIError());        // returns true
typeutil.is(function () {});        // returns true
typeutil.is(global);                // returns true
typeutil.is(JSON);                  // returns true
typeutil.is(Math);                  // returns true
typeutil.is(new Number());          // returns true
typeutil.is({});                    // returns true
typeutil.is(new RegExp());          // returns true
typeutil.is(new String());          // returns true

Running the tests

To run the test suite first install the development dependencies:

$ npm install

then run the tests:

$ npm test

License

Licensed under the MIT license.

npm loves you