intersection.js 948 B

123456789101112131415161718192021222324252627282930
  1. var arrayMap = require('./_arrayMap'),
  2. baseIntersection = require('./_baseIntersection'),
  3. castArrayLikeObject = require('./_castArrayLikeObject'),
  4. rest = require('./rest');
  5. /**
  6. * Creates an array of unique values that are included in all given arrays
  7. * using [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero)
  8. * for equality comparisons. The order of result values is determined by the
  9. * order they occur in the first array.
  10. *
  11. * @static
  12. * @memberOf _
  13. * @since 0.1.0
  14. * @category Array
  15. * @param {...Array} [arrays] The arrays to inspect.
  16. * @returns {Array} Returns the new array of intersecting values.
  17. * @example
  18. *
  19. * _.intersection([2, 1], [2, 3]);
  20. * // => [2]
  21. */
  22. var intersection = rest(function(arrays) {
  23. var mapped = arrayMap(arrays, castArrayLikeObject);
  24. return (mapped.length && mapped[0] === arrays[0])
  25. ? baseIntersection(mapped)
  26. : [];
  27. });
  28. module.exports = intersection;