intersection.js 642 B

123456789101112131415161718192021222324
  1. var unique = require('./unique');
  2. var filter = require('./filter');
  3. var every = require('./every');
  4. var contains = require('./contains');
  5. var slice = require('./slice');
  6. /**
  7. * Return a new Array with elements common to all Arrays.
  8. * - based on underscore.js implementation
  9. */
  10. function intersection(arr) {
  11. var arrs = slice(arguments, 1),
  12. result = filter(unique(arr), function(needle){
  13. return every(arrs, function(haystack){
  14. return contains(haystack, needle);
  15. });
  16. });
  17. return result;
  18. }
  19. module.exports = intersection;