Skip to content

Commit 81f36c1

Browse files
author
James Halliday
committed
docs, more examples
1 parent f7b9a47 commit 81f36c1

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

example/key_cmp.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var stringify = require('../');
2+
3+
var obj = { c: 8, b: [{z:6,y:5,x:4},7], a: 3 };
4+
var s = stringify(obj, function (a, b) {
5+
return a.key < b.key ? 1 : -1;
6+
});
7+
console.log(s);

example/value_cmp.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var stringify = require('../');
2+
3+
var obj = { d: 6, c: 5, b: [{z:3,y:2,x:1},9], a: 10 };
4+
var s = stringify(obj, function (a, b) {
5+
return a.value < b.value ? 1 : -1;
6+
});
7+
console.log(s);

readme.markdown

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# json-stable-stringify
2+
3+
deterministic version of `JSON.stringify()` so you can get a consistent hash
4+
from stringified results
5+
6+
You can also pass in a custom comparison function.
7+
8+
# example
9+
10+
``` js
11+
var stringify = require('json-stable-stringify');
12+
var obj = { c: 8, b: [{z:6,y:5,x:4},7], a: 3 };
13+
console.log(stringify(obj));
14+
```
15+
16+
output:
17+
18+
```
19+
{"a":3,"b":[{"x":4,"y":5,"z":6},7],"c":8}
20+
```
21+
22+
# methods
23+
24+
``` js
25+
var stringify = require('json-stable-stringify')
26+
```
27+
28+
## var str = stringify(obj, opts)
29+
30+
Return a deterministic stringified string `str` from the object `obj`.
31+
32+
If `opts` is given, you can supply an `opts.cmp` to have a custom comparison
33+
function for object keys. Your function `opts.cmp` is called with these
34+
parameters:
35+
36+
``` js
37+
opts.cmp({ key: akey, value: avalue }, { key: bkey, value: bvalue })
38+
```
39+
40+
For example, to sort on the object key names in reverse order you could write:
41+
42+
``` js
43+
var stringify = require('json-stable-stringify');
44+
45+
var obj = { c: 8, b: [{z:6,y:5,x:4},7], a: 3 };
46+
var s = stringify(obj, function (a, b) {
47+
return a.key < b.key ? 1 : -1;
48+
});
49+
console.log(s);
50+
```
51+
52+
which results in the output string:
53+
54+
```
55+
{"c":8,"b":[{"z":6,"y":5,"x":4},7],"a":3}
56+
```
57+
58+
Or if you wanted to sort on the object values in reverse order, you could write:
59+
60+
```
61+
var stringify = require('json-stable-stringify');
62+
63+
var obj = { d: 6, c: 5, b: [{z:3,y:2,x:1},9], a: 10 };
64+
var s = stringify(obj, function (a, b) {
65+
return a.value < b.value ? 1 : -1;
66+
});
67+
console.log(s);
68+
```
69+
70+
which outputs:
71+
72+
```
73+
{"d":6,"c":5,"b":[{"z":3,"y":2,"x":1},9],"a":10}
74+
```
75+
76+
# install
77+
78+
With [npm](https://npmjs.org) do:
79+
80+
```
81+
npm install json-stable-stringify
82+
```
83+
84+
# license
85+
86+
MIT

0 commit comments

Comments
 (0)