es-toolkit icon indicating copy to clipboard operation
es-toolkit copied to clipboard

fix(intersectionBy): remove duplicates when mapper produces same values

Open wo-o29 opened this issue 5 months ago • 5 comments

Summary

Fixes: #956

Fixed a bug in intersectionBy where it returned duplicate elements from the first array when the mapper function produced identical values for multiple elements. Now correctly returns only the first occurrence of each mapped value.

For example, intersectionBy([2.1, 2.2], [2.3, 3.4], Math.floor) now correctly returns [2.1] instead of [2.1, 2.2].

Changes

  • Modified intersectionBy to use uniqBy on the first array before filtering
  • Added test case to verify duplicate removal behavior

Comment

  • Would it be better to implement it myself rather than using the uniqBy function?

wo-o29 avatar Nov 21 '25 09:11 wo-o29

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
es-toolkit Ready Ready Preview Comment Nov 24, 2025 3:00am

vercel[bot] avatar Nov 21 '25 09:11 vercel[bot]

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 99.97%. Comparing base (b6ffb6d) to head (2475241). :warning: Report is 5 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #1528   +/-   ##
=======================================
  Coverage   99.97%   99.97%           
=======================================
  Files         474      474           
  Lines        4492     4493    +1     
  Branches     1313     1313           
=======================================
+ Hits         4491     4492    +1     
  Misses          1        1           
:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

codecov-commenter avatar Nov 21 '25 09:11 codecov-commenter

I'm sorry, but as we mentioned in the original comment, we would like to keep the original interface where we only accept two arrays and a mapper function. (We would not like to accept multiple arrays here since it violates our simplicity principle.)

However, we should fix a bug where calls like intersectionBy([2.1, 2.2], [2.3, 3.4], Math.floor) does not return [2.1]. (It is returning [2.1, 2.2].)

raon0211 avatar Nov 24 '25 02:11 raon0211

Sorry, I seem to have misunderstood your comment. I'll try to fix the bug you mentioned by submitting a PR.

wo-o29 avatar Nov 24 '25 02:11 wo-o29

@raon0211

I've fixed the bug and re-created the PR, sorry for the confusion.

wo-o29 avatar Nov 24 '25 02:11 wo-o29