UNPKG

1.15 kBJavaScriptView Raw
1var _Set =
2/*#__PURE__*/
3require("./internal/_Set");
4
5var _curry2 =
6/*#__PURE__*/
7require("./internal/_curry2");
8/**
9 * Returns a new list containing only one copy of each element in the original
10 * list, based upon the value returned by applying the supplied function to
11 * each list element. Prefers the first item if the supplied function produces
12 * the same value on two items. [`R.equals`](#equals) is used for comparison.
13 *
14 * @func
15 * @memberOf R
16 * @since v0.16.0
17 * @category List
18 * @sig (a -> b) -> [a] -> [a]
19 * @param {Function} fn A function used to produce a value to use during comparisons.
20 * @param {Array} list The array to consider.
21 * @return {Array} The list of unique items.
22 * @example
23 *
24 * R.uniqBy(Math.abs, [-1, -5, 2, 10, 1, 2]); //=> [-1, -5, 2, 10]
25 */
26
27
28var uniqBy =
29/*#__PURE__*/
30_curry2(function uniqBy(fn, list) {
31 var set = new _Set();
32 var result = [];
33 var idx = 0;
34 var appliedItem, item;
35
36 while (idx < list.length) {
37 item = list[idx];
38 appliedItem = fn(item);
39
40 if (set.add(appliedItem)) {
41 result.push(item);
42 }
43
44 idx += 1;
45 }
46
47 return result;
48});
49
50module.exports = uniqBy;
\No newline at end of file