differenceWith.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import _includesWith from "./internal/_includesWith.js";
  2. import _curry3 from "./internal/_curry3.js";
  3. /**
  4. * Finds the set (i.e. no duplicates) of all elements in the first list not
  5. * contained in the second list. Duplication is determined according to the
  6. * value returned by applying the supplied predicate to two list elements.
  7. *
  8. * @func
  9. * @memberOf R
  10. * @since v0.1.0
  11. * @category Relation
  12. * @sig ((a, a) -> Boolean) -> [a] -> [a] -> [a]
  13. * @param {Function} pred A predicate used to test whether two items are equal.
  14. * @param {Array} list1 The first list.
  15. * @param {Array} list2 The second list.
  16. * @return {Array} The elements in `list1` that are not in `list2`.
  17. * @see R.difference, R.symmetricDifference, R.symmetricDifferenceWith
  18. * @example
  19. *
  20. * const cmp = (x, y) => x.a === y.a;
  21. * const l1 = [{a: 1}, {a: 2}, {a: 3}];
  22. * const l2 = [{a: 3}, {a: 4}];
  23. * R.differenceWith(cmp, l1, l2); //=> [{a: 1}, {a: 2}]
  24. *
  25. * R.differenceWith(R.equals, [1, 2, 3, 3, 3], []); //=> [1, 2, 3]
  26. * R.differenceWith(R.equals, [1, 2, 3, 3, 3], [1]); //=> [2, 3]
  27. */
  28. var differenceWith =
  29. /*#__PURE__*/
  30. _curry3(function differenceWith(pred, first, second) {
  31. var out = [];
  32. var idx = 0;
  33. var firstLen = first.length;
  34. while (idx < firstLen) {
  35. if (!_includesWith(pred, first[idx], second) && !_includesWith(pred, first[idx], out)) {
  36. out.push(first[idx]);
  37. }
  38. idx += 1;
  39. }
  40. return out;
  41. });
  42. export default differenceWith;