forked from remeda/remeda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
intersection.ts
63 lines (58 loc) · 1.46 KB
/
intersection.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
import { purry } from './purry';
import { _reduceLazy, LazyResult } from './_reduceLazy';
/**
* Returns a list of elements that exist in both array.
* @param array the source array
* @param other the second array
* @signature
* R.intersection(array, other)
* @example
* R.intersection([1, 2, 3], [2, 3, 5]) // => [2, 3]
* @dataFirst
* @category Array
* @pipeable
*/
export function intersection<T>(
source: ReadonlyArray<T>,
other: ReadonlyArray<T>
): Array<T>;
/**
* Returns a list of elements that exist in both array.
* @param array the source array
* @param other the second array
* @signature
* R.intersection(other)(array)
* @example
* R.intersection([2, 3, 5])([1, 2, 3]) // => [2, 3]
* @dataLast
* @category Array
* @pipeable
*/
export function intersection<T, K>(
other: ReadonlyArray<T>
): (source: ReadonlyArray<K>) => Array<T>;
export function intersection() {
return purry(_intersection, arguments, intersection.lazy);
}
function _intersection<T>(array: Array<T>, other: Array<T>) {
const lazy = intersection.lazy(other);
return _reduceLazy(array, lazy);
}
export namespace intersection {
export function lazy<T>(other: Array<T>) {
return (value: T): LazyResult<T> => {
const set = new Set(other);
if (set.has(value)) {
return {
done: false,
hasNext: true,
next: value,
};
}
return {
done: false,
hasNext: false,
};
};
}
}