-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
Copy pathbaseIntersection.js
62 lines (56 loc) · 1.83 KB
/
baseIntersection.js
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
import SetCache from './SetCache';
import arrayIncludes from './arrayIncludes';
import arrayIncludesWith from './arrayIncludesWith';
import arrayMap from './arrayMap';
import baseUnary from './baseUnary';
import cacheHas from './cacheHas';
/**
* The base implementation of methods like `_.intersection`, without support
* for iteratee shorthands, that accepts an array of arrays to inspect.
*
* @private
* @param {Array} arrays The arrays to inspect.
* @param {Function} [iteratee] The iteratee invoked per element.
* @param {Function} [comparator] The comparator invoked per element.
* @returns {Array} Returns the new array of shared values.
*/
function baseIntersection(arrays, iteratee, comparator) {
var includes = comparator ? arrayIncludesWith : arrayIncludes,
othLength = arrays.length,
othIndex = othLength,
caches = Array(othLength),
result = [];
while (othIndex--) {
var array = arrays[othIndex];
if (othIndex && iteratee) {
array = arrayMap(array, baseUnary(iteratee));
}
caches[othIndex] = !comparator && (iteratee || array.length >= 120)
? new SetCache(othIndex && array)
: undefined;
}
array = arrays[0];
var index = -1,
length = array.length,
seen = caches[0];
outer:
while (++index < length) {
var value = array[index],
computed = iteratee ? iteratee(value) : value;
if (!(seen ? cacheHas(seen, computed) : includes(result, computed, comparator))) {
var othIndex = othLength;
while (--othIndex) {
var cache = caches[othIndex];
if (!(cache ? cacheHas(cache, computed) : includes(arrays[othIndex], computed, comparator))) {
continue outer;
}
}
if (seen) {
seen.push(computed);
}
result.push(value);
}
}
return result;
}
export default baseIntersection;