ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

es-toolkit/fp 的 find 函数:在 pipe 管道中查找第一个匹配值

es-toolkit/fp 的 find 函数:在 pipe 管道中查找第一个匹配值 es-toolkit/fp 的 find 函数在 pipe 管道中查找第一个匹配值【免费下载链接】es-toolkitA modern JavaScript utility library thats 2-3 times faster and up to 97% smaller, a major upgrade to lodash.项目地址: https://gitcode.com/GitHub_Trending/es/es-toolkites-toolkit/fp提供了一组遵循数据最后data-last约定的函数式编程工具find就是其中之一它接收一个predicate判定函数返回一个把数组映射为第一个匹配值的新函数专门用于嵌入pipe管道。本文基于 docs/ja/fp/reference/find.md对应英文版 docs/fp/reference/find.md结合仓库源码与测试用例讲解find的用法、参数约定、类型签名以及底层实现原理帮助你写出可读性更高、更易组合的链式数据处理代码。什么是 es-toolkit/fp 的 find在es-toolkit主库中find是一个直接接收数组和判定函数的普通工具函数而在es-toolkit/fp子模块中find被改写为柯里化、数据最后的形式先接收predicate返回一个等待数组传入的函数。这样它就能作为pipe管道中的一步与其他操作符自然衔接const result pipe(array, find(predicate));原文档明确指出这个辅助函数是es-toolkit/fp专用的。当你需要把查找第一个匹配值作为pipe管道中的一个步骤时才使用它如果只是简单地对单个数组做查找直接使用主库的数组 API 即可。使用示例find会返回被管道传入的数组中predicate返回true的第一个值如果没有值匹配则返回undefinedimport { find, pipe } from es-toolkit/fp; pipe( [1, 2, 3, 4], find(value value 2) ); // 3在这个例子中管道从[1, 2, 3, 4]开始find(value value 2)依次测试每个元素3是第一个满足value 2的值因此结果为3。该示例同时出现在官方文档与单元测试 src/fp/array/find.spec.ts 中是验证find行为的最直接依据。参数与返回值参数predicate(value: T, index: number) boolean用于逐个测试每个值的判定函数。文档给出的签名只列了value与index两个参数而从源码 src/fp/array/find.ts 的重载签名可以看到实际传给predicate的还包括完整的输入数组即(value: T, index: number, array: readonly T[]) boolean——这与原生Array.prototype.find的回调签名保持一致让你在需要时也能访问整个源数组。返回值(array: readonly T[]) T | undefined一个将readonly T[]映射为第一个匹配值或undefined的函数。也就是说find(predicate)本身不执行查找它只是构建一个等待数组输入的函数真正的查找发生在pipe把数组传入的那一刻。源码实现本质是对 Array.prototype.find 的柯里化封装es-toolkit/fp的find实现非常精简核心逻辑位于 src/fp/array/find.tsexport function findT( predicate: (value: T, index: number, array: readonly T[]) boolean ): (array: readonly T[]) T | undefined { return function (array: readonly T[]): T | undefined { return array.find(predicate); }; }可以看到返回的闭包直接委托给原生Array.prototype.find。这带来两个重要事实行为与原生find完全一致包括从左到右的遍历顺序、命中即停的短路特性、找不到时返回undefined的约定无需额外实现任何查找逻辑也不引入性能开销。它是急切eager执行的函数与map、filter、take等惰性操作符不同find上没有挂载.lazy变换因此在 src/fp/_internal/lazy.ts 定义的OperatorFunction结构中它会被pipe归入非惰性分组逐函数依次执行。类型守卫重载让返回类型更精确find还提供了基于TypeScript 类型守卫type guard的重载版本这是文档签名之外的一个亮点。当predicate写成value is S形式的守卫函数时返回类型会被收窄为S | undefined而不是宽泛的T | undefinedimport { find, pipe } from es-toolkit/fp; const isString (value: string | number): value is string typeof value string; pipe([1, a, 2], find(isString)); // a且 TypeScript 推断其类型为 string | undefined这一重载同样定义在 src/fp/array/find.ts 中官方注释给出的示例正是上述联合类型数组的查找场景。使用类型守卫后后续管道步骤对结果类型的处理会得到编译期的类型保障减少不必要的类型断言。在 pipe 管道中组合使用find最典型的应用场景是作为管道的终止步骤承接前面map、filter、sortBy等操作符的结果。例如import { pipe, map, filter, find } from es-toolkit/fp; interface User { id: number; name: string; active: boolean; } const users: User[] [ { id: 1, name: Alice, active: false }, { id: 2, name: Bob, active: true }, { id: 3, name: Carol, active: true }, ]; const firstActiveName pipe( users, filter(user user.active), map(user user.name), find(name name.startsWith(B)) ); // Bob管道将users依次经过filter、map、find三个步骤最终得到第一个以B开头的活跃用户名。这种写法把过滤 → 映射 → 查找的意图自上而下地展示出来避免了多层嵌套回调也无需引入临时变量。从 src/fp/pipe.ts 的实现看pipe会把传入的函数按是否惰性切分为若干组连续的惰性函数如map、filter、take会被融合成单次遍历而find属于非惰性函数会被单独顺序执行其输入是前面步骤产出的数组输出则直接作为管道的最终结果。与 find 相关的其他 fp 工具围绕查找这一主题es-toolkit/fp还提供了多个与find配套的函数统一从 src/fp/array/index.ts 导出函数返回内容无匹配时的行为find第一个匹配的值undefinedfindIndex第一个匹配值的下标-1findLast最后一个匹配的值undefinedfindLastIndex最后一个匹配值的下标-1例如findIndex的实现见 src/fp/array/findIndex.ts同样采用>import { findIndex, pipe } from es-toolkit/fp; pipe([{ id: 1 }, { id: 2 }], findIndex(item item.id 2)); // 1如果需要在find的predicate之前先对数组做变换可以配合map、sortBy等操作符在管道中先行处理如果需要从右侧开始查找则使用findLast。迭代器版本处理非数组的可迭代数据对于Iterator输入es-toolkit/fp在iterator子模块下还提供了对应的find见 src/fp/iterator/find.ts它委托给Iterator.prototype.find在第一个匹配处立即停止拉取迭代器import { pipe } from es-toolkit/fp; import { find } from es-toolkit/fp/iterator; pipe([1, 2, 3, 4].values(), find(x x 2)); // 3这对处理生成器、Set等可迭代数据源特别有用也体现了es-toolkit/fp对多种数据形态的覆盖。测试验证仓库用 Vitest 编写了find的单元测试src/fp/array/find.spec.tsimport { describe, expect, it } from vitest; import { find } from ./find.ts; import { pipe } from ../pipe.ts; describe(find, () { it(works in a pipe, () { expect( pipe( [1, 2, 3, 4], find(value value 2) ) ).toEqual(3); }); });该测试直接验证了在管道中工作的find这一核心契约管道传入[1, 2, 3, 4]find返回第一个大于 2 的值3。运行测试的方式与仓库其他单元测试一致例如在仓库根目录执行yarn test或通过 Vitest 运行对应 spec 文件。小结find是es-toolkit/fp专有的 contenteditable="false">【免费下载链接】es-toolkitA modern JavaScript utility library thats 2-3 times faster and up to 97% smaller, a major upgrade to lodash.项目地址: https://gitcode.com/GitHub_Trending/es/es-toolkit创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表