-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreflection.js
More file actions
455 lines (413 loc) · 10.4 KB
/
Copy pathreflection.js
File metadata and controls
455 lines (413 loc) · 10.4 KB
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//
// Copyright (c) Sebastian Kucharczyk <kuchen@kekse.biz>
// https://kekse.biz/ https://github.com/kekse1/javascripts/
// v3.3.1
//
// The problem was: depending on your JavaScript *environment*, which also changes
// e.g. when using <iframe> or so, the base classes are being initialized/declared/..
// not only once, but multiple times, depending on your environments.
//
// In this case a regular `instanceof` won't match, since there are other references,
// etc.. so check your items (instances, mostly) this way, maybe defining at least one
// string to be checked (as name of the class).
//
// My solution extends the `Reflect` class, and also sets two global functions, so you
// can compare by their name(s); it's like:
//
// # `[Reflect.]is()`: concrete/last class/instance name (returns String/Boolean)
// # `[Reflect.]was()`: List of all class names, including super's (returns Array/Boolean)
//
// Either you call them just with an object/item, to show their names,
// or you define one or many strings, to compare them with your params
// (which will result a Boolean type, not a String or an Array of Strings).
//
// NEW since v2.1.0: 'was()' arguments mean logical AND.. 'is()' still OR.
//
// I'm using it for a long time now, and it really works great. No problems occured,
// and I recommend you to always use this instead of `instanceof` or smth. like it.
//
// UPDATE v3.1.0: Improved the 'Object.{has,get,set,remove}()' functions (and made readable).
// UPDATE v3.3.0: the "bound *" result *can* optionally be changed to w/o "bound"; see also "DEFAULT_BOUND_HIDE".
//
//
const DEFAULT_OBJECT_SEP = '.'; // path separator/delimiter
const DEFAULT_OBJECT_NUL = true; // when creating intermediate objects, use `Object.create(null)` (if not arrays at all)!?
const DEFAULT_OBJECT_SET_BOOL = false; // `Object.set()` will return the set state, instead of the replaced item (if any)..
const DEFAULT_BOUND_HIDE = true; // without e.g. "bound class"..
//
Reflect.defineProperty(Math, 'getIndex', { value: (_index, _length) => {
if(_length < 1)
{
return null;
}
else if((_index %= _length) < 0)
{
_index = ((_length + _index) % _length);
}
return (_index || 0);
}});
Reflect.defineProperty(Object, 'isNull', { value: (... _args) => {
if(_args.length === 0) return null;
else for(var i = 0; i < _args.length; ++i) {
if(typeof _args[i] !== 'object' || _args[i] === null) return false;
else if(Reflect.getPrototypeOf(_args[i]) !== null) return false; }
return true;
}});
Reflect.defineProperty(Reflect, 'getPrototypesOf', { value: (_item) => {
const result = []; var proto = _item; try { do {
if(proto = Reflect.getPrototypeOf(proto)) result.push(proto); else break;
} while(true); } catch(_err) {}; return result; }});
Reflect.defineProperty(Reflect, 'was', { value: (_item, ... _args) => {
var boundHide = DEFAULT_BOUND_HIDE; for(var i = _args.length - 1; i >= 0; --i) {
if(_args[i] === null) boundHide = !boundHide;
else if(typeof _args[i] !== 'string' || _args[i].length === 0) _args.splice(i, 1); }
if(_args.length > 0) _args = Array.from(new Set(_args)); //my "Array.unique()" interpretation
const result = []; const prototypes = Reflect.getPrototypesOf(_item);
if(prototypes.length === 0) result[0] = Reflect.is(_item,
(boundHide !== DEFAULT_BOUND_HIDE ? null : undefined));
else { var name; for(var i = 0, j = 0; i < prototypes.length; ++i)
if(typeof (name = Reflect.is(prototypes[i])) === 'string')
result[j++] = name; }
if(!result[0]) return (_args.length === 0 ? [] : false);
if(_args.length === 0) return result; for(var i = 0; i < _args.length; ++i)
if(!result.includes(_args[i])) return false;
return true;
}});
Reflect.defineProperty(Reflect, 'is', { value: (_item, ... _args) => {
var boundHide = DEFAULT_BOUND_HIDE; var className = true;
for(var i = 0; i < _args.length; ++i) {
if(typeof _args[i] === 'boolean') className = _args.splice(i--, 1)[0];
else if(_args[i] === null) { _args.splice(i--, 1); boundHide = !boundHide; }
else if(typeof _args[i] !== 'string' || _args[i].length === 0) _args.splice(i--, 1); }
if(_args.length > 0) _args = Array.from(new Set(_args)); //my "Array.unique()" interpretation
const tryConstructorName = () => {
try { return _item.constructor.name; } catch(_err) { return ''; }};
const tryClassName = () => {
try { return _item.name; } catch(_err) { return ''; }};
var result;
if(typeof _item === 'undefined') result = 'undefined';
else if(_item === null) result = 'null';
else if(Object.isNull(_item)) result = 'Object[null]';
else result = tryConstructorName();
if(!result && className) result = tryClassName();
if(!result && _args.length > 0) return false;
if(result.startsWith('bound ')) { if(boundHide || _args.length > 0) result = result.substr(6); }
if(_args.length === 0) return result;
return _args.includes(result);
}});
//
if(typeof window === 'undefined')
{
global.was = Reflect.was;
global.is = Reflect.is;
}
else
{
window.was = Reflect.was;
window.is = Reflect.is;
}
//
const getPathArray = (_path, _sep = DEFAULT_OBJECT_SEP) => {
if(typeof _path === 'number')
{
return [ Math.trunc(_path) ];
}
else if(Array.isArray(_path))
{
return _path;
}
else if(typeof _path !== 'string')
{
return null;
}
else if(_path.length === 0)
{
return null;
}
const result = [ '' ];
for(var i = 0, j = 0; i < _path.length; ++i)
{
if(_path.at(i, _sep))
{
if(result[j].length > 0)
{
result[++j] = '';
}
}
else
{
result[j] += _path[i];
}
}
for(var i = 0; i < result.length; ++i)
{
if(result[i].length === 0)
{
result.splice(i--, 1);
}
else if(!isNaN(result[i]))
{
result[i] = Math.trunc(Number(result[i]));
}
}
if(result.length === 0)
{
return null;
}
return result;
};
Reflect.defineProperty(Object, 'has', { value: (_path, _context = global, _sep = DEFAULT_OBJECT_SEP) => {
if((_path = getPathArray(_path, _sep)) === null) return _context; var ctx = _context; var done; try
{
for(var i = 0; i < _path.length; ++i)
{
done = false;
if(Array.isArray(ctx))
{
if(ctx.length === 0)
{
return false;
}
else if(typeof _path[i] === 'number' && _path[i] < 0)
{
ctx = ctx[Math.getIndex(_path[i], ctx.length)];
done = true;
}
}
if(!done)
{
if(_path[i] in ctx)
{
ctx = ctx[_path[i]];
}
else
{
return false;
}
}
}
}
catch(_err)
{
return false;
}
return true;
}});
Reflect.defineProperty(Object, 'get', { value: (_path, _context = global, _sep = DEFAULT_OBJECT_SEP) => {
if((_path = getPathArray(_path, _sep)) === null) return _context; var ctx = _context; var done; const last = _path.pop(); try
{
for(var i = 0; i < _path.length; ++i)
{
done = false;
if(Array.isArray(ctx))
{
if(ctx.length === 0)
{
return undefined;
}
else if(typeof _path[i] === 'number' && _path[i] < 0)
{
ctx = ctx[Math.getIndex(_path[i], ctx.length)];
done = true;
}
}
if(!done)
{
if(_path[i] in ctx)
{
ctx = ctx[_path[i]];
}
else
{
return undefined;
}
}
}
if(Array.isArray(ctx))
{
if(ctx.length === 0)
{
return undefined;
}
else if(typeof last === 'number' && last < 0)
{
return ctx[Math.getIndex(last, ctx.length)];
}
}
if(last in ctx)
{
return ctx[last];
}
}
catch(_err)
{
return undefined;
}
return undefined;
}});
Reflect.defineProperty(Object, 'set', { value: (_path, _value, _context = global, _sep = DEFAULT_OBJECT_SEP, _null = DEFAULT_OBJECT_NUL) => {
if((_path = getPathArray(_path, _sep)) === null) return _context; var ctx = _context; var last = _path.pop(); var result; try
{
const getNextTargetItem = (_index) => {
if(typeof _path[_index + 1] === 'undefined')
{
if(typeof last === 'number')
{
return [];
}
else if(_null)
{
return Object.create(null);
}
return {};
}
else if(typeof _path[_index + 1] === 'number')
{
return [];
}
else if(_null)
{
return Object.create(null);
}
return {};
};
for(var i = 0; i < _path.length; ++i)
{
if(Array.isArray(ctx) && typeof _path[i] === 'number')
{
if(_path[i] >= ctx.length)
{
ctx = ctx[_path[i]] = getNextTargetItem();
}
else
{
if(_path[i] < 0)
{
if(ctx.length === 0)
{
_path[i] = 0;
}
else
{
_path[i] = Math.getIndex(_path[i], ctx.length);
}
}
ctx = ctx[_path[i]];
}
}
else if(_path[i] in ctx)
{
ctx = ctx[_path[i]];
}
else
{
ctx = ctx[_path[i]] = getNextTargetItem();
}
}
//
if(Array.isArray(ctx) && typeof last === 'number')
{
if(last >= ctx.length)
{
result = ctx[last];
ctx[last] = _value;
}
else
{
if(last < 0)
{
if(ctx.length === 0)
{
last = 0;
}
else
{
last = Math.getIndex(last, ctx.length);
}
}
result = ctx[last];
ctx[last] = _value;
}
}
else
{
result = ctx[last];
ctx[last] = _value;
}
}
catch(_err)
{
if(DEFAULT_OBJECT_SET_BOOL)
{
return false;
}
return undefined;
}
if(DEFAULT_OBJECT_SET_BOOL)
{
return true;
}
return result;
}});
Reflect.defineProperty(Object, 'remove', { value: (_path, _context = global, _sep = DEFAULT_OBJECT_SEP) => {
if((_path = getPathArray(_path, _sep)) === null) return _context; var ctx = _context; var done; const last = _path.pop(); try
{
for(var i = 0; i < _path.length; ++i)
{
done = false;
if(Array.isArray(ctx))
{
if(ctx.length === 0)
{
return undefined;
}
else if(typeof _path[i] === 'number' && _path[i] < 0)
{
ctx = ctx[Math.getIndex(_path[i], ctx.length)];
done = true;
}
}
if(!done)
{
if(_path[i] in ctx)
{
ctx = ctx[_path[i]];
}
else
{
return undefined;
}
}
}
if(Array.isArray(ctx))
{
if(ctx.length === 0)
{
return undefined;
}
else if(typeof last === 'number')
{
if(last < 0)
{
const alternative = Math.getIndex(last, ctx.length);
return ctx.splice(alternative, 1)[0];
}
return ctx.splice(last, 1)[0];
}
}
if(last in ctx)
{
const result = ctx[last];
delete ctx[last];
return result;
}
}
catch(_err)
{
return undefined;
}
return undefined;
}});
//
export default { was: Reflect.was, is: Reflect.is, getPrototypesOf: Reflect.getPrototypesOf, isNull: Object.isNull,
has: Object.has, get: Object.get, set: Object.set, remove: Object.remove };