0.1.2 • Published 12 months ago

@ze-ts/composition v0.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

@ze-ts/composition

This package provides a set of utilities to help you compose your functions in a more readable and functional way.

Installation

npm install @ze-ts/composition

Usage

pipe

Pipe is a function with arity n that the first argument is a value and the rest are functions that will be applied to the value in sequence.

Signature

pipe: (value: any, ...fns: Function[]) => any;

Example

import { pipe } from '@ze-ts/composition';

const add = (a: number) => (b: number) => a + b;
const multiply = (a: number) => (b: number) => a * b;

const result = pipe(5, add(3), multiply(2));

console.log(result); // 16

flow

Flow is a function with arity n that takes a list of functions and returns a new function that receives a value and applies the functions in sequence.

Signature

flow: (...fns: Function[]) =>
  (value: any) =>
    any;

Example

import { flow } from '@ze-ts/composition';

const add = (a: number) => (b: number) => a + b;
const multiply = (a: number) => (b: number) => a * b;

const addAndMultiply = flow(add(3), multiply(2));

addAndMultiply(2); // 10

compose

Compose is a function with arity n that takes a list of functions and returns a new function that receives a value and applies the functions in reverse order (rigth-to-left).

Signature

// fns will be applied in reverse order
compose: (...fns: Function[]) =>
  (value: any) =>
    any;

Example

import { compose } from '@ze-ts/composition';

const add = (a: number) => (b: number) => a + b;
const multiply = (a: number) => (b: number) => a * b;

const addAndMultiply = compose(multiply(2), add(3));

addAndMultiply(2); // 10
0.1.2

12 months ago

0.1.1

12 months ago

0.1.0

12 months ago

0.0.0

12 months ago