Skip to content

Commit 8756e5b

Browse files
authored
Merge pull request #10 from sxwei123/develop
Add more test cases and update readme
2 parents c0de6fd + 39fe70b commit 8756e5b

File tree

4 files changed

+75
-19
lines changed

4 files changed

+75
-19
lines changed

README.md

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,10 @@ yarn add quick-stable-stringify
3030

3131
## Examples
3232

33-
Node.JS with CommonJS:
34-
3533
```js
34+
// Node.JS with CommonJS
3635
const stringify = require("quick-stable-stringify");
37-
38-
const obj = { c: 8, b: [{ z: 6, y: 5, x: 4 }, 7], a: 3 };
39-
console.log(stringify(obj));
40-
```
41-
42-
Typescript or modern Javascript environment:
43-
44-
```ts
36+
// Typescript or modern Javascript environment
4537
import stringify from "quick-stable-stringify";
4638

4739
const obj = { c: 8, b: [{ z: 6, y: 5, x: 4 }, 7], a: 3 };
@@ -69,11 +61,11 @@ type ComparatorFunction = (a: KeyValue, b: KeyValue) => number;
6961
For example, to sort by the object keys in reverse order:
7062

7163
```js
72-
var stringify = require("quick-stable-stringify");
64+
const stringify = require("quick-stable-stringify");
7365

74-
var obj = { c: 8, b: [{ z: 6, y: 5, x: 4 }, 7], a: 3 };
75-
var s = stringify(obj, function (a, b) {
76-
return a.key < b.key ? 1 : -1;
66+
const obj = { c: 8, b: [{ z: 6, y: 5, x: 4 }, 7], a: 3 };
67+
const s = stringify(obj, function (a, b) {
68+
return a.key < b.key ? 1 : a.key === b.key ? 0 : -1;
7769
});
7870
console.log(s);
7971
```
@@ -87,11 +79,11 @@ which results in the output string:
8779
To sort by the object values in reverse order:
8880

8981
```js
90-
var stringify = require("quick-stable-stringify");
82+
const stringify = require("quick-stable-stringify");
9183

92-
var obj = { d: 6, c: 5, b: [{ z: 3, y: 2, x: 1 }, 9], a: 10 };
93-
var s = stringify(obj, function (a, b) {
94-
return a.value < b.value ? 1 : -1;
84+
const obj = { d: 6, c: 5, b: [{ z: 3, y: 2, x: 1 }, 9], a: 10 };
85+
const s = stringify(obj, function (a, b) {
86+
return a.value < b.value ? 1 : a.value === b.value ? 0 : -1;
9587
});
9688
console.log(s);
9789
```

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.1.5",
3+
"version": "0.1.6",
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",

test/str.spec.ts renamed to test/object.spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import stringify from "../src";
22

3+
test("empty object", () => {
4+
expect(stringify({})).toEqual("{}");
5+
});
6+
37
test("simple object", () => {
48
const obj = { c: 6, b: [4, 5], a: 3, z: null };
59
expect(stringify(obj)).toEqual('{"a":3,"b":[4,5],"c":6,"z":null}');
@@ -30,7 +34,38 @@ test("object with empty string", () => {
3034
expect(stringify(obj)).toEqual('{"a":3,"z":""}');
3135
});
3236

37+
test("object with Symbol", () => {
38+
const obj = { a: 12, z: Symbol("") };
39+
expect(stringify(obj)).toEqual('{"a":12}');
40+
});
41+
42+
test("non-enumerable properties", () => {
43+
const obj = Object.create(null, {
44+
x: { value: "x", enumerable: false },
45+
y: { value: "y", enumerable: true },
46+
});
47+
expect(stringify(obj)).toEqual('{"y":"y"}');
48+
});
49+
3350
test("array with empty string", () => {
3451
const obj = [4, "", 6];
3552
expect(stringify(obj)).toEqual('[4,"",6]');
3653
});
54+
55+
test("simple function", () => {
56+
const obj = () => {
57+
// empty
58+
};
59+
expect(stringify(obj)).toEqual(undefined);
60+
});
61+
62+
test("standard data structures", () => {
63+
expect(
64+
stringify([
65+
new Set([1]),
66+
new Map([[1, 2]]),
67+
new WeakSet([{ a: 1 }]),
68+
new WeakMap([[{ a: 1 }, 2]]),
69+
])
70+
).toEqual("[{},{},{},{}]");
71+
});

test/primitive.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import stringify from "../src";
2+
3+
test("undefined", () => {
4+
expect(stringify(undefined)).toEqual(undefined);
5+
});
6+
7+
test("true", () => {
8+
expect(stringify(true)).toEqual("true");
9+
});
10+
11+
test("string", () => {
12+
expect(stringify("foo")).toEqual(`"foo"`);
13+
});
14+
15+
test("symbol", () => {
16+
expect(stringify(Symbol(""))).toEqual(undefined);
17+
});
18+
19+
test("NaN", () => {
20+
expect(stringify(NaN)).toEqual("null");
21+
});
22+
23+
test("Infinity", () => {
24+
expect(stringify(Infinity)).toEqual("null");
25+
});
26+
27+
test("null", () => {
28+
expect(stringify(null)).toEqual("null");
29+
});

0 commit comments

Comments
 (0)