-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: intersection, intersectionWith
- Loading branch information
Showing
9 changed files
with
215 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# intersectionWith | ||
|
||
Creates an `array` of unique values contained in all given arrays, and supports custom comparison functions. | ||
|
||
### Usage | ||
|
||
```ts | ||
import { intersectionWith } from 'rattail' | ||
|
||
intersectionWith( | ||
[{ num: 1 }, { num: 2 }, { num: 3 }, { num: 3 }], | ||
[{ num: 2 }, { num: 3 }, { num: 4 }], | ||
(a, b) => a.num === b.num, | ||
) | ||
// return [{ num: 2 }, { num: 3 }] | ||
intersectionWith( | ||
[{ num: 1 }, { num: 2 }, { num: 3 }, { num: 3 }], | ||
[{ num: 2 }, { num: 3 }, { num: 4 }], | ||
[{ num: 3 }, { num: 4 }, { num: 5 }], | ||
(a, b) => a.num === b.num, | ||
) | ||
// return [{ num: 3 }] | ||
``` | ||
|
||
### Arguments | ||
|
||
| Arg | Type | Defaults | | ||
| ----------- | ------------------------- | -------- | | ||
| `...values` | `Array<Array>` | | | ||
| `fn` | `(a: any, b: any) => any` | | | ||
|
||
### Return | ||
|
||
| Type | | ||
| ------- | | ||
| `Array` | |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# intersection | ||
|
||
Creates an `array` of unique values contained in all given arrays. | ||
|
||
### Usage | ||
|
||
```ts | ||
import { intersection } from 'rattail' | ||
|
||
intersection([1, 2, 3, 3], [2, 3, 4]) | ||
// return [2, 3] | ||
intersection([1, 2, 3, 3], [2, 3, 4], [3, 4, 5]) | ||
// return [3] | ||
``` | ||
|
||
### Arguments | ||
|
||
| Arg | Type | Defaults | | ||
| ----------- | -------------- | -------- | | ||
| `...values` | `Array<Array>` | | | ||
|
||
### Return | ||
|
||
| Type | | ||
| ------- | | ||
| `Array` | |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# intersectionWith | ||
|
||
创建一个去重数组,数组中的每个值都被包含在所有的给定数组中(数组取交集),支持传入比较函数。 | ||
|
||
### 使用 | ||
|
||
```ts | ||
import { intersectionWith } from 'rattail' | ||
|
||
intersectionWith( | ||
[{ num: 1 }, { num: 2 }, { num: 3 }, { num: 3 }], | ||
[{ num: 2 }, { num: 3 }, { num: 4 }], | ||
(a, b) => a.num === b.num, | ||
) | ||
// return [{ num: 2 }, { num: 3 }] | ||
intersectionWith( | ||
[{ num: 1 }, { num: 2 }, { num: 3 }, { num: 3 }], | ||
[{ num: 2 }, { num: 3 }, { num: 4 }], | ||
[{ num: 3 }, { num: 4 }, { num: 5 }], | ||
(a, b) => a.num === b.num, | ||
) | ||
// return [{ num: 3 }] | ||
``` | ||
|
||
### 参数 | ||
|
||
| 参数 | 类型 | 默认值 | | ||
| ----------- | ------------------------- | ------ | | ||
| `...values` | `Array<Array>` | | | ||
| `fn` | `(a: any, b: any) => any` | | | ||
|
||
### 返回值 | ||
|
||
| 类型 | | ||
| ------- | | ||
| `Array` | |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# intersection | ||
|
||
创建一个去重数组,数组中的每个值都被包含在所有的给定数组中(数组取交集)。 | ||
|
||
### 使用 | ||
|
||
```ts | ||
import { intersection } from 'rattail' | ||
|
||
intersection([1, 2, 3, 3], [2, 3, 4]) | ||
// return [2, 3] | ||
intersection([1, 2, 3, 3], [2, 3, 4], [3, 4, 5]) | ||
// return [3] | ||
``` | ||
|
||
### 参数 | ||
|
||
| 参数 | 类型 | 默认值 | | ||
| ----------- | -------------- | ------ | | ||
| `...values` | `Array<Array>` | | | ||
|
||
### 返回值 | ||
|
||
| 类型 | | ||
| ------- | | ||
| `Array` | |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { intersectionWith } from './intersectionWith' | ||
|
||
export function intersection<T>(...values: T[][]): T[] { | ||
return intersectionWith(...values, (a, b) => a === b) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { at } from './at' | ||
import { uniqBy } from './uniqBy' | ||
|
||
type Fn<T> = (a: T, b: T) => any | ||
|
||
export function intersectionWith<T>(...values: [...T[][], fn: Fn<T>]): T[] { | ||
const fn = at(values, -1) as Fn<T> | ||
const targets = values.slice(0, -1) as T[][] | ||
|
||
if (targets.length === 0) { | ||
return [] | ||
} | ||
|
||
if (targets.length === 1) { | ||
return uniqBy(targets[0], fn) | ||
} | ||
|
||
function baseIntersectionWith(arr1: T[], arr2: T[]): T[] { | ||
return arr1.filter((item) => arr2.some((value) => fn(item, value))) | ||
} | ||
|
||
return uniqBy( | ||
targets.reduce((result, target) => baseIntersectionWith(result, target)), | ||
fn, | ||
) | ||
} |
This file contains 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