-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
395 lines (334 loc) · 9.9 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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
'use strict';
var whichTypedArray = require('which-typed-array');
var taSlice = require('typedarray.prototype.slice');
var gopd = require('gopd');
// TODO: use call-bind, is-date, is-regex, is-string, is-boolean-object, is-number-object
function toS(obj) { return Object.prototype.toString.call(obj); }
function isDate(obj) { return toS(obj) === '[object Date]'; }
function isRegExp(obj) { return toS(obj) === '[object RegExp]'; }
function isError(obj) { return toS(obj) === '[object Error]'; }
function isBoolean(obj) { return toS(obj) === '[object Boolean]'; }
function isNumber(obj) { return toS(obj) === '[object Number]'; }
function isString(obj) { return toS(obj) === '[object String]'; }
// TODO: use isarray
var isArray = Array.isArray || function isArray(xs) {
return Object.prototype.toString.call(xs) === '[object Array]';
};
// TODO: use for-each?
function forEach(xs, fn) {
if (xs.forEach) { return xs.forEach(fn); }
for (var i = 0; i < xs.length; i++) {
fn(xs[i], i, xs);
}
return void undefined;
}
// TODO: use object-keys
var objectKeys = Object.keys || function keys(obj) {
var res = [];
for (var key in obj) { res[res.length] = key; } // eslint-disable-line no-restricted-syntax
return res;
};
var propertyIsEnumerable = Object.prototype.propertyIsEnumerable;
var getOwnPropertySymbols = Object.getOwnPropertySymbols; // eslint-disable-line id-length
// TODO: use reflect.ownkeys and filter out non-enumerables
function ownEnumerableKeys(obj) {
var res = objectKeys(obj);
// Include enumerable symbol properties.
if (getOwnPropertySymbols) {
var symbols = getOwnPropertySymbols(obj);
for (var i = 0; i < symbols.length; i++) {
if (propertyIsEnumerable.call(obj, symbols[i])) {
res[res.length] = symbols[i];
}
}
}
return res;
}
// TODO: use object.hasown
var hasOwnProperty = Object.prototype.hasOwnProperty || function (obj, key) {
return key in obj;
};
function isWritable(object, key) {
if (typeof gopd !== 'function') {
return true;
}
var desc = gopd(object, key);
return !desc || !desc.writable;
}
function copy(src, options) {
if (typeof src === 'object' && src !== null) {
var dst;
if (isArray(src)) {
dst = [];
} else if (isDate(src)) {
dst = new Date(src.getTime ? src.getTime() : src);
} else if (isRegExp(src)) {
dst = new RegExp(src);
} else if (isError(src)) {
dst = { message: src.message };
} else if (isBoolean(src) || isNumber(src) || isString(src)) {
dst = Object(src);
} else {
var ta = whichTypedArray(src);
if (ta) {
return taSlice(src);
} else if (Object.create && Object.getPrototypeOf) {
dst = Object.create(Object.getPrototypeOf(src));
} else if (src.constructor === Object) {
dst = {};
} else {
var proto = (src.constructor && src.constructor.prototype)
|| src.__proto__
|| {};
var T = function T() {}; // eslint-disable-line func-style, func-name-matching
T.prototype = proto;
dst = new T();
}
}
var iteratorFunction = options.includeSymbols ? ownEnumerableKeys : objectKeys;
forEach(iteratorFunction(src), function (key) {
dst[key] = src[key];
});
return dst;
}
return src;
}
/** @type {TraverseOptions} */
var emptyNull = { __proto__: null };
function walk(root, cb) {
var path = [];
var parents = [];
var alive = true;
var options = arguments.length > 2 ? arguments[2] : emptyNull;
var iteratorFunction = options.includeSymbols ? ownEnumerableKeys : objectKeys;
var immutable = !!options.immutable;
return (function walker(node_) {
var node = immutable ? copy(node_, options) : node_;
var modifiers = { __proto__: null };
var keepGoing = true;
var state = {
node: node,
node_: node_,
path: [].concat(path),
parent: parents[parents.length - 1],
parents: parents,
key: path[path.length - 1],
removedKeys: { __proto__: null },
isRoot: path.length === 0,
level: path.length,
circular: null,
update: function (x, stopHere) {
if (!state.isRoot) {
state.parent.node[state.key] = x;
}
state.node = x;
if (stopHere) { keepGoing = false; }
},
delete: function (stopHere) {
delete state.parent.node[state.key];
state.parent.removedKeys[state.key] = true;
if (stopHere) { keepGoing = false; }
},
remove: function (stopHere) {
if (isArray(state.parent.node)) {
state.parent.node.splice(state.key, 1);
state.parent.removedKeys[state.key] = true;
if (stopHere) { keepGoing = false; }
} else {
state.delete(stopHere);
}
},
keys: null,
before: function (f) { modifiers.before = f; },
after: function (f) { modifiers.after = f; },
pre: function (f) { modifiers.pre = f; },
post: function (f) { modifiers.post = f; },
stop: function () { alive = false; },
block: function () { keepGoing = false; },
};
if (!alive) { return state; }
function updateState() {
if (typeof state.node === 'object' && state.node !== null) {
if (!state.keys || state.node_ !== state.node) {
state.keys = iteratorFunction(state.node);
}
state.isLeaf = state.keys.length === 0;
for (var i = 0; i < parents.length; i++) {
if (parents[i].node_ === node_) {
state.circular = parents[i];
break; // eslint-disable-line no-restricted-syntax
}
}
} else {
state.isLeaf = true;
state.keys = null;
}
state.notLeaf = !state.isLeaf;
state.notRoot = !state.isRoot;
}
updateState();
// use return values to update if defined
var ret = cb.call(state, state.node);
if (ret !== undefined && state.update) { state.update(ret); }
if (modifiers.before) { modifiers.before.call(state, state.node); }
if (!keepGoing) { return state; }
if (
typeof state.node === 'object'
&& state.node !== null
&& !state.circular
) {
parents[parents.length] = state;
updateState();
forEach(state.keys, function (key, i) {
var prevIsRemoved = (i - 1) in state.removedKeys;
if (prevIsRemoved) {
key = state.keys[i - 1]; // eslint-disable-line no-param-reassign
}
path[path.length] = (key);
if (modifiers.pre) { modifiers.pre.call(state, state.node[key], key); }
var child = walker(state.node[key]);
if (
immutable
&& hasOwnProperty.call(state.node, key)
&& !isWritable(state.node, key)
&& !prevIsRemoved
) {
state.node[key] = child.node;
}
child.isLast = i === state.keys.length - 1;
child.isFirst = i === 0;
if (modifiers.post) { modifiers.post.call(state, child); }
path.pop();
});
parents.pop();
}
if (modifiers.after) { modifiers.after.call(state, state.node); }
return state;
}(root)).node;
}
/** @typedef {{ immutable?: boolean, includeSymbols?: boolean }} TraverseOptions */
/**
* A traverse constructor
* @param {object} obj - the object to traverse
* @param {TraverseOptions | undefined} [options] - options for the traverse
* @constructor
*/
function Traverse(obj) {
/** @type {TraverseOptions} */
this.options = arguments.length > 1 ? arguments[1] : emptyNull;
this.value = obj;
}
/** @type {(ps: PropertyKey[]) => Traverse['value']} */
Traverse.prototype.get = function (ps) {
var node = this.value;
for (var i = 0; node && i < ps.length; i++) {
var key = ps[i];
if (
!hasOwnProperty.call(node, key)
|| (!this.options.includeSymbols && typeof key === 'symbol')
) {
return void undefined;
}
node = node[key];
}
return node;
};
/** @type {(ps: PropertyKey[]) => boolean} */
Traverse.prototype.has = function (ps) {
var node = this.value;
// TODO: remove ps.length check
if (!node && ps.length > 0) {
return false;
}
for (var i = 0; node && i < ps.length; i++) {
var key = ps[i];
if (!hasOwnProperty.call(node, key) || (!this.options.includeSymbols && typeof key === 'symbol')) {
return false;
}
node = node[key];
}
return true;
};
Traverse.prototype.set = function (ps, value) {
var node = this.value;
for (var i = 0; i < ps.length - 1; i++) {
var key = ps[i];
if (!hasOwnProperty.call(node, key)) { node[key] = {}; }
node = node[key];
}
node[ps[i]] = value;
return value;
};
Traverse.prototype.map = function (cb) {
return walk(this.value, cb, { __proto__: null, immutable: true, includeSymbols: !!this.options.includeSymbols });
};
Traverse.prototype.forEach = function (cb) {
this.value = walk(this.value, cb, this.options);
return this.value;
};
Traverse.prototype.reduce = function (cb, init) {
var skip = arguments.length === 1;
var acc = skip ? this.value : init;
this.forEach(function (x) {
if (!this.isRoot || !skip) {
acc = cb.call(this, acc, x);
}
});
return acc;
};
Traverse.prototype.paths = function () {
var acc = [];
this.forEach(function () {
acc[acc.length] = this.path;
});
return acc;
};
Traverse.prototype.nodes = function () {
var acc = [];
this.forEach(function () {
acc[acc.length] = this.node;
});
return acc;
};
Traverse.prototype.clone = function () {
var parents = [];
var nodes = [];
var options = this.options;
if (whichTypedArray(this.value)) {
return taSlice(this.value);
}
return (function clone(src) {
for (var i = 0; i < parents.length; i++) {
if (parents[i] === src) {
return nodes[i];
}
}
if (typeof src === 'object' && src !== null) {
var dst = copy(src, options);
parents[parents.length] = (src);
nodes[nodes.length] = (dst);
var iteratorFunction = options.includeSymbols ? ownEnumerableKeys : objectKeys;
forEach(iteratorFunction(src), function (key) {
dst[key] = clone(src[key]);
});
parents.pop();
nodes.pop();
return dst;
}
return src;
}(this.value));
};
/** @type {(obj: object, options?: TraverseOptions) => Traverse} */
function traverse(obj) {
var options = arguments.length > 1 ? arguments[1] : emptyNull;
return new Traverse(obj, options);
}
// TODO: replace with object.assign?
forEach(ownEnumerableKeys(Traverse.prototype), function (key) {
traverse[key] = function (obj) {
var args = [].slice.call(arguments, 1);
var t = new Traverse(obj);
return t[key].apply(t, args);
};
});
module.exports = traverse;