UNPKG

3.02 kBJavaScriptView Raw
1// Gratefully borrowed with light modification from https://github.com/substack/json-stable-stringify/blob/master/index.js
2
3var json = typeof JSON !== 'undefined' ? JSON : require('jsonify');
4
5export function jsonStableSerialize(obj, opts) {
6 if (!opts) opts = {};
7 if (typeof opts === 'function') opts = { cmp: opts };
8 var space = opts.space || '';
9 if (typeof space === 'number') space = Array(space+1).join(' ');
10 var cycles = (typeof opts.cycles === 'boolean') ? opts.cycles : false;
11 var replacer = opts.replacer || function(key, value) { return value; };
12
13 var cmp = opts.cmp && (function (f) {
14 return function (node) {
15 return function (a, b) {
16 var aobj = { key: a, value: node[a] };
17 var bobj = { key: b, value: node[b] };
18 return f(aobj, bobj);
19 };
20 };
21 })(opts.cmp);
22
23 var seen = [];
24 return (function jsonStableSerialize (parent, key, node, level) {
25 var indent = space ? ('\n' + new Array(level + 1).join(space)) : '';
26 var colonSeparator = space ? ': ' : ':';
27
28 if (node && node.toJSON && typeof node.toJSON === 'function') {
29 node = node.toJSON();
30 }
31
32 node = replacer.call(parent, key, node);
33
34 if (node === undefined) {
35 return;
36 }
37 if (typeof node !== 'object' || node === null) {
38 return json.stringify(node);
39 }
40 if (isArray(node)) {
41 var out = [];
42 for (var i = 0; i < node.length; i++) {
43 var item = jsonStableSerialize(node, i, node[i], level+1) || json.stringify(null);
44 out.push(indent + space + item);
45 }
46 return '[' + out.join(',') + indent + ']';
47 }
48 else {
49 if (seen.indexOf(node) !== -1) {
50 if (cycles) return json.stringify('__cycle__');
51 throw new TypeError('Converting circular structure to JSON');
52 }
53 else seen.push(node);
54
55 var keys = objectKeys(node).sort(cmp && cmp(node));
56 var out = [];
57 for (var i = 0; i < keys.length; i++) {
58 var key = keys[i];
59 var value = jsonStableSerialize(node, key, node[key], level+1);
60
61 if(!value) continue;
62
63 var keyValue = json.stringify(key)
64 + colonSeparator
65 + value;
66 ;
67 out.push(indent + space + keyValue);
68 }
69 seen.splice(seen.indexOf(node), 1);
70 return '{' + out.join(',') + indent + '}';
71 }
72 })({ '': obj }, '', obj, 0);
73};
74
75var isArray = Array.isArray || function (x) {
76 return {}.toString.call(x) === '[object Array]';
77};
78
79var objectKeys = Object.keys || function (obj) {
80 var has = Object.prototype.hasOwnProperty || function () { return true };
81 var keys = [];
82 for (var key in obj) {
83 if (has.call(obj, key)) keys.push(key);
84 }
85 return keys;
86};
87