1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | 'use strict';
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | function dedupeJSONStringify(jsonValue) {
|
18 |
|
19 |
|
20 | jsonValue = JSON.parse(JSON.stringify(jsonValue));
|
21 | var metadataForHash = new Map();
|
22 | var metadataForVal = new WeakMap();
|
23 | var varDefs = [];
|
24 | collectMetadata(jsonValue);
|
25 | collectDuplicates(jsonValue);
|
26 | var code = printJSCode(false, '', jsonValue);
|
27 | return varDefs.length === 0 ? code : "(function(){\nvar ".concat(varDefs.join(',\n'), ";\nreturn ").concat(code, ";\n})()");
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | function collectMetadata(value) {
|
33 | if (value == null || typeof value !== 'object') {
|
34 | return JSON.stringify(value);
|
35 | }
|
36 |
|
37 | var hash;
|
38 |
|
39 | if (Array.isArray(value)) {
|
40 | hash = '[';
|
41 |
|
42 | for (var i = 0; i < value.length; i++) {
|
43 | hash += collectMetadata(value[i]) + ',';
|
44 | }
|
45 | } else {
|
46 | hash = '{';
|
47 |
|
48 | for (var k in value) {
|
49 | if (value.hasOwnProperty(k) && value[k] !== undefined) {
|
50 | hash += k + ':' + collectMetadata(value[k]) + ',';
|
51 | }
|
52 | }
|
53 | }
|
54 |
|
55 | var metadata = metadataForHash.get(hash);
|
56 |
|
57 | if (!metadata) {
|
58 | metadata = {
|
59 | value: value,
|
60 | hash: hash,
|
61 | isDuplicate: false
|
62 | };
|
63 | metadataForHash.set(hash, metadata);
|
64 | }
|
65 |
|
66 | metadataForVal.set(value, metadata);
|
67 | return hash;
|
68 | }
|
69 |
|
70 |
|
71 |
|
72 | function collectDuplicates(value) {
|
73 | if (value == null || typeof value !== 'object') {
|
74 | return;
|
75 | }
|
76 |
|
77 | var metadata = metadataForVal.get(value);
|
78 |
|
79 | if (metadata && metadata.value !== value && metadata.hash.length > 2) {
|
80 | metadata.isDuplicate = true;
|
81 | return;
|
82 | }
|
83 |
|
84 | if (Array.isArray(value)) {
|
85 | for (var i = 0; i < value.length; i++) {
|
86 | collectDuplicates(value[i]);
|
87 | }
|
88 | } else {
|
89 | for (var k in value) {
|
90 | if (value.hasOwnProperty(k) && value[k] !== undefined) {
|
91 | collectDuplicates(value[k]);
|
92 | }
|
93 | }
|
94 | }
|
95 | }
|
96 |
|
97 |
|
98 | function printJSCode(isDupedVar, depth, value) {
|
99 | if (value == null || typeof value !== 'object') {
|
100 | return JSON.stringify(value);
|
101 | }
|
102 |
|
103 |
|
104 | if (depth !== '') {
|
105 | var metadata = metadataForVal.get(value);
|
106 |
|
107 | if (metadata && metadata.isDuplicate) {
|
108 | if (!metadata.varName) {
|
109 | var refCode = printJSCode(true, '', value);
|
110 | metadata.varName = 'v' + varDefs.length;
|
111 | varDefs.push(metadata.varName + ' = ' + refCode);
|
112 | }
|
113 |
|
114 | return '(' + metadata.varName + '/*: any*/)';
|
115 | }
|
116 | }
|
117 |
|
118 | var str;
|
119 | var isEmpty = true;
|
120 | var depth2 = depth + ' ';
|
121 |
|
122 | if (Array.isArray(value)) {
|
123 |
|
124 |
|
125 |
|
126 | if (isDupedVar && value.length === 0) {
|
127 | return '([]/*: any*/)';
|
128 | }
|
129 |
|
130 | str = '[';
|
131 |
|
132 | for (var i = 0; i < value.length; i++) {
|
133 | str += (isEmpty ? '\n' : ',\n') + depth2 + printJSCode(isDupedVar, depth2, value[i]);
|
134 | isEmpty = false;
|
135 | }
|
136 |
|
137 | str += isEmpty ? ']' : "\n".concat(depth, "]");
|
138 | } else {
|
139 | str = '{';
|
140 |
|
141 | for (var k in value) {
|
142 | if (value.hasOwnProperty(k) && value[k] !== undefined) {
|
143 | str += (isEmpty ? '\n' : ',\n') + depth2 + JSON.stringify(k) + ': ' + printJSCode(isDupedVar, depth2, value[k]);
|
144 | isEmpty = false;
|
145 | }
|
146 | }
|
147 |
|
148 | str += isEmpty ? '}' : "\n".concat(depth, "}");
|
149 | }
|
150 |
|
151 | return str;
|
152 | }
|
153 | }
|
154 |
|
155 | module.exports = dedupeJSONStringify; |
\ | No newline at end of file |