-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
39 lines (36 loc) · 1.02 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const collator = new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' });
const sortArr = function f(array: any[], reverse: boolean = false, func: Function): any[] {
let arr: any = [];
for (let i = 0; i < array.length; i++) {
const x = array[i];
if (typeof x === 'string' || typeof x === 'number') {
arr.push(x)
} else {
arr.push(func(x, reverse))
}
}
arr = arr.sort(collator.compare)
if (reverse) arr = arr.reverse();
return arr;
}
function deepSorter(o: any, reverse: boolean = false): any {
let ops = Object.keys(o).sort();
if (reverse)
ops = ops.reverse();
if (Array.isArray(o)) return sortArr(o, reverse, deepSorter);
return ops.reduce(
(obj: any, key: string) => {
const p = o[key];
if (Array.isArray(p)) {
obj[key] = sortArr(p, reverse, deepSorter);
} else if (typeof p === 'object') {
obj[key] = deepSorter(p, reverse);
} else {
obj[key] = o[key]
}
return obj;
},
{}
)
}
export default deepSorter;