Skip to content

Commit

Permalink
✨ Add uniq function
Browse files Browse the repository at this point in the history
Closes #67
  • Loading branch information
TomokiMiyauci committed May 20, 2021
1 parent 6bcb032 commit 2bcf021
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 6 deletions.
6 changes: 2 additions & 4 deletions api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,16 +306,14 @@ const api: Api = {
rambda: 'takeLast',
lodash: 'takeRight'
},
tryCatch: ['rambda', 'ramda']
tryCatch: ['rambda', 'ramda'],
// test: {
// fonction: undefined
// },
// type: {
// fonction: undefined
// },
// uniq: {
// fonction: undefined
// },
uniq: ['ramda', 'rambda', 'lodash']
// update: {
// fonction: undefined
// },
Expand Down
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export type {
Primitive,
Space
} from './src/types/index.ts'
export { uniq } from './src/uniq.ts'
export { upperCase } from './src/upperCase.ts'
export { values } from './src/values.ts'
export { xor } from './src/xor.ts'
30 changes: 28 additions & 2 deletions src/uniq.ts
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 }
61 changes: 61 additions & 0 deletions test/uniq.test.ts
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))
})

0 comments on commit 2bcf021

Please sign in to comment.