0.7.11 • Published 6 months ago

pipe-and-combine v0.7.11

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
6 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

6 months ago

0.7.10

6 months ago

0.7.9

6 months ago

0.7.6

6 months ago

0.7.5

7 months ago

0.7.8

6 months ago

0.7.7

6 months ago

0.8.0-next.2

6 months ago

0.8.0-next.0

6 months ago

0.8.0-next.1

6 months ago

0.7.2

7 months ago

0.7.1

7 months ago

0.7.4

7 months ago

0.7.3

7 months ago

0.7.0

7 months ago

0.6.0

8 months ago

0.5.7

9 months ago

0.5.6

9 months ago

0.5.5

9 months ago

0.5.4

9 months ago

0.5.3

9 months ago

0.5.2

9 months ago

0.5.1

9 months ago

0.5.0

9 months ago

0.4.0

9 months ago

0.3.0

9 months ago

0.2.1

9 months ago

0.2.0

9 months ago

0.1.0

9 months ago