# @blakek/compose

> 🛠 Compose functions and promises

Latest version **4.0.1** (published 2020-09-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install @blakek/compose
pnpm add @blakek/compose
yarn add @blakek/compose
bun add @blakek/compose
```

## Health

**Score 40/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 4.0.1 |
| Published | 2020-09-24 |
| First published | 2020-06-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 103.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Blake Knight |
| Maintainers | blakek |
| Keywords | function, pipeline, promise |

## Links

- npm: https://www.npmjs.com/package/@blakek/compose
- Repository: https://github.com/blakek/compose
- Issues: https://github.com/blakek/compose/issues
- npm.io page: https://npm.io/package/@blakek/compose

## Dependencies (1)

- [@blakek/is-promise](https://npm.io/package/@blakek/is-promise.md) ^1.0.0

## Alternatives

- [byte-size](https://npm.io/package/byte-size.md) — 2.1M weekly downloads
- [speed-limiter](https://npm.io/package/speed-limiter.md) — 16.0K weekly downloads
- [@powersync/node](https://npm.io/package/@powersync/node.md) — 10.9K weekly downloads
- [@ledgerhq/coin-cardano](https://npm.io/package/@ledgerhq/coin-cardano.md) — 1.0K weekly downloads
- [@jayesol/jayeson.lib.streamfinder](https://npm.io/package/@jayesol/jayeson.lib.streamfinder.md) — 1.0K weekly downloads

## Recent versions

- 4.0.1 (latest) — 2020-09-24
- 4.0.0 — 2020-09-24
- 3.0.1 — 2020-09-24
- 3.0.0 — 2020-06-10
- 2.0.0 — 2020-06-10
- 1.0.0 — 2020-06-05

## README

# compose

> 🛠 Compose functions and promises

Code can become complex when several functions wrap others or `reduce()` is used
to combine a list of funtions.

This allows composing a pipeline of functions. `compose` calls each function
from right-to-left and passes the output from the previous to the next. `pipe`
is the same, but works from left-to-right.

## Install

Using [Yarn]:

```bash
$ yarn add @blakek/compose
```

…or using [npm]:

```bash
$ npm i --save @blakek/compose
```

## Usage

```js
import { compose, pipe } from '@blakek/compose';

const fetchUsers = () =>
  Promise.resolve([
    { sites: { github: { username: 'blakek' } } },
    { sites: { github: { username: 'gsandf' } } },
    { sites: { github: { username: 'google' } } }
  ]);

// using `compose`
const getUsers = compose(
  users => users.map(user => user.sites.github.username),
  fetchUsers
);

// using `pipe`
const getUsersPipe = pipe(fetchUsers, users =>
  users.map(user => user.sites.github.username)
);

getUsers().then(console.log); //» [ 'blakek', 'gsandf', 'google' ]

// NOTE: awaiting is only required if one of the arguments is a Promise
pipe(
  n => n + 2,
  n => n * 3
)(3); //» 15

await pipe(
  () => delay(500),
  n => n + 2,
  n => n * 3
)(3); //» 15
```

## API

### `compose`

```ts
function compose(...[fn, ...fns]: Function[]): Function;
```

### `pipe`

```ts
function pipe(...[fn, ...fns]: Function[]): Function;
```

## Contributing

[Node.js] and [Yarn] are required to work with this project.

To install all dependencies, run:

```bash
yarn
```

### Useful Commands

|                     |                                                 |
| ------------------- | ----------------------------------------------- |
| `yarn build`        | Builds the project to `./dist`                  |
| `yarn format`       | Format the source following the Prettier styles |
| `yarn test`         | Run project tests                               |
| `yarn test --watch` | Run project tests, watching for file changes    |

## License

MIT

[node.js]: https://nodejs.org/
[npm]: https://npmjs.com/
[yarn]: https://yarnpkg.com/en/docs/

---
_Source: https://npm.io/package/@blakek/compose · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
