node-red-contrib-timer-events 0.1.0

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.
@@ -0,0 +1,182 @@
1
+ /*
2
+ cycle.js
3
+ 2018-05-15
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
+ // The file uses the WeakMap feature of ES6.
17
+
18
+ /*jslint eval */
19
+
20
+ /*property
21
+ $ref, decycle, forEach, get, indexOf, isArray, keys, length, push,
22
+ retrocycle, set, stringify, test
23
+ */
24
+
25
+ if (typeof JSON.decycle !== "function") {
26
+ JSON.decycle = function decycle(object, replacer) {
27
+ "use strict";
28
+
29
+ // Make a deep copy of an object or array, assuring that there is at most
30
+ // one instance of each object or array in the resulting structure. The
31
+ // duplicate references (which might be forming cycles) are replaced with
32
+ // an object of the form
33
+
34
+ // {"$ref": PATH}
35
+
36
+ // where the PATH is a JSONPath string that locates the first occurance.
37
+
38
+ // So,
39
+
40
+ // var a = [];
41
+ // a[0] = a;
42
+ // return JSON.stringify(JSON.decycle(a));
43
+
44
+ // produces the string '[{"$ref":"$"}]'.
45
+
46
+ // If a replacer function is provided, then it will be called for each value.
47
+ // A replacer function receives a value and returns a replacement value.
48
+
49
+ // JSONPath is used to locate the unique object. $ indicates the top level of
50
+ // the object or array. [NUMBER] or [STRING] indicates a child element or
51
+ // property.
52
+
53
+ var objects = new WeakMap(); // object to path mappings
54
+
55
+ return (function derez(value, path) {
56
+
57
+ // The derez function recurses through the object, producing the deep copy.
58
+
59
+ var old_path; // The path of an earlier occurance of value
60
+ var nu; // The new object or array
61
+
62
+ // If a replacer function was provided, then call it to get a replacement value.
63
+
64
+ if (replacer !== undefined) {
65
+ value = replacer(value);
66
+ }
67
+
68
+ // typeof null === "object", so go on if this value is really an object but not
69
+ // one of the weird builtin objects.
70
+
71
+ if (
72
+ typeof value === "object"
73
+ && value !== null
74
+ && !(value instanceof Boolean)
75
+ && !(value instanceof Date)
76
+ && !(value instanceof Number)
77
+ && !(value instanceof RegExp)
78
+ && !(value instanceof String)
79
+ ) {
80
+
81
+ // If the value is an object or array, look to see if we have already
82
+ // encountered it. If so, return a {"$ref":PATH} object. This uses an
83
+ // ES6 WeakMap.
84
+
85
+ old_path = objects.get(value);
86
+ if (old_path !== undefined) {
87
+ return {$ref: old_path};
88
+ }
89
+
90
+ // Otherwise, accumulate the unique value and its path.
91
+
92
+ objects.set(value, path);
93
+
94
+ // If it is an array, replicate the array.
95
+
96
+ if (Array.isArray(value)) {
97
+ nu = [];
98
+ value.forEach(function (element, i) {
99
+ nu[i] = derez(element, path + "[" + i + "]");
100
+ });
101
+ } else {
102
+
103
+ // If it is an object, replicate the object.
104
+
105
+ nu = {};
106
+ Object.keys(value).forEach(function (name) {
107
+ nu[name] = derez(
108
+ value[name],
109
+ path + "[" + JSON.stringify(name) + "]"
110
+ );
111
+ });
112
+ }
113
+ return nu;
114
+ }
115
+ return value;
116
+ }(object, "$"));
117
+ };
118
+ }
119
+
120
+
121
+ if (typeof JSON.retrocycle !== "function") {
122
+ JSON.retrocycle = function retrocycle($) {
123
+ "use strict";
124
+
125
+ // Restore an object that was reduced by decycle. Members whose values are
126
+ // objects of the form
127
+ // {$ref: PATH}
128
+ // are replaced with references to the value found by the PATH. This will
129
+ // restore cycles. The object will be mutated.
130
+
131
+ // The eval function is used to locate the values described by a PATH. The
132
+ // root object is kept in a $ variable. A regular expression is used to
133
+ // assure that the PATH is extremely well formed. The regexp contains nested
134
+ // * quantifiers. That has been known to have extremely bad performance
135
+ // problems on some browsers for very long strings. A PATH is expected to be
136
+ // reasonably short. A PATH is allowed to belong to a very restricted subset of
137
+ // Goessner's JSONPath.
138
+
139
+ // So,
140
+ // var s = '[{"$ref":"$"}]';
141
+ // return JSON.retrocycle(JSON.parse(s));
142
+ // produces an array containing a single element which is the array itself.
143
+
144
+ var px = /^\$(?:\[(?:\d+|"(?:[^\\"\u0000-\u001f]|\\(?:[\\"\/bfnrt]|u[0-9a-zA-Z]{4}))*")\])*$/;
145
+
146
+ (function rez(value) {
147
+
148
+ // The rez function walks recursively through the object looking for $ref
149
+ // properties. When it finds one that has a value that is a path, then it
150
+ // replaces the $ref object with a reference to the value that is found by
151
+ // the path.
152
+
153
+ if (value && typeof value === "object") {
154
+ if (Array.isArray(value)) {
155
+ value.forEach(function (element, i) {
156
+ if (typeof element === "object" && element !== null) {
157
+ var path = element.$ref;
158
+ if (typeof path === "string" && px.test(path)) {
159
+ value[i] = eval(path);
160
+ } else {
161
+ rez(element);
162
+ }
163
+ }
164
+ });
165
+ } else {
166
+ Object.keys(value).forEach(function (name) {
167
+ var item = value[name];
168
+ if (typeof item === "object" && item !== null) {
169
+ var path = item.$ref;
170
+ if (typeof path === "string" && px.test(path)) {
171
+ value[name] = eval(path);
172
+ } else {
173
+ rez(item);
174
+ }
175
+ }
176
+ });
177
+ }
178
+ }
179
+ }($));
180
+ return $;
181
+ };
182
+ }