uniqWith.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import _curry2 from "./internal/_curry2.js";
  2. import _dispatchable from "./internal/_dispatchable.js";
  3. import _includesWith from "./internal/_includesWith.js";
  4. import _xuniqWith from "./internal/_xuniqWith.js";
  5. /**
  6. * Returns a new list containing only one copy of each element in the original
  7. * list, based upon the value returned by applying the supplied predicate to
  8. * two list elements. Prefers the first item if two items compare equal based
  9. * on the predicate.
  10. *
  11. * Acts as a transducer if a transformer is given in list position.
  12. *
  13. * @func
  14. * @memberOf R
  15. * @since v0.2.0
  16. * @category List
  17. * @sig ((a, a) -> Boolean) -> [a] -> [a]
  18. * @param {Function} pred A predicate used to test whether two items are equal.
  19. * @param {Array} list The array to consider.
  20. * @return {Array} The list of unique items.
  21. * @example
  22. *
  23. * const strEq = R.eqBy(String);
  24. * R.uniqWith(strEq)([1, '1', 2, 1]); //=> [1, 2]
  25. * R.uniqWith(strEq)([{}, {}]); //=> [{}]
  26. * R.uniqWith(strEq)([1, '1', 1]); //=> [1]
  27. * R.uniqWith(strEq)(['1', 1, 1]); //=> ['1']
  28. */
  29. var uniqWith =
  30. /*#__PURE__*/
  31. _curry2(
  32. /*#__PURE__*/
  33. _dispatchable([], _xuniqWith, function (pred, list) {
  34. var idx = 0;
  35. var len = list.length;
  36. var result = [];
  37. var item;
  38. while (idx < len) {
  39. item = list[idx];
  40. if (!_includesWith(pred, item, result)) {
  41. result[result.length] = item;
  42. }
  43. idx += 1;
  44. }
  45. return result;
  46. }));
  47. export default uniqWith;