Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Rework intersect, add overlap #8

Merged
merged 7 commits into from
Apr 27, 2021
Merged

Conversation

andreidmt
Copy link
Collaborator

@andreidmt andreidmt commented Apr 11, 2021

Thoughts and proposals on #7 by @dgilperez:

  • Current implementation of intersect is misleading. Implemented it as you proposed and as any sane person would expect intersect to behave, return common items.
  • I don't like using by default deepEquals so I've created intersectBy to be geared towards object arrays and left intersect with a shallow equals.
  • Renamed the previous implementation of intersect as overlap/overlapBy. As I'm writing, union seems a better name for it.
  • An important feature of both intersect and overlap is the return of distinct items.

Primary

  1. Rework intersect to return common and distinct items from two arrays.
  2. Add intersectBy. Allow custom predicate and merge functions, geared towards object arrays.
intersect([1, 2, 3, 3, 4], [3, 3, 4, 5])
// => [3, 4]

intersectBy(
  (a, b) => a.id === b.id,
  (a, b) => ({ ...a, ...b }),
  [
    { id: 1, lorem: "ipsum" },
    { id: 2, foo: "bar" },
  ],
  [
    { id: 2, comments: [] },
    { id: 3, comments: [] },
  ]
)
// => [{ id: 2, foo: "bar", comments: [] }]
  1. Add overlap and overlapBy functions to combine two arrays into one with distinct items.
overlap([1, 1, 2, 3, 3], [3, 3, 4, 4, 5])
// => [1, 2, 3, 4, 5]

overlapBy(
  (a, b) => a.id === b.id,
  (a, b) => ({ ...a, ...b }),
  [{ id: 1}, { id: 2 }, { id: 2 }],
  [{ id: 1, foo: "bar" }, { id: 3 }]
)
// => [{ id: 1, foo: "bar" }, { id: 2 }, { id: 3 }]

Secondary

  1. Update distinct to use shallow compare.
  2. Add distinctBy. Allow custom compare function, geared towards object arrays.
// old
distinct([1, {a: 2}, {a: 2}])
// => [1, {a: 2}]

// new
import deepEquals from "fast-deep-equal"

distinctBy(deepEquals, [1, { a: 2 }, { a: 2 }])
// => [1, {a: 2}]

Checklist

  • Tests
  • JSDocs
  • Export in src/index.js

…allow custom compare function

BREAKING CHANGE:

`distinct` no longer uses deep equal compare. Use `distinctBy` with custom
compare function to parse object arrays.

```js
// old
distinct([1, {a: 2}, {a: 2}])
// => [1, {a: 2}]

// new
import deepEquals from "fast-deep-equal"

distinctBy(deepEquals, [1, { a: 2 }, { a: 2 }])
// => [1, {a: 2}]
```
@andreidmt andreidmt self-assigned this Apr 11, 2021
@andreidmt
Copy link
Collaborator Author

Hey @dgilperez, added this PR based on your #7 proposal.

Waiting for your thought and can continue with difference after we finish with these.

@dgilperez
Copy link
Collaborator

Hey, sorry for the delay @andreidmt

The changes on intersect and distinct make a lot of sense in my opinion.

Otoh, I think overlap is a confusing term - for me, overlap is almost a synonym for intersection. I'd argue the proposed implementation for overlap is actually a join. If we go to SQL joins, there are a number of different joins of course (left, right, inner, outer ...), adding to the confusion, but in general terms I think is a better term for this operation.

BREAKING CHANGE:

Functions renamed:
- "join" -> "unit"
- "overlap" -> "join"
@andreidmt
Copy link
Collaborator Author

I like the idea of syncing the naming with SQL operations (where applicable). Renamed "overlap" -> "join" and the old join to "unite".

@andreidmt andreidmt merged commit f80680b into master Apr 27, 2021
@andreidmt andreidmt deleted the feature/intersect-overlap branch April 27, 2021 14:46
andreidmt pushed a commit that referenced this pull request Apr 27, 2021
# [6.0.0](v5.4.0...v6.0.0) (2021-04-27)

### Features

* Rework `intersect`, add `overlap` ([#8](#8)) ([f80680b](f80680b))

### BREAKING CHANGES

* `distinct` no longer uses deep equal compare. Use `distinctBy` with custom
compare function to parse object arrays.

```js
// old
distinct([1, {a: 2}, {a: 2}])
// => [1, {a: 2}]

// new
import deepEquals from "fast-deep-equal"

distinctBy(deepEquals, [1, { a: 2 }, { a: 2 }])
// => [1, {a: 2}]
```

* feat: Add `overlap` and `overlapBy` functions to combine 2 arrays into a set (array of unique items)

* feat: Add `intersect` and `intersectBy` functions to obtain common items in 2 arrays

* feat: rename "join" -> "unit", "overlap" -> "join"
* Functions renamed:
- "join" -> "unit"
- "overlap" -> "join"

* chore: fix linting errors
@andreidmt
Copy link
Collaborator Author

🎉 This PR is included in version 6.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants