Skip to content

chain

创建按顺序运行的函数链

93 bytes
since v12.1.0

使用方法

链接函数将使它们依次执行,将每个函数的输出作为下一个函数的输入传递,在链的末尾返回最终输出。

import * as _ from "radashi";
const add = (y: number) => (x: number) => x + y;
const multiply = (y: number) => (x: number) => x * y;
const addFive = add(5);
const double = multiply(2);
const chained = _.chain(addFive, double);
chained(0); // => 10
chained(7); // => 24

示例

import * as _ from "radashi";
type Deity = {
name: string;
rank: number;
};
const gods: Deity[] = [
{ rank: 8, name: "Ra" },
{ rank: 7, name: "Zeus" },
{ rank: 9, name: "Loki" },
];
const getName = (god: Deity) => god.name;
const upperCase = (text: string) => text.toUpperCase() as Uppercase<string>;
const getUpperName = _.chain(getName, upperCase);
getUpperName(gods[0]); // => 'RA'
gods.map(getUpperName); // => ['RA', 'ZEUS', 'LOKI']