-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
92 additions
and
6 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 |
---|---|---|
@@ -1,4 +1,30 @@ | ||
// Copyright 2021-present the Fonction authors. All rights reserved. MIT license. | ||
const uniq = <T>(val: T[]): T[] => Array.from(new Set(val)); | ||
import { equal } from './equal.ts' | ||
import { ifElse } from './ifElse.ts' | ||
|
||
export { uniq }; | ||
/** | ||
* Returns a new `Array` containing only one copy of each element in the original array. `equal` is used to determine equality. | ||
* | ||
* @param val - Input any `array` | ||
* @returns The list of unique items | ||
* | ||
* @example | ||
* ```ts | ||
* uniq([1, 2, 1, 1]) // [1, 2] | ||
* uniq([{}, {}, [], []]) // [{}, []] | ||
* uniq([[1, 2, 3], [1, 2, 3]]) // [[1, 2, 3]] | ||
* ``` | ||
* | ||
* @category `Array` | ||
* | ||
* @see Related to {@link equal} | ||
* | ||
* @beta | ||
*/ | ||
const uniq = <T extends unknown>(val: readonly T[]): T[] => | ||
Array.from(new Set(val)).reduce((acc, cur) => { | ||
const aaa = acc.some((a) => equal(cur, a)) | ||
return ifElse(aaa, acc, () => [...acc, cur]) | ||
}, [] as T[]) | ||
|
||
export { uniq } |
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,61 @@ | ||
// Copyright 2021-present the Fonction authors. All rights reserved. MIT license. | ||
import { assertEquals } from '../dev_deps.ts' | ||
import { uniq } from '../src/uniq.ts' | ||
import { assertEqual } from './asserts.ts' | ||
|
||
Deno.test('uniq', () => { | ||
const table: [unknown[], unknown[]][] = [ | ||
[[], []], | ||
[ | ||
['', '', '', '', '', 2, '', 1], | ||
['', 2, 1] | ||
], | ||
[ | ||
[null, undefined, 1n, null, undefined, 2n], | ||
[null, undefined, 1n, 2n] | ||
], | ||
[ | ||
[1, 2, 3], | ||
[1, 2, 3] | ||
], | ||
[ | ||
[1, 1, 2, 2, 3, 3], | ||
[1, 2, 3] | ||
], | ||
[[{}, {}, {}], [{}]], | ||
[ | ||
[[], {}, [], {}], | ||
[[], {}] | ||
], | ||
[ | ||
[[], {}, [], {}], | ||
[[], {}] | ||
], | ||
[ | ||
[[1, 2, 3], { hoge: 'huga' }, [], { hage: 'hoge' }], | ||
[[1, 2, 3], { hoge: 'huga' }, [], { hage: 'hoge' }] | ||
], | ||
[ | ||
[ | ||
[1, [], [1, [2, [3]]]], | ||
{ hoge: 'huga', 1: 0 }, | ||
[1, [], [1, [2, [3]]]], | ||
{ hoge: 'huga', 1: 0, 2: 3 } | ||
], | ||
[ | ||
[1, [], [1, [2, [3]]]], | ||
{ hoge: 'huga', 1: 0 }, | ||
{ hoge: 'huga', 1: 0, 2: 3 } | ||
] | ||
] | ||
] | ||
table.forEach(([val, expected]) => { | ||
assertEquals(uniq(val), expected, `uniq(${val}) -> ${expected}`) | ||
}) | ||
|
||
assertEqual<never[]>(uniq([])) | ||
assertEqual<string[]>(uniq([''])) | ||
assertEqual<''[]>(uniq([''] as const)) | ||
assertEqual<('' | '1' | '2')[]>(uniq(['', '1', '2'] as const)) | ||
assertEqual<('' | '1' | '2')[]>(uniq(['', '1', '2', '1', '2'] as const)) | ||
}) |