Version 1
[yaffs-website] / node_modules / uncss / node_modules / lodash / intersectionWith.js
1 var arrayMap = require('./internal/arrayMap'),
2     baseIntersection = require('./internal/baseIntersection'),
3     last = require('./last'),
4     rest = require('./rest'),
5     toArrayLikeObject = require('./internal/toArrayLikeObject');
6
7 /**
8  * This method is like `_.intersection` except that it accepts `comparator`
9  * which is invoked to compare elements of `arrays`. The comparator is invoked
10  * with two arguments: (arrVal, othVal).
11  *
12  * @static
13  * @memberOf _
14  * @category Array
15  * @param {...Array} [arrays] The arrays to inspect.
16  * @param {Function} [comparator] The comparator invoked per element.
17  * @returns {Array} Returns the new array of shared values.
18  * @example
19  *
20  * var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
21  * var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];
22  *
23  * _.intersectionWith(objects, others, _.isEqual);
24  * // => [{ 'x': 1, 'y': 2 }]
25  */
26 var intersectionWith = rest(function(arrays) {
27   var comparator = last(arrays),
28       mapped = arrayMap(arrays, toArrayLikeObject);
29
30   if (comparator === last(mapped)) {
31     comparator = undefined;
32   } else {
33     mapped.pop();
34   }
35   return (mapped.length && mapped[0] === arrays[0])
36     ? baseIntersection(mapped, undefined, comparator)
37     : [];
38 });
39
40 module.exports = intersectionWith;