0.7.11 • Published 7 months ago

pipe-and-combine v0.7.11

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
7 months ago

pipe and combine

Readability is important because code is read more often than it is written. This library contains a pipe function to call functions without nesting them. by writing them in a nice way without nesting and taking care of getting the value and passing it to the next function.

import { pipe } from "pipe-and-combine";

const pipeline = pipe(add(1), multiply(2), subtract(1));

const result = pipeline(3); // result -> 7

Installation

    npm install pipe-and-combine

Usage

This library contains functions for sync and async usage.

Pipe

The usual pipe function will simply chain your functions together. pipeAsync` will also wait for the previous function to finish before calling the next one.

// general functions
const inc = (by: number) => (x: number) => x + by;
const dec = (by: number) => (x: number) => x - by;
const multiplyBy = (by: number) => (x: number) => x * by;
const divideBy = (by: number) => (x: number) => x / by;
const toStr = () => (x: number) => x.toString();

// prepare the pipeline
const pipeline = pipe(inc(2), multiplyBy(7), dec(7), divideBy(3), toStr());

// execute the pipeline
expect(pipeline(2)).toBe("7");
const double = async (x: number) => x * 2;
const increment = async (x: number) => Promise.resolve(x + 1);
const square = async (x: number) =>
  new Promise<number>((resolve) => setTimeout(() => resolve(x * x), 100));
const toStr = async (x: number) => x.toString();

const pipeline = asyncPipe(double, increment, square, increment, toStr);
expect(pipeline(2)).resolves.toBe("26");

Combine

async version currently not available

The `combine' function calls all functions with the same input and returns an array of results.

const add = (a: number, b: number) => a + b;
const multiply = (a: number, b: number) => a * b;
const divide = (a: number, b: number) => a / b;
const other = (a: number, b: number) => (a - b).toString();
const str = (a: number) => a.toString();

const c = combine(add, multiply, divide, other, str);
expect(c(1, 2)).toEqual([3, 2, 0.5, "-1", "1"]);
0.7.11

7 months ago

0.7.10

7 months ago

0.7.9

7 months ago

0.7.6

7 months ago

0.7.5

8 months ago

0.7.8

7 months ago

0.7.7

7 months ago

0.8.0-next.2

7 months ago

0.8.0-next.0

7 months ago

0.8.0-next.1

7 months ago

0.7.2

8 months ago

0.7.1

8 months ago

0.7.4

8 months ago

0.7.3

8 months ago

0.7.0

8 months ago

0.6.0

8 months ago

0.5.7

10 months ago

0.5.6

10 months ago

0.5.5

10 months ago

0.5.4

10 months ago

0.5.3

10 months ago

0.5.2

10 months ago

0.5.1

10 months ago

0.5.0

10 months ago

0.4.0

10 months ago

0.3.0

10 months ago

0.2.1

10 months ago

0.2.0

10 months ago

0.1.0

10 months ago