
es-toolkit/fp 的at函数在 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是 es-toolkit 面向函数式编程的入口其中的at用于创建一个按指定索引从数组中取值的函数供pipe管道组合使用。本文围绕 docs/ja/fp/reference/at.md 展开结合源码与测试讲解其用法、参数、边界行为与底层实现帮助你在函数式数据流中高效提取数组片段。一、at在 fp 体系中的定位在 es-toolkit 中at有两个版本主库版本es-toolkit的at位于 src/array/at.ts直接以at(array, indices)的形式调用fp 版本es-toolkit/fp的at位于 src/fp/array/at.ts采用数据最后data-last的柯里化形态先接收indices配置返回一个等待数组传入的函数专门与pipe一起使用。fp 版本的基本形态如下const result pipe(array, at(indices));为什么需要 fp 版本因为pipe的每个步骤都必须是接收上一个步骤输出的一元函数。主库的at(array, indices)需要同时拿到数组和索引无法直接塞进管道fp 版先固定索引配置返回的(array) T[]正是管道步骤所需的一元函数形态。使用建议来自官方文档在普通命令式代码中推荐使用原版at只有在用pipe串联变换时才使用这个fp版本。二、基本用法import { at, pipe } from es-toolkit/fp; pipe([a, b, c], at([0, -1])); // [a, c]at从被管道传入的数组中依次读取indices中每个索引位置的值。上面的例子读取了第 0 个元素a和倒数第 1 个元素c得到新数组[a, c]。从源码结构看src/fp/array/at.tsfp 版本身只是一个薄封装export function atT(indices: readonly number[]): (array: readonly T[]) T[] { return function (array: readonly T[]): T[] { return atToolkit(array, indices); }; }它接收indices返回闭包函数闭包被调用时把参数原样转交给主库的atToolkit。这意味着 fp 版与主库版共享同一套取值逻辑行为完全一致只是调用方式不同。三、参数与返回值参数indicesnumber[]要读取的索引数组。支持负数索引——负数从数组末尾往前数语义与原生Array.prototype.at一致详见下文。注意源码中的类型签名为readonly number[]src/fp/array/at.ts因此传入只读数组或普通数组均可。返回值(array: readonly T[]) T[]一个将readonly T[]映射为所选值组成的新数组的函数。该函数正是pipe步骤所要求的形态。完整签名对比版本签名用途主库atatT(arr, indices): T[]直接传入数组调用fpatatT(indices): (array) T[]在pipe中作为步骤使用四、负数索引与边界行为负数索引负数索引从数组末尾倒数与Array.prototype.at一致pipe([a, b, c], at([-1, -2])); // [c, b]边界情况有测试用例佐证主库实现src/array/at.ts以及测试文件 src/array/at.spec.ts 共同确定了以下行为索引顺序决定结果顺序at([a, b, c], [2, 0])返回[c, a]——结果数组的顺序跟随indices的书写顺序而非原数组顺序。不存在的索引返回undefinedat([a, b, c], [2, 4, 0, -4])返回[c, undefined, a, undefined]索引 4 越界、-4 超出负数范围时均得到undefined。空索引数组返回空数组at([a, b, c], [])返回[]。非整数索引的底层处理主库实现中有一行值得关注的处理src/array/at.tsindex Number.isInteger(index) ? index : Math.trunc(index) || 0;整数索引直接使用非整数索引先做Math.trunc截断若截断结果为0例如1.5截断为1、-1.5截断为-1之外的NaN、Infinity等则回退为0。对应测试src/array/at.spec.ts验证了1.5、-1.5、NaN、Infinity、-Infinity等输入与原生Array.prototype.at结果一致const data [a, b, c]; const indices [1.5, -1.5, NaN, Infinity, -Infinity]; expect(at(data, indices)).toEqual(indices.map(i data.at(i)));负数索引的换算逻辑底层对负数索引做了显式换算src/array/at.tsif (index 0) { index length; }即index length换算成从 0 开始的正向索引随后直接按arr[index]取值换算后仍为负数或越界时自然得到undefined。五、在 pipe 管道中的实战组合at常与filter、map、take等 fp 函数组合实现先变换、再按位置取样的完整数据流。例如import { at, map, pipe } from es-toolkit/fp; const result pipe( [10, 20, 30, 40, 50], map((x: number) x * 2), // [20, 40, 60, 80, 100] at([1, -1]) // [40, 100] ); // [40, 100]也可以与filter、sortBy等组合从处理后的结果中抽取特定位置import { at, filter, pipe } from es-toolkit/fp; pipe( [1, 2, 3, 4, 5, 6], filter((x: number) x % 2 0), // [2, 4, 6] at([0, 2]) // [2, 6] ); // [2, 6]pipe的实现位于 src/fp/pipe.ts它从左到右依次把每个步骤的输出喂给下一步骤src/fp/pipe.ts。当连续步骤中出现map、filter、take等具备懒求值能力的函数时pipe会将其融合为单次遍历并支持提前终止src/fp/pipe.ts而at属于普通步骤在管道中按序执行不会改变其按索引取值的语义。六、测试与验证fp 版at的管道行为有专门测试覆盖src/fp/array/at.spec.tsimport { describe, expect, it } from vitest; import { at } from ./at.ts; import { pipe } from ../pipe.ts; describe(at, () { it(works in a pipe, () { expect(pipe([a, b, c], at([0, -1]))).toEqual([a, c]); }); });而主库at的更全面行为索引顺序、负数、越界、空索引、非整数由 src/array/at.spec.ts 覆盖。由于 fp 版最终委托给主库实现两套测试共同保证了 fp 版在管道内外的一致性。七、小结es-toolkit/fp的at返回一个按索引取值的一元函数专用于pipe管道支持负数索引从末尾倒数行为与Array.prototype.at一致结果顺序跟随indices顺序越界索引返回undefined空索引数组返回空数组非整数索引按截断规则处理普通代码中请使用主库at管道组合时才使用 fp 版本其实现是对主库 src/array/at.ts 的薄封装行为完全一致测试覆盖完备。更多 fp 函数用法可参考 es-toolkit/fp 入门文档 与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-toolkit创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考