curry
🍛 Simple curry functions
A simple helper to help apply arguments to a function over time.
Install
Using Yarn:
$ yarn add @blakek/curry
…or using npm:
$ npm i --save @blakek/curry
Usage
import { curry, curryRight } from '@blakek/curry';
const multiply = (a, b) => a * b;
const timesTwo = curry(multiply)(2);
timesTwo(5);
const sum = (...args) => args.reduce((a, b) => a + b, 0);
const topFiveTotal = curry(sum, 5);
topFiveTotal(1, 5, 6)(6)(7);
const prop = (object, key) => object[key];
const getName = curryRight(prop)('name');
getName({ name: 'John Smith', age: 42 });
API
curry
curry(fn: Function, arity?: number): any;
Returns functions that take in the arguments of another function until all have
been provided.
curryRight
curryRight(fn: Function, arity?: number): any;
Same as curry
, but reverses all arguments once the limit has been provided.
Provided for the common use-case of needing to supply context-specific arguments
before providing the first arguments. For example:
import { curryRight } from '@blakek/curry';
const users = [
{ username: 'blakek' },
{ username: 'gsandf' },
{ username: 'google' }
];
const prop = (object, key) => object[key];
const getUsername = curryRight(prop)('username');
users.map(getUsername);
Contributing
Node.js and Yarn are required to work with this project.
To install all dependencies, run:
yarn
Useful Commands
| |
---|
yarn build | Builds the project to ./dist |
yarn format | Format the source following the Prettier styles |
yarn test | Run project tests |
yarn test --watch | Run project tests, watching for file changes |
License
MIT