# @arrows/composition 中文文档

规范地址：https://npmcn.com/package/%40arrows/composition/

版本：1\.2\.2
npm 元数据更新时间：2022-04-04T15:35:16.210Z
中文译文更新时间：2026-11-06T20:23:32.740Z
软件包许可：ISC

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40arrows/composition
原文对照：https://npmcn.com/package/%40arrows/composition/?lang=original
项目主页：https://caderek.github.io/arrows/packages/composition/
源码仓库：https://github.com/caderek/arrows/tree/master/packages/composition

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：89.7 kB，90 个文件
- 模块格式：CommonJS
- TypeScript 类型：未声明
- 运行依赖：0 个
- 首次发布：2019-09-17T01:02:43.565Z
- 最新版本发布：2020-01-22T19:57:10.392Z
- 近 30 天下载量：237228（2026-10-07 至 2026-11-05，npm 官方下载统计）
- 已发布版本数：16（近 12 个月 0 个）

## 中文简介

职能构成图书馆

## 安装

```sh
npm install @arrows/composition
```

也可以使用：`pnpm add @arrows/composition`、`yarn add @arrows/composition`、`bun add @arrows/composition`

## README

![arrows - composition](https://raw.githubusercontent.com/caderek/arrows/master/assets/arrows-composition.svg?sanitize=true)

# @ arrows/ 组合

![npm (scoped)](https://img.shields.io/npm/v/@arrows/composition) ![CircleCI](https://img.shields.io/circleci/build/github/caderek/arrows) ![David (path)](https://img.shields.io/david/caderek/arrows?path=packages%2Fcomposition) ![Codecov](https://img.shields.io/codecov/c/github/caderek/arrows?token=c6adb715d638431786fefe69ca08ab00) ![npm bundle size (scoped)](https://img.shields.io/bundlephobia/minzip/@arrows/composition) ![GitHub](https://img.shields.io/github/license/caderek/arrows)

## 目录

1.  [导言](https://npmcn.com/package/%40arrows/composition/#readme-introduction)
2.  [安装](https://npmcn.com/package/%40arrows/composition/#readme-installation)
3.  [API 参考](https://npmcn.com/package/%40arrows/composition/#readme-api-reference)
    -   [链式](https://npmcn.com/package/%40arrows/composition/#readme-chain)
    -   [链式右](https://npmcn.com/package/%40arrows/composition/#readme-chainRight)
    -   [编曲](https://npmcn.com/package/%40arrows/composition/#readme-compose)
    -   [咖喱](https://npmcn.com/package/%40arrows/composition/#readme-curry)
    -   [烟斗](https://npmcn.com/package/%40arrows/composition/#readme-pipe)
    -   [现在](https://npmcn.com/package/%40arrows/composition/#readme-pipenow)
    -   [铁路](https://npmcn.com/package/%40arrows/composition/#readme-rail)
    -   [铁路. async](https://npmcn.com/package/%40arrows/composition/#readme-rail.async)
    -   [铁轨右](https://npmcn.com/package/%40arrows/composition/#readme-railRight)
    -   [rail Right.async (英语)](https://npmcn.com/package/%40arrows/composition/#readme-railRight.async)
    -   [窃听](https://npmcn.com/package/%40arrows/composition/#readme-tap)
4.  [许可证](https://npmcn.com/package/%40arrows/composition/#readme-license)

## 导言

库中包含一个更高排序的函数集合,用于将您的应用程序编组为可重复使用的函数.

所有类似链的函数都是手动块(分块). 允许某个功能接受某一段中数量变化多端的争论.

图书馆有 **内置类型定义**,提供了极佳的IDE支持.

## 安装

通过国家预防机制:

```sh
npm i @arrows/composition
```

维亚·亚恩:

```sh
yarn add @arrows/composition
```

所有模块都可以独立导入(以减小捆绑大小),这里有一些导入方法(您可以使用普通JS或ES模块):

```js
import composition from '@arrows/composition'
```

```js
import { pipe } from '@arrows/composition'
```

```js
import pipe from '@arrows/composition/pipe'
```

## API 参考

### 链式

允许您构建自己的类似链式功能

执行从左到右提供函数,将结果从一个函数传递到另一个函数. 接受一个可以执行附加指令的折叠函数,然后执行每个传递的函数.

#### 参数

以下所列论点必须作为分段单独通过.

-   `wrappingFn` - 双参数函数,负责在链中执行每个函数.
    
-   `...fns` - 在链条中任意执行一个参数函数的数量
    
-   `initialArg` - 初始参数,传递到链条中的第一个函数(通过包接函数)
    

折叠函数的参数 :

-   `fn` - 链中函数
    
-   `input` - 现值
    
-   `isLast` - 布尔旗表示一个函数是否是链条上最后一个函数
    

#### 回返

-   返回最终值.

#### 接口

```
(wrapping_function) => (fn, fn?, ..., fn?) => (initial_value) => result
```

#### 实例

```javascript
const { chain } = require('@arrows/composition')

// Create custom wrapping function:
const wrappingFn = (fn, input) => {
  console.log(input)
  return fn(input)
}

// Create reusable chain-like function:
const pipeWithLog = chain(wrappingFn)

// Create concrete function
const calculate = pipeWithLog((x) => x + 1, (x) => x * 2)

console.log(calculate(0))

// -> 0
// -> 1
// -> 2 (final result)
```

* * *

### 链式右

工作喜欢 [链式](https://npmcn.com/package/%40arrows/composition/#readme-chain),但从右到左执行函数.

#### 参数

以下所列论点必须作为分段单独通过.

-   `wrappingFn` - 双参数函数,负责在链中执行每个函数.
    
-   `...fns` - 在链条中任意执行一个参数函数的数量
    
-   `initialArg` - 初始参数,传递到链条中的第一个函数(通过包接函数)
    

折叠函数的参数 :

-   `fn` - 链中函数
    
-   `input` - 现值
    
-   `isLast` - 布尔旗表示一个函数是否是链条上最后一个函数
    

#### 回返

-   返回最终值.

#### 接口

```
(wrapping_function) => (fn, fn?, ..., fn?) => (initial_value) => result
```

#### 示例

```javascript
const { chainRight } = require('@arrows/composition')

// Create custom wrapping function:
const wrappingFn = (fn, input) => {
  console.log(input)
  return fn(input)
}

// Create reusable chain-like function:
const composeWithLog = chainRight(wrappingFn)

// Create concrete function
const calculateRight = composeWithLog((x) => x + 1, (x) => x * 2)

console.log(calculateRight(0))

// -> 0
// -> 0
// -> 1 (final result)
```

* * *

### 编曲

链条提供了从右到左的功能.

#### 参数

以下所列论点必须作为分段单独通过.

-   `...fns` - 在链条中任意执行一个参数函数的数量
    
-   `initialArg` - 初始参数,传递到链条中的第一个函数
    

#### 回返

-   返回最终值.

#### 接口

```
(fn, fn?, ..., fn?) => (initial_value) => result
```

#### 示例

```javascript
const { compose } = require('@arrows/composition')

const addPrefixes = compose(
  (text) => `prefix1-${text}`,
  (text) => `prefix2-${text}`,
)

addPrefixes('arrows') // -> "prefix1-prefix2-arrows"
```

* * *

### 咖喱

环绕功能为自动曲折的 .

#### 参数

-   `fn` - 任意职能

#### 回返

-   返回函数的曲尾版本.

#### 接口

```
(fn) =>  curried_fn
```

#### 示例

```javascript
const { curry } = require('@arrows/composition')

const rawAdd = (a, b) => a + b

const add = curry(rawAdd)

add(1, 2) // -> 3
add(1)(2) // -> 3
```

* * *

### 烟斗

链条提供了从左到右函数 .

#### 参数

以下所列论点必须作为分段单独通过.

-   `...fns` - 在链条中任意执行一个参数函数的数量
    
-   `initialArg` - 初始参数,传递到链条中的第一个函数
    

#### 回返

-   返回最终值.

#### 接口

```
(fn, fn?, ..., fn?) => (initial_value) => result
```

#### 示例

```javascript
const { pipe } = require('@arrows/composition')

const addSuffixes = pipe(
  (text) => `${text}-suffix1`,
  (text) => `${text}-suffix2`,
)

addSuffixes('arrows') // -> "arrows-suffix1-suffix2"
```

* * *

### 现在

链条提供了从左到右的函数,以初始值作为第一个参数.

为立即评估的计算提供更好的类型推断的替代API.

#### 参数

-   `initialArg` - 初始参数,传递到链条中的第一个函数
-   `...fns` - 在链条中任意执行一个参数函数的数量

#### 回返

-   返回最终值.

#### 接口

```
(initial_value, fn, fn?, ..., fn?) => result
```

#### 示例

```javascript
const { pipe } = require('@arrows/composition')

const result = pipe.now(
  'arrows',
  (text) => `${text}-suffix1`,
  (text) => `${text}-suffix2`,
) // -> "arrows-suffix1-suffix2"
```

* * *

### 铁路

工作喜欢 [烟斗](https://npmcn.com/package/%40arrows/composition/#readme-pipe),但还有:

-   如果函数之一丢出或返回出错 - 返回出错,
-   如果其中一个函数返回未定义 - 将先前的参数传递到下一个函数.

#### 参数

以下所列论点必须作为分段单独通过.

-   `...fns` - 在链条中任意执行一个参数函数的数量
    
-   `initialArg` - 初始参数,传递到链条中的第一个函数
    

#### 回返

-   返回最终值.

#### 接口

```
(fn, fn?, ..., fn?) => (initial_value) => result
```

#### 实例

自动传递出错 :

```javascript
const { rail } = require('@arrows/composition')
const { filter, map, reduce } = require('@arrows/array')

const sumDogsAge = rail(
  filter((pet) => pet.specie === 'dog'),
  map((pet) => {
    if (pet.age < 0) {
      throw new Error('Wrong age!')
    }
    return pet.age
  }),
  reduce((a, b) => a + b, 0),
)

const pets = [
  { specie: 'dog', name: 'Charlie', age: 4 },
  { specie: 'cat', name: 'Luna', age: 6 },
  { specie: 'dog', name: 'Ollie', age: -10 },
]

const result = sumDogsAge(pets)

if (result instanceof Error) {
  console.log('Oops!')
} else {
  console.log(`Sum: ${result}`)
}

// -> "Oops!"
```

如果函数返回, 自动传递上一个参数 `undefined`数字 :

```js
const { rail } = require('../../lib/index')

const addUser = rail(
  (user) => ({ type: 'user', data: user }),
  (entry) => {
    console.log(`Saving ${entry.data.name} to the database.`)
    // Returns undefined, argument will be automatically
    // passed to the next function.
  },
  (entry) => `Saved user: ${entry.data.name}`,
)

const user = { name: 'Joe' }

console.log(addUser(user))

/* Output:
Saving Joe to the database.
Saved user: Joe
*/
```

* * *

### 铁路. async

类似工程 [铁路](https://npmcn.com/package/%40arrows/composition/#readme-rail)但是,另外,如果争论是一种承诺——解决这一承诺,然后才转到下一个职能.

#### 参数

以下所列论点必须作为分段单独通过.

-   `...fns` - 在链条中任意执行一个参数函数的数量
    
-   `initialArg` - 初始参数,传递到链条中的第一个函数
    

#### 回返

-   返回以承诺包裹的最后值.

#### 接口

```
(fn, fn?, ..., fn?) => (initial_value) => result
```

#### 示例

自动传递出错 :

```javascript
const { railAsync } = require('../../lib/index')

const dbFake = {
  id: 0,
  save() {
    const result = Promise.resolve(this.id)
    this.id++
    return result
  },
}

const addArticle = railAsync(
  (article) => ({ type: 'article', data: article }),
  (entry) => dbFake.save(entry),
  (id) => `Article id: ${id}`,
)

const main = async () => {
  const article = {
    title: 'Railway oriented programming',
    content: 'Lorem ipsum...',
  }

  const result = await addArticle(article)
  console.log(result)
}

main() // -> "Article id: 0"
```

### 铁轨右

工作喜欢 [铁路](https://npmcn.com/package/%40arrows/composition/#readme-rail),但从右到左执行函数.

### rail Right.async (英语)

工作喜欢 [铁轨合成](https://npmcn.com/package/%40arrows/composition/#readme-railAsync),但从右到左执行函数.

### 窃听

执行提供了函数, 提供了参数, 并返回一个参数, 无需更改 .

用于执行空函数而不中断链条 .

_说明: `rail*` 函数自动处理,以便直接使用空函数._

#### 参数

-   `fn` - 任意职能
    
-   `arg` - 任何价值
    

#### 回返

-   返回参数为-is.

#### 接口

```
(fn, arg) => arg
```

示例

```js
const { pipe, tap } = require('../../lib/index')
const { sort, reduce, _rest, _butLast } = require('@arrows/array')

const sumNotes = pipe(
  sort((a, b) => a - b),
  tap(console.log),
  _rest,
  tap(console.log),
  _butLast,
  tap(console.log),
  reduce((a, b) => a + b, 0),
)

const notes = [16, 17.5, 19, 15, 18]

console.log(sumNotes(notes))

/* Output:
[ 15, 16, 17.5, 18, 19 ]
[ 16, 17.5, 18, 19 ]
[ 16, 17.5, 18 ]
51.5
*/
```

## 许可证

项目处于开放式、非限制性 [ISC 许可证](https://npmcn.com/package/%40arrows/composition/LICENSE)。 。 。 .

## 常见问题

### @arrows/composition 是什么？

职能构成图书馆

### 如何安装 @arrows/composition？

在项目目录运行 npm install @arrows/composition，或使用 pnpm add / yarn add / bun add。

### @arrows/composition 支持 TypeScript 吗？

该包的 package\.json 没有声明类型入口。需要类型提示时，可以在 npm 上查找社区维护的 @types/arrows\_\_composition 是否存在。

### @arrows/composition 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @arrows/composition 可以商用吗？

@arrows/composition 使用 ISC 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @arrows/composition 的下载量有多少？

根据 npm 官方下载统计，@arrows/composition 在 2026/10/07 – 2026/11/05 这 30 天内被下载 237,228 次，日均约 7,908 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @arrows/composition 还在维护吗？

最新版本 1\.2\.2 发布于 2020/01/23（6 年前）；至今共发布 16 个版本，其中过去 12 个月发布了 0 个；首次发布于 2019/09/17。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

