Skip to content

es-toolkit/compat

es-toolkit/compat 提供与 Lodash 完全一致的接口和行为。它的目标是让你保留现有 Lodash 代码的调用方式,逐步迁移到 es-toolkit

如果你的项目本来就不使用 Lodash,请使用 es-toolkit

✅ 自 v1.39.3 起保证与 Lodash 100% 兼容

es-toolkit/compat 直接通过 Lodash 自身的测试代码。行为完全相同,但更小、更快。

ts
// 与 lodash 相同的调用方式,可以直接在 es-toolkit/compat 中使用
import { chunk } from 'es-toolkit/compat';

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

迁移流程

从现有代码中移除 Lodash 时,推荐按以下顺序操作:

  1. lodash / lodash-es 的 import 路径替换为 es-toolkit/compat。调用代码可以保持不变。
  2. 慢慢整理调用代码,并将 import 切换为 es-toolkit。完成迁移后,你将获得更小的包体积和更快的运行速度。

es-toolkit 的区别

  • API 形态:与 Lodash 1:1 一致,包括隐式类型转换、多种参数形态以及已弃用的辅助函数。es-toolkit 只暴露类型安全的现代形态。
  • 包体积与性能:比 es-toolkit 稍大、稍慢,因为内部包含了与 Lodash 对齐行为所需的额外处理。
  • 已弃用函数:Lodash 中已弃用的函数仍保留在 compat 中以保持兼容,但不会加入 es-toolkit。建议在迁移过程中一并清理。

各函数的详细文档请参见 兼容性参考

设计原则

INFO

设计原则可能会有所变化。

es-toolkit/compat 的目标是为以下功能提供与 lodash 100% 相同的实现:

  • 在 lodash 中作为测试用例编写的功能。
  • 可以从 @types/lodash@types/lodash-es 类型推断的功能。
  • 在将代码从 lodash 迁移到 es-toolkit 时识别出的功能差异(请将这些差异报告到我们的问题页面)。

但是,以下内容超出 es-toolkit/compat 的范围:

  • 隐式类型转换,例如将空字符串转换为零或 false。
  • 针对特定类型数组具有专门实现的函数,如 sortedUniq
  • 处理修改了内置对象(如 Array.prototype)原型的情况。
  • 处理 JavaScript Realm 的情况。
  • 通过 "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

采用 MIT 许可证发布。