-
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.
- Loading branch information
Showing
12 changed files
with
156 additions
and
4 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
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,27 @@ | ||
# xorWith | ||
|
||
XOR(Exclusive OR) the passed array and return a new `array`, and supports custom comparison functions. | ||
|
||
### Usage | ||
|
||
```ts | ||
import { xorWith } from 'rattail' | ||
|
||
xorWith([{ num: 1 }, { num: 2 }, { num: 2 }], [{ num: 1 }, { num: 3 }], (a, b) => a.num === b.num) | ||
// return [{ num: 2 }, { num: 3 }] | ||
xorWith([{ num: 1 }, { num: 2 }], [{ num: 1 }, { num: 2 }], [{ num: 3 }], (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 @@ | ||
# xor | ||
|
||
XOR(Exclusive OR) the passed array and return a new `array`. | ||
|
||
### Usage | ||
|
||
```ts | ||
import { xor } from 'rattail' | ||
|
||
xor([1, 2, 2], [1, 3]) | ||
// return [2, 3] | ||
xor([1, 2], [1, 2], [3]) | ||
// 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
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,27 @@ | ||
# xorWith | ||
|
||
对传入的数组进行异或(Exclusive OR)计算,返回一个新的 `数组`,并支持自定义比较函数。 | ||
|
||
### 使用 | ||
|
||
```ts | ||
import { xorWith } from 'rattail' | ||
|
||
xorWith([{ num: 1 }, { num: 2 }, { num: 2 }], [{ num: 1 }, { num: 3 }], (a, b) => a.num === b.num) | ||
// return [{ num: 2 }, { num: 3 }] | ||
xorWith([{ num: 1 }, { num: 2 }], [{ num: 1 }, { num: 2 }], [{ num: 3 }], (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 @@ | ||
# xor | ||
|
||
对传入的数组进行异或(Exclusive OR)计算,返回一个新的 `数组`。 | ||
|
||
### 使用 | ||
|
||
```ts | ||
import { xor } from 'rattail' | ||
|
||
xor([1, 2, 2], [1, 3]) | ||
// return [2, 3] | ||
xor([1, 2], [1, 2], [3]) | ||
// 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
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 { xorWith } from './xorWith' | ||
|
||
export function xor<T>(...values: T[][]): T[] { | ||
return xorWith(...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,17 @@ | ||
import { at } from './at' | ||
import { differenceWith } from './differenceWith' | ||
import { uniqBy } from './uniqBy' | ||
|
||
type Fn<T> = (a: T, b: T) => any | ||
|
||
export function xorWith<T>(...values: [...T[][], fn: Fn<T>]): T[] { | ||
const fn = at(values, -1) as Fn<T> | ||
const targets = values.slice(0, -1) as T[][] | ||
|
||
return uniqBy( | ||
targets.reduce((result, target) => { | ||
return [...differenceWith(result, target, fn), ...differenceWith(target, result, fn)] | ||
}), | ||
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