source: node_modules/json-stable-stringify/README.md

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 4.2 KB
RevLine 
[d24f17c]1# json-stable-stringify <sup>[![Version Badge][npm-version-svg]][package-url]</sup>
2
3[![github actions][actions-image]][actions-url]
4[![coverage][codecov-image]][codecov-url]
5[![License][license-image]][license-url]
6[![Downloads][downloads-image]][downloads-url]
7
8[![npm badge][npm-badge-png]][package-url]
9
10deterministic version of `JSON.stringify()` so you can get a consistent hash from stringified results
11
12You can also pass in a custom comparison function.
13
14[![browser support](https://ci.testling.com/ljharb/json-stable-stringify.png)](https://ci.testling.com/ljharb/json-stable-stringify)
15
16[![build status](https://secure.travis-ci.org/ljharb/json-stable-stringify.png)](http://travis-ci.org/ljharb/json-stable-stringify)
17
18# example
19
20``` js
21const stringify = require('json-stable-stringify');
22
23const obj = { c: 8, b: [{ z: 6, y: 5, x: 4 }, 7], a: 3 };
24
25console.log(stringify(obj));
26```
27
28output:
29
30```
31{"a":3,"b":[{"x":4,"y":5,"z":6},7],"c":8}
32```
33
34# methods
35
36``` js
37const stringify = require('json-stable-stringify')
38```
39
40<a id="var-str--stringifyobj-opts"></a>
41## const str = stringify(obj, opts)
42
43Return a deterministic stringified string `str` from the object `obj`.
44
45## options
46
47### cmp
48
49If `opts` is given, you can supply an `opts.cmp` to have a custom comparison function for object keys.
50Your function `opts.cmp` is called with these parameters:
51
52``` js
53opts.cmp({ key: akey, value: avalue }, { key: bkey, value: bvalue }, { get(key): value })
54```
55
56For example, to sort on the object key names in reverse order you could write:
57
58``` js
59const stringify = require('json-stable-stringify');
60
61const obj = { c: 8, b: [{ z: 6, y: 5, x: 4 },7], a: 3 };
62
63const s = stringify(obj, function (a, b) {
64 return b.key.localeCompare(a.key);
65});
66
67console.log(s);
68```
69
70which results in the output string:
71
72``` js
73{"c":8,"b":[{"z":6,"y":5,"x":4},7],"a":3}
74```
75
76Or if you wanted to sort on the object values in reverse order, you could write:
77
78``` js
79const stringify = require('json-stable-stringify');
80
81const obj = { d: 6, c: 5, b: [{ z: 3, y: 2, x: 1 }, 9], a: 10 };
82
83const s = stringify(obj, function (a, b) {
84 return a.value < b.value ? 1 : -1;
85});
86
87console.log(s);
88```
89
90which outputs:
91
92``` js
93{"d":6,"c":5,"b":[{"z":3,"y":2,"x":1},9],"a":10}
94```
95
96An additional param `get(key)` returns the value of the key from the object being currently compared.
97
98### space
99
100If you specify `opts.space`, it will indent the output for pretty-printing.
101Valid values are strings (e.g. `{space: \t}`) or a number of spaces
102(`{space: 3}`).
103
104For example:
105
106```js
107const obj = { b: 1, a: { foo: 'bar', and: [1, 2, 3] } };
108
109const s = stringify(obj, { space: ' ' });
110
111console.log(s);
112```
113
114which outputs:
115
116```
117{
118 "a": {
119 "and": [
120 1,
121 2,
122 3
123 ],
124 "foo": "bar"
125 },
126 "b": 1
127}
128```
129
130### replacer
131
132The replacer parameter is a function `opts.replacer(key, value)` that behaves the same as the replacer
133[from the core JSON object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_native_JSON#The_replacer_parameter).
134
135# install
136
137With [npm](https://npmjs.org) do:
138
139```
140npm install json-stable-stringify
141```
142
143# license
144
145MIT
146
147[package-url]: https://npmjs.org/package/json-stable-stringify
148[npm-version-svg]: https://versionbadg.es/ljharb/json-stable-stringify.svg
149[deps-svg]: https://david-dm.org/ljharb/json-stable-stringify.svg
150[deps-url]: https://david-dm.org/ljharb/json-stable-stringify
151[dev-deps-svg]: https://david-dm.org/ljharb/json-stable-stringify/dev-status.svg
152[dev-deps-url]: https://david-dm.org/ljharb/json-stable-stringify#info=devDependencies
153[npm-badge-png]: https://nodei.co/npm/json-stable-stringify.png?downloads=true&stars=true
154[license-image]: https://img.shields.io/npm/l/json-stable-stringify.svg
155[license-url]: LICENSE
156[downloads-image]: https://img.shields.io/npm/dm/json-stable-stringify.svg
157[downloads-url]: https://npm-stat.com/charts.html?package=json-stable-stringify
158[codecov-image]: https://codecov.io/gh/ljharb/json-stable-stringify/branch/main/graphs/badge.svg
159[codecov-url]: https://app.codecov.io/gh/ljharb/json-stable-stringify/
160[actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/ljharb/json-stable-stringify
161[actions-url]: https://github.com/ljharb/json-stable-stringify/actions
Note: See TracBrowser for help on using the repository browser.