forked from gliviu/json-easy-filter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jef-browser.js
354 lines (317 loc) · 11.7 KB
/
jef-browser.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
var Jef = function () {
"use strict";
var _getPathStr = function (path, delimiter) {
if (!delimiter) {
delimiter = '.';
}
var res = '';
for (var i = 0; i < path.length; i++) {
if (i !== 0) {
res += delimiter;
}
res += path[i];
}
return res;
};
var JefLocalContext = function (localNode, rootNode) {
this.isRoot = false;
this.root = rootNode;
if (rootNode.path === localNode.path) {
this.isRoot = true;
}
this.level = localNode.pathArray.length - rootNode.pathArray.length;
this.pathArray = localNode.pathArray.slice(rootNode.pathArray.length, localNode.pathArray.length);
this.path = _getPathStr(this.pathArray);
};
var JefNode = function (obj) {
this.isEmpty = function () {
return this.count === 0;
};
this.get = function (relPathStr) {
if (!relPathStr) {
return this;
}
var absolutePath;
if (this.isRoot) {
absolutePath = relPathStr;
} else {
absolutePath = this.path + '.' + relPathStr;
}
return this._nodeHash[rootkey + '.' + absolutePath];
};
/**
* Internal method for iterating 'this' and its children.
*/
this._iterate = function (iterCallback) {
var that = this;
traverse(this.value, function (key, val, pathArr, parentKey, parentVal, level, isRoot) {
var pathStr = that._internalPath + (isRoot ? '' : '.' + _getPathStr(pathArr));
var node = that._nodeHash[pathStr];
iterCallback(node);
});
};
this._filter = function (callback, level) {
var result = [];
var that = this;
this._iterate(function (node) {
var localContext = new JefLocalContext(node, that)
if(!level || (level && localContext.level === level)){
var resCallBack = callback(node, localContext);
if (resCallBack !== undefined) {
result.push(resCallBack);
}
}
});
return result;
};
this.filter = function (callback) {
return this._filter(callback);
};
this.filterFirst = function(callback){
return this._filter(callback, 1);
}
this.filterLevel = function(level, callback){
return this._filter(callback, level);
}
this.validate = function (callback) {
var result = true;
this.filter(function (node, localContext) {
var resCallBack = callback(node, localContext);
if (resCallBack === false) {
result = false;
}
});
return result;
};
this.remove = function (callback) {
var toDelete = this.filter(function (node, localContext) {
return callback(node, localContext);
});
var successful = true;
// one toDelete element may be a JefNode or an array of JefNodes
// recurses into result and removes nodes.
var removeNodes = function (node) {
if (node instanceof Array) {
node.forEach(function (arrayElem) {
removeNodes(arrayElem);
});
} else if (node instanceof JefNode) {
if (node.isRoot) {
console.log("Root node cannot be deleted.");
successful = false;
} else if (node.parent.type() === 'array') {
var array = node.parent.value;
var index = array.indexOf(node.value);
array.splice(index, 1);
} else {
delete node.parent.value[node.key];
}
} else {
console.log("Only JefNode objects must be returned from delete callback.");
successful = false;
}
};
removeNodes(toDelete);
return successful;
};
this.has = function (key) {
if (!this.value) {
return false;
}
if (key instanceof RegExp) {
if (this.type() === 'object') {
for ( var k in this.value) {
if (key.test(k)) {
return true;
}
}
return false;
} else {
return false;
}
} else {
if (this.value[key] !== undefined) {
return true;
} else {
return false;
}
}
};
/**
* Deprecated.
*/
this.getType = function () {
return this.type();
};
/**
* Returns one of 'string', 'array', 'object', 'function', 'number',
* 'boolean', 'undefined', 'null'
*/
this.type = function () {
return ({}).toString.call(this.value).match(/\s([a-zA-Z]+)/)[1].toLowerCase();
};
/**
* Returns true if current value has any of the types given as
* arguments.
*/
this.hasType = function () {
if (arguments.length === 1) {
return this.type() === arguments[0];
} else {
for (var i = 0; i < arguments.length; i++) {
if (this.type() === arguments[i]) {
return true;
}
}
return false;
}
};
this.refresh = function () {
// mark all children to be deleted. later they will be unmarked if
// necessary
var toDelete = {};
var internalPath = this._internalPath + '.';
for ( var absolutePath in this._nodeHash) {
if (absolutePath.indexOf(internalPath) === 0) {
toDelete[absolutePath] = true;
}
}
var that = this;
traverse(this.value, function (key, val, path, parentKey, parentVal, level, isRoot, isLeaf, isCircular) {
if (isRoot) {
return;
}
var internalPath = that._internalPath + '.' + _getPathStr(path);
toDelete[internalPath] = false;
// create or get existing node
var node;
if (!that._nodeHash[internalPath]) {
node = new JefNode();
node._nodeHash = that._nodeHash;
node.root = that._nodeHash[rootkey];
node._internalPath = internalPath;
node.pathArray = that.pathArray.concat(path);
node.path = that.isRoot ? _getPathStr(node.pathArray) : that.path + '.' + _getPathStr(node.pathArray);
node.key = key;
that._nodeHash[internalPath] = node;
// parent
var parentPath = node.pathArray.slice(0, node.pathArray.length - 1);
var parentNode;
if (parentPath.length === 0) {
// parent is root node
parentNode = that._nodeHash[rootkey];
} else {
parentNode = that._nodeHash[rootkey + '.' + _getPathStr(parentPath)];
}
if (parentNode) {
node.parent = parentNode;
}
// count
node.parent.count++;
} else {
node = that._nodeHash[internalPath];
}
// update node
node.value = val;
node.level = that.level + level;
node.isRoot = false;
node.isLeaf = isLeaf;
node.isCircular = isCircular;
});
// remove nodes that no longer exist
for ( var internalPath in toDelete) {
if (toDelete[internalPath] === true) {
delete this._nodeHash[internalPath];
}
}
};
// ////////////////////////
// Private stuff
// ////////////////////////
this._nodeHash = null;
this._internalPath = null; // Just like this.path only it starts with
this.print = function (showContent) {
var res = [];
var that = this;
this._iterate(function (node) {
if (showContent) {
res.push(node._internalPath.replace(rootkey, 'root') + ' - ' + JSON.stringify(that._nodeHash[node._internalPath].value));
} else {
res.push(node._internalPath.replace(rootkey, 'root'));
}
});
return res;
};
// ////////////////////////
// Constructor
// ////////////////////////
this.count = 0;
if (obj) {
this.root = this;
this._nodeHash = {};
this.pathArray = [];
this.path = '';
this.value = obj;
this.level = 0;
this.isRoot = true;
this.isLeaf = isItLeaf(obj);
this.isCircular = false;
this._internalPath = rootkey;
this._nodeHash[rootkey] = this;
this.parent = this;
this.refresh();
}
};
var isItLeaf = function (val) {
var isArray = val instanceof Array;
var isObject = typeof val === 'object' && !isArray;
// Leaf
var notLeaf = (isObject && val !== null) || isArray;
return !notLeaf;
};
var traverse = function (obj, callback) {
var seenObjects = [];
var recurse = function (key, val, parentKey, parentVal, isRoot, path, level) {
var isArray = val instanceof Array;
var isObject = typeof val === 'object' && !isArray;
// Leaf
var isLeaf = isItLeaf(val);
// Circular
var isCircular = false;
if (seenObjects.indexOf(val) !== -1) {
isCircular = true;
} else {
seenObjects.push(val);
}
callback(key, val, path, parentKey, parentVal, level, isRoot, isLeaf, isCircular);
if (!isCircular) {
var childKey, newPath;
if (isObject) {
for (childKey in val) {
newPath = path.concat([
childKey
]);
recurse(childKey, val[childKey], key, val, false, newPath, level + 1);
}
}
if (isArray) {
for (var i = 0; i < val.length; i++) {
childKey = '' + i;
newPath = path.concat([
childKey
]);
recurse(childKey, val[i], key, val, false, newPath, level + 1);
}
}
}
};
recurse(undefined, obj, undefined, undefined, true, [], 0);
};
var rootkey = '$root_4285190851';
return {
'JefNode' : JefNode,
'traverse' : traverse
};
}();
var JefNode = Jef.JefNode;
var traverse = Jef.traverse;