Why?

In JavaScript, functions can be called with any number and any type of arguments.
When writing a function, the easiest way is to just assume that the function
will be called with the correct input. This leaves the function's behavior on
invalid input undefined. The function may throw some error, or worse,
it may silently fail or return wrong results. Typical errors are
TypeError: undefined is not a function or TypeError: Cannot call method
'request' of undefined. These error messages are not very helpful. It can be
hard to debug them, as they can be the result of a series of nested function
calls manipulating and propagating invalid or incomplete data.

Often, JavaScript developers add some basic type checking where it is important,
using checks like typeof fn === 'function', date instanceof Date, and
Array.isArray(arr). For functions supporting multiple signatures,
the type checking logic can grow quite a bit, and distract from the actual
logic of the function.

For functions dealing with a considerable amount of type checking and conversion
logic, or functions facing a public API, it can be very useful to use the
typed-function module to handle the type-checking logic. This way:

Users of the function get useful and consistent error messages when using
the function wrongly.

Correct type of input is assured inside the function. The function's code
becomes easier to understand as it only contains the actual function logic.
Lower level utility functions called by the type-checked function can
possibly be kept simpler as they don't need to do additional type checking.

It's important however not to overuse type checking:

Locking down the type of input that a function accepts can unnecessary limit
it's flexibility. Keep functions as flexible and forgiving as possible,
follow the
robustness principle
here: "be liberal in what you accept and conservative in what you send"
(Postel's law).

There is no need to apply type checking to all functions. It may be
enough to apply type checking to one tier of public facing functions.

There is a performance penalty involved for all type checking, so applying
it everywhere can unnecessarily worsen the performance.

Load

Install via npm:

npm install typed-function

Usage

Here some usage examples. More examples are available in the
/examples folder.

Methods

Convert an value to another type. Only applicable when conversions have
been defined in typed.conversions (see section Properties).
Example:

typed.conversions.push({

from:'number',

to:'string',

convert:function(x){

return+x;

});

var str =typed.convert(2.3,'string');// '2.3'

typed.create() : function

Create a new, isolated instance of typed-function. Example:

var typed =require('typed-function');// default instance

var typed2 =typed.create();// a second instance

typed.find(fn: function, signature: string | Array) : function | null

Find a specific signature from a typed function. The function currently
only finds exact matching signatures.

For example:

var fn =typed(...);

var f =typed.find(fn,['number','string']);

var f =typed.find(fn,'number, string');

typed.addType(type: {name: string, test: function})

Add a new type. A type object contains a name and a test function.
The order of the types determines in which order function arguments are
type-checked, so for performance it's important to put the most used types
first. All types are added to the Array typed.types.

Add a new conversion. Conversions are added to the Array typed.conversions.

typed.addConversion({

from:'boolean',

to:'number',

convert:function(x){

return+x;

});

Properties

typed.types: Array.<{name: string, test: function}>

Array with types. Each object contains a type name and a test function.
The order of the types determines in which order function arguments are
type-checked, so for performance it's important to put the most used types
first. Custom types can be added like: