UNPKG

789 BJavaScriptView Raw
1function compare(a, b) {
2 const itemA = a === undefined ? 0 : a;
3 const itemB = b === undefined ? 0 : b;
4 if (Array.isArray(itemA) && Array.isArray(itemB)) {
5 if (itemA.length === 0 && itemB.length === 0)
6 return 0;
7 const diff = compare(itemA[0], itemB[0]);
8 if (diff !== 0)
9 return diff;
10 return compare(itemA.slice(1), itemB.slice(1));
11 }
12 if (itemA < itemB)
13 return -1;
14 if (itemA > itemB)
15 return 1;
16 return 0;
17}
18export function sortBy(arr, fn) {
19 return arr.sort((a, b) => compare(fn(a), fn(b)));
20}
21export function uniq(arr) {
22 return arr.filter((a, i) => arr.indexOf(a) === i);
23}
24export function uniqWith(arr, fn) {
25 return arr.filter((a, i) => !arr.some((b, j) => j > i && fn(a, b)));
26}