UNPKG

1.46 kBMarkdownView Raw
1# sort-keys
2
3> Sort the keys of an object
4
5Useful to get a deterministically ordered object, as the order of keys can vary between engines.
6
7## Install
8
9```
10$ npm install sort-keys
11```
12
13## Usage
14
15```js
16import sortKeys from 'sort-keys';
17
18sortKeys({c: 0, a: 0, b: 0});
19//=> {a: 0, b: 0, c: 0}
20
21sortKeys({b: {b: 0, a: 0}, a: 0}, {deep: true});
22//=> {a: 0, b: {a: 0, b: 0}}
23
24sortKeys({b: [{b: 0, a: 0}], a: 0}, {deep: true});
25//=> {a: 0, b: [{a: 0, b: 0}]}
26
27sortKeys({c: 0, a: 0, b: 0}, {
28 compare: (a, b) => -a.localeCompare(b)
29});
30//=> {c: 0, b: 0, a: 0}
31
32sortKeys([{b: 0, a:2}], {deep: true});
33//=> [{a: 2, b: 0}]
34```
35
36## API
37
38### sortKeys(object, options?)
39
40Returns a new object with sorted keys.
41
42#### object
43
44Type: `object | Array`
45
46#### options
47
48Type: `object`
49
50##### deep
51
52Type: `boolean`\
53Default: `false`
54
55Recursively sort keys, including keys of objects inside arrays.
56
57##### compare
58
59Type: `Function`
60
61[Compare function.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)
62
63---
64
65<div align="center">
66 <b>
67 <a href="https://tidelift.com/subscription/pkg/npm-sort-keys?utm_source=npm-sort-keys&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
68 </b>
69 <br>
70 <sub>
71 Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
72 </sub>
73</div>