Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/unsplash/pipe-ts
https://github.com/unsplash/pipe-ts
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/unsplash/pipe-ts
- Owner: unsplash
- Created: 2019-06-28T11:40:08.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-06-20T10:28:17.000Z (over 1 year ago)
- Last Synced: 2024-07-11T18:49:56.922Z (2 months ago)
- Language: TypeScript
- Homepage:
- Size: 30.3 KB
- Stars: 32
- Watchers: 6
- Forks: 4
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-list - pipe-ts
README
# pipe-ts
[What is `pipe`?](https://dev.to/benlesh/a-simple-explanation-of-functional-pipe-in-javascript-2hbj)
## Installation
```sh
yarn add pipe-tsnpm install pipe-ts
```## `pipe`
Create a new function which pipes its value through the list functions.
```ts
const add1 = (n: number) => n + 1;
const times2 = (n: number) => n * 2;const add1ThenTimes2 = pipe(
add1,
times2,
);
const result: number = add1ThenTimes2(1);assert.strictEqual(result, 4);
```Allows first function to have any number of parameters (0+), [thanks to TypeScript's generic rest parameters](https://github.com/Microsoft/TypeScript/issues/29904#issuecomment-471334674)
```ts
// First function has multiple parametersconst difference = (a: number, b: number) => a - b;
const add1 = (n: number) => n + 1;const differenceThenAdd1 = pipe(
difference,
add1,
);
const result: number = differenceThenAdd1(5, 4);assert.strictEqual(result, 2);
``````ts
// First function has 0 parametersconst getNumber = () => 1;
const add1 = (n: number) => n + 1;const getNumberThenAdd1 = pipe(
getNumber,
add1,
);
const result: number = getNumberThenAdd1();assert.strictEqual(result, 2);
```## `pipeWith`
Transform a value by piping it through the listed functions. Sugar syntax for `pipe(f, g)(value)`.
```ts
const add1 = (n: number) => n + 1;
const times2 = (n: number) => n * 2;const result: number = pipeWith(1, add1, times2);
assert.strictEqual(result, 4);
```## Note about number of functions
`pipe` and `pipeWith` currently support up to 9 functions. If need be, we are open to adding more overloads to increase this limit.
We are [also open to adding an array overload](https://github.com/unsplash/pipe-ts/issues/5), so any number of funtions can be passed.
## When `this` matters
If a function passed to `pipe` or `pipeWith` relies on a specific context of execution ([`this`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this)), you cannot call it by passing the reference (_point-free style_).
Use an anonymous function instead.
**Bad**
```ts
pipeWith('foo', localStorage.getItem);
```**Good**
```ts
pipeWith('foo', key => localStorage.getItem(key));
```**Also good**
```ts
pipeWith('foo', localStorage.getItem.bind(localStorage)),
```## Development
```
yarn
npm run start
```