function-composer
Advanced tools
Comparing version 0.0.1 to 0.1.0
{ | ||
"name": "function-composer", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "Compose functions with multiple type signatures", | ||
@@ -14,12 +14,4 @@ "author": "Jos de Jong <wjosdejong@gmail.com> (https://github.com/josdejong)", | ||
"function", | ||
"mathematics", | ||
"functions", | ||
"numeric", | ||
"parser", | ||
"expression", | ||
"number", | ||
"bignumber", | ||
"complex", | ||
"matrix", | ||
"unit" | ||
"compose", | ||
"signature" | ||
], | ||
@@ -26,0 +18,0 @@ "dependencies": {}, |
function-composer | ||
================= | ||
Compose functions with multiple type signatures | ||
Compose functions with multiple type signatures. | ||
Supported environments: node.js, Chrome, Firefox, Safari, Opera, IE9+. | ||
# Load | ||
Install via npm: | ||
npm install function-composer | ||
# Usage | ||
Example usage: | ||
```js | ||
var compose = require('function-composer'); | ||
// compose a new function | ||
var fn = compose({ | ||
'number': function (a) { | ||
return 'a is a number'; | ||
}, | ||
'number, boolean': function (a, b) { | ||
return 'a is a number, b is a boolean'; | ||
}, | ||
'number, number': function (a, b) { | ||
return 'a is a number, b is a number'; | ||
} | ||
}); | ||
// use the function | ||
console.log(fn(2, true)); // outputs 'a is a number, b is a boolean' | ||
console.log(fn(2)); // outputs 'a is a number' | ||
// calling the function with a non-supported type signature will throw an error | ||
try { | ||
fn('hello world'); | ||
} | ||
catch (err) { | ||
console.log(err.toString()); // outputs: 'TypeError: Wrong function signature' | ||
} | ||
``` | ||
# Roadmap | ||
- Create named functions. | ||
- Add new types (already possible but not yet documented) | ||
- Automatic casting, for example from boolean to number. | ||
- Extend function signatures: | ||
- Any type arguments like `*, boolean`. | ||
- Ellipsis like `string, ...`. | ||
- Optional arguments like `number?, array`. | ||
- Create a good benchmark, to get insight in the overhead. | ||
- Add a bundle for use in the browser. |
// test parse | ||
var assert = require('assert'); | ||
var composer = require('../index'); | ||
var compose = require('../index'); | ||
describe('parse', function() { | ||
it('should work', function() { | ||
// TODO | ||
it('should compose a function with zero arguments', function() { | ||
var fns = { | ||
'': function () { | ||
return 'noargs'; | ||
} | ||
}; | ||
var fn = compose(fns); | ||
assert.equal(fn(), 'noargs'); | ||
assert(fn.signatures instanceof Object); | ||
assert.strictEqual(Object.keys(fn.signatures).length, 1); | ||
assert.strictEqual(fn.signatures[''], fns['']); | ||
}); | ||
it('should compose a function with one argument', function() { | ||
var fns = { | ||
'number': function (value) { | ||
return 'number:' + value; | ||
}, | ||
'string': function (value) { | ||
return 'string:' + value; | ||
}, | ||
'boolean': function (value) { | ||
return 'boolean:' + value; | ||
} | ||
}; | ||
var fn = compose(fns); | ||
assert.equal(fn(2), 'number:2'); | ||
assert.equal(fn('hi'), 'string:hi'); | ||
assert.equal(fn(false), 'boolean:false'); | ||
assert(fn.signatures instanceof Object); | ||
assert.strictEqual(Object.keys(fn.signatures).length, 3); | ||
assert.strictEqual(fn.signatures['number'], fns['number']); | ||
assert.strictEqual(fn.signatures['string'], fns['string']); | ||
assert.strictEqual(fn.signatures['boolean'], fns['boolean']); | ||
}); | ||
it('should compose a function with multiple arguments', function() { | ||
var fns = { | ||
'number': function (value) { | ||
return 'number:' + value; | ||
}, | ||
'string': function (value) { | ||
return 'string:' + value; | ||
}, | ||
'number, boolean': function (a, b) { // mind space after the comma, should be normalized by composer | ||
return 'number,boolean:' + a + ',' + b; | ||
} | ||
}; | ||
var fn = compose(fns); | ||
assert.equal(fn(2), 'number:2'); | ||
assert.equal(fn('hi'), 'string:hi'); | ||
assert.equal(fn(2, false), 'number,boolean:2,false'); | ||
assert(fn.signatures instanceof Object); | ||
assert.strictEqual(Object.keys(fn.signatures).length, 3); | ||
assert.strictEqual(fn.signatures['number'], fns['number']); | ||
assert.strictEqual(fn.signatures['string'], fns['string']); | ||
assert.strictEqual(fn.signatures['number,boolean'], fns['number, boolean']); | ||
}); | ||
it('should throw an error when providing an unsupported type of argument', function() { | ||
var fn = compose({ | ||
'number': function (value) { | ||
return 'number:' + value; | ||
} | ||
}); | ||
assert.throws(function () {fn(new Date())}, /TypeError: Wrong function signature/); | ||
}); | ||
it('should throw an error when providing a wrong number of arguments', function() { | ||
var fn = compose({ | ||
'number': function (value) { | ||
return 'number:' + value; | ||
} | ||
}); | ||
assert.throws(function () {fn(1, 2)}, /TypeError: Wrong number of arguments/); // TODO: should be changed into an ArgumentsError? | ||
}); | ||
// TODO: test compose.types | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
26450
13
693
60
2
2