1 | /* eslint-disable import/export */
|
2 |
|
3 | export interface Options {
|
4 | /**
|
5 | Recursively sort keys, including keys of objects inside arrays.
|
6 |
|
7 | @default false
|
8 | */
|
9 | readonly deep?: boolean;
|
10 |
|
11 | /**
|
12 | [Compare function.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)
|
13 | */
|
14 | readonly compare?: (left: string, right: string) => number;
|
15 | }
|
16 |
|
17 | /**
|
18 | Sort the keys of an object.
|
19 |
|
20 | @returns A new object with sorted keys.
|
21 |
|
22 | @example
|
23 | ```
|
24 | import sortKeys from 'sort-keys';
|
25 |
|
26 | sortKeys({c: 0, a: 0, b: 0});
|
27 | //=> {a: 0, b: 0, c: 0}
|
28 |
|
29 | sortKeys({b: {b: 0, a: 0}, a: 0}, {deep: true});
|
30 | //=> {a: 0, b: {a: 0, b: 0}}
|
31 |
|
32 | sortKeys({b: [{b: 0, a: 0}], a: 0}, {deep: true});
|
33 | //=> {a: 0, b: [{a: 0, b: 0}]}
|
34 |
|
35 | sortKeys({c: 0, a: 0, b: 0}, {
|
36 | compare: (a, b) => -a.localeCompare(b)
|
37 | });
|
38 | //=> {c: 0, b: 0, a: 0}
|
39 |
|
40 | sortKeys([{b: 0, a:2}], {deep: true});
|
41 | //=> [{a: 2, b: 0}]
|
42 | ```
|
43 | */
|
44 | export default function sortKeys<T extends Record<string, any>>(
|
45 | object: T,
|
46 | options?: Options
|
47 | ): T;
|
48 | export default function sortKeys<T>(
|
49 | object: T[],
|
50 | options?: Options
|
51 | ): T[];
|