Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

function-composer

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

function-composer - npm Package Compare versions

Comparing version 0.0.1 to 0.1.0

benchmark/benchmark.bundle.js

14

package.json
{
"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
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc