Skip to content

与 Lodash 兼容性 ​

tsx
// es-toolkit/compat 的目标是提供与 lodash 百分之百的功能兼容性
import { chunk } from 'es-toolkit/compat';

chunk([1, 2, 3, 4], 0);
// 返回 [], 与 lodash 完全相同

为了最大限度地兼容 lodash,请使用 es-toolkit/compat,这是一个弥合这两个库之间差距的兼容性层。

该模块旨在提供与 lodash 相同的 API,使得在这两个库之间能够更加容易地进行切换。

es-toolkit/compat 已经对 lodash 的实际测试用例进行了全面测试。

需要注意的是,与原始 es-toolkit 相比,es-toolkit/compat 可能会对性能产生轻微影响,并且包大小可能会更大。该模块旨在促进平滑过渡,一旦迁移完成,应替换回原始的 es-toolkit 以获得最佳性能。

设计原则 ​

INFO

设计原则可能会发生变化。

我们的兼容层旨在实现以下功能百分之百的特性一致性:

  • 作为 lodash 测试用例编写的功能。
  • 可以从 @types/lodash 或 @types/lodash-es 的类型推断出的功能。

然而,以下内容不在 es-toolkit/compat 的范围之内:

  • 隐式类型转换,例如将空字符串转换为零或假。
  • 对特定类型数组有专门实现的函数,比如 sortedUniq。
  • 处理内部对象原型(例如 Array.prototype)被修改的情况。
  • 处理涉及 JavaScript 领域(Realms)的情况。
  • 通过 "Seq" 方法支持方法链。

实现状态 ​

INFO

以下表情符号表示每个功能的状态:

  • ✅: 已完成(该功能已完全实现,并通过了所有lodash测试代码。)
  • 📝: 审查中(该功能已实现,但尚未使用lodash测试代码进行测试。)
  • ❌: 未实现(该功能尚未实现。)

即使某个功能标记为“审查中”,它也可能已经在审查中,以确保其与lodash完全一致,并且可能已经提供了相同的功能。

"Array" 工具

函数名称实现状态
chunk✅
compact✅
concat✅
difference✅
differenceBy✅
differenceWith✅
drop✅
dropRight✅
dropRightWhile✅
dropWhile✅
fill✅
findIndex✅
findLastIndex✅
flatten✅
flattenDeep✅
flattenDepth✅
fromPairs✅
head✅
indexOf✅
initial✅
intersection✅
intersectionBy✅
intersectionWith📝
join✅
last✅
lastIndexOf✅
nth✅
pull✅
pullAll✅
pullAllBy❌
pullAllWith❌
pullAt📝
remove✅
reverse✅
slice✅
sortedIndex不支持
sortedIndexBy不支持
sortedIndexOf不支持
sortedLastIndex不支持
sortedLastIndexBy不支持
sortedLastIndexOf不支持
sortedUniq不支持
sortedUniqBy不支持
tail✅
take✅
takeRight✅
takeRightWhile✅
takeWhile📝
union✅
unionBy📝
unionWith📝
uniq✅
uniqBy✅
uniqWith📝
unzip✅
unzipWith📝
without✅
xor📝
xorBy📝
xorWith📝
zip✅
zipObject📝
zipObjectDeep✅
zipWith📝

"Collection" 工具

函数名称实现状态
countBy📝
every✅
filter✅
find✅
findLast❌
flatMap📝
flatMapDeep📝
flatMapDepth❌
forEach✅
forEachRight📝
groupBy📝
includes✅
invokeMap❌
keyBy📝
map✅
orderBy✅
partition📝
reduce❌
reduceRight❌
reject❌
sample✅
sampleSize📝
shuffle📝
size✅
some✅
sortBy✅

"Date" 工具

函数名称实现状态
now✅

"Function" 工具

函数名称实现状态
after✅
ary✅
before✅
bind✅
bindKey✅
curry✅
curryRight✅
debounce✅
defer✅
delay✅
flip✅
memoize📝
negate✅
once✅
overArgs❌
partial📝
partialRight📝
rearg✅
rest✅
spread✅
throttle✅
unary✅
wrap❌

"Lang" 工具

函数名称实现状态
castArray✅
clone📝
cloneDeep✅
cloneDeepWith✅
cloneWith❌
conformsTo✅
eq✅
gt✅
gte✅
isArguments✅
isArray✅
isArrayBuffer✅
isArrayLike✅
isArrayLikeObject✅
isBoolean✅
isBuffer✅
isDate✅
isElement✅
isEmpty✅
isEqual✅
isEqualWith✅
isError✅
isFinite✅
isFunction✅
isInteger✅
isLength✅
isMap✅
isMatch✅
isMatchWith❌
isNaN✅
isNative❌
isNil✅
isNull✅
isNumber✅
isObject✅
isObjectLike✅
isPlainObject✅
isRegExp✅
isSafeInteger✅
isSet✅
isString✅
isSymbol✅
isTypedArray✅
isUndefined✅
isWeakMap✅
isWeakSet✅
lt✅
lte✅
toArray✅
toFinite✅
toInteger✅
toLength✅
toNumber✅
toPlainObject✅
toSafeInteger✅
toString✅

"Math" 工具

函数名称实现状态
add✅
ceil✅
divide❌
floor✅
max✅
maxBy📝
mean📝
meanBy📝
min✅
minBy📝
multiply✅
round✅
subtract✅
sum✅
sumBy✅

"Number" 工具

函数名称实现状态
clamp✅
inRange✅
random✅

"Object" 工具

函数名称实现状态
assign❌
assignIn✅
assignInWith❌
assignWith❌
at📝
create❌
defaults✅
defaultsDeep❌
findKey✅
findLastKey❌
forIn❌
forInRight❌
forOwn❌
forOwnRight❌
functions❌
functionsIn❌
get✅
has✅
hasIn❌
invert✅
invertBy✅
invoke✅
keys✅
keysIn✅
mapKeys✅
mapValues✅
merge✅
mergeWith✅
omit✅
omitBy📝
pick✅
pickBy📝
result❌
set✅
setWith❌
toPairs❌
toPairsIn❌
transform❌
unset✅
update❌
updateWith❌
values❌
valuesIn❌

"String" 工具

函数名称实现状态
camelCase✅
capitalize✅
deburr✅
endsWith✅
escape✅
escapeRegExp✅
kebabCase✅
lowerCase✅
lowerFirst✅
pad✅
padEnd✅
padStart✅
parseInt✅
repeat✅
replace✅
snakeCase✅
split❌
startCase✅
startsWith✅
template✅
toLower✅
toUpper✅
trim✅
trimEnd✅
trimStart✅
truncate❌
unescape✅
upperCase✅
upperFirst✅
words✅

"Util" 工具

函数名称实现状态
attempt✅
bindAll❌
cond❌
conforms✅
constant✅
defaultTo✅
flow✅
flowRight✅
identity✅
iteratee✅
matches✅
matchesProperty✅
method✅
methodOf✅
mixin不支持
noConflict不支持
noop✅
nthArg✅
over❌
overEvery❌
overSome❌
property✅
propertyOf✅
range✅
rangeRight✅
runInContext不支持
stubArray✅
stubFalse✅
stubObject✅
stubString✅
stubTrue✅
times✅
toPath✅
uniqueId✅