index.js 958 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*!
  2. * arr-diff <https://github.com/jonschlinkert/arr-diff>
  3. *
  4. * Copyright (c) 2014 Jon Schlinkert, contributors.
  5. * Licensed under the MIT License
  6. */
  7. 'use strict';
  8. var flatten = require('arr-flatten');
  9. var slice = [].slice;
  10. /**
  11. * Return the difference between the first array and
  12. * additional arrays.
  13. *
  14. * ```js
  15. * var diff = require('{%= name %}');
  16. *
  17. * var a = ['a', 'b', 'c', 'd'];
  18. * var b = ['b', 'c'];
  19. *
  20. * console.log(diff(a, b))
  21. * //=> ['a', 'd']
  22. * ```
  23. *
  24. * @param {Array} `a`
  25. * @param {Array} `b`
  26. * @return {Array}
  27. * @api public
  28. */
  29. function diff(arr, arrays) {
  30. var argsLen = arguments.length;
  31. var len = arr.length, i = -1;
  32. var res = [], arrays;
  33. if (argsLen === 1) {
  34. return arr;
  35. }
  36. if (argsLen > 2) {
  37. arrays = flatten(slice.call(arguments, 1));
  38. }
  39. while (++i < len) {
  40. if (!~arrays.indexOf(arr[i])) {
  41. res.push(arr[i]);
  42. }
  43. }
  44. return res;
  45. }
  46. /**
  47. * Expose `diff`
  48. */
  49. module.exports = diff;