@ arrows/ 组合
目录
导言
库中包含一个更高排序的函数集合,用于将您的应用程序编组为可重复使用的函数.
所有类似链的函数都是手动块(分块). 允许某个功能接受某一段中数量变化多端的争论.
图书馆有 内置类型定义,提供了极佳的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 许可证。 。 。 .