1 | import arrayMap from './_arrayMap';
|
2 | import baseCastArrayLikeObject from './_baseCastArrayLikeObject';
|
3 | import baseIntersection from './_baseIntersection';
|
4 | import last from './last';
|
5 | import rest from './rest';
|
6 |
|
7 | /**
|
8 | * This method is like `_.intersection` except that it accepts `comparator`
|
9 | * which is invoked to compare elements of `arrays`. Result values are chosen
|
10 | * from the first array. The comparator is invoked with two arguments:
|
11 | * (arrVal, othVal).
|
12 | *
|
13 | * @static
|
14 | * @memberOf _
|
15 | * @since 4.0.0
|
16 | * @category Array
|
17 | * @param {...Array} [arrays] The arrays to inspect.
|
18 | * @param {Function} [comparator] The comparator invoked per element.
|
19 | * @returns {Array} Returns the new array of intersecting values.
|
20 | * @example
|
21 | *
|
22 | * var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
|
23 | * var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];
|
24 | *
|
25 | * _.intersectionWith(objects, others, _.isEqual);
|
26 | * // => [{ 'x': 1, 'y': 2 }]
|
27 | */
|
28 | var intersectionWith = rest(function(arrays) {
|
29 | var comparator = last(arrays),
|
30 | mapped = arrayMap(arrays, baseCastArrayLikeObject);
|
31 |
|
32 | if (comparator === last(mapped)) {
|
33 | comparator = undefined;
|
34 | } else {
|
35 | mapped.pop();
|
36 | }
|
37 | return (mapped.length && mapped[0] === arrays[0])
|
38 | ? baseIntersection(mapped, undefined, comparator)
|
39 | : [];
|
40 | });
|
41 |
|
42 | export default intersectionWith;
|