difference.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import _curry2 from "./internal/_curry2.js";
  2. import _Set from "./internal/_Set.js";
  3. /**
  4. * Finds the set (i.e. no duplicates) of all elements in the first list not
  5. * contained in the second list. Objects and Arrays are compared in terms of
  6. * value equality, not reference equality.
  7. *
  8. * @func
  9. * @memberOf R
  10. * @since v0.1.0
  11. * @category Relation
  12. * @sig [*] -> [*] -> [*]
  13. * @param {Array} list1 The first list.
  14. * @param {Array} list2 The second list.
  15. * @return {Array} The elements in `list1` that are not in `list2`.
  16. * @see R.differenceWith, R.symmetricDifference, R.symmetricDifferenceWith, R.without
  17. * @example
  18. *
  19. * R.difference([1,2,3,4], [7,6,5,4,3]); //=> [1,2]
  20. * R.difference([7,6,5,4,3], [1,2,3,4]); //=> [7,6,5]
  21. * R.difference([{a: 1}, {b: 2}], [{a: 1}, {c: 3}]) //=> [{b: 2}]
  22. */
  23. var difference =
  24. /*#__PURE__*/
  25. _curry2(function difference(first, second) {
  26. var out = [];
  27. var idx = 0;
  28. var firstLen = first.length;
  29. var secondLen = second.length;
  30. var toFilterOut = new _Set();
  31. for (var i = 0; i < secondLen; i += 1) {
  32. toFilterOut.add(second[i]);
  33. }
  34. while (idx < firstLen) {
  35. if (toFilterOut.add(first[idx])) {
  36. out[out.length] = first[idx];
  37. }
  38. idx += 1;
  39. }
  40. return out;
  41. });
  42. export default difference;