Repository navigation
fix(compat/unionWith): match lodash comparator argument order - #2114
Merged
Merged
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
ssi02014
commented
Sep 21, 2026
Comment on lines
+104
to
+107
| // `es-toolkit`'s `uniqWith` invokes the comparator as `(kept, candidate)`, but | ||
| // lodash documents and invokes it as `(candidate, kept)`. Swap the arguments so | ||
| // that asymmetric comparators behave the same as in lodash. | ||
| return uniqWith(flattened, (kept, candidate) => lastValue(candidate, kept)); |
Contributor
Author
There was a problem hiding this comment.
Just like with compat/uniqWith, I added comments
export function uniqWith<T>(arr: ArrayLike<T> | null | undefined, comparator?: Comparator<T>): T[] {
if (!isArrayLike(arr)) {
return [];
}
if (typeof comparator !== 'function') {
return uniqToolkit(Array.from(arr));
}
// `es-toolkit`'s `uniqWith` invokes the comparator as `(kept, candidate)`, but
// lodash documents and invokes it as `(candidate, kept)`. Swap the arguments so
// that asymmetric comparators behave the same as in lodash.
return uniqWithToolkit(Array.from(arr), (kept, candidate) => comparator(candidate, kept));
}
Contributor
Author
There was a problem hiding this comment.
Once the pull request mentioned above is merged, it is recommended that you switch from using uniq/uniqWith to using compat/uniq and compat/uniqWith.
raon0211
approved these changes
Sep 30, 2026
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
es-toolkit/compat'sunionWithpasses the comparator's two arguments in the opposite order from Lodash.Lodash invokes the comparator as
comparator(candidate, kept).compatforwarded the comparator straight toes-toolkit'suniqWith, which invokes it asareItemsEqual(kept, candidate).A symmetric comparator such as
isEqualcannot observe the difference, which is why every existing test passed. An asymmetric one — any comparator wherecomparator(a, b) !== comparator(b, a)— gets the wrong answer, andunionWithcan emit duplicate values, the one thing it exists to prevent.The difference
The
uniqWithfunction ines-toolkit/compatalready resolves this issue by calling the comparator in the same order as Lodash.Type Improvements
isArrayLikeObjectis declared asvalue is object & { length: number }. Because JavaScript functions are objects with a numericlengthproperty (representing the number of parameters), comparator function types like(a: T, b: T) => booleansatisfy this type predicate just like anArrayLike<T>does.as-is
Consequently, using
isArrayLikeObjectfor type narrowing causes thefalsebranch to strip away the function type along with array-likes, collapsinglastValue's type tonever.to-be
Narrowing on
typeof lastValue !== 'function'instead giveslastValueits real type —(a: T, b: T) => boolean— so the comparator is genuinely type-checked at the call site from now on.