Skip to content

es-toolkit/compat ​

es-toolkit/compatは、Lodashと同じインターフェースと動作を提供するモジュールです。Lodashを使っている既存のコードをそのままにしたまま、少しずつes-toolkitへ移行できるように作られています。

既存のプロジェクトでLodashを使っていないなら、es-toolkitを使ってください。

✅ 1.39.3からLodashと100%の互換性を保証しています

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に切り替えてください。すべて移行できれば、バンドルがより小さく、より高速になります。

関数を個別にインポートする ​

lodash/mergeと同じように、compatのすべての関数は関数ごとのエントリーポイントからもインポートできます。es-toolkit/compat全体ではなく、その関数に必要なファイルだけが読み込まれます。

ts
import merge from 'es-toolkit/compat/merge';

CommonJSのrequire()やReact Nativeのようにツリーシェイキングが使えない環境や、バンドラーなしでNode.js上で直接実行するコードで特に役立ちます。

ts
const merge = require('es-toolkit/compat/merge');

es-toolkitとの違い ​

  • APIの形: Lodashと1:1で一致しています。暗黙的な型変換、さまざまな引数の形、非推奨のヘルパーまでそのまま含まれます。es-toolkitは型安全で整理された形だけを提供します。
  • バンドルサイズと速度: es-toolkitより少し大きく、少し遅いです。Lodashと動作を合わせるための追加処理が入っているためです。
  • 非推奨の関数: Lodashで非推奨になった関数も互換性のためにcompatには残っていますが、es-toolkitには含まれません。マイグレーション中に一緒に整理してください。

関数ごとの詳細なドキュメントは互換性リファレンスをご覧ください。

設計原則 ​

INFO

es-toolkit/compatの設計原則の方向性は変わる可能性があります。

es-toolkit/compatは、次の機能についてlodashと100%同じ機能を提供することを目指しています。

  • lodashのテストケースとして書かれている機能
  • @types/lodashまたは@types/lodash-esの型から推測できる機能
  • lodashからes-toolkitへコードをマイグレーションする際に見つかった機能の違い(Issuesページに報告してください)

ただし、以下はes-toolkit/compatの対象外です。

  • 暗黙的な型変換: 空文字列を0またはfalseに変換するような動作
  • 特殊なケースに最適化された実装: sortedUniqのように、ソートされた配列だけを受け取る関数
  • Array.prototypeのような組み込みオブジェクトのプロトタイプが変更されたケースへの対応
  • JavaScript Realmへの対応
  • メソッドチェーン: _(arr).map(...).filter(...)のようなメソッドチェーン

実装状況 ​

INFO

以下の絵文字で、各機能の現在の状態を表しています。

  • ✅: 完了(実装されており、lodashのテストコードをすべて通過しています)
  • 📝: レビュー中(実装されていますが、lodashのテストコードでテストされていません)
  • ❌: 未実装

「レビュー中」と書かれていても、すでにlodashと100%同じ機能を提供している場合があります。

"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 ライセンスの下で配布されています。