@casual-simulation/fast-json-stable-stringify 2.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +126 -0
- package/index.d.ts.map +1 -0
- package/index.js +119 -0
- package/index.spec.ts +109 -0
- package/index.ts +148 -0
- package/package.json +48 -0
- package/tsconfig.json +14 -0
- package/tsconfig.tsbuildinfo +8405 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
This software is released under the MIT license:
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017 Evgeny Poberezkin
|
|
4
|
+
Copyright (c) 2013 James Halliday
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
7
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
8
|
+
the Software without restriction, including without limitation the rights to
|
|
9
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
10
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
11
|
+
subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
18
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
19
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
20
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
21
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# fast-json-stable-stringify
|
|
2
|
+
|
|
3
|
+
> Forked from https://github.com/epoberezkin/fast-json-stable-stringify.
|
|
4
|
+
> Changes include converting to TypeScript and adding the ability to indent JSON output.
|
|
5
|
+
|
|
6
|
+
Deterministic `JSON.stringify()` - a faster version of [@substack](https://github.com/substack)'s json-stable-strigify without [jsonify](https://github.com/substack/jsonify).
|
|
7
|
+
|
|
8
|
+
You can also pass in a custom comparison function.
|
|
9
|
+
|
|
10
|
+
# example
|
|
11
|
+
|
|
12
|
+
```js
|
|
13
|
+
var stringify = require('fast-json-stable-stringify');
|
|
14
|
+
var obj = { c: 8, b: [{ z: 6, y: 5, x: 4 }, 7], a: 3 };
|
|
15
|
+
console.log(stringify(obj));
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
output:
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
{"a":3,"b":[{"x":4,"y":5,"z":6},7],"c":8}
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
# methods
|
|
25
|
+
|
|
26
|
+
```js
|
|
27
|
+
var stringify = require('fast-json-stable-stringify');
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## var str = stringify(obj, opts)
|
|
31
|
+
|
|
32
|
+
Return a deterministic stringified string `str` from the object `obj`.
|
|
33
|
+
|
|
34
|
+
## options
|
|
35
|
+
|
|
36
|
+
### cmp
|
|
37
|
+
|
|
38
|
+
If `opts` is given, you can supply an `opts.cmp` to have a custom comparison
|
|
39
|
+
function for object keys. Your function `opts.cmp` is called with these
|
|
40
|
+
parameters:
|
|
41
|
+
|
|
42
|
+
```js
|
|
43
|
+
opts.cmp({ key: akey, value: avalue }, { key: bkey, value: bvalue });
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
For example, to sort on the object key names in reverse order you could write:
|
|
47
|
+
|
|
48
|
+
```js
|
|
49
|
+
var stringify = require('fast-json-stable-stringify');
|
|
50
|
+
|
|
51
|
+
var obj = { c: 8, b: [{ z: 6, y: 5, x: 4 }, 7], a: 3 };
|
|
52
|
+
var s = stringify(obj, function (a, b) {
|
|
53
|
+
return a.key < b.key ? 1 : -1;
|
|
54
|
+
});
|
|
55
|
+
console.log(s);
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
which results in the output string:
|
|
59
|
+
|
|
60
|
+
```
|
|
61
|
+
{"c":8,"b":[{"z":6,"y":5,"x":4},7],"a":3}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Or if you wanted to sort on the object values in reverse order, you could write:
|
|
65
|
+
|
|
66
|
+
```
|
|
67
|
+
var stringify = require('fast-json-stable-stringify');
|
|
68
|
+
|
|
69
|
+
var obj = { d: 6, c: 5, b: [{z:3,y:2,x:1},9], a: 10 };
|
|
70
|
+
var s = stringify(obj, function (a, b) {
|
|
71
|
+
return a.value < b.value ? 1 : -1;
|
|
72
|
+
});
|
|
73
|
+
console.log(s);
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
which outputs:
|
|
77
|
+
|
|
78
|
+
```
|
|
79
|
+
{"d":6,"c":5,"b":[{"z":3,"y":2,"x":1},9],"a":10}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### cycles
|
|
83
|
+
|
|
84
|
+
Pass `true` in `opts.cycles` to stringify circular property as `__cycle__` - the result will not be a valid JSON string in this case.
|
|
85
|
+
|
|
86
|
+
TypeError will be thrown in case of circular object without this option.
|
|
87
|
+
|
|
88
|
+
# install
|
|
89
|
+
|
|
90
|
+
With [npm](https://npmjs.org) do:
|
|
91
|
+
|
|
92
|
+
```
|
|
93
|
+
npm install fast-json-stable-stringify
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
# benchmark
|
|
97
|
+
|
|
98
|
+
To run benchmark (requires Node.js 6+):
|
|
99
|
+
|
|
100
|
+
```
|
|
101
|
+
node benchmark
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Results:
|
|
105
|
+
|
|
106
|
+
```
|
|
107
|
+
fast-json-stable-stringify x 17,189 ops/sec ±1.43% (83 runs sampled)
|
|
108
|
+
json-stable-stringify x 13,634 ops/sec ±1.39% (85 runs sampled)
|
|
109
|
+
fast-stable-stringify x 20,212 ops/sec ±1.20% (84 runs sampled)
|
|
110
|
+
faster-stable-stringify x 15,549 ops/sec ±1.12% (84 runs sampled)
|
|
111
|
+
The fastest is fast-stable-stringify
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## Enterprise support
|
|
115
|
+
|
|
116
|
+
fast-json-stable-stringify package is a part of [Tidelift enterprise subscription](https://tidelift.com/subscription/pkg/npm-fast-json-stable-stringify?utm_source=npm-fast-json-stable-stringify&utm_medium=referral&utm_campaign=enterprise&utm_term=repo) - it provides a centralised commercial support to open-source software users, in addition to the support provided by software maintainers.
|
|
117
|
+
|
|
118
|
+
## Security contact
|
|
119
|
+
|
|
120
|
+
To report a security vulnerability, please use the
|
|
121
|
+
[Tidelift security contact](https://tidelift.com/security).
|
|
122
|
+
Tidelift will coordinate the fix and disclosure. Please do NOT report security vulnerability via GitHub issues.
|
|
123
|
+
|
|
124
|
+
# license
|
|
125
|
+
|
|
126
|
+
[MIT](https://github.com/epoberezkin/fast-json-stable-stringify/blob/master/LICENSE)
|
package/index.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,YAAY;IACzB,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,GAAG,CAAC;CACd;AAED,oBAAY,eAAe,GAAG,CAC1B,KAAK,EAAE,YAAY,EACnB,MAAM,EAAE,YAAY,KACnB,MAAM,CAAC;AAEZ,MAAM,WAAW,gBAAgB;IAC7B,GAAG,CAAC,EAAE,eAAe,CAAC;IACtB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;CAC3B;AAED,MAAM,CAAC,OAAO,WACV,IAAI,EAAE,GAAG,EACT,OAAO,CAAC,EAAE,gBAAgB,GAAG,eAAe,UAuE/C"}
|
package/index.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
export default function (data, options) {
|
|
2
|
+
if (!options)
|
|
3
|
+
options = {};
|
|
4
|
+
if (typeof options === 'function')
|
|
5
|
+
options = { cmp: options };
|
|
6
|
+
let cycles = typeof options.cycles === 'boolean' ? options.cycles : false;
|
|
7
|
+
options.space = options.space || '';
|
|
8
|
+
let space = typeof options.space === 'number'
|
|
9
|
+
? Array(options.space + 1).join(' ')
|
|
10
|
+
: options.space;
|
|
11
|
+
let cmp = options.cmp &&
|
|
12
|
+
((f) => {
|
|
13
|
+
return (node) => {
|
|
14
|
+
return (a, b) => {
|
|
15
|
+
let aobj = { key: a, value: node[a] };
|
|
16
|
+
let bobj = { key: b, value: node[b] };
|
|
17
|
+
return f(aobj, bobj);
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
})(options.cmp);
|
|
21
|
+
let seen = [];
|
|
22
|
+
return (function stringify(node, level) {
|
|
23
|
+
let indent = space ? '\n' + new Array(level + 1).join(space) : '';
|
|
24
|
+
let colonSeparator = space ? ': ' : ':';
|
|
25
|
+
if (node && node.toJSON && typeof node.toJSON === 'function') {
|
|
26
|
+
node = node.toJSON();
|
|
27
|
+
}
|
|
28
|
+
if (node === undefined)
|
|
29
|
+
return;
|
|
30
|
+
if (typeof node === 'number')
|
|
31
|
+
return isFinite(node) ? '' + node : 'null';
|
|
32
|
+
if (typeof node !== 'object')
|
|
33
|
+
return JSON.stringify(node, undefined, space);
|
|
34
|
+
let i;
|
|
35
|
+
let out;
|
|
36
|
+
if (Array.isArray(node)) {
|
|
37
|
+
out = '[';
|
|
38
|
+
for (i = 0; i < node.length; i++) {
|
|
39
|
+
if (i)
|
|
40
|
+
out += ',';
|
|
41
|
+
out +=
|
|
42
|
+
indent + space + (stringify(node[i], level + 1) || 'null');
|
|
43
|
+
}
|
|
44
|
+
return out + indent + ']';
|
|
45
|
+
}
|
|
46
|
+
if (node === null)
|
|
47
|
+
return 'null';
|
|
48
|
+
if (seen.indexOf(node) !== -1) {
|
|
49
|
+
if (cycles)
|
|
50
|
+
return JSON.stringify('__cycle__');
|
|
51
|
+
throw new TypeError('Converting circular structure to JSON');
|
|
52
|
+
}
|
|
53
|
+
let seenIndex = seen.push(node) - 1;
|
|
54
|
+
let keys = Object.keys(node).sort(cmp && cmp(node));
|
|
55
|
+
out = '';
|
|
56
|
+
for (i = 0; i < keys.length; i++) {
|
|
57
|
+
let key = keys[i];
|
|
58
|
+
let value = stringify(node[key], level + 1);
|
|
59
|
+
if (!value)
|
|
60
|
+
continue;
|
|
61
|
+
if (out)
|
|
62
|
+
out += ',';
|
|
63
|
+
out +=
|
|
64
|
+
indent + space + JSON.stringify(key) + colonSeparator + value;
|
|
65
|
+
}
|
|
66
|
+
seen.splice(seenIndex, 1);
|
|
67
|
+
return '{' + out + indent + '}';
|
|
68
|
+
})(data, 0);
|
|
69
|
+
}
|
|
70
|
+
// module.exports = function (data, opts) {
|
|
71
|
+
// if (!opts) opts = {};
|
|
72
|
+
// if (typeof opts === 'function') opts = { cmp: opts };
|
|
73
|
+
// var cycles = (typeof opts.cycles === 'boolean') ? opts.cycles : false;
|
|
74
|
+
// var cmp = opts.cmp && (function (f) {
|
|
75
|
+
// return function (node) {
|
|
76
|
+
// return function (a, b) {
|
|
77
|
+
// var aobj = { key: a, value: node[a] };
|
|
78
|
+
// var bobj = { key: b, value: node[b] };
|
|
79
|
+
// return f(aobj, bobj);
|
|
80
|
+
// };
|
|
81
|
+
// };
|
|
82
|
+
// })(opts.cmp);
|
|
83
|
+
// var seen = [];
|
|
84
|
+
// return (function stringify (node) {
|
|
85
|
+
// if (node && node.toJSON && typeof node.toJSON === 'function') {
|
|
86
|
+
// node = node.toJSON();
|
|
87
|
+
// }
|
|
88
|
+
// if (node === undefined) return;
|
|
89
|
+
// if (typeof node == 'number') return isFinite(node) ? '' + node : 'null';
|
|
90
|
+
// if (typeof node !== 'object') return JSON.stringify(node);
|
|
91
|
+
// var i, out;
|
|
92
|
+
// if (Array.isArray(node)) {
|
|
93
|
+
// out = '[';
|
|
94
|
+
// for (i = 0; i < node.length; i++) {
|
|
95
|
+
// if (i) out += ',';
|
|
96
|
+
// out += stringify(node[i]) || 'null';
|
|
97
|
+
// }
|
|
98
|
+
// return out + ']';
|
|
99
|
+
// }
|
|
100
|
+
// if (node === null) return 'null';
|
|
101
|
+
// if (seen.indexOf(node) !== -1) {
|
|
102
|
+
// if (cycles) return JSON.stringify('__cycle__');
|
|
103
|
+
// throw new TypeError('Converting circular structure to JSON');
|
|
104
|
+
// }
|
|
105
|
+
// var seenIndex = seen.push(node) - 1;
|
|
106
|
+
// var keys = Object.keys(node).sort(cmp && cmp(node));
|
|
107
|
+
// out = '';
|
|
108
|
+
// for (i = 0; i < keys.length; i++) {
|
|
109
|
+
// var key = keys[i];
|
|
110
|
+
// var value = stringify(node[key]);
|
|
111
|
+
// if (!value) continue;
|
|
112
|
+
// if (out) out += ',';
|
|
113
|
+
// out += JSON.stringify(key) + ':' + value;
|
|
114
|
+
// }
|
|
115
|
+
// seen.splice(seenIndex, 1);
|
|
116
|
+
// return '{' + out + '}';
|
|
117
|
+
// })(data);
|
|
118
|
+
// };
|
|
119
|
+
//# sourceMappingURL=index.js.map
|
package/index.spec.ts
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import stringify from './index';
|
|
2
|
+
|
|
3
|
+
describe('fast-json-stable-stringify', () => {
|
|
4
|
+
it('should support nested objects', () => {
|
|
5
|
+
var obj = { c: 8, b: [{ z: 6, y: 5, x: 4 }, 7], a: 3 };
|
|
6
|
+
expect(stringify(obj)).toBe(
|
|
7
|
+
'{"a":3,"b":[{"x":4,"y":5,"z":6},7],"c":8}'
|
|
8
|
+
);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('should error on cyclic objects', () => {
|
|
12
|
+
expect(() => {
|
|
13
|
+
let one: any = { a: 1 };
|
|
14
|
+
let two: any = { a: 2, one: one };
|
|
15
|
+
one.two = two;
|
|
16
|
+
stringify(one);
|
|
17
|
+
}).toThrowError(new TypeError('Converting circular structure to JSON'));
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('should support cyclic objects when specified', () => {
|
|
21
|
+
let one: any = { a: 1 };
|
|
22
|
+
let two: any = { a: 2, one: one };
|
|
23
|
+
one.two = two;
|
|
24
|
+
expect(stringify(one, { cycles: true })).toBe(
|
|
25
|
+
'{"a":1,"two":{"a":2,"one":"__cycle__"}}'
|
|
26
|
+
);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('should support repeated non-cyclic values', () => {
|
|
30
|
+
let one = { x: 1 };
|
|
31
|
+
let two = { a: one, b: one };
|
|
32
|
+
expect(stringify(two)).toBe('{"a":{"x":1},"b":{"x":1}}');
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('should support non-cyclic objects with reused obj-property pointers', () => {
|
|
36
|
+
let x = { a: 1 };
|
|
37
|
+
let y = { b: x, c: x };
|
|
38
|
+
expect(stringify(y)).toBe('{"b":{"a":1},"c":{"a":1}}');
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('should support custom comparision functions', () => {
|
|
42
|
+
var obj = { c: 8, b: [{ z: 6, y: 5, x: 4 }, 7], a: 3 };
|
|
43
|
+
var s = stringify(obj, function (a, b) {
|
|
44
|
+
return a.key < b.key ? 1 : -1;
|
|
45
|
+
});
|
|
46
|
+
expect(s).toBe('{"c":8,"b":[{"z":6,"y":5,"x":4},7],"a":3}');
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const cases = [
|
|
50
|
+
[
|
|
51
|
+
'simple object',
|
|
52
|
+
{ c: 6, b: [4, 5], a: 3, z: null },
|
|
53
|
+
'{"a":3,"b":[4,5],"c":6,"z":null}',
|
|
54
|
+
],
|
|
55
|
+
['object with undefined', { a: 3, z: undefined }, '{"a":3}'],
|
|
56
|
+
['object with null', { a: 3, z: null }, '{"a":3,"z":null}'],
|
|
57
|
+
[
|
|
58
|
+
'object with NaN and Infinity',
|
|
59
|
+
{ a: 3, b: NaN, c: Infinity },
|
|
60
|
+
'{"a":3,"b":null,"c":null}',
|
|
61
|
+
],
|
|
62
|
+
['array with undefined', [4, undefined, 6], '[4,null,6]'],
|
|
63
|
+
['object with empty string', { a: 3, z: '' }, '{"a":3,"z":""}'],
|
|
64
|
+
['array with empty string', [4, '', 6], '[4,"",6]'],
|
|
65
|
+
] as [string, any, string][];
|
|
66
|
+
|
|
67
|
+
it.each(cases)('should stringify %s', (name, obj, expected) => {
|
|
68
|
+
expect(stringify(obj)).toBe(expected);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('should support toJSON that returns object', () => {
|
|
72
|
+
var obj = {
|
|
73
|
+
one: 1,
|
|
74
|
+
two: 2,
|
|
75
|
+
toJSON: function () {
|
|
76
|
+
return { one: 1 };
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
expect(stringify(obj)).toBe('{"one":1}');
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('should support toJSON that returns string', () => {
|
|
83
|
+
var obj = {
|
|
84
|
+
one: 1,
|
|
85
|
+
two: 2,
|
|
86
|
+
toJSON: function () {
|
|
87
|
+
return { one: 1 };
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
expect(stringify(obj)).toBe('{"one":1}');
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('should support toJSON that returns array', () => {
|
|
94
|
+
var obj = {
|
|
95
|
+
one: 1,
|
|
96
|
+
two: 2,
|
|
97
|
+
toJSON: function () {
|
|
98
|
+
return ['one'];
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
expect(stringify(obj)).toBe('["one"]');
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('should pretty format objects when specified', () => {
|
|
105
|
+
expect(stringify({ a: 1, b: 2 }, { space: 2 })).toBe(
|
|
106
|
+
'{\n "a": 1,\n "b": 2\n}'
|
|
107
|
+
);
|
|
108
|
+
});
|
|
109
|
+
});
|
package/index.ts
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
export interface CompareValue {
|
|
2
|
+
key: string;
|
|
3
|
+
value: any;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export type CompareFunction = (
|
|
7
|
+
first: CompareValue,
|
|
8
|
+
second: CompareValue
|
|
9
|
+
) => number;
|
|
10
|
+
|
|
11
|
+
export interface StringifyOptions {
|
|
12
|
+
cmp?: CompareFunction;
|
|
13
|
+
cycles?: boolean;
|
|
14
|
+
space?: string | number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export default function (
|
|
18
|
+
data: any,
|
|
19
|
+
options?: StringifyOptions | CompareFunction
|
|
20
|
+
) {
|
|
21
|
+
if (!options) options = {};
|
|
22
|
+
if (typeof options === 'function') options = { cmp: options };
|
|
23
|
+
let cycles = typeof options.cycles === 'boolean' ? options.cycles : false;
|
|
24
|
+
options.space = options.space || '';
|
|
25
|
+
let space =
|
|
26
|
+
typeof options.space === 'number'
|
|
27
|
+
? Array(options.space + 1).join(' ')
|
|
28
|
+
: options.space;
|
|
29
|
+
|
|
30
|
+
let cmp =
|
|
31
|
+
options.cmp &&
|
|
32
|
+
((f) => {
|
|
33
|
+
return (node: any) => {
|
|
34
|
+
return (a: string, b: string) => {
|
|
35
|
+
let aobj = { key: a, value: node[a] };
|
|
36
|
+
let bobj = { key: b, value: node[b] };
|
|
37
|
+
return f(aobj, bobj);
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
})(options.cmp);
|
|
41
|
+
|
|
42
|
+
let seen: any[] = [];
|
|
43
|
+
return (function stringify(node: any, level: number) {
|
|
44
|
+
let indent = space ? '\n' + new Array(level + 1).join(space) : '';
|
|
45
|
+
let colonSeparator = space ? ': ' : ':';
|
|
46
|
+
if (node && node.toJSON && typeof node.toJSON === 'function') {
|
|
47
|
+
node = node.toJSON();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (node === undefined) return;
|
|
51
|
+
if (typeof node === 'number')
|
|
52
|
+
return isFinite(node) ? '' + node : 'null';
|
|
53
|
+
if (typeof node !== 'object')
|
|
54
|
+
return JSON.stringify(node, undefined, space);
|
|
55
|
+
|
|
56
|
+
let i: number;
|
|
57
|
+
let out: string;
|
|
58
|
+
if (Array.isArray(node)) {
|
|
59
|
+
out = '[';
|
|
60
|
+
for (i = 0; i < node.length; i++) {
|
|
61
|
+
if (i) out += ',';
|
|
62
|
+
out +=
|
|
63
|
+
indent + space + (stringify(node[i], level + 1) || 'null');
|
|
64
|
+
}
|
|
65
|
+
return out + indent + ']';
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (node === null) return 'null';
|
|
69
|
+
|
|
70
|
+
if (seen.indexOf(node) !== -1) {
|
|
71
|
+
if (cycles) return JSON.stringify('__cycle__');
|
|
72
|
+
throw new TypeError('Converting circular structure to JSON');
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
let seenIndex = seen.push(node) - 1;
|
|
76
|
+
let keys = Object.keys(node).sort(cmp && cmp(node));
|
|
77
|
+
out = '';
|
|
78
|
+
for (i = 0; i < keys.length; i++) {
|
|
79
|
+
let key = keys[i];
|
|
80
|
+
let value = stringify(node[key], level + 1);
|
|
81
|
+
|
|
82
|
+
if (!value) continue;
|
|
83
|
+
if (out) out += ',';
|
|
84
|
+
out +=
|
|
85
|
+
indent + space + JSON.stringify(key) + colonSeparator + value;
|
|
86
|
+
}
|
|
87
|
+
seen.splice(seenIndex, 1);
|
|
88
|
+
return '{' + out + indent + '}';
|
|
89
|
+
})(data, 0);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// module.exports = function (data, opts) {
|
|
93
|
+
// if (!opts) opts = {};
|
|
94
|
+
// if (typeof opts === 'function') opts = { cmp: opts };
|
|
95
|
+
// var cycles = (typeof opts.cycles === 'boolean') ? opts.cycles : false;
|
|
96
|
+
|
|
97
|
+
// var cmp = opts.cmp && (function (f) {
|
|
98
|
+
// return function (node) {
|
|
99
|
+
// return function (a, b) {
|
|
100
|
+
// var aobj = { key: a, value: node[a] };
|
|
101
|
+
// var bobj = { key: b, value: node[b] };
|
|
102
|
+
// return f(aobj, bobj);
|
|
103
|
+
// };
|
|
104
|
+
// };
|
|
105
|
+
// })(opts.cmp);
|
|
106
|
+
|
|
107
|
+
// var seen = [];
|
|
108
|
+
// return (function stringify (node) {
|
|
109
|
+
// if (node && node.toJSON && typeof node.toJSON === 'function') {
|
|
110
|
+
// node = node.toJSON();
|
|
111
|
+
// }
|
|
112
|
+
|
|
113
|
+
// if (node === undefined) return;
|
|
114
|
+
// if (typeof node == 'number') return isFinite(node) ? '' + node : 'null';
|
|
115
|
+
// if (typeof node !== 'object') return JSON.stringify(node);
|
|
116
|
+
|
|
117
|
+
// var i, out;
|
|
118
|
+
// if (Array.isArray(node)) {
|
|
119
|
+
// out = '[';
|
|
120
|
+
// for (i = 0; i < node.length; i++) {
|
|
121
|
+
// if (i) out += ',';
|
|
122
|
+
// out += stringify(node[i]) || 'null';
|
|
123
|
+
// }
|
|
124
|
+
// return out + ']';
|
|
125
|
+
// }
|
|
126
|
+
|
|
127
|
+
// if (node === null) return 'null';
|
|
128
|
+
|
|
129
|
+
// if (seen.indexOf(node) !== -1) {
|
|
130
|
+
// if (cycles) return JSON.stringify('__cycle__');
|
|
131
|
+
// throw new TypeError('Converting circular structure to JSON');
|
|
132
|
+
// }
|
|
133
|
+
|
|
134
|
+
// var seenIndex = seen.push(node) - 1;
|
|
135
|
+
// var keys = Object.keys(node).sort(cmp && cmp(node));
|
|
136
|
+
// out = '';
|
|
137
|
+
// for (i = 0; i < keys.length; i++) {
|
|
138
|
+
// var key = keys[i];
|
|
139
|
+
// var value = stringify(node[key]);
|
|
140
|
+
|
|
141
|
+
// if (!value) continue;
|
|
142
|
+
// if (out) out += ',';
|
|
143
|
+
// out += JSON.stringify(key) + ':' + value;
|
|
144
|
+
// }
|
|
145
|
+
// seen.splice(seenIndex, 1);
|
|
146
|
+
// return '{' + out + '}';
|
|
147
|
+
// })(data);
|
|
148
|
+
// };
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@casual-simulation/fast-json-stable-stringify",
|
|
3
|
+
"version": "2.0.12",
|
|
4
|
+
"description": "deterministic `JSON.stringify()` - a faster version of substack's json-stable-strigify without jsonify",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"module": "index.js",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"eslint": "eslint index.js test",
|
|
10
|
+
"test-spec": "tape test/*.js",
|
|
11
|
+
"test": "npm run eslint && nyc npm run test-spec"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git@github.com:casual-simulation/casualos.git"
|
|
16
|
+
},
|
|
17
|
+
"homepage": "https://github.com/casual-simulation/casualos/tree/develop/src/fast-json-stable-stringify",
|
|
18
|
+
"keywords": [
|
|
19
|
+
"json",
|
|
20
|
+
"stringify",
|
|
21
|
+
"deterministic",
|
|
22
|
+
"hash",
|
|
23
|
+
"stable"
|
|
24
|
+
],
|
|
25
|
+
"author": {
|
|
26
|
+
"name": "James Halliday",
|
|
27
|
+
"email": "mail@substack.net",
|
|
28
|
+
"url": "http://substack.net"
|
|
29
|
+
},
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"nyc": {
|
|
32
|
+
"exclude": [
|
|
33
|
+
"test",
|
|
34
|
+
"node_modules"
|
|
35
|
+
],
|
|
36
|
+
"reporter": [
|
|
37
|
+
"lcov",
|
|
38
|
+
"text-summary"
|
|
39
|
+
]
|
|
40
|
+
},
|
|
41
|
+
"engines": {
|
|
42
|
+
"node": ">=8"
|
|
43
|
+
},
|
|
44
|
+
"publishConfig": {
|
|
45
|
+
"access": "public"
|
|
46
|
+
},
|
|
47
|
+
"gitHead": "fdcd097e87afbd35b7cf9050c8b8c0cde73c1b2b"
|
|
48
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../tsconfig.base.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"baseUrl": ".",
|
|
5
|
+
"outDir": ".",
|
|
6
|
+
"declaration": true,
|
|
7
|
+
"declarationMap": true,
|
|
8
|
+
"composite": true,
|
|
9
|
+
"incremental": true
|
|
10
|
+
},
|
|
11
|
+
"include": ["./**/*"],
|
|
12
|
+
"exclude": ["node_modules", "lib", "**/*.spec.ts"],
|
|
13
|
+
"references": []
|
|
14
|
+
}
|