function-composer
Advanced tools
Comparing version 0.3.0 to 0.3.1
# History | ||
## 2014-11-05, version 0.3.1 | ||
- Module renamed to `typed-function`. | ||
## 2014-11-05, version 0.3.0 | ||
@@ -5,0 +10,0 @@ |
{ | ||
"name": "function-composer", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "Compose functions with multiple type signatures", | ||
@@ -5,0 +5,0 @@ "author": "Jos de Jong <wjosdejong@gmail.com> (https://github.com/josdejong)", |
164
README.md
@@ -1,163 +0,7 @@ | ||
function-composer | ||
================= | ||
# `function-composer` is deprecated | ||
Compose functions with multiple type signatures. | ||
This module has been renamed to `typed-function`, use this module instead. | ||
Features: | ||
npm install typed-function | ||
- Compose multiple functions with different signatures into one. | ||
- Type-checking of input arguments. | ||
- Automatic type conversion of arguments. | ||
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' | ||
} | ||
``` | ||
## Performance | ||
Type checking input arguments adds some overhead to a function. For very small | ||
functions this overhead can be larger than the function execution itself is, | ||
but for any non-trivial function the overhead is typically small to neglectable. | ||
You need to keep in mind though that you probably would have to do the type | ||
checking done by `function-composer` anyway. | ||
## API | ||
### Construction | ||
A function is constructed as: | ||
```js | ||
compose(signatures: Object.<string, function>) : function | ||
compose(name: string, signatures: Object.<string, function>) : function | ||
``` | ||
### Properties | ||
- `compose.types: Object` | ||
A map with the object types as key and a type checking test as value. | ||
Custom types can be added like: | ||
```js | ||
function Person(...) { | ||
... | ||
} | ||
compose.types['Person'] = function (x) { | ||
return x instanceof Person; | ||
}; | ||
``` | ||
- `compose.conversions: Array` | ||
An Array with built-in conversions. Empty by default. Can be used for example | ||
to defined conversions from `boolean` to `number`. For example: | ||
```js | ||
compose.conversions.push({ | ||
from: 'boolean', | ||
to: 'number', | ||
convert: function (x) { | ||
return +x; | ||
}); | ||
``` | ||
### Types | ||
function-composer has the following built-in types: | ||
- `null` | ||
- `boolean` | ||
- `number` | ||
- `string` | ||
- `function` | ||
- `Array` | ||
- `Date` | ||
- `RegExp` | ||
- `Object` | ||
- `*` (anytype) | ||
### Output | ||
The functions generated with `compose({...})` have: | ||
- A `toString()` function which returns well readable code, giving insight in | ||
what the function exactly does. | ||
- A property `signatures`, which holds a map with the (normalized) | ||
signatures as key and the original sub-functions as value. | ||
## Roadmap | ||
### Version 1 | ||
- Extend function signatures: | ||
- Any type arguments like `'*, boolean'` | ||
- Ellipsis like `'string, ...'` | ||
- Optional arguments like `'number?, array'` | ||
- Multiple types per argument like `number | string, number'` | ||
- Detailed error messages. | ||
- Create a good benchmark, to get insight in the overhead. | ||
- Allow conversions not to be able to convert any input (for example string to | ||
number is not always possible). | ||
### Version 2 | ||
- Extend function signatures: | ||
- Constants like `'"linear" | "cubic"'`. | ||
- Object definitions like `'{name: string, age: number}'` | ||
- Object definitions like `'Object.<string, Person>'` | ||
- Array definitions like `'Array.<Person>'` | ||
## Test | ||
To test the library, run: | ||
npm test | ||
## Minify | ||
To generate the minified version of the library, run: | ||
npm run minify | ||
https://github.com/josdejong/typed-function |
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
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
0
36047
8