/
uniq.ts
66 lines (60 loc) · 1.77 KB
/
uniq.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* eslint-disable jsdoc/check-param-names -- ignore for deprecated files */
import { _reduceLazy } from "./_reduceLazy";
import type { LazyEvaluator } from "./pipe";
import { purry } from "./purry";
/**
* Returns a new array containing only one copy of each element in the original list.
* Elements are compared by reference using Set.
*
* ! **DEPRECATED**: Use `R.unique(array)`. Will be removed in V2.
*
* @param array - The array to filter.
* @signature
* R.uniq(array)
* @example
* R.uniq([1, 2, 2, 5, 1, 6, 7]) // => [1, 2, 5, 6, 7]
* @dataFirst
* @pipeable
* @category Deprecated
* @deprecated Use `R.unique(array)`. Will be removed in V2.
*/
export function uniq<T>(array: ReadonlyArray<T>): Array<T>;
/**
* Returns a new array containing only one copy of each element in the original list.
* Elements are compared by reference using Set.
*
* ! **DEPRECATED**: Use `R.unique()`. Will be removed in V2.
*
* @param array - The array to filter.
* @signature
* R.uniq()(array)
* @example
* R.pipe(
* [1, 2, 2, 5, 1, 6, 7], // only 4 iterations
* R.uniq(),
* R.take(3)
* ) // => [1, 2, 5]
* @dataLast
* @pipeable
* @category Deprecated
* @deprecated Use `R.unique()`. Will be removed in V2.
*/
export function uniq<T>(): (array: ReadonlyArray<T>) => Array<T>;
export function uniq(): unknown {
return purry(_uniq, arguments, uniq.lazy);
}
function _uniq<T>(array: ReadonlyArray<T>): Array<T> {
return _reduceLazy(array, uniq.lazy());
}
export namespace uniq {
export function lazy<T>(): LazyEvaluator<T> {
const set = new Set<T>();
return (value) => {
if (set.has(value)) {
return { done: false, hasNext: false };
}
set.add(value);
return { done: false, hasNext: true, next: value };
};
}
}