1 | var SetCache = require('./_SetCache'),
|
2 | arrayIncludes = require('./_arrayIncludes'),
|
3 | arrayIncludesWith = require('./_arrayIncludesWith'),
|
4 | arrayMap = require('./_arrayMap'),
|
5 | baseUnary = require('./_baseUnary'),
|
6 | cacheHas = require('./_cacheHas');
|
7 |
|
8 |
|
9 | var nativeMin = Math.min;
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
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 | module.exports = baseIntersection;
|