main
Last change
on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago |
Initial commit
|
-
Property mode
set to
100644
|
File size:
1.3 KB
|
Line | |
---|
1 | import _Set from "./internal/_Set.js";
|
---|
2 | import _curry2 from "./internal/_curry2.js";
|
---|
3 | import _dispatchable from "./internal/_dispatchable.js";
|
---|
4 | import _xuniqBy from "./internal/_xuniqBy.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 function to
|
---|
8 | * each list element. Prefers the first item if the supplied function produces
|
---|
9 | * the same value on two items. [`R.equals`](#equals) is used for comparison.
|
---|
10 | *
|
---|
11 | * Acts as a transducer if a transformer is given in list position.
|
---|
12 | *
|
---|
13 | * @func
|
---|
14 | * @memberOf R
|
---|
15 | * @since v0.16.0
|
---|
16 | * @category List
|
---|
17 | * @sig (a -> b) -> [a] -> [a]
|
---|
18 | * @param {Function} fn A function used to produce a value to use during comparisons.
|
---|
19 | * @param {Array} list The array to consider.
|
---|
20 | * @return {Array} The list of unique items.
|
---|
21 | * @example
|
---|
22 | *
|
---|
23 | * R.uniqBy(Math.abs, [-1, -5, 2, 10, 1, 2]); //=> [-1, -5, 2, 10]
|
---|
24 | */
|
---|
25 |
|
---|
26 | var uniqBy =
|
---|
27 | /*#__PURE__*/
|
---|
28 | _curry2(
|
---|
29 | /*#__PURE__*/
|
---|
30 | _dispatchable([], _xuniqBy, function (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 |
|
---|
50 | export default uniqBy; |
---|
Note:
See
TracBrowser
for help on using the repository browser.