1.0.1 • Published 8 years ago

function-x v1.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
8 years ago

function-x.js

Extensions to JavaScript Function type

API

// Wraps the target function in an argument types checking function
// The argsTypeInfo variadic argument specifies expected types of the arguments, respectively by position. 
// Where an argument can have multiple types, the corresponding argsTypeInfo element is an array of the allowed types
// Use 'any' for an argument that can be of any type
function expecting(...argsTypeInfo)

// Wraps the target function in an return value type checking function
// The typeNames variadic argument specifies allowed types of the return value.
function returning(...typeNames)

Example

require ("function-x");

var fx = function(str, num){
	return Array(num).join(str);
}.expecting("string", "number");

console.log(fx("hello ", 2)); // prints "hello"
console.log(fx(1, 2)); // throws error since first argument should be string instead of number