This repository has been archived by the owner on May 12, 2022. It is now read-only.
/
index.js
213 lines (178 loc) · 4.58 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* globals window */
(function () {
var cyclicalJSON = {};
var versionString = '"version":"cyclical-json@' + '@MODULE_VERSION@' + '"';
var WeakMap = (function () {
var WM = function () {
Object.defineProperties(this, {
keys: {
value: []
},
values: {
value: []
}
});
};
WM.prototype.get = function (key) {
var index = this.keys.indexOf(key);
return this.values[index];
};
WM.prototype.has = function (key) {
return this.keys.indexOf(key) >= 0;
};
WM.prototype.set = function (key, value) {
var index = this.keys.indexOf(key);
if (index >= 0) {
this.values[index] = value;
} else {
this.keys.push(key);
this.values.push(value);
}
return this;
};
return WM;
}());
var shouldPassThrough = function (value) {
return typeof value !== 'object' ||
value === null ||
typeof value.toJSON === 'function' ||
value instanceof String ||
value instanceof Number ||
value instanceof RegExp ||
value instanceof Date ||
value instanceof Boolean;
};
var specialChar = '~';
var isSpecialLiteral = function (value) {
return typeof value === 'string' &&
value.indexOf(specialChar + specialChar) === 0;
};
var isSpecial = function (value) {
return typeof value === 'string' &&
value.indexOf(specialChar) === 0 &&
!isSpecialLiteral(value);
};
var escapeSpecialChar = function (value) {
// append one specialChar to act as an escape
return isSpecial(value) || isSpecialLiteral(value)
? specialChar + value
: value;
};
var trimSpecialChar = function (value) {
return value.slice(1);
};
var generateReplacer = function (replacer) {
if (typeof replacer !== 'function') {
return replacer;
}
return function (key, value) {
if (isSpecialLiteral(value)) {
return escapeSpecialChar(replacer(key, trimSpecialChar(value)));
}
return isSpecial(value)
? value : replacer(key, value);
};
};
var generateReviver = function (reviver) {
if (typeof reviver !== 'function') {
return reviver;
}
return function (key, value) {
if (isSpecialLiteral(value)) {
return escapeSpecialChar(reviver(key, trimSpecialChar(value)));
}
return isSpecial(value) ? value : reviver(key, value);
};
};
var decycle = function (base) {
var legend = [];
var weakMap = new WeakMap;
var walk = function (current, path) {
var modified = current;
if (!shouldPassThrough(current)) {
if (weakMap.has(current)) {
if (weakMap.get(current) instanceof Array) {
legend.push(weakMap.get(current));
weakMap.set(current, String(specialChar + (legend.length - 1)));
}
modified = weakMap.get(current);
} else {
weakMap.set(current, path);
modified = Object.keys(current).reduce(function (obj, sub) {
obj[sub] = walk(current[sub], path.concat(sub));
return obj;
}, current instanceof Array ? [] : {});
}
}
if (typeof current === 'string') {
modified = escapeSpecialChar(current);
}
return modified;
};
return {
legend: legend,
main: walk(base, [])
};
};
var recycle = function (master) {
var walk = function (current, key, parent) {
var modified = current;
var index;
if (!shouldPassThrough(current)) {
Object.keys(current).forEach(function (sub) {
return walk(current[sub], sub, current);
});
}
if (isSpecial(current)) {
modified = master.main;
index = Number(trimSpecialChar(current));
master.legend[index].forEach(function (sub) {
return modified = modified[sub];
});
}
if (isSpecialLiteral(current)) {
modified = trimSpecialChar(current);
}
if (parent) {
parent[key] = modified;
}
return modified;
};
if (
typeof master !== 'object' ||
master === null ||
master.main === undefined ||
master.legend === undefined ||
!(master.legend instanceof Array) ||
master.version === undefined ||
master.version.indexOf('cyclical-json') < 0
) { // is not a cyclicalJSON string
return master;
}
return walk(master.main);
};
cyclicalJSON.stringify = function (value, replacer, space) {
var master = decycle(value);
var legend = JSON.stringify(master.legend);
var main = JSON.stringify(
master.main,
generateReplacer(replacer),
space
);
return main !== undefined
? '{' +
'"legend":' + legend + ',' +
'"main":' + main + ',' +
versionString +
'}'
: main;
};
cyclicalJSON.parse = function (text, reviver) {
return recycle(JSON.parse(text, generateReviver(reviver)));
};
try {
module.exports = cyclicalJSON;
} catch (err) {
window.cyclicalJSON = cyclicalJSON;
}
}());