1 | import SetCache from './_SetCache.js';
|
---|
2 | import arrayIncludes from './_arrayIncludes.js';
|
---|
3 | import arrayIncludesWith from './_arrayIncludesWith.js';
|
---|
4 | import arrayMap from './_arrayMap.js';
|
---|
5 | import baseUnary from './_baseUnary.js';
|
---|
6 | import cacheHas from './_cacheHas.js';
|
---|
7 |
|
---|
8 | /* Built-in method references for those with the same name as other `lodash` methods. */
|
---|
9 | var nativeMin = Math.min;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * The base implementation of methods like `_.intersection`, without support
|
---|
13 | * for iteratee shorthands, that accepts an array of arrays to inspect.
|
---|
14 | *
|
---|
15 | * @private
|
---|
16 | * @param {Array} arrays The arrays to inspect.
|
---|
17 | * @param {Function} [iteratee] The iteratee invoked per element.
|
---|
18 | * @param {Function} [comparator] The comparator invoked per element.
|
---|
19 | * @returns {Array} Returns the new array of shared values.
|
---|
20 | */
|
---|
21 | function baseIntersection(arrays, iteratee, comparator) {
|
---|
22 | var includes = comparator ? arrayIncludesWith : arrayIncludes,
|
---|
23 | length = arrays[0].length,
|
---|
24 | othLength = arrays.length,
|
---|
25 | othIndex = othLength,
|
---|
26 | caches = Array(othLength),
|
---|
27 | maxLength = Infinity,
|
---|
28 | result = [];
|
---|
29 |
|
---|
30 | while (othIndex--) {
|
---|
31 | var array = arrays[othIndex];
|
---|
32 | if (othIndex && iteratee) {
|
---|
33 | array = arrayMap(array, baseUnary(iteratee));
|
---|
34 | }
|
---|
35 | maxLength = nativeMin(array.length, maxLength);
|
---|
36 | caches[othIndex] = !comparator && (iteratee || (length >= 120 && array.length >= 120))
|
---|
37 | ? new SetCache(othIndex && array)
|
---|
38 | : undefined;
|
---|
39 | }
|
---|
40 | array = arrays[0];
|
---|
41 |
|
---|
42 | var index = -1,
|
---|
43 | seen = caches[0];
|
---|
44 |
|
---|
45 | outer:
|
---|
46 | while (++index < length && result.length < maxLength) {
|
---|
47 | var value = array[index],
|
---|
48 | computed = iteratee ? iteratee(value) : value;
|
---|
49 |
|
---|
50 | value = (comparator || value !== 0) ? value : 0;
|
---|
51 | if (!(seen
|
---|
52 | ? cacheHas(seen, computed)
|
---|
53 | : includes(result, computed, comparator)
|
---|
54 | )) {
|
---|
55 | othIndex = othLength;
|
---|
56 | while (--othIndex) {
|
---|
57 | var cache = caches[othIndex];
|
---|
58 | if (!(cache
|
---|
59 | ? cacheHas(cache, computed)
|
---|
60 | : includes(arrays[othIndex], computed, comparator))
|
---|
61 | ) {
|
---|
62 | continue outer;
|
---|
63 | }
|
---|
64 | }
|
---|
65 | if (seen) {
|
---|
66 | seen.push(computed);
|
---|
67 | }
|
---|
68 | result.push(value);
|
---|
69 | }
|
---|
70 | }
|
---|
71 | return result;
|
---|
72 | }
|
---|
73 |
|
---|
74 | export default baseIntersection;
|
---|