@domx/functional
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

functional · GitHub license Build Status Lines npm

Contains methods for functional JavaScript patterns.

Installation

npm install @domx/functional

Usage

Pipe and compose methods combine functions; calling each function with the output of the last one.

The primary difference is that a pipe is from left-to-right (top-to-bottom), where as compose is right-to-left (bottom-to-top).

pipe

const returnValue = pipe(
    (x) => x + 2,
    (x) => x * 3
)(1);
// returnValue = 9

compose

const returnValue = compose(
    (x) => x + 2,
    (x) => x * 3
)(1);
// returnValue = 5

TypeScript

Both pipe and compose will infer the type of each functions argument and return value. If needing to use different types you can use <any>.

 const returnValue = pipe<any>(
    text => parseInt(text),
    num => num + 1,
    num => String(num) + " - it worked"
)("1");
// returnValue = "2 - it worked"

Async Versions

pipeAsync and composeAsync can be used if any one of the methods in the chain is async or returns a promise.

Readme

Keywords

Package Sidebar

Install

npm i @domx/functional

Weekly Downloads

2

Version

1.0.0

License

MIT

Unpacked Size

78.1 kB

Total Files

36

Last publish

Collaborators

  • jhorback