UNPKG

490 BPlain TextView Raw
1interface AnyObject {
2 [key: string]: any;
3}
4
5/**
6 * Returns a new clone of `obj` with its keys sorted alphabetically
7 * @param obj
8 */
9export function sortObject(obj: object): object {
10 return Object.entries(obj)
11 .sort((left, right) => {
12 if (left[0] < right[0]) {
13 return -1;
14 }
15 if (left[0] > right[0]) {
16 return 1;
17 }
18
19 return 0;
20 })
21 .reduce<AnyObject>((acc, [key, value]) => {
22 acc[key] = value;
23 return acc;
24 }, {});
25}
26
\No newline at end of file