Skip to content
smidhonzaPublic

About

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

@smidhonza/curry

A tiny TypeScript helper for calling a function one argument at a time, all at once, or anywhere in between.

Pass in a function, and curry returns a new one that remembers the arguments you've supplied so far. Once it has them all, it calls the original function.

Install

npm i @smidhonza/curry

Simple Example

import { curry } from '@smidhonza/curry';

const describe = curry((count: number, item: string, ready: boolean) =>
    `${count} ${item} ${ready ? 'ready' : 'pending'}`,
);

console.log(describe(2)('orders')(true)); // outputs "2 orders ready"
console.log(describe(2, 'orders')(true)); // outputs "2 orders ready"
console.log(describe(2, 'orders', true)); // outputs "2 orders ready"

You can also keep a partial function for later:

const describeTwo = describe(2);

describeTwo('orders')(true);    // "2 orders ready"
describeTwo('packages', false); // "2 packages pending"

API

curry(fn)

Wraps a function so its arguments can be supplied in groups. Each call accepts the next one or more arguments. When all required arguments have been supplied, the result of fn is returned.

TypeScript tracks the remaining parameter types and the final return type:

const format = curry((count: number, label: string) => `${count} ${label}`);

const withCount = format(3); // expects a string next
const result: string = withCount('books');

The Curried<Args, Result> type is also exported if you need to name a curried function type. In most cases, TypeScript infers it from curry(fn).

curry uses the original function's JavaScript length to decide when to call it. Use functions with a fixed number of required parameters; default, optional, and rest parameters do not have the same runtime arity.

Development

npm test           # Run the Jest tests
npm run test:types # Type-check the tests
npm run build      # Compile the package

License

ISC

About

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages