Utilities for working with functions in JavaScript, with TypeScript
npm install functools



> Utilities for working with functions in JavaScript, with TypeScript.
_(Inspired by functools of the same name)_
```
npm install functools --save
Always returns the same value supplied to it.
`js`
identity(42); //=> 42
Returns a function that always returns the same value supplied to it.
`js`
identity(42); //=> 42
Optimize a function to speed up consecutive calls by caching the result of calls with identical input arguments. The cache can be overridden for features such as an LRU cache.
`js
let i = 0;
const fn = memoize(() => ++i);
fn("foo"); //=> 1
fn("foo"); //=> 1
fn("bar"); //=> 2
fn("bar"); //=> 2
`
Memoize the result of fn after the first invocation.
`js
let i = 0;
const fn = memoize0(() => ++i);
fn(); // => 1
fn(); // => 1
fn(); // => 1
`
Memoize the result of a function based on the most recent arguments.
`js
let i = 0;
const fn = memoize(() => ++i);
fn("foo"); //=> 1
fn("foo"); //=> 1
fn("bar"); //=> 2
fn("bar"); //=> 2
fn("foo"); //=> 3
fn("foo"); //=> 3
`
Return a function that fetches key from its operand.
`js`
prop("foo")({ foo: 123 }); //=> 123
Return a function that calls the method name on its operand. If additional arguments are given, they will be given to the method as well.
`js`
invoke("add", 5, 5)({ add: (a, b) => a + b }); //=> 10
Wrap a function to rate-limit the function executions to once every ms milliseconds.
`js
let i = 0
const fn = throttle(() => ++i, 100)
fn() // i == 1
fn() // i == 1
fn() // i == 1
setTimeout(() => / i == 2 /, 200)
`
Tip: Use fn.clear and fn.flush for finer execution control.
- fn.clear Unconditionally clears the current timeoutfn.flush
- When fn is pending, executes fn() and starts a new interval
Given a fn, return a wrapper that accepts an array of fn arguments.
`js`
Promise.all([1, 2, 3]).then(spread(add));
Flip a binary fn argument order.
`js`
flip(subtract)(5, 10); //=> 5
Returns a partially applied fn with the supplied arguments.
`js`
partial(subtract, 10)(5); //=> 5
Left-to-right function composition.
`js`
sequence(partial(add, 10), partial(multiply, 5))(5); //=> 75
Right-to-left function composition.
`js`
compose(partial(add, 10), partial(multiply, 5))(5); //=> 35
Fix the number of receivable arguments in origFn to n.
`js``
["1", "2", "3"].map(nary(1, fn)); //=> [1, 2, 3]
This module uses TypeScript and publishes type definitions on NPM.
Apache 2.0