Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50640 views
1
/*
2
cycle.js
3
2013-02-19
4
5
Public Domain.
6
7
NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
8
9
This code should be minified before deployment.
10
See http://javascript.crockford.com/jsmin.html
11
12
USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO
13
NOT CONTROL.
14
*/
15
16
/*jslint evil: true, regexp: true */
17
18
/*members $ref, apply, call, decycle, hasOwnProperty, length, prototype, push,
19
retrocycle, stringify, test, toString
20
*/
21
22
var cycle = exports;
23
24
cycle.decycle = function decycle(object) {
25
'use strict';
26
27
// Make a deep copy of an object or array, assuring that there is at most
28
// one instance of each object or array in the resulting structure. The
29
// duplicate references (which might be forming cycles) are replaced with
30
// an object of the form
31
// {$ref: PATH}
32
// where the PATH is a JSONPath string that locates the first occurance.
33
// So,
34
// var a = [];
35
// a[0] = a;
36
// return JSON.stringify(JSON.decycle(a));
37
// produces the string '[{"$ref":"$"}]'.
38
39
// JSONPath is used to locate the unique object. $ indicates the top level of
40
// the object or array. [NUMBER] or [STRING] indicates a child member or
41
// property.
42
43
var objects = [], // Keep a reference to each unique object or array
44
paths = []; // Keep the path to each unique object or array
45
46
return (function derez(value, path) {
47
48
// The derez recurses through the object, producing the deep copy.
49
50
var i, // The loop counter
51
name, // Property name
52
nu; // The new object or array
53
54
// typeof null === 'object', so go on if this value is really an object but not
55
// one of the weird builtin objects.
56
57
if (typeof value === 'object' && value !== null &&
58
!(value instanceof Boolean) &&
59
!(value instanceof Date) &&
60
!(value instanceof Number) &&
61
!(value instanceof RegExp) &&
62
!(value instanceof String)) {
63
64
// If the value is an object or array, look to see if we have already
65
// encountered it. If so, return a $ref/path object. This is a hard way,
66
// linear search that will get slower as the number of unique objects grows.
67
68
for (i = 0; i < objects.length; i += 1) {
69
if (objects[i] === value) {
70
return {$ref: paths[i]};
71
}
72
}
73
74
// Otherwise, accumulate the unique value and its path.
75
76
objects.push(value);
77
paths.push(path);
78
79
// If it is an array, replicate the array.
80
81
if (Object.prototype.toString.apply(value) === '[object Array]') {
82
nu = [];
83
for (i = 0; i < value.length; i += 1) {
84
nu[i] = derez(value[i], path + '[' + i + ']');
85
}
86
} else {
87
88
// If it is an object, replicate the object.
89
90
nu = {};
91
for (name in value) {
92
if (Object.prototype.hasOwnProperty.call(value, name)) {
93
nu[name] = derez(value[name],
94
path + '[' + JSON.stringify(name) + ']');
95
}
96
}
97
}
98
return nu;
99
}
100
return value;
101
}(object, '$'));
102
};
103
104
105
cycle.retrocycle = function retrocycle($) {
106
'use strict';
107
108
// Restore an object that was reduced by decycle. Members whose values are
109
// objects of the form
110
// {$ref: PATH}
111
// are replaced with references to the value found by the PATH. This will
112
// restore cycles. The object will be mutated.
113
114
// The eval function is used to locate the values described by a PATH. The
115
// root object is kept in a $ variable. A regular expression is used to
116
// assure that the PATH is extremely well formed. The regexp contains nested
117
// * quantifiers. That has been known to have extremely bad performance
118
// problems on some browsers for very long strings. A PATH is expected to be
119
// reasonably short. A PATH is allowed to belong to a very restricted subset of
120
// Goessner's JSONPath.
121
122
// So,
123
// var s = '[{"$ref":"$"}]';
124
// return JSON.retrocycle(JSON.parse(s));
125
// produces an array containing a single element which is the array itself.
126
127
var px =
128
/^\$(?:\[(?:\d+|\"(?:[^\\\"\u0000-\u001f]|\\([\\\"\/bfnrt]|u[0-9a-zA-Z]{4}))*\")\])*$/;
129
130
(function rez(value) {
131
132
// The rez function walks recursively through the object looking for $ref
133
// properties. When it finds one that has a value that is a path, then it
134
// replaces the $ref object with a reference to the value that is found by
135
// the path.
136
137
var i, item, name, path;
138
139
if (value && typeof value === 'object') {
140
if (Object.prototype.toString.apply(value) === '[object Array]') {
141
for (i = 0; i < value.length; i += 1) {
142
item = value[i];
143
if (item && typeof item === 'object') {
144
path = item.$ref;
145
if (typeof path === 'string' && px.test(path)) {
146
value[i] = eval(path);
147
} else {
148
rez(item);
149
}
150
}
151
}
152
} else {
153
for (name in value) {
154
if (typeof value[name] === 'object') {
155
item = value[name];
156
if (item) {
157
path = item.$ref;
158
if (typeof path === 'string' && px.test(path)) {
159
value[name] = eval(path);
160
} else {
161
rez(item);
162
}
163
}
164
}
165
}
166
}
167
}
168
}($));
169
return $;
170
};
171
172