Skip to content

Commit f22cccf

Browse files
authored
Merge pull request #3 from sxwei123/develop
Improve document
2 parents daed177 + 3f9b7d2 commit f22cccf

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

README.md

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Node.JS with CommonJS:
2222

2323
```js
2424
const stringify = require("quick-stable-stringify");
25+
2526
const obj = { c: 8, b: [{ z: 6, y: 5, x: 4 }, 7], a: 3 };
2627
console.log(stringify(obj));
2728
```
@@ -37,17 +38,23 @@ console.log(stringify(obj));
3738

3839
## Options
3940

41+
Options can be a comparator function or an object which has two optional properties: `cmp` and `cycles`.
42+
4043
### cmp
4144

42-
If `opts` is given, you can supply an `opts.cmp` to have a custom comparison
43-
function for object keys. Your function `opts.cmp` is called with these
44-
parameters:
45+
`opts.cmp` is the custom comparator function that user can specify. If custom comparator function is not provided, the JSON string of an object will be sorted by the alphanumeric order of object keys.
46+
The type of the comparator function is defined as:
4547

46-
```js
47-
opts.cmp({ key: akey, value: avalue }, { key: bkey, value: bvalue });
48+
```ts
49+
interface KeyValue {
50+
key: string;
51+
value: any;
52+
}
53+
54+
type ComparatorFunction = (a: KeyValue, b: KeyValue) => number;
4855
```
4956

50-
For example, to sort on the object key names in reverse order you could write:
57+
For example, to sort on the object key names in reverse order:
5158

5259
```js
5360
var stringify = require("quick-stable-stringify");
@@ -65,14 +72,14 @@ which results in the output string:
6572
{"c":8,"b":[{"z":6,"y":5,"x":4},7],"a":3}
6673
```
6774

68-
Or if you wanted to sort on the object values in reverse order, you could write:
75+
To sort on the object values in reverse order:
6976

70-
```
71-
var stringify = require('fast-json-stable-stringify');
77+
```js
78+
var stringify = require("quick-stable-stringify");
7279

73-
var obj = { d: 6, c: 5, b: [{z:3,y:2,x:1},9], a: 10 };
80+
var obj = { d: 6, c: 5, b: [{ z: 3, y: 2, x: 1 }, 9], a: 10 };
7481
var s = stringify(obj, function (a, b) {
75-
return a.value < b.value ? 1 : -1;
82+
return a.value < b.value ? 1 : -1;
7683
});
7784
console.log(s);
7885
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "quick-stable-stringify",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"description": "Deterministic `JSON.stringify()` - a faster version of Evgeny's fast-json-stable-strigify. Built with Typescript and modern Javascript.",
55
"main": "./dist/index.js",
66
"exports": "./dist/index.js",

0 commit comments

Comments
 (0)