@ygracs/xobj-lib-js 0.2.0 → 0.2.2
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.
- package/CHANGELOG.md +56 -41
- package/LICENSE +9 -9
- package/README.md +18 -18
- package/doc/xObj.md +1191 -790
- package/index.js +18 -18
- package/lib/xObj-defs.js +166 -166
- package/lib/xObj-lib.js +1568 -1562
- package/package.json +40 -41
package/lib/xObj-lib.js
CHANGED
|
@@ -1,1562 +1,1568 @@
|
|
|
1
|
-
// [v0.2.
|
|
2
|
-
|
|
3
|
-
// === module init block ===
|
|
4
|
-
|
|
5
|
-
const {
|
|
6
|
-
valueToIndex, valueToIDString,
|
|
7
|
-
readAsString, readAsBoolEx, readAsNumberEx,
|
|
8
|
-
isNullOrUndef,
|
|
9
|
-
isInteger,
|
|
10
|
-
isArray, isObject, isPlainObject,
|
|
11
|
-
readAsListS,
|
|
12
|
-
} = require('@ygracs/bsfoc-lib-js');
|
|
13
|
-
|
|
14
|
-
// === module extra block (helper functions) ===
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* @typedef RVAL_reason
|
|
18
|
-
* @
|
|
19
|
-
* @property {string}
|
|
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
|
-
const
|
|
74
|
-
|
|
75
|
-
const
|
|
76
|
-
const
|
|
77
|
-
const
|
|
78
|
-
const
|
|
79
|
-
const
|
|
80
|
-
const
|
|
81
|
-
const
|
|
82
|
-
const
|
|
83
|
-
const
|
|
84
|
-
const
|
|
85
|
-
const
|
|
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
|
-
let
|
|
138
|
-
let
|
|
139
|
-
let
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
let
|
|
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
|
-
if (
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
isSucceed
|
|
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
|
-
if (typeof force !== 'boolean') force = false;
|
|
360
|
-
if (typeof ripOldies !== 'boolean') ripOldies = false;
|
|
361
|
-
if (typeof acceptIfList !== 'boolean') acceptIfList = false;
|
|
362
|
-
if (force && (ripOldies || !isObject(prop))) {
|
|
363
|
-
obj[key] = prop = item;
|
|
364
|
-
isSucceed = true;
|
|
365
|
-
} else {
|
|
366
|
-
isSucceed = isPlainObject(prop) || (isArray(prop) && acceptIfList);
|
|
367
|
-
};
|
|
368
|
-
};
|
|
369
|
-
return {
|
|
370
|
-
isSucceed,
|
|
371
|
-
item: isSucceed ? prop : null,
|
|
372
|
-
};
|
|
373
|
-
};
|
|
374
|
-
|
|
375
|
-
/**
|
|
376
|
-
* @function deleteXObjElement
|
|
377
|
-
* @param {object} obj
|
|
378
|
-
* @param {string} name
|
|
379
|
-
* @returns {boolean}
|
|
380
|
-
* @throws {TypeError} if first param is not an object
|
|
381
|
-
* @description Deletes an element addressed by its key from a given object.
|
|
382
|
-
*/
|
|
383
|
-
function deleteXObjElement(...args) {
|
|
384
|
-
let isSucceed = false;
|
|
385
|
-
try {
|
|
386
|
-
({ isSucceed } = deleteXObjElementEx(...args));
|
|
387
|
-
} catch (err) {
|
|
388
|
-
switch (err.code) {
|
|
389
|
-
case XOBJ_TE_NSTR_ECODE :
|
|
390
|
-
case XOBJ_TE_KNES_ECODE : {
|
|
391
|
-
break;
|
|
392
|
-
}
|
|
393
|
-
default: {
|
|
394
|
-
throw err;
|
|
395
|
-
}
|
|
396
|
-
};
|
|
397
|
-
};
|
|
398
|
-
return isSucceed;
|
|
399
|
-
};
|
|
400
|
-
|
|
401
|
-
/**
|
|
402
|
-
* @function deleteXObjElementEx
|
|
403
|
-
* @param {object} obj
|
|
404
|
-
* @param {string} name
|
|
405
|
-
* @returns {RVAL_emodif}
|
|
406
|
-
* @throws {TypeError} if first param is not an object
|
|
407
|
-
* @throws {TypeError} if second param is empty string or not a string at all
|
|
408
|
-
* @description Deletes an element addressed by its key from a given object.
|
|
409
|
-
*/
|
|
410
|
-
function deleteXObjElementEx(obj, name) {
|
|
411
|
-
if (!isPlainObject(obj)) {
|
|
412
|
-
const err = new TypeError(XOBJ_TE_NPOBJ_EMSG);
|
|
413
|
-
err.code = XOBJ_TE_NPOBJ_ECODE;
|
|
414
|
-
throw err;
|
|
415
|
-
};
|
|
416
|
-
let { isSucceed, value: key } = evalKeyName(name);
|
|
417
|
-
if (!isSucceed) {
|
|
418
|
-
const { code, msg } = key;
|
|
419
|
-
const err = new TypeError(msg);
|
|
420
|
-
err.code = code;
|
|
421
|
-
throw err;
|
|
422
|
-
};
|
|
423
|
-
let prop = obj[key];
|
|
424
|
-
isSucceed = false;
|
|
425
|
-
// // TODO: catch errors in strict mode
|
|
426
|
-
isSucceed = delete obj[key];
|
|
427
|
-
return {
|
|
428
|
-
isSucceed,
|
|
429
|
-
item: isSucceed && isObject(prop) ? prop : null,
|
|
430
|
-
};
|
|
431
|
-
};
|
|
432
|
-
|
|
433
|
-
/**
|
|
434
|
-
* @function renameXObjElement
|
|
435
|
-
* @param {object} obj
|
|
436
|
-
* @param {string} name -
|
|
437
|
-
* @param {string}
|
|
438
|
-
* @returns {boolean}
|
|
439
|
-
* @throws {TypeError} if first param is not an object
|
|
440
|
-
* @throws {TypeError} if second param is not a string
|
|
441
|
-
* @throws {TypeError} if third param is not a string
|
|
442
|
-
* @description Renames an element addressed by its key.
|
|
443
|
-
*/
|
|
444
|
-
function renameXObjElement(obj, name = '',
|
|
445
|
-
if (!isPlainObject(obj)) {
|
|
446
|
-
const err = new TypeError(XOBJ_TE_NPOBJ_EMSG);
|
|
447
|
-
err.code = XOBJ_TE_NPOBJ_ECODE;
|
|
448
|
-
throw err;
|
|
449
|
-
};
|
|
450
|
-
const opt = false;
|
|
451
|
-
let { isSucceed: isAcceptON, value: oname } = evalKeyName(name, opt);
|
|
452
|
-
if (!isAcceptON) {
|
|
453
|
-
const { code, msg } = oname;
|
|
454
|
-
const err = new TypeError(msg);
|
|
455
|
-
err.code = code;
|
|
456
|
-
throw err;
|
|
457
|
-
};
|
|
458
|
-
let { isSucceed: isAcceptNN, value: nname } = evalKeyName(
|
|
459
|
-
if (!isAcceptNN) {
|
|
460
|
-
const { code, msg } = nname;
|
|
461
|
-
const err = new TypeError(msg);
|
|
462
|
-
err.code = code;
|
|
463
|
-
throw err;
|
|
464
|
-
};
|
|
465
|
-
let isSucceed = false;
|
|
466
|
-
if (oname !== '' && oname in obj && nname !== '') {
|
|
467
|
-
isSucceed = true;
|
|
468
|
-
if (oname !== nname) {
|
|
469
|
-
const prop = obj[oname];
|
|
470
|
-
obj[
|
|
471
|
-
isSucceed = delete obj[oname];
|
|
472
|
-
if (!isSucceed) {
|
|
473
|
-
// undo change if failed delete old element
|
|
474
|
-
delete obj[nname];
|
|
475
|
-
};
|
|
476
|
-
};
|
|
477
|
-
};
|
|
478
|
-
return isSucceed;
|
|
479
|
-
};
|
|
480
|
-
|
|
481
|
-
/**
|
|
482
|
-
* @function checkXObjAttribute
|
|
483
|
-
* @param {object} obj
|
|
484
|
-
* @param {string} attr
|
|
485
|
-
* @param {string} [key]
|
|
486
|
-
* @returns {boolean}
|
|
487
|
-
* @throws {TypeError} if first param is not an object
|
|
488
|
-
* @description Checks
|
|
489
|
-
*/
|
|
490
|
-
function checkXObjAttribute(obj, attr = '', key) {
|
|
491
|
-
let _obj = null;
|
|
492
|
-
try {
|
|
493
|
-
_obj = getXObjAttributes(obj, key);
|
|
494
|
-
} catch (err) {
|
|
495
|
-
throw err;
|
|
496
|
-
};
|
|
497
|
-
if (_obj === null) return false;
|
|
498
|
-
let { isSucceed, value: name } = evalKeyName(attr, false);
|
|
499
|
-
if (isSucceed) {
|
|
500
|
-
if (name === '') return false;
|
|
501
|
-
} else {
|
|
502
|
-
const { code, msg } = name;
|
|
503
|
-
const err = new TypeError(msg);
|
|
504
|
-
err.code = code;
|
|
505
|
-
throw err;
|
|
506
|
-
};
|
|
507
|
-
return _obj[name] !== undefined;
|
|
508
|
-
};
|
|
509
|
-
|
|
510
|
-
/**
|
|
511
|
-
* @function deleteXObjAttribute
|
|
512
|
-
* @param {object} obj
|
|
513
|
-
* @param {string} attr
|
|
514
|
-
* @param {string} [key]
|
|
515
|
-
* @returns {boolean}
|
|
516
|
-
* @throws {TypeError} if first param is not an object
|
|
517
|
-
* @description Deletes an attribute addressed by a given name.
|
|
518
|
-
*/
|
|
519
|
-
function deleteXObjAttribute(obj, attr = '', key) {
|
|
520
|
-
let _obj = null;
|
|
521
|
-
try {
|
|
522
|
-
_obj = getXObjAttributes(obj, key);
|
|
523
|
-
} catch (err) {
|
|
524
|
-
throw err;
|
|
525
|
-
};
|
|
526
|
-
if (_obj === null) return false;
|
|
527
|
-
let { isSucceed, value: name } = evalKeyName(attr, false);
|
|
528
|
-
if (!isSucceed) {
|
|
529
|
-
const { code, msg } = name;
|
|
530
|
-
const err = new TypeError(msg);
|
|
531
|
-
err.code = code;
|
|
532
|
-
throw err;
|
|
533
|
-
};
|
|
534
|
-
isSucceed = false;
|
|
535
|
-
if (name !== '') {
|
|
536
|
-
// // TODO: catch errors in strict mode
|
|
537
|
-
isSucceed = delete _obj[name];
|
|
538
|
-
};
|
|
539
|
-
return isSucceed;
|
|
540
|
-
};
|
|
541
|
-
|
|
542
|
-
/**
|
|
543
|
-
* @
|
|
544
|
-
* @
|
|
545
|
-
* @param {
|
|
546
|
-
* @param {string}
|
|
547
|
-
* @param {string}
|
|
548
|
-
* @
|
|
549
|
-
* @
|
|
550
|
-
* @throws {TypeError} if
|
|
551
|
-
* @throws {TypeError} if
|
|
552
|
-
* @
|
|
553
|
-
* @description Renames an attribute addressed by a given name.
|
|
554
|
-
*/
|
|
555
|
-
function renameXObjAttribute(obj, attr = '',
|
|
556
|
-
if (!isPlainObject(obj)) {
|
|
557
|
-
const err = new TypeError(XOBJ_TE_NPOBJ_EMSG);
|
|
558
|
-
err.code = XOBJ_TE_NPOBJ_ECODE;
|
|
559
|
-
throw err;
|
|
560
|
-
};
|
|
561
|
-
const opt = false;
|
|
562
|
-
let { isSucceed, value: _key } = evalKeyName(key, opt);
|
|
563
|
-
if (!isSucceed) {
|
|
564
|
-
const { code, msg } = _key;
|
|
565
|
-
const err = new TypeError(msg);
|
|
566
|
-
err.code = code;
|
|
567
|
-
throw err;
|
|
568
|
-
};
|
|
569
|
-
isSucceed = false;
|
|
570
|
-
if (_key !== '') {
|
|
571
|
-
let _obj = obj[_key];
|
|
572
|
-
if (isPlainObject(_obj)) {
|
|
573
|
-
let { isSucceed: isAcceptON, value: oname } = evalKeyName(attr, opt);
|
|
574
|
-
if (!isAcceptON) {
|
|
575
|
-
const { code, msg } = oname;
|
|
576
|
-
const err = new TypeError(msg);
|
|
577
|
-
err.code = code;
|
|
578
|
-
throw err;
|
|
579
|
-
};
|
|
580
|
-
let { isSucceed: isAcceptNN, value: nname } = evalKeyName(
|
|
581
|
-
if (!isAcceptNN) {
|
|
582
|
-
const { code, msg } = nname;
|
|
583
|
-
const err = new TypeError(msg);
|
|
584
|
-
err.code = code;
|
|
585
|
-
throw err;
|
|
586
|
-
};
|
|
587
|
-
if (oname !== '' && oname in _obj && nname !== '') {
|
|
588
|
-
if (oname !== nname) {
|
|
589
|
-
_obj = Object.entries(_obj);
|
|
590
|
-
const index = _obj.findIndex((item) => item[0] === oname);
|
|
591
|
-
_obj[index][0] = nname;
|
|
592
|
-
obj[_key] = Object.fromEntries(_obj);
|
|
593
|
-
};
|
|
594
|
-
isSucceed = true;
|
|
595
|
-
};
|
|
596
|
-
};
|
|
597
|
-
};
|
|
598
|
-
return isSucceed;
|
|
599
|
-
};
|
|
600
|
-
|
|
601
|
-
/**
|
|
602
|
-
* @function readXObjParamRaw
|
|
603
|
-
* @param {object} obj
|
|
604
|
-
* @param {string} [key=XOBJ_DEF_PARAM_TNAME]
|
|
605
|
-
* @returns {any}
|
|
606
|
-
* @throws {TypeError} if first param is not an object
|
|
607
|
-
* @throws {TypeError} if third param is not a string
|
|
608
|
-
* @description Extracts a parameter 'AS IS' from a given object.
|
|
609
|
-
*/
|
|
610
|
-
function readXObjParamRaw(obj, key = XOBJ_DEF_PARAM_TNAME) {
|
|
611
|
-
if (!isPlainObject(obj)) {
|
|
612
|
-
const err = new TypeError(XOBJ_TE_NPOBJ_EMSG);
|
|
613
|
-
err.code = XOBJ_TE_NPOBJ_ECODE;
|
|
614
|
-
throw err;
|
|
615
|
-
};
|
|
616
|
-
let { isSucceed, value: _key } = evalKeyName(key, false);
|
|
617
|
-
if (!isSucceed) {
|
|
618
|
-
const { code, msg } = _key;
|
|
619
|
-
const err = new TypeError(msg);
|
|
620
|
-
err.code = code;
|
|
621
|
-
throw err;
|
|
622
|
-
};
|
|
623
|
-
return _key !== '' ? obj[_key] : undefined;
|
|
624
|
-
};
|
|
625
|
-
|
|
626
|
-
/**
|
|
627
|
-
* @function writeXObjParamRaw
|
|
628
|
-
* @param {object} obj
|
|
629
|
-
* @param {any} value
|
|
630
|
-
* @param {string} [key=XOBJ_DEF_PARAM_TNAME]
|
|
631
|
-
* @returns {boolean}
|
|
632
|
-
* @throws {TypeError} if first param is not an object
|
|
633
|
-
* @throws {TypeError} if third param is not a string
|
|
634
|
-
* @description Writes a parameter 'AS IS' into a given object.
|
|
635
|
-
*/
|
|
636
|
-
function writeXObjParamRaw(obj, value, key = XOBJ_DEF_PARAM_TNAME) {
|
|
637
|
-
if (!isPlainObject(obj)) {
|
|
638
|
-
const err = new TypeError(XOBJ_TE_NPOBJ_EMSG);
|
|
639
|
-
err.code = XOBJ_TE_NPOBJ_ECODE;
|
|
640
|
-
throw err;
|
|
641
|
-
};
|
|
642
|
-
let { isSucceed, value: _key } = evalKeyName(key, false);
|
|
643
|
-
if (!isSucceed) {
|
|
644
|
-
const { code, msg } = _key;
|
|
645
|
-
const err = new TypeError(msg);
|
|
646
|
-
err.code = code;
|
|
647
|
-
throw err;
|
|
648
|
-
};
|
|
649
|
-
if (_key === '' || value === undefined) {
|
|
650
|
-
isSucceed = false;
|
|
651
|
-
} else {
|
|
652
|
-
obj[_key] = value;
|
|
653
|
-
};
|
|
654
|
-
return isSucceed;
|
|
655
|
-
};
|
|
656
|
-
|
|
657
|
-
/**
|
|
658
|
-
* @function readXObjAttrRaw
|
|
659
|
-
* @param {object} obj
|
|
660
|
-
* @param {string} attr
|
|
661
|
-
* @param {string} [key]
|
|
662
|
-
* @returns {any}
|
|
663
|
-
* @throws {TypeError} if first param is not an object
|
|
664
|
-
* @throws {TypeError} if third param is not a string
|
|
665
|
-
* @description Extracts an attribute 'AS IS' from a given object.
|
|
666
|
-
*/
|
|
667
|
-
function readXObjAttrRaw(obj, attr = '', key) {
|
|
668
|
-
let _obj = null;
|
|
669
|
-
try {
|
|
670
|
-
_obj = getXObjAttributes(obj, key);
|
|
671
|
-
} catch (err) {
|
|
672
|
-
throw err;
|
|
673
|
-
};
|
|
674
|
-
if (_obj !== null) {
|
|
675
|
-
let { isSucceed, value: name } = evalKeyName(attr, false);
|
|
676
|
-
if (!isSucceed) {
|
|
677
|
-
const { code, msg } = name;
|
|
678
|
-
const err = new TypeError(msg);
|
|
679
|
-
err.code = code;
|
|
680
|
-
throw err;
|
|
681
|
-
};
|
|
682
|
-
if (name !== '') return _obj[name];
|
|
683
|
-
};
|
|
684
|
-
};
|
|
685
|
-
|
|
686
|
-
/**
|
|
687
|
-
* @function writeXObjAttrRaw
|
|
688
|
-
* @param {object} obj
|
|
689
|
-
* @param {string} attr
|
|
690
|
-
* @param {any} value
|
|
691
|
-
* @param {string} [key]
|
|
692
|
-
* @returns {boolean}
|
|
693
|
-
* @throws {TypeError} if first param is not an object
|
|
694
|
-
* @throws {TypeError} if third param is not a string
|
|
695
|
-
* @description Writes a parameter into a given object.
|
|
696
|
-
*/
|
|
697
|
-
function writeXObjAttrRaw(obj, attr = '', value, key = XOBJ_DEF_ATTR_TNAME) {
|
|
698
|
-
let { isSucceed, value: name } = evalKeyName(attr, false);
|
|
699
|
-
if (!isSucceed) {
|
|
700
|
-
const { code, msg } = name;
|
|
701
|
-
const err = new TypeError(msg);
|
|
702
|
-
err.code = code;
|
|
703
|
-
throw err;
|
|
704
|
-
};
|
|
705
|
-
isSucceed = false;
|
|
706
|
-
if (name !== '' && value !== undefined) {
|
|
707
|
-
let _obj = null;
|
|
708
|
-
try {
|
|
709
|
-
const opt = { force: true, acceptIfList: true };
|
|
710
|
-
_obj = insertXObjElement(obj, key, opt);
|
|
711
|
-
} catch (err) {
|
|
712
|
-
throw err;
|
|
713
|
-
};
|
|
714
|
-
if (isArray(_obj)) {
|
|
715
|
-
// force a replacement of the old element if it's array
|
|
716
|
-
try {
|
|
717
|
-
const opt = { force: true, ripOldies: true };
|
|
718
|
-
_obj = insertXObjElement(obj, key, opt);
|
|
719
|
-
} catch (err) {
|
|
720
|
-
throw err;
|
|
721
|
-
};
|
|
722
|
-
};
|
|
723
|
-
if (_obj !== null) {
|
|
724
|
-
_obj[name] = value;
|
|
725
|
-
isSucceed = true;
|
|
726
|
-
};
|
|
727
|
-
};
|
|
728
|
-
return isSucceed;
|
|
729
|
-
};
|
|
730
|
-
|
|
731
|
-
/**
|
|
732
|
-
* @function readXObjParam
|
|
733
|
-
* @param {object} obj
|
|
734
|
-
* @param {string} [key]
|
|
735
|
-
* @returns {string}
|
|
736
|
-
* @throws {TypeError} if first param is not an object
|
|
737
|
-
* @description Extracts a parameter from a given object
|
|
738
|
-
* and returns it as string.
|
|
739
|
-
*/
|
|
740
|
-
function readXObjParam(obj, key) {
|
|
741
|
-
const opt = {
|
|
742
|
-
useTrim: false,
|
|
743
|
-
numberToString: true,
|
|
744
|
-
boolToString: true,
|
|
745
|
-
defValue: '',
|
|
746
|
-
};
|
|
747
|
-
let result = undefined;
|
|
748
|
-
try {
|
|
749
|
-
result = readXObjParamEx(obj, opt, key);
|
|
750
|
-
} catch (err) {
|
|
751
|
-
throw err;
|
|
752
|
-
};
|
|
753
|
-
return result;
|
|
754
|
-
};
|
|
755
|
-
|
|
756
|
-
/**
|
|
757
|
-
* @function readXObjParamAsBool
|
|
758
|
-
* @param {object} obj
|
|
759
|
-
* @param {boolean} [defValue]
|
|
760
|
-
* @param {string} [key]
|
|
761
|
-
* @returns {boolean}
|
|
762
|
-
* @throws {TypeError} if first param is not an object
|
|
763
|
-
* @description Extracts a parameter from a given object
|
|
764
|
-
* and returns it as boolean.
|
|
765
|
-
*/
|
|
766
|
-
function readXObjParamAsBool(obj, defValue, key) {
|
|
767
|
-
let result = undefined;
|
|
768
|
-
try {
|
|
769
|
-
result = readXObjParamRaw(obj, key);
|
|
770
|
-
} catch (err) {
|
|
771
|
-
switch (err.code) {
|
|
772
|
-
case XOBJ_TE_NSTR_ECODE : {
|
|
773
|
-
break;
|
|
774
|
-
}
|
|
775
|
-
default: {
|
|
776
|
-
throw err;
|
|
777
|
-
}
|
|
778
|
-
};
|
|
779
|
-
};
|
|
780
|
-
return readAsBoolEx(result, defValue);
|
|
781
|
-
};
|
|
782
|
-
|
|
783
|
-
/**
|
|
784
|
-
* @function readXObjParamAsNum
|
|
785
|
-
* @param {object} obj
|
|
786
|
-
* @param {number} [defValue]
|
|
787
|
-
* @param {string} [key]
|
|
788
|
-
* @returns {number}
|
|
789
|
-
* @throws {TypeError} if first param is not an object
|
|
790
|
-
* @description Extracts a parameter from a given object
|
|
791
|
-
* and returns it as number.
|
|
792
|
-
*/
|
|
793
|
-
function readXObjParamAsNum(obj, defValue, key) {
|
|
794
|
-
let result = undefined;
|
|
795
|
-
try {
|
|
796
|
-
result = readXObjParamRaw(obj, key);
|
|
797
|
-
} catch (err) {
|
|
798
|
-
switch (err.code) {
|
|
799
|
-
case XOBJ_TE_NSTR_ECODE : {
|
|
800
|
-
break;
|
|
801
|
-
}
|
|
802
|
-
default: {
|
|
803
|
-
throw err;
|
|
804
|
-
}
|
|
805
|
-
};
|
|
806
|
-
};
|
|
807
|
-
return readAsNumberEx(result, defValue);
|
|
808
|
-
};
|
|
809
|
-
|
|
810
|
-
/**
|
|
811
|
-
* @function readXObjParamEx
|
|
812
|
-
* @param {object} obj
|
|
813
|
-
* @param {
|
|
814
|
-
* @param {string} [key]
|
|
815
|
-
* @returns {string}
|
|
816
|
-
* @throws {TypeError} if first param is not an object
|
|
817
|
-
* @description Extracts a parameter from a given object
|
|
818
|
-
* and returns it as string.
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
* @
|
|
849
|
-
* @param {
|
|
850
|
-
* @
|
|
851
|
-
* @
|
|
852
|
-
* @
|
|
853
|
-
*
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
* @
|
|
875
|
-
* @param {
|
|
876
|
-
* @param {
|
|
877
|
-
* @
|
|
878
|
-
* @
|
|
879
|
-
* @
|
|
880
|
-
*
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
* @
|
|
901
|
-
* @param {
|
|
902
|
-
* @param {
|
|
903
|
-
* @param {
|
|
904
|
-
* @
|
|
905
|
-
* @
|
|
906
|
-
* @
|
|
907
|
-
*
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
* @
|
|
932
|
-
* @param {
|
|
933
|
-
* @param {
|
|
934
|
-
* @param {
|
|
935
|
-
* @
|
|
936
|
-
* @
|
|
937
|
-
* @
|
|
938
|
-
*
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
* @
|
|
963
|
-
* @param {
|
|
964
|
-
* @param {
|
|
965
|
-
* @
|
|
966
|
-
* @
|
|
967
|
-
* @
|
|
968
|
-
*
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
* @
|
|
993
|
-
* @param {
|
|
994
|
-
* @param {any}
|
|
995
|
-
* @param {
|
|
996
|
-
* @
|
|
997
|
-
* @
|
|
998
|
-
* @
|
|
999
|
-
*
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
};
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
* @
|
|
1039
|
-
* @param {
|
|
1040
|
-
* @
|
|
1041
|
-
* @
|
|
1042
|
-
* @
|
|
1043
|
-
*
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
}
|
|
1058
|
-
|
|
1059
|
-
};
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
* @
|
|
1065
|
-
* @param {
|
|
1066
|
-
* @param {string}
|
|
1067
|
-
* @
|
|
1068
|
-
* @
|
|
1069
|
-
* @
|
|
1070
|
-
*
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
};
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
* @
|
|
1093
|
-
* @param {
|
|
1094
|
-
* @param {string}
|
|
1095
|
-
* @
|
|
1096
|
-
* @
|
|
1097
|
-
* @
|
|
1098
|
-
*
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
};
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
* @
|
|
1121
|
-
* @param {
|
|
1122
|
-
* @param {string}
|
|
1123
|
-
* @
|
|
1124
|
-
* @
|
|
1125
|
-
* @
|
|
1126
|
-
*
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
}
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
};
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
* @
|
|
1159
|
-
* @
|
|
1160
|
-
* @
|
|
1161
|
-
* @
|
|
1162
|
-
*
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
}
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
};
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
* @
|
|
1186
|
-
* @param {
|
|
1187
|
-
* @
|
|
1188
|
-
* @
|
|
1189
|
-
* @
|
|
1190
|
-
*
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
* @
|
|
1213
|
-
* @param {
|
|
1214
|
-
* @param {string}
|
|
1215
|
-
* @
|
|
1216
|
-
* @
|
|
1217
|
-
* @
|
|
1218
|
-
*
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
}
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
};
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
* @
|
|
1245
|
-
* @param {
|
|
1246
|
-
* @param {string}
|
|
1247
|
-
* @
|
|
1248
|
-
* @
|
|
1249
|
-
* @
|
|
1250
|
-
*
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
}
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
};
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
* @
|
|
1277
|
-
* @param {
|
|
1278
|
-
* @
|
|
1279
|
-
* @
|
|
1280
|
-
* @
|
|
1281
|
-
*
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
}
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
};
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
* @
|
|
1308
|
-
* @param {
|
|
1309
|
-
* @param {string}
|
|
1310
|
-
* @
|
|
1311
|
-
* @
|
|
1312
|
-
* @
|
|
1313
|
-
*
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
}
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
};
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
* @
|
|
1355
|
-
* @
|
|
1356
|
-
* @
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
* @
|
|
1389
|
-
* @
|
|
1390
|
-
* @
|
|
1391
|
-
* @
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
}
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
};
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
* @
|
|
1416
|
-
* @
|
|
1417
|
-
* @
|
|
1418
|
-
* @
|
|
1419
|
-
* @
|
|
1420
|
-
* @
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
let
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
)
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
}
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
};
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
* @
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
module
|
|
1508
|
-
|
|
1509
|
-
module.exports.
|
|
1510
|
-
module.exports.
|
|
1511
|
-
|
|
1512
|
-
module.exports.
|
|
1513
|
-
module.exports.
|
|
1514
|
-
module.exports.
|
|
1515
|
-
module.exports.
|
|
1516
|
-
module.exports.
|
|
1517
|
-
module.exports.
|
|
1518
|
-
|
|
1519
|
-
module.exports.
|
|
1520
|
-
module.exports.
|
|
1521
|
-
module.exports.
|
|
1522
|
-
module.exports.
|
|
1523
|
-
module.exports.
|
|
1524
|
-
|
|
1525
|
-
module.exports.
|
|
1526
|
-
module.exports.
|
|
1527
|
-
module.exports.
|
|
1528
|
-
module.exports.
|
|
1529
|
-
module.exports.
|
|
1530
|
-
module.exports.
|
|
1531
|
-
|
|
1532
|
-
module.exports.
|
|
1533
|
-
module.exports.
|
|
1534
|
-
module.exports.
|
|
1535
|
-
module.exports.
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
module.exports.
|
|
1539
|
-
module.exports.
|
|
1540
|
-
module.exports.
|
|
1541
|
-
module.exports.
|
|
1542
|
-
|
|
1543
|
-
module.exports.
|
|
1544
|
-
|
|
1545
|
-
module.exports.
|
|
1546
|
-
module.exports.
|
|
1547
|
-
module.exports.
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
module.exports.
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1
|
+
// [v0.2.101-20250401]
|
|
2
|
+
|
|
3
|
+
// === module init block ===
|
|
4
|
+
|
|
5
|
+
const {
|
|
6
|
+
valueToIndex, valueToIDString,
|
|
7
|
+
readAsString, readAsBoolEx, readAsNumberEx,
|
|
8
|
+
isNullOrUndef,
|
|
9
|
+
isInteger,
|
|
10
|
+
isArray, isObject, isPlainObject,
|
|
11
|
+
readAsListS,
|
|
12
|
+
} = require('@ygracs/bsfoc-lib-js');
|
|
13
|
+
|
|
14
|
+
// === module extra block (helper functions) ===
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @typedef {Object} RVAL_reason
|
|
18
|
+
* @property {string} code - message ID
|
|
19
|
+
* @property {string} msg - message text
|
|
20
|
+
* @description A result of a value check ops.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @typedef {Object} VCOR_evalkname
|
|
25
|
+
* @property {boolean} isSucceed - ops flag
|
|
26
|
+
* @property {(string|RVAL_reason)} value - result value or reson if failed
|
|
27
|
+
* @description A result of a value check ops.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @function evalKeyName
|
|
32
|
+
* @param {any} value - key to validate
|
|
33
|
+
* @param {boolean} [opt=true]
|
|
34
|
+
* @returns {VCOR_evalkname}
|
|
35
|
+
* @inner
|
|
36
|
+
* @description Tries to convert a value into an ID.
|
|
37
|
+
*/
|
|
38
|
+
function evalKeyName(value, opt = true) {
|
|
39
|
+
if (typeof value !== 'string') {
|
|
40
|
+
return {
|
|
41
|
+
isSucceed: false,
|
|
42
|
+
value: {
|
|
43
|
+
code: XOBJ_TE_NSTR_ECODE,
|
|
44
|
+
msg: XOBJ_TE_NSTR_EMSG,
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
const key = value.trim();
|
|
49
|
+
if (opt && key === '') {
|
|
50
|
+
return {
|
|
51
|
+
isSucceed: false,
|
|
52
|
+
value: {
|
|
53
|
+
code: XOBJ_TE_KNES_ECODE,
|
|
54
|
+
msg: XOBJ_TE_KNES_EMSG,
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
return {
|
|
59
|
+
isSucceed: true,
|
|
60
|
+
value: key,
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
// === module main block ===
|
|
65
|
+
|
|
66
|
+
/***
|
|
67
|
+
* (* constant definitions *)
|
|
68
|
+
*/
|
|
69
|
+
|
|
70
|
+
const XOBJ_DEF_PARAM_TNAME = '__text';
|
|
71
|
+
const XOBJ_DEF_ATTR_TNAME = '__attr';
|
|
72
|
+
|
|
73
|
+
const XOBJ_TE_INVARG_EMSG = 'invalid argument';
|
|
74
|
+
const XOBJ_TE_NOBJ_EMSG = `${XOBJ_TE_INVARG_EMSG} (an object expected)`;
|
|
75
|
+
const XOBJ_TE_NOBJ_ECODE = 'ERR_XOBJ_NOBJ';
|
|
76
|
+
const XOBJ_TE_NARR_EMSG = `${XOBJ_TE_INVARG_EMSG} (an array expected)`;
|
|
77
|
+
const XOBJ_TE_NARR_ECODE = 'ERR_XOBJ_NARR';
|
|
78
|
+
const XOBJ_TE_NSTR_EMSG = `${XOBJ_TE_INVARG_EMSG} (a string expected)`;
|
|
79
|
+
const XOBJ_TE_NSTR_ECODE = 'ERR_XOBJ_NSTR';
|
|
80
|
+
const XOBJ_TE_NPOBJ_EMSG = `${XOBJ_TE_INVARG_EMSG} (a plain object expected)`;
|
|
81
|
+
const XOBJ_TE_NPOBJ_ECODE = 'ERR_XOBJ_NPOBJ';
|
|
82
|
+
const XOBJ_TE_ANES_EMSG = '<attr_name> must be a non-empty string';
|
|
83
|
+
const XOBJ_TE_ANES_ECODE = 'ERR_XOBJ_INVARG_ATTR';
|
|
84
|
+
const XOBJ_TE_KNES_EMSG = '<key_name> must be a non-empty string';
|
|
85
|
+
const XOBJ_TE_KNES_ECODE = 'ERR_XOBJ_INVARG_KEY';
|
|
86
|
+
|
|
87
|
+
/***
|
|
88
|
+
* (* function definitions *)
|
|
89
|
+
*/
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* @function evalXObjEName
|
|
93
|
+
* @param {any} value - some value to evaluate
|
|
94
|
+
* @returns {(null|number|string)}
|
|
95
|
+
* @description Tries to convert a value into an ID.
|
|
96
|
+
*/
|
|
97
|
+
function evalXObjEName(value) {
|
|
98
|
+
//return valueToIDString(value); // // TODO: [?]
|
|
99
|
+
let name = value;
|
|
100
|
+
switch (typeof name) {
|
|
101
|
+
case 'number' : {
|
|
102
|
+
if (valueToIndex(name) === -1) name = null;
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
case 'string' : {
|
|
106
|
+
name = name.trim();
|
|
107
|
+
if (name !== '') {
|
|
108
|
+
let value = Number(name);
|
|
109
|
+
if (!Number.isNaN(value)) {
|
|
110
|
+
name = (
|
|
111
|
+
value < 0 || !isInteger(value)
|
|
112
|
+
? null
|
|
113
|
+
: value
|
|
114
|
+
);
|
|
115
|
+
};
|
|
116
|
+
} else {
|
|
117
|
+
name = null;
|
|
118
|
+
};
|
|
119
|
+
break;
|
|
120
|
+
}
|
|
121
|
+
default: {
|
|
122
|
+
name = null;
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
return name;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* @function genXObjENameDescr
|
|
130
|
+
* @param {any} value
|
|
131
|
+
* @returns {object}
|
|
132
|
+
* @description Tries to convert a value into an element name description.
|
|
133
|
+
*/
|
|
134
|
+
function genXObjENameDescr(value) {
|
|
135
|
+
let result = null;
|
|
136
|
+
let name = null;
|
|
137
|
+
let conditions = null;
|
|
138
|
+
let isERR = false;
|
|
139
|
+
let tail = null;
|
|
140
|
+
if (typeof value === 'string') {
|
|
141
|
+
const re = /^\s*([^\[\]\s]+)?(\s*)(\[.*])?\s*$/;
|
|
142
|
+
tail = value.match(re);
|
|
143
|
+
//console.log('CHECK: '+JSON.stringify(tail, null, 2));
|
|
144
|
+
if (tail) {
|
|
145
|
+
if (tail[1]) {
|
|
146
|
+
name = tail[1];
|
|
147
|
+
if (name) {
|
|
148
|
+
if (tail[2] === '') {
|
|
149
|
+
if (tail[3]) {
|
|
150
|
+
const re = /\[(@{0,1})(?:([^\[=]*?)(?:(?=[=])(=)((?![\"\'])[^\]]*|(?=([\"\']))\5(.*)\5))?)](\s*.*)/;
|
|
151
|
+
tail = tail[3].match(re);
|
|
152
|
+
//console.log('CHECK: '+JSON.stringify(tail, null, 2));
|
|
153
|
+
if (tail) {
|
|
154
|
+
let name = evalXObjEName(tail[2]);
|
|
155
|
+
let type = tail[1] === '@' ? 'attribute' : 'child';
|
|
156
|
+
let value = undefined;
|
|
157
|
+
if (tail[3] === '=') value = tail[5] ? tail[6] : tail[4];
|
|
158
|
+
if (tail[7]) isERR = true;
|
|
159
|
+
conditions = {
|
|
160
|
+
name,
|
|
161
|
+
type,
|
|
162
|
+
value,
|
|
163
|
+
};
|
|
164
|
+
};
|
|
165
|
+
};
|
|
166
|
+
} else if (tail[3]) {
|
|
167
|
+
name = null;
|
|
168
|
+
};
|
|
169
|
+
};
|
|
170
|
+
};
|
|
171
|
+
};
|
|
172
|
+
} else {
|
|
173
|
+
name = value;
|
|
174
|
+
};
|
|
175
|
+
name = evalXObjEName(name);
|
|
176
|
+
if (name === null) isERR = true;
|
|
177
|
+
result = {
|
|
178
|
+
name,
|
|
179
|
+
conditions,
|
|
180
|
+
isERR,
|
|
181
|
+
};
|
|
182
|
+
return result;
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* @function getXObjElement
|
|
187
|
+
* @param {object} obj - some object
|
|
188
|
+
* @param {string} name - some child element
|
|
189
|
+
* @returns {?any}
|
|
190
|
+
* @throws {TypeError} if first param is not an object
|
|
191
|
+
* @throws {TypeError} if second param is empty string or not a string at all
|
|
192
|
+
* @description Extracts an element from a given object by its key.
|
|
193
|
+
*/
|
|
194
|
+
function getXObjElement(obj, name) {
|
|
195
|
+
if (!isPlainObject(obj)) {
|
|
196
|
+
const err = new TypeError(XOBJ_TE_NPOBJ_EMSG);
|
|
197
|
+
err.code = XOBJ_TE_NPOBJ_ECODE;
|
|
198
|
+
throw err;
|
|
199
|
+
};
|
|
200
|
+
let { isSucceed, value: key } = evalKeyName(name);
|
|
201
|
+
if (!isSucceed) {
|
|
202
|
+
const { code, msg } = key;
|
|
203
|
+
const err = new TypeError(msg);
|
|
204
|
+
err.code = code;
|
|
205
|
+
throw err;
|
|
206
|
+
};
|
|
207
|
+
// TODO: [?] check type of obj[key_name]
|
|
208
|
+
return obj[key] !== undefined ? obj[key] : null;
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* @function getXObjAttributes
|
|
213
|
+
* @param {object} obj - some object
|
|
214
|
+
* @param {string} [key=XOBJ_DEF_ATTR_TNAME] - some key
|
|
215
|
+
* @returns {?object}
|
|
216
|
+
* @throws {TypeError} if first param is not an object
|
|
217
|
+
* @description Extracts an attributes from a given object by its key.
|
|
218
|
+
*/
|
|
219
|
+
function getXObjAttributes(obj, key = XOBJ_DEF_ATTR_TNAME) {
|
|
220
|
+
let result = null;
|
|
221
|
+
try {
|
|
222
|
+
result = getXObjElement(obj, key);
|
|
223
|
+
} catch (err) {
|
|
224
|
+
switch (err.code) {
|
|
225
|
+
case XOBJ_TE_NSTR_ECODE :
|
|
226
|
+
case XOBJ_TE_KNES_ECODE : {
|
|
227
|
+
break;
|
|
228
|
+
}
|
|
229
|
+
default: {
|
|
230
|
+
throw err;
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
};
|
|
234
|
+
return isPlainObject(result) ? result : null;
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* @typedef {Object} RVAL_emodif
|
|
239
|
+
* @property {boolean} isSucceed - flag that indicates whether an ops is succeed or not
|
|
240
|
+
* @property {?(object|object[])} item - some element as a result
|
|
241
|
+
* @description A result of an xObj modification ops
|
|
242
|
+
*/
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* @function addXObjElement
|
|
246
|
+
* @param {object} obj - some object
|
|
247
|
+
* @param {string} name - some child element
|
|
248
|
+
* @returns {RVAL_emodif}
|
|
249
|
+
* @throws {TypeError} if first param is not an object
|
|
250
|
+
* @throws {TypeError} if second param is empty string or not a string at all
|
|
251
|
+
* @description Adds an element addressed by its key to a given object.
|
|
252
|
+
*/
|
|
253
|
+
function addXObjElement(obj, name) {
|
|
254
|
+
if (!isPlainObject(obj)) {
|
|
255
|
+
const err = new TypeError(XOBJ_TE_NPOBJ_EMSG);
|
|
256
|
+
err.code = XOBJ_TE_NPOBJ_ECODE;
|
|
257
|
+
throw err;
|
|
258
|
+
};
|
|
259
|
+
let { isSucceed, value: key } = evalKeyName(name);
|
|
260
|
+
if (!isSucceed) {
|
|
261
|
+
const { code, msg } = key;
|
|
262
|
+
const err = new TypeError(msg);
|
|
263
|
+
err.code = code;
|
|
264
|
+
throw err;
|
|
265
|
+
};
|
|
266
|
+
const item = {};
|
|
267
|
+
let prop = obj[key];
|
|
268
|
+
isSucceed = false;
|
|
269
|
+
// // TODO: [?] consider whether or not do ops if new_key exists
|
|
270
|
+
if (isNullOrUndef(prop)) {
|
|
271
|
+
obj[key] = prop = item;
|
|
272
|
+
isSucceed = true;
|
|
273
|
+
} else if (isObject(prop)) {
|
|
274
|
+
if (isArray(prop)) {
|
|
275
|
+
prop.push(item);
|
|
276
|
+
} else {
|
|
277
|
+
obj[key] = [ prop, item ];
|
|
278
|
+
};
|
|
279
|
+
prop = item;
|
|
280
|
+
isSucceed = true;
|
|
281
|
+
};
|
|
282
|
+
return {
|
|
283
|
+
isSucceed,
|
|
284
|
+
item: isSucceed ? prop : null,
|
|
285
|
+
};
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* @typedef {Object} OPT_inselops_L
|
|
290
|
+
* @property {boolean} [force=false]
|
|
291
|
+
* @property {boolean} [ripOldies=false]
|
|
292
|
+
* @property {boolean} [acceptIfList=false]
|
|
293
|
+
*/
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* @function insertXObjElement
|
|
297
|
+
* @param {object} obj - some object
|
|
298
|
+
* @param {string} name - some child element
|
|
299
|
+
* @param {OPT_inselops_L} [opt] - options
|
|
300
|
+
* @returns {?object}
|
|
301
|
+
* @throws {TypeError} if first param is not an object
|
|
302
|
+
* @description Inserts an element addressed by its key into a given object.
|
|
303
|
+
*/
|
|
304
|
+
function insertXObjElement(...args) {
|
|
305
|
+
let item = null;
|
|
306
|
+
try {
|
|
307
|
+
({ item } = insertXObjElementEx(...args));
|
|
308
|
+
} catch (err) {
|
|
309
|
+
switch (err.code) {
|
|
310
|
+
case XOBJ_TE_NSTR_ECODE :
|
|
311
|
+
case XOBJ_TE_KNES_ECODE : {
|
|
312
|
+
break;
|
|
313
|
+
}
|
|
314
|
+
default: {
|
|
315
|
+
throw err;
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
};
|
|
319
|
+
return item;
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* @since 0.2.0
|
|
324
|
+
* @function insertXObjElementEx
|
|
325
|
+
* @param {object} obj - some object
|
|
326
|
+
* @param {string} name - some child element
|
|
327
|
+
* @param {OPT_inselops_L} [opt] - options
|
|
328
|
+
* @returns {RVAL_emodif}
|
|
329
|
+
* @throws {TypeError} if first param is not an object
|
|
330
|
+
* @throws {TypeError} if second param is empty string or not a string at all
|
|
331
|
+
* @description Inserts an element addressed by its key into a given object.
|
|
332
|
+
*/
|
|
333
|
+
function insertXObjElementEx(obj, name, opt) {
|
|
334
|
+
if (!isPlainObject(obj)) {
|
|
335
|
+
const err = new TypeError(XOBJ_TE_NPOBJ_EMSG);
|
|
336
|
+
err.code = XOBJ_TE_NPOBJ_ECODE;
|
|
337
|
+
throw err;
|
|
338
|
+
};
|
|
339
|
+
let { isSucceed, value: key } = evalKeyName(name);
|
|
340
|
+
if (!isSucceed) {
|
|
341
|
+
const { code, msg } = key;
|
|
342
|
+
const err = new TypeError(msg);
|
|
343
|
+
err.code = code;
|
|
344
|
+
throw err;
|
|
345
|
+
};
|
|
346
|
+
const item = {};
|
|
347
|
+
let prop = obj[key];
|
|
348
|
+
isSucceed = false;
|
|
349
|
+
if (isNullOrUndef(prop)) {
|
|
350
|
+
obj[key] = prop = item;
|
|
351
|
+
isSucceed = true;
|
|
352
|
+
} else {
|
|
353
|
+
/** @type {OPT_inselops_L} */
|
|
354
|
+
let {
|
|
355
|
+
force,
|
|
356
|
+
ripOldies,
|
|
357
|
+
acceptIfList,
|
|
358
|
+
} = isPlainObject(opt) ? opt : {};
|
|
359
|
+
if (typeof force !== 'boolean') force = false;
|
|
360
|
+
if (typeof ripOldies !== 'boolean') ripOldies = false;
|
|
361
|
+
if (typeof acceptIfList !== 'boolean') acceptIfList = false;
|
|
362
|
+
if (force && (ripOldies || !isObject(prop))) {
|
|
363
|
+
obj[key] = prop = item;
|
|
364
|
+
isSucceed = true;
|
|
365
|
+
} else {
|
|
366
|
+
isSucceed = isPlainObject(prop) || (isArray(prop) && acceptIfList);
|
|
367
|
+
};
|
|
368
|
+
};
|
|
369
|
+
return {
|
|
370
|
+
isSucceed,
|
|
371
|
+
item: isSucceed ? prop : null,
|
|
372
|
+
};
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* @function deleteXObjElement
|
|
377
|
+
* @param {object} obj - some object
|
|
378
|
+
* @param {string} name - some child element
|
|
379
|
+
* @returns {boolean}
|
|
380
|
+
* @throws {TypeError} if first param is not an object
|
|
381
|
+
* @description Deletes an element addressed by its key from a given object.
|
|
382
|
+
*/
|
|
383
|
+
function deleteXObjElement(...args) {
|
|
384
|
+
let isSucceed = false;
|
|
385
|
+
try {
|
|
386
|
+
({ isSucceed } = deleteXObjElementEx(...args));
|
|
387
|
+
} catch (err) {
|
|
388
|
+
switch (err.code) {
|
|
389
|
+
case XOBJ_TE_NSTR_ECODE :
|
|
390
|
+
case XOBJ_TE_KNES_ECODE : {
|
|
391
|
+
break;
|
|
392
|
+
}
|
|
393
|
+
default: {
|
|
394
|
+
throw err;
|
|
395
|
+
}
|
|
396
|
+
};
|
|
397
|
+
};
|
|
398
|
+
return isSucceed;
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* @function deleteXObjElementEx
|
|
403
|
+
* @param {object} obj - some object
|
|
404
|
+
* @param {string} name - some child element
|
|
405
|
+
* @returns {RVAL_emodif}
|
|
406
|
+
* @throws {TypeError} if first param is not an object
|
|
407
|
+
* @throws {TypeError} if second param is empty string or not a string at all
|
|
408
|
+
* @description Deletes an element addressed by its key from a given object.
|
|
409
|
+
*/
|
|
410
|
+
function deleteXObjElementEx(obj, name) {
|
|
411
|
+
if (!isPlainObject(obj)) {
|
|
412
|
+
const err = new TypeError(XOBJ_TE_NPOBJ_EMSG);
|
|
413
|
+
err.code = XOBJ_TE_NPOBJ_ECODE;
|
|
414
|
+
throw err;
|
|
415
|
+
};
|
|
416
|
+
let { isSucceed, value: key } = evalKeyName(name);
|
|
417
|
+
if (!isSucceed) {
|
|
418
|
+
const { code, msg } = key;
|
|
419
|
+
const err = new TypeError(msg);
|
|
420
|
+
err.code = code;
|
|
421
|
+
throw err;
|
|
422
|
+
};
|
|
423
|
+
let prop = obj[key];
|
|
424
|
+
isSucceed = false;
|
|
425
|
+
// // TODO: catch errors in strict mode
|
|
426
|
+
isSucceed = delete obj[key];
|
|
427
|
+
return {
|
|
428
|
+
isSucceed,
|
|
429
|
+
item: isSucceed && isObject(prop) ? prop : null,
|
|
430
|
+
};
|
|
431
|
+
};
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* @function renameXObjElement
|
|
435
|
+
* @param {object} obj - some object
|
|
436
|
+
* @param {string} name - some child element
|
|
437
|
+
* @param {string} value - new element ID
|
|
438
|
+
* @returns {boolean}
|
|
439
|
+
* @throws {TypeError} if first param is not an object
|
|
440
|
+
* @throws {TypeError} if second param is not a string
|
|
441
|
+
* @throws {TypeError} if third param is not a string
|
|
442
|
+
* @description Renames an element addressed by its key.
|
|
443
|
+
*/
|
|
444
|
+
function renameXObjElement(obj, name = '', value = '') {
|
|
445
|
+
if (!isPlainObject(obj)) {
|
|
446
|
+
const err = new TypeError(XOBJ_TE_NPOBJ_EMSG);
|
|
447
|
+
err.code = XOBJ_TE_NPOBJ_ECODE;
|
|
448
|
+
throw err;
|
|
449
|
+
};
|
|
450
|
+
const opt = false;
|
|
451
|
+
let { isSucceed: isAcceptON, value: oname } = evalKeyName(name, opt);
|
|
452
|
+
if (!isAcceptON) {
|
|
453
|
+
const { code, msg } = oname;
|
|
454
|
+
const err = new TypeError(msg);
|
|
455
|
+
err.code = code;
|
|
456
|
+
throw err;
|
|
457
|
+
};
|
|
458
|
+
let { isSucceed: isAcceptNN, value: nname } = evalKeyName(value, opt);
|
|
459
|
+
if (!isAcceptNN) {
|
|
460
|
+
const { code, msg } = nname;
|
|
461
|
+
const err = new TypeError(msg);
|
|
462
|
+
err.code = code;
|
|
463
|
+
throw err;
|
|
464
|
+
};
|
|
465
|
+
let isSucceed = false;
|
|
466
|
+
if (oname !== '' && oname in obj && nname !== '') {
|
|
467
|
+
isSucceed = true;
|
|
468
|
+
if (oname !== nname) {
|
|
469
|
+
const prop = obj[oname];
|
|
470
|
+
obj[nname] = prop;
|
|
471
|
+
isSucceed = delete obj[oname];
|
|
472
|
+
if (!isSucceed) {
|
|
473
|
+
// undo change if failed delete old element
|
|
474
|
+
delete obj[nname];
|
|
475
|
+
};
|
|
476
|
+
};
|
|
477
|
+
};
|
|
478
|
+
return isSucceed;
|
|
479
|
+
};
|
|
480
|
+
|
|
481
|
+
/**
|
|
482
|
+
* @function checkXObjAttribute
|
|
483
|
+
* @param {object} obj - some object
|
|
484
|
+
* @param {string} attr - some attribute ID
|
|
485
|
+
* @param {string} [key] - some key
|
|
486
|
+
* @returns {boolean}
|
|
487
|
+
* @throws {TypeError} if first param is not an object
|
|
488
|
+
* @description Checks whether an attribute is exists.
|
|
489
|
+
*/
|
|
490
|
+
function checkXObjAttribute(obj, attr = '', key) {
|
|
491
|
+
let _obj = null;
|
|
492
|
+
try {
|
|
493
|
+
_obj = getXObjAttributes(obj, key);
|
|
494
|
+
} catch (err) {
|
|
495
|
+
throw err;
|
|
496
|
+
};
|
|
497
|
+
if (_obj === null) return false;
|
|
498
|
+
let { isSucceed, value: name } = evalKeyName(attr, false);
|
|
499
|
+
if (isSucceed) {
|
|
500
|
+
if (name === '') return false;
|
|
501
|
+
} else {
|
|
502
|
+
const { code, msg } = name;
|
|
503
|
+
const err = new TypeError(msg);
|
|
504
|
+
err.code = code;
|
|
505
|
+
throw err;
|
|
506
|
+
};
|
|
507
|
+
return _obj[name] !== undefined;
|
|
508
|
+
};
|
|
509
|
+
|
|
510
|
+
/**
|
|
511
|
+
* @function deleteXObjAttribute
|
|
512
|
+
* @param {object} obj - some object
|
|
513
|
+
* @param {string} attr - some attribute ID
|
|
514
|
+
* @param {string} [key] - some key
|
|
515
|
+
* @returns {boolean}
|
|
516
|
+
* @throws {TypeError} if first param is not an object
|
|
517
|
+
* @description Deletes an attribute addressed by a given name.
|
|
518
|
+
*/
|
|
519
|
+
function deleteXObjAttribute(obj, attr = '', key) {
|
|
520
|
+
let _obj = null;
|
|
521
|
+
try {
|
|
522
|
+
_obj = getXObjAttributes(obj, key);
|
|
523
|
+
} catch (err) {
|
|
524
|
+
throw err;
|
|
525
|
+
};
|
|
526
|
+
if (_obj === null) return false;
|
|
527
|
+
let { isSucceed, value: name } = evalKeyName(attr, false);
|
|
528
|
+
if (!isSucceed) {
|
|
529
|
+
const { code, msg } = name;
|
|
530
|
+
const err = new TypeError(msg);
|
|
531
|
+
err.code = code;
|
|
532
|
+
throw err;
|
|
533
|
+
};
|
|
534
|
+
isSucceed = false;
|
|
535
|
+
if (name !== '') {
|
|
536
|
+
// // TODO: catch errors in strict mode
|
|
537
|
+
isSucceed = delete _obj[name];
|
|
538
|
+
};
|
|
539
|
+
return isSucceed;
|
|
540
|
+
};
|
|
541
|
+
|
|
542
|
+
/**
|
|
543
|
+
* @since 0.2.0
|
|
544
|
+
* @function renameXObjAttribute
|
|
545
|
+
* @param {object} obj - some object
|
|
546
|
+
* @param {string} attr - some attribute ID
|
|
547
|
+
* @param {string} value - new attribute ID
|
|
548
|
+
* @param {string} [key] - some key
|
|
549
|
+
* @returns {boolean}
|
|
550
|
+
* @throws {TypeError} if first param is not an object
|
|
551
|
+
* @throws {TypeError} if second param is not an object
|
|
552
|
+
* @throws {TypeError} if third param is not an object
|
|
553
|
+
* @description Renames an attribute addressed by a given name.
|
|
554
|
+
*/
|
|
555
|
+
function renameXObjAttribute(obj, attr = '', value = '', key = XOBJ_DEF_ATTR_TNAME) {
|
|
556
|
+
if (!isPlainObject(obj)) {
|
|
557
|
+
const err = new TypeError(XOBJ_TE_NPOBJ_EMSG);
|
|
558
|
+
err.code = XOBJ_TE_NPOBJ_ECODE;
|
|
559
|
+
throw err;
|
|
560
|
+
};
|
|
561
|
+
const opt = false;
|
|
562
|
+
let { isSucceed, value: _key } = evalKeyName(key, opt);
|
|
563
|
+
if (!isSucceed) {
|
|
564
|
+
const { code, msg } = _key;
|
|
565
|
+
const err = new TypeError(msg);
|
|
566
|
+
err.code = code;
|
|
567
|
+
throw err;
|
|
568
|
+
};
|
|
569
|
+
isSucceed = false;
|
|
570
|
+
if (_key !== '') {
|
|
571
|
+
let _obj = obj[_key];
|
|
572
|
+
if (isPlainObject(_obj)) {
|
|
573
|
+
let { isSucceed: isAcceptON, value: oname } = evalKeyName(attr, opt);
|
|
574
|
+
if (!isAcceptON) {
|
|
575
|
+
const { code, msg } = oname;
|
|
576
|
+
const err = new TypeError(msg);
|
|
577
|
+
err.code = code;
|
|
578
|
+
throw err;
|
|
579
|
+
};
|
|
580
|
+
let { isSucceed: isAcceptNN, value: nname } = evalKeyName(value, opt);
|
|
581
|
+
if (!isAcceptNN) {
|
|
582
|
+
const { code, msg } = nname;
|
|
583
|
+
const err = new TypeError(msg);
|
|
584
|
+
err.code = code;
|
|
585
|
+
throw err;
|
|
586
|
+
};
|
|
587
|
+
if (oname !== '' && oname in _obj && nname !== '') {
|
|
588
|
+
if (oname !== nname) {
|
|
589
|
+
_obj = Object.entries(_obj);
|
|
590
|
+
const index = _obj.findIndex((item) => item[0] === oname);
|
|
591
|
+
_obj[index][0] = nname;
|
|
592
|
+
obj[_key] = Object.fromEntries(_obj);
|
|
593
|
+
};
|
|
594
|
+
isSucceed = true;
|
|
595
|
+
};
|
|
596
|
+
};
|
|
597
|
+
};
|
|
598
|
+
return isSucceed;
|
|
599
|
+
};
|
|
600
|
+
|
|
601
|
+
/**
|
|
602
|
+
* @function readXObjParamRaw
|
|
603
|
+
* @param {object} obj - some object
|
|
604
|
+
* @param {string} [key=XOBJ_DEF_PARAM_TNAME] - some key
|
|
605
|
+
* @returns {any}
|
|
606
|
+
* @throws {TypeError} if first param is not an object
|
|
607
|
+
* @throws {TypeError} if third param is not a string
|
|
608
|
+
* @description Extracts a parameter 'AS IS' from a given object.
|
|
609
|
+
*/
|
|
610
|
+
function readXObjParamRaw(obj, key = XOBJ_DEF_PARAM_TNAME) {
|
|
611
|
+
if (!isPlainObject(obj)) {
|
|
612
|
+
const err = new TypeError(XOBJ_TE_NPOBJ_EMSG);
|
|
613
|
+
err.code = XOBJ_TE_NPOBJ_ECODE;
|
|
614
|
+
throw err;
|
|
615
|
+
};
|
|
616
|
+
let { isSucceed, value: _key } = evalKeyName(key, false);
|
|
617
|
+
if (!isSucceed) {
|
|
618
|
+
const { code, msg } = _key;
|
|
619
|
+
const err = new TypeError(msg);
|
|
620
|
+
err.code = code;
|
|
621
|
+
throw err;
|
|
622
|
+
};
|
|
623
|
+
return _key !== '' ? obj[_key] : undefined;
|
|
624
|
+
};
|
|
625
|
+
|
|
626
|
+
/**
|
|
627
|
+
* @function writeXObjParamRaw
|
|
628
|
+
* @param {object} obj - some object
|
|
629
|
+
* @param {any} value - some value
|
|
630
|
+
* @param {string} [key=XOBJ_DEF_PARAM_TNAME] - some key
|
|
631
|
+
* @returns {boolean}
|
|
632
|
+
* @throws {TypeError} if first param is not an object
|
|
633
|
+
* @throws {TypeError} if third param is not a string
|
|
634
|
+
* @description Writes a parameter 'AS IS' into a given object.
|
|
635
|
+
*/
|
|
636
|
+
function writeXObjParamRaw(obj, value, key = XOBJ_DEF_PARAM_TNAME) {
|
|
637
|
+
if (!isPlainObject(obj)) {
|
|
638
|
+
const err = new TypeError(XOBJ_TE_NPOBJ_EMSG);
|
|
639
|
+
err.code = XOBJ_TE_NPOBJ_ECODE;
|
|
640
|
+
throw err;
|
|
641
|
+
};
|
|
642
|
+
let { isSucceed, value: _key } = evalKeyName(key, false);
|
|
643
|
+
if (!isSucceed) {
|
|
644
|
+
const { code, msg } = _key;
|
|
645
|
+
const err = new TypeError(msg);
|
|
646
|
+
err.code = code;
|
|
647
|
+
throw err;
|
|
648
|
+
};
|
|
649
|
+
if (_key === '' || value === undefined) {
|
|
650
|
+
isSucceed = false;
|
|
651
|
+
} else {
|
|
652
|
+
obj[_key] = value;
|
|
653
|
+
};
|
|
654
|
+
return isSucceed;
|
|
655
|
+
};
|
|
656
|
+
|
|
657
|
+
/**
|
|
658
|
+
* @function readXObjAttrRaw
|
|
659
|
+
* @param {object} obj - some object
|
|
660
|
+
* @param {string} attr - some attribute
|
|
661
|
+
* @param {string} [key] - some key
|
|
662
|
+
* @returns {any}
|
|
663
|
+
* @throws {TypeError} if first param is not an object
|
|
664
|
+
* @throws {TypeError} if third param is not a string
|
|
665
|
+
* @description Extracts an attribute 'AS IS' from a given object.
|
|
666
|
+
*/
|
|
667
|
+
function readXObjAttrRaw(obj, attr = '', key) {
|
|
668
|
+
let _obj = null;
|
|
669
|
+
try {
|
|
670
|
+
_obj = getXObjAttributes(obj, key);
|
|
671
|
+
} catch (err) {
|
|
672
|
+
throw err;
|
|
673
|
+
};
|
|
674
|
+
if (_obj !== null) {
|
|
675
|
+
let { isSucceed, value: name } = evalKeyName(attr, false);
|
|
676
|
+
if (!isSucceed) {
|
|
677
|
+
const { code, msg } = name;
|
|
678
|
+
const err = new TypeError(msg);
|
|
679
|
+
err.code = code;
|
|
680
|
+
throw err;
|
|
681
|
+
};
|
|
682
|
+
if (name !== '') return _obj[name];
|
|
683
|
+
};
|
|
684
|
+
};
|
|
685
|
+
|
|
686
|
+
/**
|
|
687
|
+
* @function writeXObjAttrRaw
|
|
688
|
+
* @param {object} obj - some object
|
|
689
|
+
* @param {string} attr - some attribute
|
|
690
|
+
* @param {any} value - some value
|
|
691
|
+
* @param {string} [key=XOBJ_DEF_ATTR_TNAME] - some key
|
|
692
|
+
* @returns {boolean}
|
|
693
|
+
* @throws {TypeError} if first param is not an object
|
|
694
|
+
* @throws {TypeError} if third param is not a string
|
|
695
|
+
* @description Writes a parameter into a given object.
|
|
696
|
+
*/
|
|
697
|
+
function writeXObjAttrRaw(obj, attr = '', value, key = XOBJ_DEF_ATTR_TNAME) {
|
|
698
|
+
let { isSucceed, value: name } = evalKeyName(attr, false);
|
|
699
|
+
if (!isSucceed) {
|
|
700
|
+
const { code, msg } = name;
|
|
701
|
+
const err = new TypeError(msg);
|
|
702
|
+
err.code = code;
|
|
703
|
+
throw err;
|
|
704
|
+
};
|
|
705
|
+
isSucceed = false;
|
|
706
|
+
if (name !== '' && value !== undefined) {
|
|
707
|
+
let _obj = null;
|
|
708
|
+
try {
|
|
709
|
+
const opt = { force: true, acceptIfList: true };
|
|
710
|
+
_obj = insertXObjElement(obj, key, opt);
|
|
711
|
+
} catch (err) {
|
|
712
|
+
throw err;
|
|
713
|
+
};
|
|
714
|
+
if (isArray(_obj)) {
|
|
715
|
+
// force a replacement of the old element if it's array
|
|
716
|
+
try {
|
|
717
|
+
const opt = { force: true, ripOldies: true };
|
|
718
|
+
_obj = insertXObjElement(obj, key, opt);
|
|
719
|
+
} catch (err) {
|
|
720
|
+
throw err;
|
|
721
|
+
};
|
|
722
|
+
};
|
|
723
|
+
if (_obj !== null) {
|
|
724
|
+
_obj[name] = value;
|
|
725
|
+
isSucceed = true;
|
|
726
|
+
};
|
|
727
|
+
};
|
|
728
|
+
return isSucceed;
|
|
729
|
+
};
|
|
730
|
+
|
|
731
|
+
/**
|
|
732
|
+
* @function readXObjParam
|
|
733
|
+
* @param {object} obj - some object
|
|
734
|
+
* @param {string} [key] - some key
|
|
735
|
+
* @returns {string}
|
|
736
|
+
* @throws {TypeError} if first param is not an object
|
|
737
|
+
* @description Extracts a parameter from a given object
|
|
738
|
+
* and returns it as string.
|
|
739
|
+
*/
|
|
740
|
+
function readXObjParam(obj, key) {
|
|
741
|
+
const opt = {
|
|
742
|
+
useTrim: false,
|
|
743
|
+
numberToString: true,
|
|
744
|
+
boolToString: true,
|
|
745
|
+
defValue: '',
|
|
746
|
+
};
|
|
747
|
+
let result = undefined;
|
|
748
|
+
try {
|
|
749
|
+
result = readXObjParamEx(obj, opt, key);
|
|
750
|
+
} catch (err) {
|
|
751
|
+
throw err;
|
|
752
|
+
};
|
|
753
|
+
return result;
|
|
754
|
+
};
|
|
755
|
+
|
|
756
|
+
/**
|
|
757
|
+
* @function readXObjParamAsBool
|
|
758
|
+
* @param {object} obj - some object
|
|
759
|
+
* @param {boolean} [defValue] - default value
|
|
760
|
+
* @param {string} [key] - some key
|
|
761
|
+
* @returns {boolean}
|
|
762
|
+
* @throws {TypeError} if first param is not an object
|
|
763
|
+
* @description Extracts a parameter from a given object
|
|
764
|
+
* and returns it as boolean.
|
|
765
|
+
*/
|
|
766
|
+
function readXObjParamAsBool(obj, defValue, key) {
|
|
767
|
+
let result = undefined;
|
|
768
|
+
try {
|
|
769
|
+
result = readXObjParamRaw(obj, key);
|
|
770
|
+
} catch (err) {
|
|
771
|
+
switch (err.code) {
|
|
772
|
+
case XOBJ_TE_NSTR_ECODE : {
|
|
773
|
+
break;
|
|
774
|
+
}
|
|
775
|
+
default: {
|
|
776
|
+
throw err;
|
|
777
|
+
}
|
|
778
|
+
};
|
|
779
|
+
};
|
|
780
|
+
return readAsBoolEx(result, defValue);
|
|
781
|
+
};
|
|
782
|
+
|
|
783
|
+
/**
|
|
784
|
+
* @function readXObjParamAsNum
|
|
785
|
+
* @param {object} obj - some object
|
|
786
|
+
* @param {number} [defValue] - default value
|
|
787
|
+
* @param {string} [key] - some key
|
|
788
|
+
* @returns {number}
|
|
789
|
+
* @throws {TypeError} if first param is not an object
|
|
790
|
+
* @description Extracts a parameter from a given object
|
|
791
|
+
* and returns it as number.
|
|
792
|
+
*/
|
|
793
|
+
function readXObjParamAsNum(obj, defValue, key) {
|
|
794
|
+
let result = undefined;
|
|
795
|
+
try {
|
|
796
|
+
result = readXObjParamRaw(obj, key);
|
|
797
|
+
} catch (err) {
|
|
798
|
+
switch (err.code) {
|
|
799
|
+
case XOBJ_TE_NSTR_ECODE : {
|
|
800
|
+
break;
|
|
801
|
+
}
|
|
802
|
+
default: {
|
|
803
|
+
throw err;
|
|
804
|
+
}
|
|
805
|
+
};
|
|
806
|
+
};
|
|
807
|
+
return readAsNumberEx(result, defValue);
|
|
808
|
+
};
|
|
809
|
+
|
|
810
|
+
/**
|
|
811
|
+
* @function readXObjParamEx
|
|
812
|
+
* @param {object} obj - some object
|
|
813
|
+
* @param {object} [opt] - options
|
|
814
|
+
* @param {string} [key] - some key
|
|
815
|
+
* @returns {string}
|
|
816
|
+
* @throws {TypeError} if first param is not an object
|
|
817
|
+
* @description Extracts a parameter from a given object
|
|
818
|
+
* and returns it as string.
|
|
819
|
+
* @todo [since `v0.2.1`] deprecate use of `opt` as `string`
|
|
820
|
+
*/
|
|
821
|
+
function readXObjParamEx(obj, opt, key) {
|
|
822
|
+
let result = undefined;
|
|
823
|
+
try {
|
|
824
|
+
result = readXObjParamRaw(obj, key);
|
|
825
|
+
} catch (err) {
|
|
826
|
+
switch (err.code) {
|
|
827
|
+
case XOBJ_TE_NSTR_ECODE : {
|
|
828
|
+
break;
|
|
829
|
+
}
|
|
830
|
+
default: {
|
|
831
|
+
throw err;
|
|
832
|
+
}
|
|
833
|
+
};
|
|
834
|
+
};
|
|
835
|
+
let _opt = opt;
|
|
836
|
+
if (!isPlainObject(_opt)) {
|
|
837
|
+
_opt = {
|
|
838
|
+
useTrim: false,
|
|
839
|
+
numberToString: true,
|
|
840
|
+
boolToString: true,
|
|
841
|
+
defValue: _opt,
|
|
842
|
+
};
|
|
843
|
+
};
|
|
844
|
+
return readAsString(result, _opt);
|
|
845
|
+
};
|
|
846
|
+
|
|
847
|
+
/**
|
|
848
|
+
* @function readXObjParamAsIndex
|
|
849
|
+
* @param {object} obj - some object
|
|
850
|
+
* @param {string} [key] - some key
|
|
851
|
+
* @returns {number}
|
|
852
|
+
* @throws {TypeError} if first param is not an object
|
|
853
|
+
* @description Extracts a parameter from a given object
|
|
854
|
+
* and returns it as 'index' value.
|
|
855
|
+
*/
|
|
856
|
+
function readXObjParamAsIndex(obj, key) {
|
|
857
|
+
let result = undefined;
|
|
858
|
+
try {
|
|
859
|
+
result = readXObjParamRaw(obj, key);
|
|
860
|
+
} catch (err) {
|
|
861
|
+
switch (err.code) {
|
|
862
|
+
case XOBJ_TE_NSTR_ECODE : {
|
|
863
|
+
break;
|
|
864
|
+
}
|
|
865
|
+
default: {
|
|
866
|
+
throw err;
|
|
867
|
+
}
|
|
868
|
+
};
|
|
869
|
+
};
|
|
870
|
+
return valueToIndex(result);
|
|
871
|
+
};
|
|
872
|
+
|
|
873
|
+
/**
|
|
874
|
+
* @function writeXObjParam
|
|
875
|
+
* @param {object} obj - some object
|
|
876
|
+
* @param {any} value - some value
|
|
877
|
+
* @param {string} [key] - some key
|
|
878
|
+
* @returns {boolean}
|
|
879
|
+
* @throws {TypeError} if first param is not an object
|
|
880
|
+
* @description Tries to convert a given value to a string
|
|
881
|
+
* and writes it as a parameter into a given object.
|
|
882
|
+
*/
|
|
883
|
+
function writeXObjParam(obj, value, key) {
|
|
884
|
+
let isSucceed = false;
|
|
885
|
+
try {
|
|
886
|
+
const opt = {
|
|
887
|
+
useTrim: false,
|
|
888
|
+
numberToString: true,
|
|
889
|
+
boolToString: true,
|
|
890
|
+
defValue: '',
|
|
891
|
+
};
|
|
892
|
+
isSucceed = writeXObjParamEx(obj, value, opt, key);
|
|
893
|
+
} catch (err) {
|
|
894
|
+
throw err;
|
|
895
|
+
};
|
|
896
|
+
return isSucceed;
|
|
897
|
+
};
|
|
898
|
+
|
|
899
|
+
/**
|
|
900
|
+
* @function writeXObjParamAsBool
|
|
901
|
+
* @param {object} obj - some object
|
|
902
|
+
* @param {any} value - some value
|
|
903
|
+
* @param {boolean} [defValue] - default value
|
|
904
|
+
* @param {string} [key] - some key
|
|
905
|
+
* @returns {boolean}
|
|
906
|
+
* @throws {TypeError} if first param is not an object
|
|
907
|
+
* @description Tries to convert a given value to a boolean
|
|
908
|
+
* and writes it as a parameter into a given object.
|
|
909
|
+
*/
|
|
910
|
+
function writeXObjParamAsBool(obj, value, defValue, key) {
|
|
911
|
+
let isSucceed = false;
|
|
912
|
+
if (value !== undefined || typeof defValue === 'boolean') {
|
|
913
|
+
const _value = readAsBoolEx(value, defValue).toString();
|
|
914
|
+
try {
|
|
915
|
+
isSucceed = writeXObjParamRaw(obj, _value, key);
|
|
916
|
+
} catch (err) {
|
|
917
|
+
switch (err.code) {
|
|
918
|
+
case XOBJ_TE_NSTR_ECODE : {
|
|
919
|
+
break;
|
|
920
|
+
}
|
|
921
|
+
default: {
|
|
922
|
+
throw err;
|
|
923
|
+
}
|
|
924
|
+
};
|
|
925
|
+
};
|
|
926
|
+
};
|
|
927
|
+
return isSucceed;
|
|
928
|
+
};
|
|
929
|
+
|
|
930
|
+
/**
|
|
931
|
+
* @function writeXObjParamAsNum
|
|
932
|
+
* @param {object} obj - some object
|
|
933
|
+
* @param {any} value - some value
|
|
934
|
+
* @param {number} [defValue] - default value
|
|
935
|
+
* @param {string} [key] - some key
|
|
936
|
+
* @returns {boolean}
|
|
937
|
+
* @throws {TypeError} if first param is not an object
|
|
938
|
+
* @description Tries to convert a given value to a number
|
|
939
|
+
* and writes it as a parameter into a given object.
|
|
940
|
+
*/
|
|
941
|
+
function writeXObjParamAsNum(obj, value, defValue, key) {
|
|
942
|
+
let isSucceed = false;
|
|
943
|
+
if (value !== undefined || typeof defValue === 'number') {
|
|
944
|
+
const _value = readAsNumberEx(value, defValue).toString();
|
|
945
|
+
try {
|
|
946
|
+
isSucceed = writeXObjParamRaw(obj, _value, key);
|
|
947
|
+
} catch (err) {
|
|
948
|
+
switch (err.code) {
|
|
949
|
+
case XOBJ_TE_NSTR_ECODE : {
|
|
950
|
+
break;
|
|
951
|
+
}
|
|
952
|
+
default: {
|
|
953
|
+
throw err;
|
|
954
|
+
}
|
|
955
|
+
};
|
|
956
|
+
};
|
|
957
|
+
};
|
|
958
|
+
return isSucceed;
|
|
959
|
+
};
|
|
960
|
+
|
|
961
|
+
/**
|
|
962
|
+
* @function writeXObjParamAsIndex
|
|
963
|
+
* @param {object} obj - some object
|
|
964
|
+
* @param {any} value - some value
|
|
965
|
+
* @param {string} [key] - some key
|
|
966
|
+
* @returns {boolean}
|
|
967
|
+
* @throws {TypeError} if first param is not an object
|
|
968
|
+
* @description Tries to convert a given value into an 'index' value
|
|
969
|
+
* and writes it as a parameter into a given object.
|
|
970
|
+
*/
|
|
971
|
+
function writeXObjParamAsIndex(obj, value, key) {
|
|
972
|
+
let isSucceed = false;
|
|
973
|
+
if (value !== undefined) {
|
|
974
|
+
const _value = valueToIndex(value).toString();
|
|
975
|
+
try {
|
|
976
|
+
isSucceed = writeXObjParamRaw(obj, _value, key);
|
|
977
|
+
} catch (err) {
|
|
978
|
+
switch (err.code) {
|
|
979
|
+
case XOBJ_TE_NSTR_ECODE : {
|
|
980
|
+
break;
|
|
981
|
+
}
|
|
982
|
+
default: {
|
|
983
|
+
throw err;
|
|
984
|
+
}
|
|
985
|
+
};
|
|
986
|
+
};
|
|
987
|
+
};
|
|
988
|
+
return isSucceed;
|
|
989
|
+
};
|
|
990
|
+
|
|
991
|
+
/**
|
|
992
|
+
* @function writeXObjParamEx
|
|
993
|
+
* @param {object} obj - some object
|
|
994
|
+
* @param {any} value - some value
|
|
995
|
+
* @param {object} [opt] - options
|
|
996
|
+
* @param {string} [key] - some key
|
|
997
|
+
* @returns {boolean}
|
|
998
|
+
* @throws {TypeError} if first param is not an object
|
|
999
|
+
* @description Tries to convert a given value to a string
|
|
1000
|
+
* and writes it as a parameter into a given object.
|
|
1001
|
+
* @todo [since `v0.2.1`] deprecate use of `opt` as `string`
|
|
1002
|
+
*/
|
|
1003
|
+
function writeXObjParamEx(obj, value, opt, key) {
|
|
1004
|
+
let isSucceed = false;
|
|
1005
|
+
if (value !== undefined) {
|
|
1006
|
+
let _opt = opt;
|
|
1007
|
+
if (!isPlainObject(_opt)) {
|
|
1008
|
+
const defValue = readAsString(_opt, {
|
|
1009
|
+
useTrim: false,
|
|
1010
|
+
numberToString: true,
|
|
1011
|
+
boolToString: true,
|
|
1012
|
+
});
|
|
1013
|
+
_opt = {
|
|
1014
|
+
useTrim: false,
|
|
1015
|
+
numberToString: true,
|
|
1016
|
+
boolToString: true,
|
|
1017
|
+
defValue,
|
|
1018
|
+
};
|
|
1019
|
+
};
|
|
1020
|
+
const _value = readAsString(value, _opt);
|
|
1021
|
+
try {
|
|
1022
|
+
isSucceed = writeXObjParamRaw(obj, _value, key);
|
|
1023
|
+
} catch (err) {
|
|
1024
|
+
switch (err.code) {
|
|
1025
|
+
case XOBJ_TE_NSTR_ECODE : {
|
|
1026
|
+
break;
|
|
1027
|
+
}
|
|
1028
|
+
default: {
|
|
1029
|
+
throw err;
|
|
1030
|
+
}
|
|
1031
|
+
};
|
|
1032
|
+
};
|
|
1033
|
+
};
|
|
1034
|
+
return isSucceed;
|
|
1035
|
+
};
|
|
1036
|
+
|
|
1037
|
+
/**
|
|
1038
|
+
* @function readXObjAttr
|
|
1039
|
+
* @param {object} obj - some object
|
|
1040
|
+
* @param {string} attr - some attribute
|
|
1041
|
+
* @param {string} [key] - some key
|
|
1042
|
+
* @returns {string}
|
|
1043
|
+
* @throws {TypeError} if first param is not an object
|
|
1044
|
+
* @description Extracts an attribute from a given object
|
|
1045
|
+
* and returns it as string.
|
|
1046
|
+
*/
|
|
1047
|
+
function readXObjAttr(obj, attr, key) {
|
|
1048
|
+
const opt = {
|
|
1049
|
+
useTrim: true,
|
|
1050
|
+
numberToString: true,
|
|
1051
|
+
boolToString: true,
|
|
1052
|
+
defValue: '',
|
|
1053
|
+
};
|
|
1054
|
+
let result = undefined;
|
|
1055
|
+
try {
|
|
1056
|
+
result = readXObjAttrEx(obj, attr, opt, key);
|
|
1057
|
+
} catch (err) {
|
|
1058
|
+
throw err;
|
|
1059
|
+
};
|
|
1060
|
+
return result;
|
|
1061
|
+
};
|
|
1062
|
+
|
|
1063
|
+
/**
|
|
1064
|
+
* @function readXObjAttrAsBool
|
|
1065
|
+
* @param {object} obj - some object
|
|
1066
|
+
* @param {string} attr - some attribute
|
|
1067
|
+
* @param {boolean} [defValue] - default value
|
|
1068
|
+
* @param {string} [key] - some key
|
|
1069
|
+
* @returns {boolean}
|
|
1070
|
+
* @throws {TypeError} if first param is not an object
|
|
1071
|
+
* @description Extracts an attribute from a given object
|
|
1072
|
+
* and returns it as boolean.
|
|
1073
|
+
*/
|
|
1074
|
+
function readXObjAttrAsBool(obj, attr, defValue, key) {
|
|
1075
|
+
let result = undefined;
|
|
1076
|
+
try {
|
|
1077
|
+
result = readXObjAttrRaw(obj, attr, key);
|
|
1078
|
+
} catch (err) {
|
|
1079
|
+
switch (err.code) {
|
|
1080
|
+
case XOBJ_TE_NSTR_ECODE : {
|
|
1081
|
+
break;
|
|
1082
|
+
}
|
|
1083
|
+
default: {
|
|
1084
|
+
throw err;
|
|
1085
|
+
}
|
|
1086
|
+
};
|
|
1087
|
+
};
|
|
1088
|
+
return readAsBoolEx(result, defValue);
|
|
1089
|
+
};
|
|
1090
|
+
|
|
1091
|
+
/**
|
|
1092
|
+
* @function readXObjAttrAsNum
|
|
1093
|
+
* @param {object} obj - some object
|
|
1094
|
+
* @param {string} attr - some attribute
|
|
1095
|
+
* @param {number} [defValue] - default value
|
|
1096
|
+
* @param {string} [key] - some key
|
|
1097
|
+
* @returns {number}
|
|
1098
|
+
* @throws {TypeError} if first param is not an object
|
|
1099
|
+
* @description Extracts an attribute from a given object
|
|
1100
|
+
* and returns it as number.
|
|
1101
|
+
*/
|
|
1102
|
+
function readXObjAttrAsNum(obj, attr, defValue, key) {
|
|
1103
|
+
let result = undefined;
|
|
1104
|
+
try {
|
|
1105
|
+
result = readXObjAttrRaw(obj, attr, key);
|
|
1106
|
+
} catch (err) {
|
|
1107
|
+
switch (err.code) {
|
|
1108
|
+
case XOBJ_TE_NSTR_ECODE : {
|
|
1109
|
+
break;
|
|
1110
|
+
}
|
|
1111
|
+
default: {
|
|
1112
|
+
throw err;
|
|
1113
|
+
}
|
|
1114
|
+
};
|
|
1115
|
+
};
|
|
1116
|
+
return readAsNumberEx(result, defValue);
|
|
1117
|
+
};
|
|
1118
|
+
|
|
1119
|
+
/**
|
|
1120
|
+
* @function readXObjAttrEx
|
|
1121
|
+
* @param {object} obj - some object
|
|
1122
|
+
* @param {string} attr - some attribute
|
|
1123
|
+
* @param {object} [opt] - options
|
|
1124
|
+
* @param {string} [key] - some key
|
|
1125
|
+
* @returns {string}
|
|
1126
|
+
* @throws {TypeError} if first param is not an object
|
|
1127
|
+
* @description Extracts an attribute from a given object
|
|
1128
|
+
* and returns it as string.
|
|
1129
|
+
* @todo [since `v0.2.1`] deprecate use of `opt` as `string`
|
|
1130
|
+
*/
|
|
1131
|
+
function readXObjAttrEx(obj, attr, opt, key) {
|
|
1132
|
+
let result = undefined;
|
|
1133
|
+
try {
|
|
1134
|
+
result = readXObjAttrRaw(obj, attr, key);
|
|
1135
|
+
} catch (err) {
|
|
1136
|
+
switch (err.code) {
|
|
1137
|
+
case XOBJ_TE_NSTR_ECODE : {
|
|
1138
|
+
break;
|
|
1139
|
+
}
|
|
1140
|
+
default: {
|
|
1141
|
+
throw err;
|
|
1142
|
+
}
|
|
1143
|
+
};
|
|
1144
|
+
};
|
|
1145
|
+
let _opt = opt;
|
|
1146
|
+
if (!isPlainObject(_opt)) {
|
|
1147
|
+
_opt = {
|
|
1148
|
+
useTrim: true,
|
|
1149
|
+
numberToString: true,
|
|
1150
|
+
boolToString: true,
|
|
1151
|
+
defValue: _opt,
|
|
1152
|
+
};
|
|
1153
|
+
};
|
|
1154
|
+
return readAsString(result, _opt);
|
|
1155
|
+
};
|
|
1156
|
+
|
|
1157
|
+
/**
|
|
1158
|
+
* @function readXObjAttrAsIndex
|
|
1159
|
+
* @param {object} obj - some object
|
|
1160
|
+
* @param {string} attr - some attribute
|
|
1161
|
+
* @param {string} [key] - some key
|
|
1162
|
+
* @returns {number}
|
|
1163
|
+
* @throws {TypeError} if first param is not an object
|
|
1164
|
+
* @description Extracts an attribute from a given object
|
|
1165
|
+
* and returns it as 'index' value.
|
|
1166
|
+
*/
|
|
1167
|
+
function readXObjAttrAsIndex(obj, attr, key) {
|
|
1168
|
+
let result = undefined;
|
|
1169
|
+
try {
|
|
1170
|
+
result = readXObjAttrRaw(obj, attr, key);
|
|
1171
|
+
} catch (err) {
|
|
1172
|
+
switch (err.code) {
|
|
1173
|
+
case XOBJ_TE_NSTR_ECODE : {
|
|
1174
|
+
break;
|
|
1175
|
+
}
|
|
1176
|
+
default: {
|
|
1177
|
+
throw err;
|
|
1178
|
+
}
|
|
1179
|
+
};
|
|
1180
|
+
};
|
|
1181
|
+
return valueToIndex(result);
|
|
1182
|
+
};
|
|
1183
|
+
|
|
1184
|
+
/**
|
|
1185
|
+
* @function writeXObjAttr
|
|
1186
|
+
* @param {object} obj - some object
|
|
1187
|
+
* @param {string} attr - some attribute
|
|
1188
|
+
* @param {any} value - some value
|
|
1189
|
+
* @param {string} [key] - some key
|
|
1190
|
+
* @returns {boolean}
|
|
1191
|
+
* @throws {TypeError} if first param is not an object
|
|
1192
|
+
* @description Tries to convert a given value to a string
|
|
1193
|
+
* and writes it as an attribute into a given object.
|
|
1194
|
+
*/
|
|
1195
|
+
function writeXObjAttr(obj, attr, value, key) {
|
|
1196
|
+
let isSucceed = false;
|
|
1197
|
+
try {
|
|
1198
|
+
const opt = {
|
|
1199
|
+
useTrim: true,
|
|
1200
|
+
numberToString: true,
|
|
1201
|
+
boolToString: true,
|
|
1202
|
+
defValue: '',
|
|
1203
|
+
};
|
|
1204
|
+
isSucceed = writeXObjAttrEx(obj, attr, value, opt, key);
|
|
1205
|
+
} catch (err) {
|
|
1206
|
+
throw err;
|
|
1207
|
+
};
|
|
1208
|
+
return isSucceed;
|
|
1209
|
+
};
|
|
1210
|
+
|
|
1211
|
+
/**
|
|
1212
|
+
* @function writeXObjAttrAsBool
|
|
1213
|
+
* @param {object} obj - some object
|
|
1214
|
+
* @param {string} attr - some attribute
|
|
1215
|
+
* @param {any} value - some value
|
|
1216
|
+
* @param {boolean} [defValue] - default value
|
|
1217
|
+
* @param {string} [key] - some key
|
|
1218
|
+
* @returns {boolean}
|
|
1219
|
+
* @throws {TypeError} if first param is not an object
|
|
1220
|
+
* @description Tries to convert a given value to a boolean
|
|
1221
|
+
* and writes it as an attribute into a given object.
|
|
1222
|
+
*/
|
|
1223
|
+
function writeXObjAttrAsBool(obj, attr, value, defValue, key) {
|
|
1224
|
+
let isSucceed = false;
|
|
1225
|
+
if (value !== undefined || typeof defValue === 'boolean') {
|
|
1226
|
+
const _value = readAsBoolEx(value, defValue).toString();
|
|
1227
|
+
try {
|
|
1228
|
+
isSucceed = writeXObjAttrRaw(obj, attr, _value, key);
|
|
1229
|
+
} catch (err) {
|
|
1230
|
+
switch (err.code) {
|
|
1231
|
+
case XOBJ_TE_NSTR_ECODE : {
|
|
1232
|
+
break;
|
|
1233
|
+
}
|
|
1234
|
+
default: {
|
|
1235
|
+
throw err;
|
|
1236
|
+
}
|
|
1237
|
+
};
|
|
1238
|
+
};
|
|
1239
|
+
};
|
|
1240
|
+
return isSucceed;
|
|
1241
|
+
};
|
|
1242
|
+
|
|
1243
|
+
/**
|
|
1244
|
+
* @function writeXObjAttrAsNum
|
|
1245
|
+
* @param {object} obj - some object
|
|
1246
|
+
* @param {string} attr - some attribute
|
|
1247
|
+
* @param {any} value - some value
|
|
1248
|
+
* @param {number} [defValue] - default value
|
|
1249
|
+
* @param {string} [key] - some key
|
|
1250
|
+
* @returns {boolean}
|
|
1251
|
+
* @throws {TypeError} if first param is not an object
|
|
1252
|
+
* @description Tries to convert a given value to a number
|
|
1253
|
+
* and writes it as an attribute into a given object.
|
|
1254
|
+
*/
|
|
1255
|
+
function writeXObjAttrAsNum(obj, attr, value, defValue, key) {
|
|
1256
|
+
let isSucceed = false;
|
|
1257
|
+
if (value !== undefined || typeof defValue === 'number') {
|
|
1258
|
+
const _value = readAsNumberEx(value, defValue).toString();
|
|
1259
|
+
try {
|
|
1260
|
+
isSucceed = writeXObjAttrRaw(obj, attr, _value, key);
|
|
1261
|
+
} catch (err) {
|
|
1262
|
+
switch (err.code) {
|
|
1263
|
+
case XOBJ_TE_NSTR_ECODE : {
|
|
1264
|
+
break;
|
|
1265
|
+
}
|
|
1266
|
+
default: {
|
|
1267
|
+
throw err;
|
|
1268
|
+
}
|
|
1269
|
+
};
|
|
1270
|
+
};
|
|
1271
|
+
};
|
|
1272
|
+
return isSucceed;
|
|
1273
|
+
};
|
|
1274
|
+
|
|
1275
|
+
/**
|
|
1276
|
+
* @function writeXObjAttrAsIndex
|
|
1277
|
+
* @param {object} obj - some object
|
|
1278
|
+
* @param {string} attr - some attribute
|
|
1279
|
+
* @param {any} value - some value
|
|
1280
|
+
* @param {string} [key] - some key
|
|
1281
|
+
* @returns {boolean}
|
|
1282
|
+
* @throws {TypeError} if first param is not an object
|
|
1283
|
+
* @description Tries to convert a given value into an 'index' value
|
|
1284
|
+
* and writes it as an attribute into a given object.
|
|
1285
|
+
*/
|
|
1286
|
+
function writeXObjAttrAsIndex(obj, attr, value, key) {
|
|
1287
|
+
let isSucceed = false;
|
|
1288
|
+
if (value !== undefined) {
|
|
1289
|
+
const _value = valueToIndex(value).toString();
|
|
1290
|
+
try {
|
|
1291
|
+
isSucceed = writeXObjAttrRaw(obj, attr, _value, key);
|
|
1292
|
+
} catch (err) {
|
|
1293
|
+
switch (err.code) {
|
|
1294
|
+
case XOBJ_TE_NSTR_ECODE : {
|
|
1295
|
+
break;
|
|
1296
|
+
}
|
|
1297
|
+
default: {
|
|
1298
|
+
throw err;
|
|
1299
|
+
}
|
|
1300
|
+
};
|
|
1301
|
+
};
|
|
1302
|
+
};
|
|
1303
|
+
return isSucceed;
|
|
1304
|
+
};
|
|
1305
|
+
|
|
1306
|
+
/**
|
|
1307
|
+
* @function writeXObjAttrEx
|
|
1308
|
+
* @param {object} obj - some object
|
|
1309
|
+
* @param {string} attr - some attribute
|
|
1310
|
+
* @param {any} value - some value
|
|
1311
|
+
* @param {object} [opt] - options
|
|
1312
|
+
* @param {string} [key] - some key
|
|
1313
|
+
* @returns {boolean}
|
|
1314
|
+
* @throws {TypeError} if first param is not an object
|
|
1315
|
+
* @description Tries to convert a given value to a string
|
|
1316
|
+
* and writes it as an attribute into a given object.
|
|
1317
|
+
* @todo [since `v0.2.1`] deprecate use of `opt` as `string`
|
|
1318
|
+
*/
|
|
1319
|
+
function writeXObjAttrEx(obj, attr, value, opt, key) {
|
|
1320
|
+
let isSucceed = false;
|
|
1321
|
+
if (value !== undefined) {
|
|
1322
|
+
let _opt = opt;
|
|
1323
|
+
if (!isPlainObject(_opt)) {
|
|
1324
|
+
const defValue = readAsString(_opt, {
|
|
1325
|
+
useTrim: true,
|
|
1326
|
+
numberToString: true,
|
|
1327
|
+
boolToString: true,
|
|
1328
|
+
});
|
|
1329
|
+
_opt = {
|
|
1330
|
+
useTrim: true,
|
|
1331
|
+
numberToString: true,
|
|
1332
|
+
boolToString: true,
|
|
1333
|
+
defValue,
|
|
1334
|
+
};
|
|
1335
|
+
};
|
|
1336
|
+
const _value = readAsString(value, _opt);
|
|
1337
|
+
try {
|
|
1338
|
+
isSucceed = writeXObjAttrRaw(obj, attr, _value, key);
|
|
1339
|
+
} catch (err) {
|
|
1340
|
+
switch (err.code) {
|
|
1341
|
+
case XOBJ_TE_NSTR_ECODE : {
|
|
1342
|
+
break;
|
|
1343
|
+
}
|
|
1344
|
+
default: {
|
|
1345
|
+
throw err;
|
|
1346
|
+
}
|
|
1347
|
+
};
|
|
1348
|
+
};
|
|
1349
|
+
};
|
|
1350
|
+
return isSucceed;
|
|
1351
|
+
};
|
|
1352
|
+
|
|
1353
|
+
/**
|
|
1354
|
+
* @function insertXObjElements
|
|
1355
|
+
* @param {object} obj
|
|
1356
|
+
* @param {...string} name
|
|
1357
|
+
* @param {OPT_inselops_L} [opt]
|
|
1358
|
+
* @returns {number}
|
|
1359
|
+
* @throws {TypeError} if first param is not an object
|
|
1360
|
+
* @description Inserts an elements into a given object.
|
|
1361
|
+
*/
|
|
1362
|
+
function insertXObjElements(obj, ...args) {
|
|
1363
|
+
if (!isPlainObject(obj)) {
|
|
1364
|
+
const err = new TypeError(XOBJ_TE_NPOBJ_EMSG);
|
|
1365
|
+
err.code = XOBJ_TE_NPOBJ_ECODE;
|
|
1366
|
+
throw err;
|
|
1367
|
+
};
|
|
1368
|
+
let count = 0;
|
|
1369
|
+
let len = args.length;
|
|
1370
|
+
const opt = (
|
|
1371
|
+
len > 0 && isPlainObject(args[len - 1])
|
|
1372
|
+
? (len--, args.pop())
|
|
1373
|
+
: {}
|
|
1374
|
+
);
|
|
1375
|
+
for (let key of args) {
|
|
1376
|
+
if (insertXObjElement(obj, key, opt) !== null) count++;
|
|
1377
|
+
};
|
|
1378
|
+
return count;
|
|
1379
|
+
};
|
|
1380
|
+
|
|
1381
|
+
/**
|
|
1382
|
+
* @typedef {Object} OPT_inselops_S
|
|
1383
|
+
* @property {boolean} [force=false]
|
|
1384
|
+
* @property {boolean} [ripOldies=false]
|
|
1385
|
+
*/
|
|
1386
|
+
|
|
1387
|
+
/**
|
|
1388
|
+
* @function insertXObjEList
|
|
1389
|
+
* @param {object} obj - some object
|
|
1390
|
+
* @param {string} name - some child element
|
|
1391
|
+
* @param {OPT_inselops_S} [opt] - options
|
|
1392
|
+
* @returns {?(object|object[])}
|
|
1393
|
+
* @throws {TypeError} if first param is not an object
|
|
1394
|
+
* @description Inserts a list elements into a given object.
|
|
1395
|
+
*/
|
|
1396
|
+
function insertXObjEList(...args) {
|
|
1397
|
+
let item = null;
|
|
1398
|
+
try {
|
|
1399
|
+
({ item } = insertXObjEListEx(...args));
|
|
1400
|
+
} catch (err) {
|
|
1401
|
+
switch (err.code) {
|
|
1402
|
+
case XOBJ_TE_NSTR_ECODE :
|
|
1403
|
+
case XOBJ_TE_KNES_ECODE : {
|
|
1404
|
+
break;
|
|
1405
|
+
}
|
|
1406
|
+
default: {
|
|
1407
|
+
throw err;
|
|
1408
|
+
}
|
|
1409
|
+
};
|
|
1410
|
+
};
|
|
1411
|
+
return item;
|
|
1412
|
+
};
|
|
1413
|
+
|
|
1414
|
+
/**
|
|
1415
|
+
* @since 0.2.0
|
|
1416
|
+
* @function insertXObjEListEx
|
|
1417
|
+
* @param {object} obj - some object
|
|
1418
|
+
* @param {string} name - some child element
|
|
1419
|
+
* @param {OPT_inselops_S} [opt] - options
|
|
1420
|
+
* @returns {RVAL_emodif}
|
|
1421
|
+
* @throws {TypeError} if first param is not an object
|
|
1422
|
+
* @throws {TypeError} if second param is empty string or not a string at all
|
|
1423
|
+
* @description Inserts a list elements into a given object.
|
|
1424
|
+
*/
|
|
1425
|
+
function insertXObjEListEx(obj, name, opt) {
|
|
1426
|
+
if (!isPlainObject(obj)) {
|
|
1427
|
+
const err = new TypeError(XOBJ_TE_NPOBJ_EMSG);
|
|
1428
|
+
err.code = XOBJ_TE_NPOBJ_ECODE;
|
|
1429
|
+
throw err;
|
|
1430
|
+
};
|
|
1431
|
+
let { isSucceed, value: key } = evalKeyName(name);
|
|
1432
|
+
if (!isSucceed) {
|
|
1433
|
+
const { code, msg } = key;
|
|
1434
|
+
const err = new TypeError(msg);
|
|
1435
|
+
err.code = code;
|
|
1436
|
+
throw err;
|
|
1437
|
+
};
|
|
1438
|
+
/** @type {OPT_inselops_S} */
|
|
1439
|
+
let {
|
|
1440
|
+
force,
|
|
1441
|
+
ripOldies,
|
|
1442
|
+
} = isPlainObject(opt) ? opt : {};
|
|
1443
|
+
if (typeof force !== 'boolean') force = false;
|
|
1444
|
+
if (typeof ripOldies !== 'boolean') ripOldies = false;
|
|
1445
|
+
let prop = obj[key];
|
|
1446
|
+
isSucceed = false;
|
|
1447
|
+
if (
|
|
1448
|
+
isNullOrUndef(prop)
|
|
1449
|
+
|| (force && (ripOldies || !isObject(prop)))
|
|
1450
|
+
) {
|
|
1451
|
+
obj[key] = prop = [];
|
|
1452
|
+
isSucceed = true;
|
|
1453
|
+
} else {
|
|
1454
|
+
if (isObject(prop)) {
|
|
1455
|
+
if (!isArray(prop)) prop = [ prop ];
|
|
1456
|
+
obj[key] = prop;
|
|
1457
|
+
isSucceed = true;
|
|
1458
|
+
};
|
|
1459
|
+
};
|
|
1460
|
+
return {
|
|
1461
|
+
isSucceed,
|
|
1462
|
+
item: isSucceed ? prop : null,
|
|
1463
|
+
};
|
|
1464
|
+
};
|
|
1465
|
+
|
|
1466
|
+
/**
|
|
1467
|
+
* @function insertXObjEChain
|
|
1468
|
+
* @param {object} obj - some object
|
|
1469
|
+
* @param {...string} name - some child element
|
|
1470
|
+
* @param {object} [opt] - options
|
|
1471
|
+
* @returns {?any}
|
|
1472
|
+
* @throws {TypeError} if first param is not an object
|
|
1473
|
+
* @description Inserts a chain of an elements into a given object.
|
|
1474
|
+
*/
|
|
1475
|
+
function insertXObjEChain(obj, ...args) {
|
|
1476
|
+
if (!isPlainObject(obj)) {
|
|
1477
|
+
const err = new TypeError(XOBJ_TE_NPOBJ_EMSG);
|
|
1478
|
+
err.code = XOBJ_TE_NPOBJ_ECODE;
|
|
1479
|
+
throw err;
|
|
1480
|
+
};
|
|
1481
|
+
let result = null;
|
|
1482
|
+
let len = args.length;
|
|
1483
|
+
const opt = (
|
|
1484
|
+
len > 0 && isPlainObject(args[len - 1])
|
|
1485
|
+
? (len--, args.pop())
|
|
1486
|
+
: {}
|
|
1487
|
+
);
|
|
1488
|
+
if (len > 0) {
|
|
1489
|
+
let parent = obj;
|
|
1490
|
+
let child = null;
|
|
1491
|
+
let isSucceed = false;
|
|
1492
|
+
for (let key of args) {
|
|
1493
|
+
child = insertXObjElement(parent, key, opt);
|
|
1494
|
+
isSucceed = isPlainObject(child);
|
|
1495
|
+
if (!isSucceed) break;
|
|
1496
|
+
parent = child;
|
|
1497
|
+
};
|
|
1498
|
+
result = isSucceed ? parent : null;
|
|
1499
|
+
};
|
|
1500
|
+
return result;
|
|
1501
|
+
};
|
|
1502
|
+
|
|
1503
|
+
/***
|
|
1504
|
+
* (* class definitions *)
|
|
1505
|
+
*/
|
|
1506
|
+
|
|
1507
|
+
// === module exports block ===
|
|
1508
|
+
|
|
1509
|
+
module.exports.XOBJ_DEF_PARAM_TNAME = XOBJ_DEF_PARAM_TNAME;
|
|
1510
|
+
module.exports.XOBJ_DEF_ATTR_TNAME = XOBJ_DEF_ATTR_TNAME;
|
|
1511
|
+
|
|
1512
|
+
module.exports.readXObjParamRaw = readXObjParamRaw;
|
|
1513
|
+
module.exports.readXObjParam = readXObjParam;
|
|
1514
|
+
module.exports.readXObjParamAsBool = readXObjParamAsBool;
|
|
1515
|
+
module.exports.readXObjParamAsNum = readXObjParamAsNum;
|
|
1516
|
+
module.exports.readXObjParamEx = readXObjParamEx;
|
|
1517
|
+
module.exports.readXObjParamAsIndex = readXObjParamAsIndex;
|
|
1518
|
+
module.exports.writeXObjParamRaw = writeXObjParamRaw;
|
|
1519
|
+
module.exports.writeXObjParam = writeXObjParam;
|
|
1520
|
+
module.exports.writeXObjParamAsBool = writeXObjParamAsBool;
|
|
1521
|
+
module.exports.writeXObjParamAsNum = writeXObjParamAsNum;
|
|
1522
|
+
module.exports.writeXObjParamAsIndex = writeXObjParamAsIndex;
|
|
1523
|
+
module.exports.writeXObjParamEx = writeXObjParamEx;
|
|
1524
|
+
|
|
1525
|
+
module.exports.readXObjAttrRaw = readXObjAttrRaw;
|
|
1526
|
+
module.exports.readXObjAttr = readXObjAttr;
|
|
1527
|
+
module.exports.readXObjAttrAsBool = readXObjAttrAsBool;
|
|
1528
|
+
module.exports.readXObjAttrAsNum = readXObjAttrAsNum;
|
|
1529
|
+
module.exports.readXObjAttrEx = readXObjAttrEx;
|
|
1530
|
+
module.exports.readXObjAttrAsIndex = readXObjAttrAsIndex;
|
|
1531
|
+
module.exports.writeXObjAttrRaw = writeXObjAttrRaw;
|
|
1532
|
+
module.exports.writeXObjAttr = writeXObjAttr;
|
|
1533
|
+
module.exports.writeXObjAttrAsBool = writeXObjAttrAsBool;
|
|
1534
|
+
module.exports.writeXObjAttrAsNum = writeXObjAttrAsNum;
|
|
1535
|
+
module.exports.writeXObjAttrAsIndex = writeXObjAttrAsIndex;
|
|
1536
|
+
module.exports.writeXObjAttrEx = writeXObjAttrEx;
|
|
1537
|
+
|
|
1538
|
+
module.exports.getXObjAttributes = getXObjAttributes;
|
|
1539
|
+
module.exports.checkXObjAttribute = checkXObjAttribute;
|
|
1540
|
+
module.exports.deleteXObjAttribute = deleteXObjAttribute;
|
|
1541
|
+
module.exports.renameXObjAttribute = renameXObjAttribute;
|
|
1542
|
+
|
|
1543
|
+
module.exports.getXObjElement = getXObjElement;
|
|
1544
|
+
module.exports.addXObjElement = addXObjElement;
|
|
1545
|
+
module.exports.insertXObjElement = insertXObjElement;
|
|
1546
|
+
module.exports.insertXObjElementEx = insertXObjElementEx;
|
|
1547
|
+
module.exports.deleteXObjElement = deleteXObjElement;
|
|
1548
|
+
module.exports.deleteXObjElementEx = deleteXObjElementEx;
|
|
1549
|
+
module.exports.renameXObjElement = renameXObjElement;
|
|
1550
|
+
|
|
1551
|
+
module.exports.evalXObjEName = evalXObjEName;
|
|
1552
|
+
module.exports.insertXObjEList = insertXObjEList;
|
|
1553
|
+
module.exports.insertXObjEListEx = insertXObjEListEx;
|
|
1554
|
+
|
|
1555
|
+
/* inner */
|
|
1556
|
+
module.exports.evalKeyName = evalKeyName;
|
|
1557
|
+
|
|
1558
|
+
/* experimental */
|
|
1559
|
+
module.exports.genXObjENameDescr = genXObjENameDescr;
|
|
1560
|
+
/* experimental */
|
|
1561
|
+
module.exports.insertXObjElements = insertXObjElements;
|
|
1562
|
+
/* experimental */
|
|
1563
|
+
module.exports.insertXObjEChain = insertXObjEChain;
|
|
1564
|
+
|
|
1565
|
+
/** @deprecated */
|
|
1566
|
+
module.exports.readXObjParamAsStr = readXObjParamEx;
|
|
1567
|
+
/** @deprecated */
|
|
1568
|
+
module.exports.readXObjAttrAsStr = readXObjAttrEx;
|