currino
v1.0.2
Published
currino
Downloads
14
Readme
Currino
函数式风格的 TypeScript 实用工具库
📦 安装
通过 npm:
npm install currino
通过 unpkg CDN:
<script src="https://unpkg.com/[email protected]/dist/currino.iife.js"></script>
🔨 使用
ESM:
import { sum } from 'currino';
Node.JS:
const { sum } = require('currino');
Browser:
Currino.sum(1, 1);
📚 API
列表
forEach
forEach(iteratee, arrayLink);
调用 iteratee
遍历 arrayLink
中的每个元素, iteratee 调用 3 个参数: (value, index, arrayLink)。 如果迭代函数(iteratee)显式的返回 false ,迭代会提前退出。
参数
iteratee (Function): 每次迭代调用的函数。
arrayLink (ArrayLink): 一个用来迭代的列表。
返回
(*): 返回列表 arrayLink
。
示例
forEach(
(value) => {
console.log(value);
},
[1, 2]
);
// => 输出 '1' 和 '2'。
forEachRight
forEachRight(iteratee, arrayLink);
这个方法类似 forEach
,不同之处在于,forEachRight
是从右到左遍历每一个元素。
参数
iteratee (Function): 每次迭代调用的函数。
arrayLink (ArrayLink): 一个用来迭代的列表。
返回
(*): 返回列表 arrayLink
。
示例
forEachRight(
(value) => {
console.log(value);
},
[1, 2]
);
// => 输出 '2' 和 '1'。
对象
mapKeys
mapKeys(mapping, object);
这个方法创建一个对象,对象的值与object
相同,并且 key 是通过mapping
每个自身可枚举属性的字符串产生的。如果mapping
是个函数,那么它调用 3 个参数:(value, key, object)。
参数
mapping (Object|Function): 每次迭代时获取 key 的映射。
object (Object): 一个用来迭代的对象。
返回
(*): 返回新对象。
示例
mapKeys({ b: 'c' }, { a: 1, b: 2 });
// => { a: 1, c: 2 }
mapKeys(
(value, key) => {
return key + value;
},
{ a: 1, b: 2 }
);
// => { a1: 1, a2: 2 }
树
eachTree
eachTree(iteratee, childrenKey, tree);
以深度优先的方式,调用 iteratee
遍历 tree
(集合) 中的每个节点, iteratee 调用 3 个参数: (node, path, tree)。 如果迭代函数(iteratee)显式的返回 false ,迭代会提前退出。
参数
iteratee (Function): 每次迭代调用的函数。
childrenKey (string): 子节点属性名。
tree (Object): 一个用来迭代的树。
返回
(*): 返回树 tree
。
示例
const tree = {
id: 1,
children: [
{
id: 2,
children: [{ id: 4 }],
},
{ id: 3 },
],
};
eachTree(
(node) => {
console.log(node);
},
'children',
tree
);
// => 依次输出 { id: 1 } 、{ id: 2 } 、{ id: 4 } 、{ id: 3 }。
findTree
findTree(predicate, childrenKey, tree);
遍历 tree
(树)节点,返回 predicate
(断言函数)第一个返回真值的第一个元素。predicate 调用 1 个参数: (node)。
参数
predicate (Function): 每次迭代调用的函数。
childrenKey (string): 子节点属性名。
tree (Object): 一个用来迭代的树。
返回
(*): 返回匹配节点,否则返回undefined
。
示例
const tree = {
id: 1,
children: [{ id: 2 }],
};
findTree((node) => node.id === 2, 'children', tree);
// => 输出 { id: 2 }。
mapTree
mapTree(iteratee, childrenKey, tree);
创建一个树,节点是iteratee
遍历tree
中的每个节点后返回的结果。iteratee 调用 1 个参数:(node)。
参数
iteratee (Function): 每次迭代调用的函数。
childrenKey (string): 子节点属性名。
tree (Object): 一个用来迭代的树。
返回
(*): 返回新的映射后树。
示例
const tree = {
id: 1,
children: [{ id: 2 }],
};
mapTree((node) => ({ key: node.id, children: node.children }), 'children', tree);
// => 输出 { key: 1, children: [{ key: 2 }] }。
searchTree
searchTree(predicate, childrenKey, tree);
遍历 tree
(树)节点,返回 predicate
(断言函数)所有匹配节点的路径。predicate 调用 1 个参数: (node)。
参数
predicate (Function): 每次迭代调用的函数。
childrenKey (string): 子节点属性名。
tree (Object): 一个用来迭代的树。
返回
(*): 返回匹配节点的路径节点,否则返回空数组。
示例
const tree = {
id: 1,
children: [
{ id: 2 },
{
id: 3,
children: [{ id: 4 }],
},
],
};
searchTree((node) => node.id === 4, 'children', tree);
// => 输出 { id: 1, children: [{ id: 3, children: [{ id: 4 }] }] }。
Promise
scheduler
这个方法可以控制异步函数的并发数,方法将在所有异步结束后,返回异步的结果集。
scheduler(max, tasks);
参数
max (number):最大并发数。
tasks (Function[]):异步函数集合。
返回
(*):一个 Promise,终值为所有异步函数执行的结果集。
示例
const tasks = [
() => {
return new Promise((resolve) => {
setTimeout(() => resolve(1), 200);
});
},
() => {
return new Promise((resolve, reject) => {
setTimeout(() => reject(2), 100);
});
},
];
scheduler(2, tasks); // => 输出 Promise<[{ status: 'fulfilled', value: 1 }, { status: 'rejected', reason: 2 }]>
实用函数
sleep
sleep(1000);
同步阻塞程序,等待一定时间后再执行(也许会有用 😝)。
参数
wait (number):需要睡眠的毫秒数。
示例
sleep(1000);
console.log('hello currino!'); // => 延迟一秒后输出 hello currino!
函子
Identity
Identity(value);
创建一个基础 Functor。
参数
value (*): 容器内的值。
返回
(*): 返回 Functor
。
示例
Identity(1)
.map((x) => x + 1)
.fold((x) => x);
// => 2
Maybe
这个方法类似 Identity
,不同之处在于,Maybe
可以处理空值。
参数
value (*): 容器内的值。
返回
(*): 返回 Maybe
。
示例
Maybe(null)
.map((x) => x + 1)
.fold((x) => x);
// => null
Either
Either 内部有两个值:左值(Left
)和右值(Right
)。右值是正常情况下使用的值,左值是右值不存在时使用的默认值。
参数
left (*): 左值。
right (*): 右值。
返回
(*): 返回 Either
。
示例
Either(1, 2)
.map((x) => x + 1)
.fold((x) => x);
// => 3
Either(1, null)
.map((x) => x + 1)
.fold((x) => x);
// => 1