ARTICLE DETAIL

资讯详情

深耕网站视觉设计与运营推广的一线实战洞察。

es-toolkit/compat 的 endsWith:Lodash 兼容的字符串结尾判断与原生 `String.prototype.endsWith` 对比

es-toolkit/compat 的 endsWith:Lodash 兼容的字符串结尾判断与原生 `String.prototype.endsWith` 对比 es-toolkit/compat 的 endsWithLodash 兼容的字符串结尾判断与原生String.prototype.endsWith对比【免费下载链接】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本篇指南介绍 es-toolkit 兼容层es-toolkit/compat中endsWith函数的完整用法包括其str、target、position三个参数的语义、null/undefined的容错行为以及它与原生String.prototype.endsWith的性能差异与选型建议。读完本文你将能够在迁移 Lodash 代码时正确使用endsWith并在追求极致性能时安全地替换为原生实现。函数概览endsWith用于检查一个字符串是否以给定的目标字符串结尾是 Lodash_.endsWith的 1:1 兼容实现const result endsWith(str, target);import { endsWith } from es-toolkit/compat;该函数位于 endsWith 源码并从 compat 入口 统一导出与startsWith见 startsWith 文档互为镜像操作。基本用法endsWith的典型场景是判断字符串末尾是否匹配某个子串import { endsWith } from es-toolkit/compat; // 检查字符串是否以指定字符串结尾 endsWith(fooBar, Bar); // Returns: true endsWith(fooBar, foo); // Returns: false注意第二个示例foo虽然出现在fooBar中但不在末尾因此返回false。这一点在 测试用例 中专门验证should return false if the string does not end with the target string, but does contain it。空字符串边界与 Lodash 行为一致空字符串被视为任意字符串的结尾endsWith(fooBar, ); // Returns: true endsWith(, ); // Returns: true对应测试见 endsWith.spec.ts。使用 position 参数限定搜索范围除了常规结尾判断endsWith还支持可选的position参数用于指定搜索结束的位置语义与原生String.prototype.endsWith相同——只检查str在[0, position)这个左闭右开区间内的子串是否以target结尾import { endsWith } from es-toolkit/compat; // 只检查前 3 个字符 foo 是否以 foo 结尾 endsWith(fooBar, foo, 3); // Returns: true // 从位置 5 往前看fooBar 的前 5 个字符 fooBa 并不以 abc 结尾 endsWith(fooBar, abc, 5); // Returns: falseposition的行为细节均有测试佐证见 endsWith.spec.ts默认值省略position时默认使用字符串的完整长度等价于原生endsWith(target)position length只要position不小于字符串长度就等价于完整字符串的结尾判断包括Number.MAX_SAFE_INTEGER和Infinity等极端值负数position会被当作0处理此时除空字符串外通常返回false包括-Infinity非整数position会被向下取整为整数例如endsWith(abc, ab, 2.2)返回true。null / undefined 容错与 Lodash 兼容层保持一致当str或target为null或undefined时endsWith直接返回false而不会抛出异常import { endsWith } from es-toolkit/compat; endsWith(null, test); // Returns: false endsWith(test, null); // Returns: false该行为同样覆盖undefined的各种组合测试见 endsWith.spec.ts。源码实现解析从源码结构看endsWith 实现 的核心逻辑非常精简只有三层export function endsWith(str?: string, target?: string, position?: number): boolean { if (str null || target null) { return false; } if (position null) { position str.length; } return str.endsWith(target, position); }空值守卫str null || target null同时捕获null与undefined直接短路返回false默认位置position未提供时回退为str.length使行为与原生endsWith(target)一致委托原生方法最终调用内置的String.prototype.endsWith(target, position)由引擎完成实际匹配。函数签名中三个参数均为可选str?、target?、position?这是为了在类型层面兼容 Lodash 的宽松调用方式。与之形成对照的是 es-toolkit 主包的严格 APIes-toolkit不提供endsWith它本身在src/string下没有对应实现这类 Lodash 兼容函数只存在于es-toolkit/compat中。为什么不建议直接使用与原生String.prototype.endsWith的对比官方文档在页面顶部明确给出警告优先使用 JavaScript 原生的String.prototype.endsWith。原因在于endsWith兼容函数为了处理null/undefined每次调用都要多执行空值判断与默认位置回退逻辑因此比原生方法更慢。而原生方法在现代 JavaScript 引擎中由底层直接实现性能更好且本身就是 ES2015 标准特性无需任何 polyfill// 推荐直接使用原生方法 fooBar.endsWith(Bar); // true fooBar.endsWith(foo, 3); // true // 仅当你需要 Lodash 兼容语义如 null/undefined 容错时才使用 endsWith(null, test); // false原生写法会直接抛 TypeError两者的关键差异可总结如下维度es-toolkit/compat的endsWith原生String.prototype.endsWithnull/undefined入参返回false不抛错抛TypeError性能稍慢额外的空值检查与位置回退更快引擎原生实现position语义与原生一致默认字符串长度标准语义适用场景迁移 Lodash 代码、处理不可信入参新代码的默认选择在 Lodash 迁移中的定位endsWith属于es-toolkit/compat兼容层。根据 compat 使用指南es-toolkit/compat以 1:1 镜像 Lodash 的接口与行为目的是让已有 Lodash 代码库无需重写调用点即可平滑切换到 es-toolkit之后再逐步清理调用点、迁移到更严格更轻量的es-toolkit主包。因此合理的落地路径是若项目已使用 Lodash 的_.endsWith先把导入路径从lodash替换为es-toolkit/compat调用点保持不变行为完全一致后续清理代码时将endsWith(str, target, position)改写为str.endsWith(target, position)并显式处理可能为空的入参从而获得更小的打包体积与更快的运行速度对于无法保证入参非空、又不想写防御性判断的场景可以继续保留es-toolkit/compat的endsWith它的容错语义正是为此设计的。小结es-toolkit/compat的endsWith是一个轻量、行为与 Lodash 完全一致的字符串结尾判断函数支持position限定范围、对null/undefined安全、空字符串语义正确并有完整的 Vitest 测试套件 覆盖各种边界。它最适合作为 Lodash 迁移过程中的过渡方案而在新代码中直接使用原生String.prototype.endsWith是更现代、更快的选择。【免费下载链接】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),仅供参考
返回列表