@arrows/composition

中文文档
1.2.2发布于 2020/01/23ISCCommonJS零依赖近 30 天下载 23.7万

职能构成图书馆

中文使用文档
文档目录16 节
  1. 目录
  2. 导言
  3. 安装
  4. API 参考
  5. 链式
  6. 链式右
  7. 编曲
  8. 咖喱
  9. 烟斗
  10. 现在
  11. 铁路
  12. 铁路. async
  13. 铁轨右
  14. rail Right.async (英语)
  15. 窃听
  16. 许可证

arrows - composition

@ arrows/ 组合

npm (scoped) CircleCI David (path) Codecov npm bundle size (scoped) GitHub

目录

  1. 导言
  2. 安装
  3. API 参考
  4. 许可证

导言

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

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

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

安装

通过国家预防机制:

npm i @arrows/composition

维亚·亚恩:

yarn add @arrows/composition

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

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

API 参考

链式

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

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

参数

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

  • wrappingFn - 双参数函数,负责在链中执行每个函数.

  • ...fns - 在链条中任意执行一个参数函数的数量

  • initialArg - 初始参数,传递到链条中的第一个函数(通过包接函数)

折叠函数的参数 :

  • fn - 链中函数

  • input - 现值

  • isLast - 布尔旗表示一个函数是否是链条上最后一个函数

回返

  • 返回最终值.

接口

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

实例

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)

链式右

工作喜欢 链式,但从右到左执行函数.

参数

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

  • wrappingFn - 双参数函数,负责在链中执行每个函数.

  • ...fns - 在链条中任意执行一个参数函数的数量

  • initialArg - 初始参数,传递到链条中的第一个函数(通过包接函数)

折叠函数的参数 :

  • fn - 链中函数

  • input - 现值

  • isLast - 布尔旗表示一个函数是否是链条上最后一个函数

回返

  • 返回最终值.

接口

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

示例

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

示例

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

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

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

咖喱

环绕功能为自动曲折的 .

参数

  • fn - 任意职能

回返

  • 返回函数的曲尾版本.

接口

(fn) =>  curried_fn

示例

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

示例

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

示例

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

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

铁路

工作喜欢 烟斗,但还有:

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

参数

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

  • ...fns - 在链条中任意执行一个参数函数的数量

  • initialArg - 初始参数,传递到链条中的第一个函数

回返

  • 返回最终值.

接口

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

实例

自动传递出错 :

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数字 :

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

类似工程 铁路但是,另外,如果争论是一种承诺——解决这一承诺,然后才转到下一个职能.

参数

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

  • ...fns - 在链条中任意执行一个参数函数的数量

  • initialArg - 初始参数,传递到链条中的第一个函数

回返

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

接口

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

示例

自动传递出错 :

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"

铁轨右

工作喜欢 铁路,但从右到左执行函数.

rail Right.async (英语)

工作喜欢 铁轨合成,但从右到左执行函数.

窃听

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

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

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

参数

  • fn - 任意职能

  • arg - 任何价值

回返

  • 返回参数为-is.

接口

(fn, arg) => arg

示例

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 许可证。 。 。 .

版本与发布记录

@arrows/composition 共发布 16 个版本,首次发布于 2019/09/17;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.2.2latest2020/01/236 年前
1.2.12019/11/067 年前
1.2.02019/11/067 年前
1.1.22019/11/027 年前
1.1.12019/10/307 年前
1.1.02019/10/307 年前

关于 @arrows/composition 的常见问题

@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 和提交记录判断。