typeutil
Typify your JavaScript functions.
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
A function signature consists of zero, one, or more parameters and a return-parameter.
([<parameter>, ...]) => <return-parameter>
Parameter and return-parameter types
Type | Accepted value |
---|---|
any | any object and any primitive except null and undefined |
any? | any object and any primitive |
boolean | a boolean primitive |
boolean? | a boolean primitive, null and undefined |
number | a number primitive |
number? | a number primitive, null and undefined |
object | any object |
object? | any object, null and undefined |
string | a string primitive |
string? | a string primitive, null and undefined |
Parameter and return-parameter object classes
Object class | Accepted value |
---|---|
Arguments | an arguments object |
Arguments? | an arguments object, null and undefined |
Array | an array object |
Array? | an array object, null and undefined |
Boolean | a boolean object |
Boolean? | a boolean object, null and undefined |
Date | a date object |
Date? | a date object, null and undefined |
Error | an error object |
Error? | an error object, null and undefined |
Function | a function object |
Function? | a function object, null and undefined |
Global | the Global/Window object |
Global? | the Global/Window object, null and undefined |
JSON | the JSON object |
JSON? | the JSON object, null and undefined |
Math | the Math object |
Math? | the Math object, null and undefined |
Number | a number object |
Number? | a number object, null and undefined |
Object | a plain object |
Object? | a plain object, null and undefined |
RegExp | a regexp object |
RegExp? | a regexp object, null and undefined |
String | a string object |
String? | a string object, null and undefined |
Special return-parameter type
Type | Accepted value |
---|---|
void | null and undefined |
Example: log(message: string) => void
var log = typeutil.typify(function (message) {
// ...
}, '(string) => void');
log(''); // passes arguments ['']
log(); // throws TypeError: (!>string) => void
log({}); // throws TypeError: (!>string) => void
log({}, ''); // throws TypeError: (!>string) => void
log({}, {}); // throws TypeError: (!>string) => void
log({}, null); // throws TypeError: (!>string) => void
log({}, undefined); // throws TypeError: (!>string) => void
log(null); // throws TypeError: (!>string) => void
log(undefined); // throws TypeError: (!>string) => void
log('', ''); // throws TypeError: (string, !>...) => void
log('', {}); // throws TypeError: (string, !>...) => void
log('', null); // throws TypeError: (string, !>...) => void
log('', undefined); // throws TypeError: (string, !>...) => void
Example: assert(expression: any?, message: string?) => void
var assert = typeutil.typify(function (expression, message) {
// ...
}, '(any?, string?) => void');
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?, !>...) => void
Handling of optional arguments
var readFile = typeutil.typify(function (filename, options, callback) {
// ...
}, '(string, Object?, Function) => void');
readFile('', {}, function () {}); // passes arguments ['', {}, function () {}]
readFile('', function () {}); // passes arguments ['', null, function () {}]
Conventional:
var readFile = function (filename, options, callback) {
if (typeof options === 'function') {
callback = options;
options = null;
}
// ...
};
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(''); // 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(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
typeutil.isInteger(number: any?) => boolean
typeutil.isInteger(-2147483648); // returns true
typeutil.isInteger(-1); // returns true
typeutil.isInteger(-0); // returns true
typeutil.isInteger(0); // returns true
typeutil.isInteger(1); // returns true
typeutil.isInteger(2147483647); // returns true
typeutil.isInteger(-Infinity); // returns false
typeutil.isInteger(-2147483649); // returns false
typeutil.isInteger(-1.1); // returns false
typeutil.isInteger(-0.1); // returns false
typeutil.isInteger(0.1); // returns false
typeutil.isInteger(1.1); // returns false
typeutil.isInteger(2147483648); // returns false
typeutil.isInteger(Infinity); // returns false
typeutil.isInteger(NaN); // returns false
typeutil.isInteger(); // returns false
typeutil.isInteger(false); // returns false
typeutil.isInteger(null); // returns false
typeutil.isInteger(''); // returns false
typeutil.isInteger(undefined); // returns false
typeutil.isInteger(arguments); // returns false
typeutil.isInteger([]); // returns false
typeutil.isInteger(new Boolean()); // returns false
typeutil.isInteger(new Date()); // returns false
typeutil.isInteger(new Error()); // returns false
typeutil.isInteger(function () {}); // returns false
typeutil.isInteger(global); // returns false
typeutil.isInteger(JSON); // returns false
typeutil.isInteger(Math); // returns false
typeutil.isInteger(new Number()); // returns false
typeutil.isInteger({}); // returns false
typeutil.isInteger(new RegExp()); // returns false
typeutil.isInteger(new String()); // returns false
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.