@ygracs/xobj-lib-js 0.3.1 → 0.3.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 +6 -0
- package/index.d.ts +14 -8
- package/index.js +35 -33
- package/lib/xObj-attr.d.ts +97 -0
- package/lib/xObj-attr.js +591 -0
- package/lib/xObj-lib.d.ts +124 -245
- package/lib/xObj-lib.js +227 -1092
- package/lib/xObj-param.d.ts +69 -0
- package/lib/xObj-param.js +410 -0
- package/package.json +4 -2
package/lib/xObj-lib.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
// [v0.3.
|
|
1
|
+
// [v0.3.138-20260530]
|
|
2
2
|
|
|
3
3
|
// === module init block ===
|
|
4
4
|
|
|
5
5
|
const {
|
|
6
|
-
valueToIndex,
|
|
6
|
+
valueToIndex,
|
|
7
7
|
readAsString, readAsBoolEx, readAsNumberEx,
|
|
8
8
|
isNullOrUndef,
|
|
9
9
|
isInteger,
|
|
10
10
|
isArray, isObject, isPlainObject,
|
|
11
11
|
// * import types definitions *
|
|
12
|
-
|
|
12
|
+
IValueToStringTransformOptions,
|
|
13
13
|
} = require('@ygracs/bsfoc-lib-js');
|
|
14
14
|
|
|
15
15
|
const {
|
|
@@ -21,6 +21,8 @@ const {
|
|
|
21
21
|
XOBJ_DEF_TNAMES,
|
|
22
22
|
} = require('./xObj-defs');
|
|
23
23
|
|
|
24
|
+
// === module inner block ===
|
|
25
|
+
|
|
24
26
|
const {
|
|
25
27
|
XOBJ_TE_NSTR_EMSG,
|
|
26
28
|
XOBJ_TE_NSTR_ECODE,
|
|
@@ -32,10 +34,16 @@ const {
|
|
|
32
34
|
|
|
33
35
|
const {
|
|
34
36
|
XOBJ_DEF_PARAM_TNAME,
|
|
35
|
-
XOBJ_DEF_ATTR_TNAME,
|
|
36
37
|
} = XOBJ_DEF_TNAMES;
|
|
37
38
|
|
|
38
|
-
|
|
39
|
+
/**
|
|
40
|
+
* A result of a value check ops.
|
|
41
|
+
* @typedef {Object} RVAL_reason
|
|
42
|
+
* @property {string} code - message ID
|
|
43
|
+
* @property {string} msg - message text
|
|
44
|
+
* @deprecated
|
|
45
|
+
* @todo \[from v0.3.0] make obsolete. Use `IEvalKeyNameReasonOnFail` instead.
|
|
46
|
+
*/
|
|
39
47
|
|
|
40
48
|
/**
|
|
41
49
|
* An error description of a value check ops on fail.
|
|
@@ -47,10 +55,11 @@ const {
|
|
|
47
55
|
|
|
48
56
|
/**
|
|
49
57
|
* A result of a value check ops.
|
|
50
|
-
* @typedef {Object}
|
|
51
|
-
* @property {
|
|
52
|
-
* @property {string}
|
|
53
|
-
* @
|
|
58
|
+
* @typedef {Object} VCOR_evalkname
|
|
59
|
+
* @property {boolean} isSucceed - ops flag
|
|
60
|
+
* @property {string|IEvalKeyNameReasonOnFail} value - result value or reson if failed
|
|
61
|
+
* @deprecated
|
|
62
|
+
* @todo \[from v0.3.0] make obsolete. Use `IEvalKeyNameResult` instead.
|
|
54
63
|
*/
|
|
55
64
|
|
|
56
65
|
/**
|
|
@@ -79,22 +88,19 @@ const {
|
|
|
79
88
|
/**
|
|
80
89
|
* A result of `evalKeyName` function
|
|
81
90
|
* @since 0.3.0
|
|
82
|
-
* @typedef {
|
|
91
|
+
* @typedef {IEvalKeyNameResultOnSuccess|IEvalKeyNameResultOnFail} IEvalKeyNameResult
|
|
83
92
|
*/
|
|
84
|
-
|
|
85
93
|
/**
|
|
86
|
-
* A
|
|
87
|
-
* @
|
|
88
|
-
* @property {boolean} isSucceed - ops flag
|
|
89
|
-
* @property {(string|RVAL_reason)} value - result value or reson if failed
|
|
90
|
-
* @todo \[from v0.3.0] make obsolete. Use `IEvalKeyNameResult` instead.
|
|
94
|
+
* A virtual constant meant for support jsdoc notation:
|
|
95
|
+
* @type {IEvalKeyNameResult}
|
|
91
96
|
*/
|
|
97
|
+
module.exports.IEvalKeyNameResult = { isSucceed: true, value: '' };
|
|
92
98
|
|
|
93
99
|
/**
|
|
94
100
|
* Tries to convert a value into an ID.
|
|
95
101
|
* @function evalKeyName
|
|
96
102
|
* @param {any} value - key to validate
|
|
97
|
-
* @param {
|
|
103
|
+
* @param {boolean|IEvalKeyNameOptions} [opt=true] - some options
|
|
98
104
|
* @returns {IEvalKeyNameResult}
|
|
99
105
|
* @inner
|
|
100
106
|
*/
|
|
@@ -126,22 +132,15 @@ function evalKeyName(value, opt = true) {
|
|
|
126
132
|
value: key,
|
|
127
133
|
};
|
|
128
134
|
};
|
|
135
|
+
module.exports.evalKeyName = evalKeyName;
|
|
129
136
|
|
|
130
137
|
// === module main block ===
|
|
131
138
|
|
|
132
|
-
/***
|
|
133
|
-
* (* constant definitions *)
|
|
134
|
-
*/
|
|
135
|
-
|
|
136
|
-
/***
|
|
137
|
-
* (* function definitions *)
|
|
138
|
-
*/
|
|
139
|
-
|
|
140
139
|
/**
|
|
141
140
|
* Tries to convert a value into an ID.
|
|
142
141
|
* @function evalXObjEName
|
|
143
142
|
* @param {any} value - some value to evaluate
|
|
144
|
-
* @returns {
|
|
143
|
+
* @returns {null|number|string}
|
|
145
144
|
*/
|
|
146
145
|
function evalXObjEName(value) {
|
|
147
146
|
//return valueToIDString(value); // // TODO: [?]
|
|
@@ -188,7 +187,7 @@ function evalXObjEName(value) {
|
|
|
188
187
|
* A result of an element name pattern evaluation ops.
|
|
189
188
|
* @typedef {Object} XObjENameDescr
|
|
190
189
|
* @property {boolean} isERR - ops flag
|
|
191
|
-
* @property {
|
|
190
|
+
* @property {null|number|string} name - element name
|
|
192
191
|
* @property {?XObjENameDescrConditions} conditions - some conditions applied to an element
|
|
193
192
|
*/
|
|
194
193
|
|
|
@@ -250,6 +249,22 @@ function genXObjENameDescr(value) {
|
|
|
250
249
|
return result;
|
|
251
250
|
};
|
|
252
251
|
|
|
252
|
+
/**
|
|
253
|
+
*
|
|
254
|
+
* @since 0.3.2
|
|
255
|
+
* @typedef {Object<string, any>} INode
|
|
256
|
+
*/
|
|
257
|
+
/**
|
|
258
|
+
* A virtual constant meant for support jsdoc notation:
|
|
259
|
+
* @type {INode}
|
|
260
|
+
*/
|
|
261
|
+
module.exports.INode = {};
|
|
262
|
+
/**
|
|
263
|
+
* A list of a `INode`-elements
|
|
264
|
+
* @since 0.3.2
|
|
265
|
+
* @typedef {INode[]} INodeList
|
|
266
|
+
*/
|
|
267
|
+
|
|
253
268
|
/**
|
|
254
269
|
* Extracts an element from a given object by its key.
|
|
255
270
|
* @function getXObjElement
|
|
@@ -274,49 +289,58 @@ function getXObjElement(obj, name) {
|
|
|
274
289
|
const { code, msg } = key;
|
|
275
290
|
throw new EvalKeyNameError(msg, { code });
|
|
276
291
|
};
|
|
277
|
-
|
|
278
|
-
/**
|
|
279
|
-
* Extracts an attributes from a given object by its key.
|
|
280
|
-
* @function getXObjAttributes
|
|
281
|
-
* @param {object} obj - some object
|
|
282
|
-
* @param {string} [key=XOBJ_DEF_ATTR_TNAME] - some key
|
|
283
|
-
* @returns {?object}
|
|
284
|
-
* @throws {TypeError} if `obj` param is not an object
|
|
285
|
-
*/
|
|
286
|
-
function getXObjAttributes(obj, key = XOBJ_DEF_ATTR_TNAME) {
|
|
287
|
-
let result = null;
|
|
288
|
-
try {
|
|
289
|
-
result = getXObjElement(obj, key);
|
|
290
|
-
} catch (err) {
|
|
291
|
-
switch (err.code) {
|
|
292
|
-
case XOBJ_TE_NSTR_ECODE :
|
|
293
|
-
case XOBJ_TE_KNES_ECODE : {
|
|
294
|
-
break;
|
|
295
|
-
}
|
|
296
|
-
default: {
|
|
297
|
-
throw err;
|
|
298
|
-
}
|
|
299
|
-
};
|
|
300
|
-
};
|
|
301
|
-
return isPlainObject(result) ? result : null;
|
|
302
|
-
};
|
|
292
|
+
module.exports.getXObjElement = getXObjElement;
|
|
303
293
|
|
|
304
294
|
/**
|
|
305
295
|
* A result of an xObj modification ops
|
|
306
296
|
* @typedef {Object} RVAL_emodif
|
|
307
297
|
* @property {boolean} isSucceed - flag that indicates whether an ops is succeed or not
|
|
308
298
|
* @property {?(object|object[])} item - some element as a result
|
|
299
|
+
* @deprecated
|
|
300
|
+
* @todo \[from v0.3.2] make obsolete. Use `INodeModifResult` instead.
|
|
309
301
|
*/
|
|
310
302
|
|
|
311
303
|
/**
|
|
312
|
-
*
|
|
313
|
-
* @
|
|
304
|
+
* A result of an xObj modification ops if failed
|
|
305
|
+
* @since 0.3.2
|
|
306
|
+
* @typedef {Object} INodeModifResultOnFail
|
|
307
|
+
* @property {false} isSucceed - flag that indicates whether an ops is succeed or not
|
|
308
|
+
* @property {null} item - some element as a result
|
|
309
|
+
*/
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* A result of an xObj modification ops if succeed
|
|
313
|
+
* @since 0.3.2
|
|
314
|
+
* @typedef {Object} INodeModifResultOnSuccess
|
|
315
|
+
* @property {true} isSucceed - flag that indicates whether an ops is succeed or not
|
|
316
|
+
* @property {object|object[]} item - some element as a result
|
|
317
|
+
*/
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* A result of an xObj modification ops
|
|
321
|
+
* @since 0.3.2
|
|
322
|
+
* @typedef {INodeModifResultOnFail|INodeModifResultOnSuccess} INodeModifResult
|
|
323
|
+
*/
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* A virtual constant meant for support jsdoc notation:
|
|
327
|
+
* @type {INodeModifResult}
|
|
328
|
+
*/
|
|
329
|
+
module.exports.INodeModifResult = { isSucceed: false, item: null };
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
*
|
|
333
|
+
* @callback addXObjElementFn
|
|
314
334
|
* @param {object} obj - some object
|
|
315
335
|
* @param {string} name - some child element
|
|
316
336
|
* @returns {?object}
|
|
317
|
-
* @throws {TypeError} if `obj` param is not an object
|
|
318
337
|
* @see {@link addXObjElementEx} for param details
|
|
319
338
|
*/
|
|
339
|
+
/**
|
|
340
|
+
* Adds an element addressed by its key to a given object.
|
|
341
|
+
* @type {addXObjElementFn}
|
|
342
|
+
* @throws {TypeError} if `obj` param is not an object
|
|
343
|
+
*/
|
|
320
344
|
function addXObjElement(...args) {
|
|
321
345
|
let item = null;
|
|
322
346
|
try {
|
|
@@ -334,6 +358,7 @@ function addXObjElement(...args) {
|
|
|
334
358
|
};
|
|
335
359
|
return item;
|
|
336
360
|
};
|
|
361
|
+
module.exports.addXObjElement = addXObjElement;
|
|
337
362
|
|
|
338
363
|
/**
|
|
339
364
|
* Adds an element addressed by its key to a given object.
|
|
@@ -341,7 +366,7 @@ function addXObjElement(...args) {
|
|
|
341
366
|
* @function addXObjElementEx
|
|
342
367
|
* @param {object} obj - some object
|
|
343
368
|
* @param {string} name - some child element
|
|
344
|
-
* @returns {
|
|
369
|
+
* @returns {INodeModifResult}
|
|
345
370
|
* @throws {TypeError} if `obj` param is not an object
|
|
346
371
|
* @throws {EvalKeyNameError} if `name` param is not valid identifier
|
|
347
372
|
*/
|
|
@@ -354,6 +379,7 @@ function addXObjElementEx(obj, name) {
|
|
|
354
379
|
};
|
|
355
380
|
const { isSucceed, value: key } = evalKeyName(name);
|
|
356
381
|
if (isSucceed) {
|
|
382
|
+
/** @type {INode} */
|
|
357
383
|
const item = {};
|
|
358
384
|
let prop = obj[key];
|
|
359
385
|
let isSucceed = false;
|
|
@@ -390,15 +416,19 @@ module.exports.addXObjElementEx = addXObjElementEx;
|
|
|
390
416
|
*/
|
|
391
417
|
|
|
392
418
|
/**
|
|
393
|
-
*
|
|
394
|
-
* @
|
|
419
|
+
*
|
|
420
|
+
* @callback insertXObjElementFn
|
|
395
421
|
* @param {object} obj - some object
|
|
396
422
|
* @param {string} name - some child element
|
|
397
423
|
* @param {OPT_inselops_L} [opt] - options
|
|
398
424
|
* @returns {?object}
|
|
399
|
-
* @throws {TypeError} if `obj` param is not an object
|
|
400
425
|
* @see {@link insertXObjElementEx} for param details
|
|
401
426
|
*/
|
|
427
|
+
/**
|
|
428
|
+
* Inserts an element addressed by its key into a given object.
|
|
429
|
+
* @type {insertXObjElementFn}
|
|
430
|
+
* @throws {TypeError} if `obj` param is not an object
|
|
431
|
+
*/
|
|
402
432
|
function insertXObjElement(...args) {
|
|
403
433
|
let item = null;
|
|
404
434
|
try {
|
|
@@ -416,6 +446,7 @@ function insertXObjElement(...args) {
|
|
|
416
446
|
};
|
|
417
447
|
return item;
|
|
418
448
|
};
|
|
449
|
+
module.exports.insertXObjElement = insertXObjElement;
|
|
419
450
|
|
|
420
451
|
/**
|
|
421
452
|
* Inserts an element addressed by its key into a given object.
|
|
@@ -424,7 +455,7 @@ function insertXObjElement(...args) {
|
|
|
424
455
|
* @param {object} obj - some object
|
|
425
456
|
* @param {string} name - some child element
|
|
426
457
|
* @param {OPT_inselops_L} [opt] - options
|
|
427
|
-
* @returns {
|
|
458
|
+
* @returns {INodeModifResult}
|
|
428
459
|
* @throws {TypeError} if `obj` param is not an object
|
|
429
460
|
* @throws {EvalKeyNameError} if `name` param is not valid identifier
|
|
430
461
|
*/
|
|
@@ -437,6 +468,7 @@ function insertXObjElementEx(obj, name, opt) {
|
|
|
437
468
|
};
|
|
438
469
|
const { isSucceed, value: key } = evalKeyName(name);
|
|
439
470
|
if (isSucceed) {
|
|
471
|
+
/** @type {INode} */
|
|
440
472
|
const item = {};
|
|
441
473
|
let prop = obj[key];
|
|
442
474
|
let isSucceed = false;
|
|
@@ -469,16 +501,21 @@ function insertXObjElementEx(obj, name, opt) {
|
|
|
469
501
|
throw new EvalKeyNameError(msg, { code });
|
|
470
502
|
};
|
|
471
503
|
};
|
|
504
|
+
module.exports.insertXObjElementEx = insertXObjElementEx;
|
|
472
505
|
|
|
473
506
|
/**
|
|
474
|
-
*
|
|
475
|
-
* @
|
|
507
|
+
*
|
|
508
|
+
* @callback deleteXObjElementFn
|
|
476
509
|
* @param {object} obj - some object
|
|
477
510
|
* @param {string} name - some child element
|
|
478
511
|
* @returns {boolean}
|
|
479
|
-
* @throws {TypeError} if `obj` param is not an object
|
|
480
512
|
* @see {@link deleteXObjElementEx} for param details
|
|
481
513
|
*/
|
|
514
|
+
/**
|
|
515
|
+
* Deletes an element addressed by its key from a given object.
|
|
516
|
+
* @type {deleteXObjElementFn}
|
|
517
|
+
* @throws {TypeError} if `obj` param is not an object
|
|
518
|
+
*/
|
|
482
519
|
function deleteXObjElement(...args) {
|
|
483
520
|
let isSucceed = false;
|
|
484
521
|
try {
|
|
@@ -496,13 +533,14 @@ function deleteXObjElement(...args) {
|
|
|
496
533
|
};
|
|
497
534
|
return isSucceed;
|
|
498
535
|
};
|
|
536
|
+
module.exports.deleteXObjElement = deleteXObjElement;
|
|
499
537
|
|
|
500
538
|
/**
|
|
501
539
|
* Deletes an element addressed by its key from a given object.
|
|
502
540
|
* @function deleteXObjElementEx
|
|
503
541
|
* @param {object} obj - some object
|
|
504
542
|
* @param {string} name - some child element
|
|
505
|
-
* @returns {
|
|
543
|
+
* @returns {INodeModifResult}
|
|
506
544
|
* @throws {TypeError} if `obj` param is not an object
|
|
507
545
|
* @throws {EvalKeyNameError} if `name` param is not valid identifier
|
|
508
546
|
*/
|
|
@@ -528,6 +566,7 @@ function deleteXObjElementEx(obj, name) {
|
|
|
528
566
|
throw new EvalKeyNameError(msg, { code });
|
|
529
567
|
};
|
|
530
568
|
};
|
|
569
|
+
module.exports.deleteXObjElementEx = deleteXObjElementEx;
|
|
531
570
|
|
|
532
571
|
/**
|
|
533
572
|
* Renames an element addressed by its key.
|
|
@@ -573,1100 +612,196 @@ function renameXObjElement(obj, name = '', value = '') {
|
|
|
573
612
|
};
|
|
574
613
|
return isSucceed;
|
|
575
614
|
};
|
|
615
|
+
module.exports.renameXObjElement = renameXObjElement;
|
|
576
616
|
|
|
577
617
|
/**
|
|
578
|
-
*
|
|
579
|
-
* @function
|
|
580
|
-
* @param {object} obj
|
|
581
|
-
* @param {string}
|
|
582
|
-
* @param {
|
|
583
|
-
* @returns {
|
|
618
|
+
* Inserts an elements into a given object.
|
|
619
|
+
* @function insertXObjElements
|
|
620
|
+
* @param {object} obj - some object
|
|
621
|
+
* @param {...string} name - some child element name
|
|
622
|
+
* @param {OPT_inselops_L} [opt]
|
|
623
|
+
* @returns {number}
|
|
584
624
|
* @throws {TypeError} if `obj` param is not an object
|
|
585
|
-
* @
|
|
625
|
+
* @experimental
|
|
586
626
|
*/
|
|
587
|
-
function
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
627
|
+
function insertXObjElements(obj, ...args) {
|
|
628
|
+
if (!isPlainObject(obj)) {
|
|
629
|
+
throw new EvalKeyNameError(
|
|
630
|
+
XOBJ_TE_NPOBJ_EMSG,
|
|
631
|
+
{ code: XOBJ_TE_NPOBJ_ECODE },
|
|
632
|
+
);
|
|
593
633
|
};
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
634
|
+
let count = 0;
|
|
635
|
+
let len = args.length;
|
|
636
|
+
const opt = (
|
|
637
|
+
len > 0 && isPlainObject(args[len - 1])
|
|
638
|
+
? (len--, args.pop())
|
|
639
|
+
: {}
|
|
640
|
+
);
|
|
641
|
+
for (let key of args) {
|
|
642
|
+
if (insertXObjElement(obj, key, opt) !== null) count++;
|
|
601
643
|
};
|
|
602
|
-
return
|
|
644
|
+
return count;
|
|
603
645
|
};
|
|
604
646
|
|
|
605
647
|
/**
|
|
606
|
-
*
|
|
607
|
-
* @
|
|
648
|
+
* An options for an element insertion ops
|
|
649
|
+
* @typedef {Object} OPT_inselops_S
|
|
650
|
+
* @property {boolean} [force=false]
|
|
651
|
+
* @property {boolean} [ripOldies=false]
|
|
652
|
+
*/
|
|
653
|
+
|
|
654
|
+
/**
|
|
655
|
+
* Inserts a list elements into a given object.
|
|
656
|
+
* @function insertXObjEList
|
|
608
657
|
* @param {object} obj - some object
|
|
609
|
-
* @param {string}
|
|
610
|
-
* @param {
|
|
611
|
-
* @returns {
|
|
658
|
+
* @param {string} name - some child element name
|
|
659
|
+
* @param {OPT_inselops_S} [opt] - options
|
|
660
|
+
* @returns {?(object|object[])}
|
|
612
661
|
* @throws {TypeError} if `obj` param is not an object
|
|
613
|
-
* @throws {EvalKeyNameError} if `attr` param is not valid identifier
|
|
614
662
|
*/
|
|
615
|
-
function
|
|
616
|
-
let
|
|
663
|
+
function insertXObjEList(...args) {
|
|
664
|
+
let item = null;
|
|
617
665
|
try {
|
|
618
|
-
|
|
666
|
+
({ item } = insertXObjEListEx(...args));
|
|
619
667
|
} catch (err) {
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
isSucceed = delete _obj[name];
|
|
668
|
+
switch (err.code) {
|
|
669
|
+
case XOBJ_TE_NSTR_ECODE :
|
|
670
|
+
case XOBJ_TE_KNES_ECODE : {
|
|
671
|
+
break;
|
|
672
|
+
}
|
|
673
|
+
default: {
|
|
674
|
+
throw err;
|
|
675
|
+
}
|
|
629
676
|
};
|
|
630
|
-
return isSucceed;
|
|
631
|
-
} else {
|
|
632
|
-
const { code, msg } = name;
|
|
633
|
-
throw new EvalKeyNameError(msg, { code });
|
|
634
677
|
};
|
|
678
|
+
return item;
|
|
635
679
|
};
|
|
636
680
|
|
|
637
681
|
/**
|
|
638
|
-
*
|
|
682
|
+
* Inserts a list elements into a given object.
|
|
639
683
|
* @since 0.2.0
|
|
640
|
-
* @function
|
|
684
|
+
* @function insertXObjEListEx
|
|
641
685
|
* @param {object} obj - some object
|
|
642
|
-
* @param {string}
|
|
643
|
-
* @param {
|
|
644
|
-
* @
|
|
645
|
-
* @returns {boolean}
|
|
686
|
+
* @param {string} name - some child element name
|
|
687
|
+
* @param {OPT_inselops_S} [opt] - options
|
|
688
|
+
* @returns {INodeModifResult}
|
|
646
689
|
* @throws {TypeError} if `obj` param is not an object
|
|
647
|
-
* @throws {EvalKeyNameError} if `
|
|
648
|
-
* @throws {EvalKeyNameError} if `value` param is not valid identifier
|
|
649
|
-
* @throws {EvalKeyNameError} if `key` param is not valid identifier
|
|
690
|
+
* @throws {EvalKeyNameError} if `name` param is not valid identifier
|
|
650
691
|
*/
|
|
651
|
-
function
|
|
692
|
+
function insertXObjEListEx(obj, name, opt) {
|
|
652
693
|
if (!isPlainObject(obj)) {
|
|
653
694
|
throw new EvalKeyNameError(
|
|
654
695
|
XOBJ_TE_NPOBJ_EMSG,
|
|
655
696
|
{ code: XOBJ_TE_NPOBJ_ECODE },
|
|
656
697
|
);
|
|
657
698
|
};
|
|
658
|
-
const
|
|
659
|
-
const { isSucceed, value: _key } = evalKeyName(key, opt);
|
|
699
|
+
const { isSucceed, value: key } = evalKeyName(name);
|
|
660
700
|
if (isSucceed) {
|
|
701
|
+
/** @type {OPT_inselops_S} */
|
|
702
|
+
let {
|
|
703
|
+
force,
|
|
704
|
+
ripOldies,
|
|
705
|
+
} = isPlainObject(opt) ? opt : {};
|
|
706
|
+
if (typeof force !== 'boolean') force = false;
|
|
707
|
+
if (typeof ripOldies !== 'boolean') ripOldies = false;
|
|
708
|
+
let prop = obj[key];
|
|
661
709
|
let isSucceed = false;
|
|
662
|
-
if (
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
throw new EvalKeyNameError(msg, { code });
|
|
674
|
-
};
|
|
675
|
-
if (oname !== '' && oname in _obj && nname !== '') {
|
|
676
|
-
if (oname !== nname) {
|
|
677
|
-
_obj = Object.entries(_obj);
|
|
678
|
-
const index = _obj.findIndex((item) => item[0] === oname);
|
|
679
|
-
_obj[index][0] = nname;
|
|
680
|
-
obj[_key] = Object.fromEntries(_obj);
|
|
681
|
-
};
|
|
682
|
-
isSucceed = true;
|
|
683
|
-
};
|
|
710
|
+
if (
|
|
711
|
+
isNullOrUndef(prop)
|
|
712
|
+
|| (force && (ripOldies || !isObject(prop)))
|
|
713
|
+
) {
|
|
714
|
+
obj[key] = prop = [];
|
|
715
|
+
isSucceed = true;
|
|
716
|
+
} else {
|
|
717
|
+
if (isObject(prop)) {
|
|
718
|
+
if (!isArray(prop)) prop = [ prop ];
|
|
719
|
+
obj[key] = prop;
|
|
720
|
+
isSucceed = true;
|
|
684
721
|
};
|
|
685
722
|
};
|
|
686
|
-
return
|
|
723
|
+
return {
|
|
724
|
+
isSucceed,
|
|
725
|
+
item: isSucceed ? prop : null,
|
|
726
|
+
};
|
|
687
727
|
} else {
|
|
688
|
-
const { code, msg } =
|
|
728
|
+
const { code, msg } = key;
|
|
689
729
|
throw new EvalKeyNameError(msg, { code });
|
|
690
730
|
};
|
|
691
731
|
};
|
|
692
732
|
|
|
693
733
|
/**
|
|
694
|
-
*
|
|
695
|
-
* @function
|
|
734
|
+
* Inserts a chain of an elements into a given object.
|
|
735
|
+
* @function insertXObjEChain
|
|
696
736
|
* @param {object} obj - some object
|
|
697
|
-
* @param {string}
|
|
698
|
-
* @
|
|
737
|
+
* @param {...string} name - some child element name
|
|
738
|
+
* @param {object} [opt] - options
|
|
739
|
+
* @returns {?any}
|
|
699
740
|
* @throws {TypeError} if `obj` param is not an object
|
|
700
|
-
* @
|
|
741
|
+
* @experimental
|
|
701
742
|
*/
|
|
702
|
-
function
|
|
743
|
+
function insertXObjEChain(obj, ...args) {
|
|
703
744
|
if (!isPlainObject(obj)) {
|
|
704
745
|
throw new EvalKeyNameError(
|
|
705
746
|
XOBJ_TE_NPOBJ_EMSG,
|
|
706
747
|
{ code: XOBJ_TE_NPOBJ_ECODE },
|
|
707
748
|
);
|
|
708
749
|
};
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
750
|
+
let result = null;
|
|
751
|
+
let len = args.length;
|
|
752
|
+
const opt = (
|
|
753
|
+
len > 0 && isPlainObject(args[len - 1])
|
|
754
|
+
? (len--, args.pop())
|
|
755
|
+
: {}
|
|
756
|
+
);
|
|
757
|
+
if (len > 0) {
|
|
758
|
+
let parent = obj;
|
|
759
|
+
let child = null;
|
|
760
|
+
let isSucceed = false;
|
|
761
|
+
for (let key of args) {
|
|
762
|
+
child = insertXObjElement(parent, key, opt);
|
|
763
|
+
isSucceed = isPlainObject(child);
|
|
764
|
+
if (!isSucceed) break;
|
|
765
|
+
parent = child;
|
|
766
|
+
};
|
|
767
|
+
result = isSucceed ? parent : null;
|
|
713
768
|
};
|
|
714
|
-
return
|
|
769
|
+
return result;
|
|
715
770
|
};
|
|
716
771
|
|
|
717
772
|
/**
|
|
718
|
-
*
|
|
719
|
-
* @
|
|
720
|
-
* @param {object} obj - some object
|
|
721
|
-
* @param {any} value - some value
|
|
722
|
-
* @param {string} [key=XOBJ_DEF_PARAM_TNAME] - some key
|
|
773
|
+
* Clears a given node
|
|
774
|
+
* @param {object|object[]} node - some node
|
|
723
775
|
* @returns {boolean}
|
|
724
|
-
* @throws {TypeError} if `
|
|
725
|
-
* @throws {EvalKeyNameError} if `key` param is not valid identifier
|
|
776
|
+
* @throws {TypeError} if `node`-param is not an object or array
|
|
726
777
|
*/
|
|
727
|
-
function
|
|
728
|
-
if (
|
|
778
|
+
function clearXObjNode(node) {
|
|
779
|
+
if (isObject(node)) {
|
|
780
|
+
if (isArray(node)) {
|
|
781
|
+
node.length = 0;
|
|
782
|
+
return true;
|
|
783
|
+
};
|
|
784
|
+
let isSucceed = false;
|
|
785
|
+
for (const key in node) {
|
|
786
|
+
// // TODO: catch errors in strict mode
|
|
787
|
+
isSucceed = delete node[key];
|
|
788
|
+
};
|
|
789
|
+
return isSucceed;
|
|
790
|
+
} else {
|
|
729
791
|
throw new EvalKeyNameError(
|
|
730
792
|
XOBJ_TE_NPOBJ_EMSG,
|
|
731
793
|
{ code: XOBJ_TE_NPOBJ_ECODE },
|
|
732
794
|
);
|
|
733
795
|
};
|
|
734
|
-
const { isSucceed, value: _key } = evalKeyName(key, false);
|
|
735
|
-
if (isSucceed) {
|
|
736
|
-
if (
|
|
737
|
-
_key === ''
|
|
738
|
-
|| value === undefined
|
|
739
|
-
|| isObject(value)
|
|
740
|
-
|| typeof value === 'function'
|
|
741
|
-
) {
|
|
742
|
-
return false;
|
|
743
|
-
};
|
|
744
|
-
obj[_key] = value;
|
|
745
|
-
return true;
|
|
746
|
-
} else {
|
|
747
|
-
const { code, msg } = _key;
|
|
748
|
-
throw new EvalKeyNameError(msg, { code });
|
|
749
|
-
};
|
|
750
796
|
};
|
|
751
|
-
|
|
752
|
-
/**
|
|
753
|
-
* Extracts an attribute 'AS IS' from a given object.
|
|
754
|
-
* @function readXObjAttrRaw
|
|
755
|
-
* @param {object} obj - some object
|
|
756
|
-
* @param {string} attr - some attribute
|
|
757
|
-
* @param {string} [key] - some key
|
|
758
|
-
* @returns {any}
|
|
759
|
-
* @throws {TypeError} if `obj` param is not an object
|
|
760
|
-
* @throws {EvalKeyNameError} if `attr` param is not valid identifier
|
|
761
|
-
*/
|
|
762
|
-
function readXObjAttrRaw(obj, attr = '', key) {
|
|
763
|
-
let _obj = null;
|
|
764
|
-
try {
|
|
765
|
-
_obj = getXObjAttributes(obj, key);
|
|
766
|
-
} catch (err) {
|
|
767
|
-
throw err;
|
|
768
|
-
};
|
|
769
|
-
if (_obj !== null) {
|
|
770
|
-
const { isSucceed, value: name } = evalKeyName(attr, false);
|
|
771
|
-
if (!isSucceed) {
|
|
772
|
-
const { code, msg } = name;
|
|
773
|
-
throw new EvalKeyNameError(msg, { code });
|
|
774
|
-
};
|
|
775
|
-
if (name !== '') return _obj[name];
|
|
776
|
-
};
|
|
777
|
-
};
|
|
778
|
-
|
|
779
|
-
/**
|
|
780
|
-
* Writes a parameter into a given object.
|
|
781
|
-
* @function writeXObjAttrRaw
|
|
782
|
-
* @param {object} obj - some object
|
|
783
|
-
* @param {string} attr - some attribute
|
|
784
|
-
* @param {any} value - some value
|
|
785
|
-
* @param {string} [key=XOBJ_DEF_ATTR_TNAME] - some key
|
|
786
|
-
* @returns {boolean}
|
|
787
|
-
* @throws {TypeError} if `obj` param is not an object
|
|
788
|
-
* @throws {EvalKeyNameError} if `attr` param is not valid identifier
|
|
789
|
-
*/
|
|
790
|
-
function writeXObjAttrRaw(obj, attr = '', value, key = XOBJ_DEF_ATTR_TNAME) {
|
|
791
|
-
const { isSucceed, value: name } = evalKeyName(attr, false);
|
|
792
|
-
if (isSucceed) {
|
|
793
|
-
let isSucceed = false;
|
|
794
|
-
if (
|
|
795
|
-
name !== ''
|
|
796
|
-
&& value !== undefined
|
|
797
|
-
&& !isObject(value)
|
|
798
|
-
&& typeof value !== 'function'
|
|
799
|
-
) {
|
|
800
|
-
let _obj = null;
|
|
801
|
-
try {
|
|
802
|
-
const opt = { force: true, acceptIfList: true };
|
|
803
|
-
_obj = insertXObjElement(obj, key, opt);
|
|
804
|
-
} catch (err) {
|
|
805
|
-
throw err;
|
|
806
|
-
};
|
|
807
|
-
if (isArray(_obj)) {
|
|
808
|
-
// force a replacement of the old element if it's array
|
|
809
|
-
try {
|
|
810
|
-
const opt = { force: true, ripOldies: true };
|
|
811
|
-
_obj = insertXObjElement(obj, key, opt);
|
|
812
|
-
} catch (err) {
|
|
813
|
-
throw err;
|
|
814
|
-
};
|
|
815
|
-
};
|
|
816
|
-
if (_obj !== null) {
|
|
817
|
-
_obj[name] = value;
|
|
818
|
-
isSucceed = true;
|
|
819
|
-
};
|
|
820
|
-
};
|
|
821
|
-
return isSucceed;
|
|
822
|
-
} else {
|
|
823
|
-
const { code, msg } = name;
|
|
824
|
-
throw new EvalKeyNameError(msg, { code });
|
|
825
|
-
};
|
|
826
|
-
};
|
|
827
|
-
|
|
828
|
-
/**
|
|
829
|
-
* Extracts a parameter from a given object and returns it as a string.
|
|
830
|
-
* @function readXObjParam
|
|
831
|
-
* @param {object} obj - some object
|
|
832
|
-
* @param {string} [key] - some key
|
|
833
|
-
* @returns {string}
|
|
834
|
-
* @throws {TypeError} if `obj` param is not an object
|
|
835
|
-
*/
|
|
836
|
-
function readXObjParam(obj, key) {
|
|
837
|
-
const opt = {
|
|
838
|
-
useTrim: false,
|
|
839
|
-
numberToString: true,
|
|
840
|
-
boolToString: true,
|
|
841
|
-
defValue: '',
|
|
842
|
-
};
|
|
843
|
-
let result = undefined;
|
|
844
|
-
try {
|
|
845
|
-
result = readXObjParamEx(obj, opt, key);
|
|
846
|
-
} catch (err) {
|
|
847
|
-
throw err;
|
|
848
|
-
};
|
|
849
|
-
return result;
|
|
850
|
-
};
|
|
851
|
-
|
|
852
|
-
/**
|
|
853
|
-
* Extracts a parameter from a given object and returns it as a boolean.
|
|
854
|
-
* @function readXObjParamAsBool
|
|
855
|
-
* @param {object} obj - some object
|
|
856
|
-
* @param {boolean} [defValue] - default value
|
|
857
|
-
* @param {string} [key] - some key
|
|
858
|
-
* @returns {boolean}
|
|
859
|
-
* @throws {TypeError} if `obj` param is not an object
|
|
860
|
-
* @see readAsBoolEx from `@ygracs/bsfoc-lib-js` on details for result
|
|
861
|
-
*/
|
|
862
|
-
function readXObjParamAsBool(obj, defValue, key) {
|
|
863
|
-
let result = undefined;
|
|
864
|
-
try {
|
|
865
|
-
result = readXObjParamRaw(obj, key);
|
|
866
|
-
} catch (err) {
|
|
867
|
-
switch (err.code) {
|
|
868
|
-
case XOBJ_TE_NSTR_ECODE : {
|
|
869
|
-
break;
|
|
870
|
-
}
|
|
871
|
-
default: {
|
|
872
|
-
throw err;
|
|
873
|
-
}
|
|
874
|
-
};
|
|
875
|
-
};
|
|
876
|
-
return readAsBoolEx(result, defValue);
|
|
877
|
-
};
|
|
878
|
-
module.exports.readXObjParamAsBool = readXObjParamAsBool;
|
|
879
|
-
|
|
880
|
-
/**
|
|
881
|
-
* Extracts a parameter from a given object and returns it as a number.
|
|
882
|
-
* @function readXObjParamAsNum
|
|
883
|
-
* @param {object} obj - some object
|
|
884
|
-
* @param {number} [defValue] - default value
|
|
885
|
-
* @param {string} [key] - some key
|
|
886
|
-
* @returns {number}
|
|
887
|
-
* @throws {TypeError} if `obj` param is not an object
|
|
888
|
-
* @see readAsNumberEx from `@ygracs/bsfoc-lib-js` on details for result
|
|
889
|
-
*/
|
|
890
|
-
function readXObjParamAsNum(obj, defValue, key) {
|
|
891
|
-
let result = undefined;
|
|
892
|
-
try {
|
|
893
|
-
result = readXObjParamRaw(obj, key);
|
|
894
|
-
} catch (err) {
|
|
895
|
-
switch (err.code) {
|
|
896
|
-
case XOBJ_TE_NSTR_ECODE : {
|
|
897
|
-
break;
|
|
898
|
-
}
|
|
899
|
-
default: {
|
|
900
|
-
throw err;
|
|
901
|
-
}
|
|
902
|
-
};
|
|
903
|
-
};
|
|
904
|
-
return readAsNumberEx(result, defValue);
|
|
905
|
-
};
|
|
906
|
-
module.exports.readXObjParamAsNum = readXObjParamAsNum;
|
|
907
|
-
|
|
908
|
-
/**
|
|
909
|
-
* Extracts a parameter from a given object and returns it as 'index' value.
|
|
910
|
-
* @function readXObjParamAsIndex
|
|
911
|
-
* @param {object} obj - some object
|
|
912
|
-
* @param {string} [key] - some key
|
|
913
|
-
* @returns {number}
|
|
914
|
-
* @throws {TypeError} if `obj` param is not an object
|
|
915
|
-
*/
|
|
916
|
-
function readXObjParamAsIndex(obj, key) {
|
|
917
|
-
let result = undefined;
|
|
918
|
-
try {
|
|
919
|
-
result = readXObjParamRaw(obj, key);
|
|
920
|
-
} catch (err) {
|
|
921
|
-
switch (err.code) {
|
|
922
|
-
case XOBJ_TE_NSTR_ECODE : {
|
|
923
|
-
break;
|
|
924
|
-
}
|
|
925
|
-
default: {
|
|
926
|
-
throw err;
|
|
927
|
-
}
|
|
928
|
-
};
|
|
929
|
-
};
|
|
930
|
-
return valueToIndex(result);
|
|
931
|
-
};
|
|
932
|
-
module.exports.readXObjParamAsIndex = readXObjParamAsIndex;
|
|
933
|
-
|
|
934
|
-
/**
|
|
935
|
-
* Extracts a parameter from a given object and returns it as a string.
|
|
936
|
-
* @function readXObjParamEx
|
|
937
|
-
* @param {object} obj - some object
|
|
938
|
-
* @param {object} [opt] - options
|
|
939
|
-
* @param {string} [key] - some key
|
|
940
|
-
* @returns {string}
|
|
941
|
-
* @throws {TypeError} if `obj` param is not an object
|
|
942
|
-
* @todo [since `v0.2.1`] deprecate use of `opt` as `string`
|
|
943
|
-
*/
|
|
944
|
-
function readXObjParamEx(obj, opt, key) {
|
|
945
|
-
let result = undefined;
|
|
946
|
-
try {
|
|
947
|
-
result = readXObjParamRaw(obj, key);
|
|
948
|
-
} catch (err) {
|
|
949
|
-
switch (err.code) {
|
|
950
|
-
case XOBJ_TE_NSTR_ECODE : {
|
|
951
|
-
break;
|
|
952
|
-
}
|
|
953
|
-
default: {
|
|
954
|
-
throw err;
|
|
955
|
-
}
|
|
956
|
-
};
|
|
957
|
-
};
|
|
958
|
-
/** @type {OPT_valtostr} */
|
|
959
|
-
const _opt = isPlainObject(opt) ? opt : {
|
|
960
|
-
useTrim: false,
|
|
961
|
-
numberToString: true,
|
|
962
|
-
boolToString: true,
|
|
963
|
-
defValue: opt,
|
|
964
|
-
};
|
|
965
|
-
return readAsString(result, _opt);
|
|
966
|
-
};
|
|
967
|
-
|
|
968
|
-
/**
|
|
969
|
-
* Tries to convert a given value to a string and writes it as a parameter
|
|
970
|
-
* into a given object.
|
|
971
|
-
* @function writeXObjParam
|
|
972
|
-
* @param {object} obj - some object
|
|
973
|
-
* @param {any} value - some value
|
|
974
|
-
* @param {string} [key] - some key
|
|
975
|
-
* @returns {boolean}
|
|
976
|
-
* @throws {TypeError} if `obj` param is not an object
|
|
977
|
-
*/
|
|
978
|
-
function writeXObjParam(obj, value, key) {
|
|
979
|
-
let isSucceed = false;
|
|
980
|
-
try {
|
|
981
|
-
const opt = {
|
|
982
|
-
useTrim: false,
|
|
983
|
-
numberToString: true,
|
|
984
|
-
boolToString: true,
|
|
985
|
-
defValue: '',
|
|
986
|
-
};
|
|
987
|
-
isSucceed = writeXObjParamEx(obj, value, opt, key);
|
|
988
|
-
} catch (err) {
|
|
989
|
-
throw err;
|
|
990
|
-
};
|
|
991
|
-
return isSucceed;
|
|
992
|
-
};
|
|
993
|
-
|
|
994
|
-
/**
|
|
995
|
-
* Tries to convert a given value to a boolean and writes it as a parameter
|
|
996
|
-
* into a given object.
|
|
997
|
-
* @function writeXObjParamAsBool
|
|
998
|
-
* @param {object} obj - some object
|
|
999
|
-
* @param {any} value - some value
|
|
1000
|
-
* @param {boolean} [defValue] - default value
|
|
1001
|
-
* @param {string} [key] - some key
|
|
1002
|
-
* @returns {boolean}
|
|
1003
|
-
* @throws {TypeError} if `obj` param is not an object
|
|
1004
|
-
*/
|
|
1005
|
-
function writeXObjParamAsBool(obj, value, defValue, key) {
|
|
1006
|
-
let isSucceed = false;
|
|
1007
|
-
if (value !== undefined || typeof defValue === 'boolean') {
|
|
1008
|
-
const _value = readAsBoolEx(value, defValue).toString();
|
|
1009
|
-
try {
|
|
1010
|
-
isSucceed = writeXObjParamRaw(obj, _value, key);
|
|
1011
|
-
} catch (err) {
|
|
1012
|
-
switch (err.code) {
|
|
1013
|
-
case XOBJ_TE_NSTR_ECODE : {
|
|
1014
|
-
break;
|
|
1015
|
-
}
|
|
1016
|
-
default: {
|
|
1017
|
-
throw err;
|
|
1018
|
-
}
|
|
1019
|
-
};
|
|
1020
|
-
};
|
|
1021
|
-
};
|
|
1022
|
-
return isSucceed;
|
|
1023
|
-
};
|
|
1024
|
-
module.exports.writeXObjParamAsBool = writeXObjParamAsBool;
|
|
1025
|
-
|
|
1026
|
-
/**
|
|
1027
|
-
* Tries to convert a given value to a number and writes it as a parameter
|
|
1028
|
-
* into a given object.
|
|
1029
|
-
* @function writeXObjParamAsNum
|
|
1030
|
-
* @param {object} obj - some object
|
|
1031
|
-
* @param {any} value - some value
|
|
1032
|
-
* @param {number} [defValue] - default value
|
|
1033
|
-
* @param {string} [key] - some key
|
|
1034
|
-
* @returns {boolean}
|
|
1035
|
-
* @throws {TypeError} if `obj` param is not an object
|
|
1036
|
-
*/
|
|
1037
|
-
function writeXObjParamAsNum(obj, value, defValue, key) {
|
|
1038
|
-
let isSucceed = false;
|
|
1039
|
-
if (value !== undefined || typeof defValue === 'number') {
|
|
1040
|
-
const _value = readAsNumberEx(value, defValue).toString();
|
|
1041
|
-
try {
|
|
1042
|
-
isSucceed = writeXObjParamRaw(obj, _value, key);
|
|
1043
|
-
} catch (err) {
|
|
1044
|
-
switch (err.code) {
|
|
1045
|
-
case XOBJ_TE_NSTR_ECODE : {
|
|
1046
|
-
break;
|
|
1047
|
-
}
|
|
1048
|
-
default: {
|
|
1049
|
-
throw err;
|
|
1050
|
-
}
|
|
1051
|
-
};
|
|
1052
|
-
};
|
|
1053
|
-
};
|
|
1054
|
-
return isSucceed;
|
|
1055
|
-
};
|
|
1056
|
-
module.exports.writeXObjParamAsNum = writeXObjParamAsNum;
|
|
1057
|
-
|
|
1058
|
-
/**
|
|
1059
|
-
* Tries to convert a given value into an 'index' value and writes it
|
|
1060
|
-
* as a parameter into a given object.
|
|
1061
|
-
* @function writeXObjParamAsIndex
|
|
1062
|
-
* @param {object} obj - some object
|
|
1063
|
-
* @param {any} value - some value
|
|
1064
|
-
* @param {string} [key] - some key
|
|
1065
|
-
* @returns {boolean}
|
|
1066
|
-
* @throws {TypeError} if `obj` param is not an object
|
|
1067
|
-
*/
|
|
1068
|
-
function writeXObjParamAsIndex(obj, value, key) {
|
|
1069
|
-
let isSucceed = false;
|
|
1070
|
-
if (value !== undefined) {
|
|
1071
|
-
const _value = valueToIndex(value).toString();
|
|
1072
|
-
try {
|
|
1073
|
-
isSucceed = writeXObjParamRaw(obj, _value, key);
|
|
1074
|
-
} catch (err) {
|
|
1075
|
-
switch (err.code) {
|
|
1076
|
-
case XOBJ_TE_NSTR_ECODE : {
|
|
1077
|
-
break;
|
|
1078
|
-
}
|
|
1079
|
-
default: {
|
|
1080
|
-
throw err;
|
|
1081
|
-
}
|
|
1082
|
-
};
|
|
1083
|
-
};
|
|
1084
|
-
};
|
|
1085
|
-
return isSucceed;
|
|
1086
|
-
};
|
|
1087
|
-
module.exports.writeXObjParamAsIndex = writeXObjParamAsIndex;
|
|
1088
|
-
|
|
1089
|
-
/**
|
|
1090
|
-
* Tries to convert a given value to a string and writes it
|
|
1091
|
-
* as a parameter into a given object.
|
|
1092
|
-
* @function writeXObjParamEx
|
|
1093
|
-
* @param {object} obj - some object
|
|
1094
|
-
* @param {any} value - some value
|
|
1095
|
-
* @param {object} [opt] - options
|
|
1096
|
-
* @param {string} [key] - some key
|
|
1097
|
-
* @returns {boolean}
|
|
1098
|
-
* @throws {TypeError} if `obj` param is not an object
|
|
1099
|
-
* @todo [since `v0.2.1`] deprecate use of `opt` as `string`
|
|
1100
|
-
*/
|
|
1101
|
-
function writeXObjParamEx(obj, value, opt, key) {
|
|
1102
|
-
let isSucceed = false;
|
|
1103
|
-
if (value !== undefined && typeof value !== 'function') {
|
|
1104
|
-
let _opt = opt;
|
|
1105
|
-
if (!isPlainObject(_opt)) {
|
|
1106
|
-
const defValue = readAsString(_opt, {
|
|
1107
|
-
useTrim: false,
|
|
1108
|
-
numberToString: true,
|
|
1109
|
-
boolToString: true,
|
|
1110
|
-
});
|
|
1111
|
-
_opt = {
|
|
1112
|
-
useTrim: false,
|
|
1113
|
-
numberToString: true,
|
|
1114
|
-
boolToString: true,
|
|
1115
|
-
defValue,
|
|
1116
|
-
};
|
|
1117
|
-
};
|
|
1118
|
-
const _value = readAsString(value, _opt);
|
|
1119
|
-
try {
|
|
1120
|
-
isSucceed = writeXObjParamRaw(obj, _value, key);
|
|
1121
|
-
} catch (err) {
|
|
1122
|
-
switch (err.code) {
|
|
1123
|
-
case XOBJ_TE_NSTR_ECODE : {
|
|
1124
|
-
break;
|
|
1125
|
-
}
|
|
1126
|
-
default: {
|
|
1127
|
-
throw err;
|
|
1128
|
-
}
|
|
1129
|
-
};
|
|
1130
|
-
};
|
|
1131
|
-
};
|
|
1132
|
-
return isSucceed;
|
|
1133
|
-
};
|
|
1134
|
-
|
|
1135
|
-
/**
|
|
1136
|
-
* Extracts an attribute from a given object and returns it as a string.
|
|
1137
|
-
* @function readXObjAttr
|
|
1138
|
-
* @param {object} obj - some object
|
|
1139
|
-
* @param {string} attr - some attribute
|
|
1140
|
-
* @param {string} [key] - some key
|
|
1141
|
-
* @returns {string}
|
|
1142
|
-
* @throws {TypeError} if `obj` param is not an object
|
|
1143
|
-
*/
|
|
1144
|
-
function readXObjAttr(obj, attr, key) {
|
|
1145
|
-
const opt = {
|
|
1146
|
-
useTrim: true,
|
|
1147
|
-
numberToString: true,
|
|
1148
|
-
boolToString: true,
|
|
1149
|
-
defValue: '',
|
|
1150
|
-
};
|
|
1151
|
-
let result = undefined;
|
|
1152
|
-
try {
|
|
1153
|
-
result = readXObjAttrEx(obj, attr, opt, key);
|
|
1154
|
-
} catch (err) {
|
|
1155
|
-
throw err;
|
|
1156
|
-
};
|
|
1157
|
-
return result;
|
|
1158
|
-
};
|
|
1159
|
-
|
|
1160
|
-
/**
|
|
1161
|
-
* Extracts an attribute from a given object and returns it as a boolean.
|
|
1162
|
-
* @function readXObjAttrAsBool
|
|
1163
|
-
* @param {object} obj - some object
|
|
1164
|
-
* @param {string} attr - some attribute
|
|
1165
|
-
* @param {boolean} [defValue] - default value
|
|
1166
|
-
* @param {string} [key] - some key
|
|
1167
|
-
* @returns {boolean}
|
|
1168
|
-
* @throws {TypeError} if `obj` param is not an object
|
|
1169
|
-
* @see readAsBoolEx from `@ygracs/bsfoc-lib-js` on details for result
|
|
1170
|
-
*/
|
|
1171
|
-
function readXObjAttrAsBool(obj, attr, defValue, key) {
|
|
1172
|
-
let result = undefined;
|
|
1173
|
-
try {
|
|
1174
|
-
result = readXObjAttrRaw(obj, attr, key);
|
|
1175
|
-
} catch (err) {
|
|
1176
|
-
switch (err.code) {
|
|
1177
|
-
case XOBJ_TE_NSTR_ECODE : {
|
|
1178
|
-
break;
|
|
1179
|
-
}
|
|
1180
|
-
default: {
|
|
1181
|
-
throw err;
|
|
1182
|
-
}
|
|
1183
|
-
};
|
|
1184
|
-
};
|
|
1185
|
-
return readAsBoolEx(result, defValue);
|
|
1186
|
-
};
|
|
1187
|
-
module.exports.readXObjAttrAsBool = readXObjAttrAsBool;
|
|
1188
|
-
|
|
1189
|
-
/**
|
|
1190
|
-
* Extracts an attribute from a given object and returns it as a number.
|
|
1191
|
-
* @function readXObjAttrAsNum
|
|
1192
|
-
* @param {object} obj - some object
|
|
1193
|
-
* @param {string} attr - some attribute
|
|
1194
|
-
* @param {number} [defValue] - default value
|
|
1195
|
-
* @param {string} [key] - some key
|
|
1196
|
-
* @returns {number}
|
|
1197
|
-
* @throws {TypeError} if `obj` param is not an object
|
|
1198
|
-
* @see readAsNumberEx from `@ygracs/bsfoc-lib-js` on details for result
|
|
1199
|
-
*/
|
|
1200
|
-
function readXObjAttrAsNum(obj, attr, defValue, key) {
|
|
1201
|
-
let result = undefined;
|
|
1202
|
-
try {
|
|
1203
|
-
result = readXObjAttrRaw(obj, attr, key);
|
|
1204
|
-
} catch (err) {
|
|
1205
|
-
switch (err.code) {
|
|
1206
|
-
case XOBJ_TE_NSTR_ECODE : {
|
|
1207
|
-
break;
|
|
1208
|
-
}
|
|
1209
|
-
default: {
|
|
1210
|
-
throw err;
|
|
1211
|
-
}
|
|
1212
|
-
};
|
|
1213
|
-
};
|
|
1214
|
-
return readAsNumberEx(result, defValue);
|
|
1215
|
-
};
|
|
1216
|
-
module.exports.readXObjAttrAsNum = readXObjAttrAsNum;
|
|
1217
|
-
|
|
1218
|
-
/**
|
|
1219
|
-
* Extracts an attribute from a given object and returns it as 'index' value.
|
|
1220
|
-
* @function readXObjAttrAsIndex
|
|
1221
|
-
* @param {object} obj - some object
|
|
1222
|
-
* @param {string} attr - some attribute
|
|
1223
|
-
* @param {string} [key] - some key
|
|
1224
|
-
* @returns {number}
|
|
1225
|
-
* @throws {TypeError} if `obj` param is not an object
|
|
1226
|
-
*/
|
|
1227
|
-
function readXObjAttrAsIndex(obj, attr, key) {
|
|
1228
|
-
let result = undefined;
|
|
1229
|
-
try {
|
|
1230
|
-
result = readXObjAttrRaw(obj, attr, key);
|
|
1231
|
-
} catch (err) {
|
|
1232
|
-
switch (err.code) {
|
|
1233
|
-
case XOBJ_TE_NSTR_ECODE : {
|
|
1234
|
-
break;
|
|
1235
|
-
}
|
|
1236
|
-
default: {
|
|
1237
|
-
throw err;
|
|
1238
|
-
}
|
|
1239
|
-
};
|
|
1240
|
-
};
|
|
1241
|
-
return valueToIndex(result);
|
|
1242
|
-
};
|
|
1243
|
-
module.exports.readXObjAttrAsIndex = readXObjAttrAsIndex;
|
|
1244
|
-
|
|
1245
|
-
/**
|
|
1246
|
-
* Extracts an attribute from a given object and returns it as a string.
|
|
1247
|
-
* @function readXObjAttrEx
|
|
1248
|
-
* @param {object} obj - some object
|
|
1249
|
-
* @param {string} attr - some attribute
|
|
1250
|
-
* @param {object} [opt] - options
|
|
1251
|
-
* @param {string} [key] - some key
|
|
1252
|
-
* @returns {string}
|
|
1253
|
-
* @throws {TypeError} if `obj` param is not an object
|
|
1254
|
-
* @todo [since `v0.2.1`] deprecate use of `opt` as `string`
|
|
1255
|
-
*/
|
|
1256
|
-
function readXObjAttrEx(obj, attr, opt, key) {
|
|
1257
|
-
let result = undefined;
|
|
1258
|
-
try {
|
|
1259
|
-
result = readXObjAttrRaw(obj, attr, key);
|
|
1260
|
-
} catch (err) {
|
|
1261
|
-
switch (err.code) {
|
|
1262
|
-
case XOBJ_TE_NSTR_ECODE : {
|
|
1263
|
-
break;
|
|
1264
|
-
}
|
|
1265
|
-
default: {
|
|
1266
|
-
throw err;
|
|
1267
|
-
}
|
|
1268
|
-
};
|
|
1269
|
-
};
|
|
1270
|
-
/** @type {OPT_valtostr} */
|
|
1271
|
-
const _opt = isPlainObject(opt) ? opt : {
|
|
1272
|
-
useTrim: true,
|
|
1273
|
-
numberToString: true,
|
|
1274
|
-
boolToString: true,
|
|
1275
|
-
defValue: opt,
|
|
1276
|
-
};
|
|
1277
|
-
return readAsString(result, _opt);
|
|
1278
|
-
};
|
|
1279
|
-
|
|
1280
|
-
/**
|
|
1281
|
-
* Tries to convert a given value to a string and writes it as an attribute
|
|
1282
|
-
* into a given object.
|
|
1283
|
-
* @function writeXObjAttr
|
|
1284
|
-
* @param {object} obj - some object
|
|
1285
|
-
* @param {string} attr - some attribute
|
|
1286
|
-
* @param {any} value - some value
|
|
1287
|
-
* @param {string} [key] - some key
|
|
1288
|
-
* @returns {boolean}
|
|
1289
|
-
* @throws {TypeError} if `obj` param is not an object
|
|
1290
|
-
*/
|
|
1291
|
-
function writeXObjAttr(obj, attr, value, key) {
|
|
1292
|
-
let isSucceed = false;
|
|
1293
|
-
try {
|
|
1294
|
-
const opt = {
|
|
1295
|
-
useTrim: true,
|
|
1296
|
-
numberToString: true,
|
|
1297
|
-
boolToString: true,
|
|
1298
|
-
defValue: '',
|
|
1299
|
-
};
|
|
1300
|
-
isSucceed = writeXObjAttrEx(obj, attr, value, opt, key);
|
|
1301
|
-
} catch (err) {
|
|
1302
|
-
throw err;
|
|
1303
|
-
};
|
|
1304
|
-
return isSucceed;
|
|
1305
|
-
};
|
|
1306
|
-
|
|
1307
|
-
/**
|
|
1308
|
-
* Tries to convert a given value to a boolean and writes it as an attribute
|
|
1309
|
-
* into a given object.
|
|
1310
|
-
* @function writeXObjAttrAsBool
|
|
1311
|
-
* @param {object} obj - some object
|
|
1312
|
-
* @param {string} attr - some attribute
|
|
1313
|
-
* @param {any} value - some value
|
|
1314
|
-
* @param {boolean} [defValue] - default value
|
|
1315
|
-
* @param {string} [key] - some key
|
|
1316
|
-
* @returns {boolean}
|
|
1317
|
-
* @throws {TypeError} if `obj` param is not an object
|
|
1318
|
-
*/
|
|
1319
|
-
function writeXObjAttrAsBool(obj, attr, value, defValue, key) {
|
|
1320
|
-
let isSucceed = false;
|
|
1321
|
-
if (value !== undefined || typeof defValue === 'boolean') {
|
|
1322
|
-
const _value = readAsBoolEx(value, defValue).toString();
|
|
1323
|
-
try {
|
|
1324
|
-
isSucceed = writeXObjAttrRaw(obj, attr, _value, key);
|
|
1325
|
-
} catch (err) {
|
|
1326
|
-
switch (err.code) {
|
|
1327
|
-
case XOBJ_TE_NSTR_ECODE : {
|
|
1328
|
-
break;
|
|
1329
|
-
}
|
|
1330
|
-
default: {
|
|
1331
|
-
throw err;
|
|
1332
|
-
}
|
|
1333
|
-
};
|
|
1334
|
-
};
|
|
1335
|
-
};
|
|
1336
|
-
return isSucceed;
|
|
1337
|
-
};
|
|
1338
|
-
module.exports.writeXObjAttrAsBool = writeXObjAttrAsBool;
|
|
1339
|
-
|
|
1340
|
-
/**
|
|
1341
|
-
* Tries to convert a given value to a number and writes it as an attribute
|
|
1342
|
-
* into a given object.
|
|
1343
|
-
* @function writeXObjAttrAsNum
|
|
1344
|
-
* @param {object} obj - some object
|
|
1345
|
-
* @param {string} attr - some attribute
|
|
1346
|
-
* @param {any} value - some value
|
|
1347
|
-
* @param {number} [defValue] - default value
|
|
1348
|
-
* @param {string} [key] - some key
|
|
1349
|
-
* @returns {boolean}
|
|
1350
|
-
* @throws {TypeError} if `obj` param is not an object
|
|
1351
|
-
*/
|
|
1352
|
-
function writeXObjAttrAsNum(obj, attr, value, defValue, key) {
|
|
1353
|
-
let isSucceed = false;
|
|
1354
|
-
if (value !== undefined || typeof defValue === 'number') {
|
|
1355
|
-
const _value = readAsNumberEx(value, defValue).toString();
|
|
1356
|
-
try {
|
|
1357
|
-
isSucceed = writeXObjAttrRaw(obj, attr, _value, key);
|
|
1358
|
-
} catch (err) {
|
|
1359
|
-
switch (err.code) {
|
|
1360
|
-
case XOBJ_TE_NSTR_ECODE : {
|
|
1361
|
-
break;
|
|
1362
|
-
}
|
|
1363
|
-
default: {
|
|
1364
|
-
throw err;
|
|
1365
|
-
}
|
|
1366
|
-
};
|
|
1367
|
-
};
|
|
1368
|
-
};
|
|
1369
|
-
return isSucceed;
|
|
1370
|
-
};
|
|
1371
|
-
module.exports.writeXObjAttrAsNum = writeXObjAttrAsNum;
|
|
1372
|
-
|
|
1373
|
-
/**
|
|
1374
|
-
* Tries to convert a given value into an 'index' value and writes it
|
|
1375
|
-
* as an attribute into a given object.
|
|
1376
|
-
* @function writeXObjAttrAsIndex
|
|
1377
|
-
* @param {object} obj - some object
|
|
1378
|
-
* @param {string} attr - some attribute
|
|
1379
|
-
* @param {any} value - some value
|
|
1380
|
-
* @param {string} [key] - some key
|
|
1381
|
-
* @returns {boolean}
|
|
1382
|
-
* @throws {TypeError} if `obj` param is not an object
|
|
1383
|
-
*/
|
|
1384
|
-
function writeXObjAttrAsIndex(obj, attr, value, key) {
|
|
1385
|
-
let isSucceed = false;
|
|
1386
|
-
if (value !== undefined) {
|
|
1387
|
-
const _value = valueToIndex(value).toString();
|
|
1388
|
-
try {
|
|
1389
|
-
isSucceed = writeXObjAttrRaw(obj, attr, _value, key);
|
|
1390
|
-
} catch (err) {
|
|
1391
|
-
switch (err.code) {
|
|
1392
|
-
case XOBJ_TE_NSTR_ECODE : {
|
|
1393
|
-
break;
|
|
1394
|
-
}
|
|
1395
|
-
default: {
|
|
1396
|
-
throw err;
|
|
1397
|
-
}
|
|
1398
|
-
};
|
|
1399
|
-
};
|
|
1400
|
-
};
|
|
1401
|
-
return isSucceed;
|
|
1402
|
-
};
|
|
1403
|
-
module.exports.writeXObjAttrAsIndex = writeXObjAttrAsIndex;
|
|
1404
|
-
|
|
1405
|
-
/**
|
|
1406
|
-
* Tries to convert a given value to a string and writes it as an attribute
|
|
1407
|
-
* into a given object.
|
|
1408
|
-
* @function writeXObjAttrEx
|
|
1409
|
-
* @param {object} obj - some object
|
|
1410
|
-
* @param {string} attr - some attribute
|
|
1411
|
-
* @param {any} value - some value
|
|
1412
|
-
* @param {object} [opt] - options
|
|
1413
|
-
* @param {string} [key] - some key
|
|
1414
|
-
* @returns {boolean}
|
|
1415
|
-
* @throws {TypeError} if `obj` param is not an object
|
|
1416
|
-
* @todo [since `v0.2.1`] deprecate use of `opt` as `string`
|
|
1417
|
-
*/
|
|
1418
|
-
function writeXObjAttrEx(obj, attr, value, opt, key) {
|
|
1419
|
-
let isSucceed = false;
|
|
1420
|
-
if (value !== undefined && typeof value !== 'function') {
|
|
1421
|
-
let _opt = opt;
|
|
1422
|
-
if (!isPlainObject(_opt)) {
|
|
1423
|
-
const defValue = readAsString(_opt, {
|
|
1424
|
-
useTrim: true,
|
|
1425
|
-
numberToString: true,
|
|
1426
|
-
boolToString: true,
|
|
1427
|
-
});
|
|
1428
|
-
_opt = {
|
|
1429
|
-
useTrim: true,
|
|
1430
|
-
numberToString: true,
|
|
1431
|
-
boolToString: true,
|
|
1432
|
-
defValue,
|
|
1433
|
-
};
|
|
1434
|
-
};
|
|
1435
|
-
const _value = readAsString(value, _opt);
|
|
1436
|
-
try {
|
|
1437
|
-
isSucceed = writeXObjAttrRaw(obj, attr, _value, key);
|
|
1438
|
-
} catch (err) {
|
|
1439
|
-
switch (err.code) {
|
|
1440
|
-
case XOBJ_TE_NSTR_ECODE : {
|
|
1441
|
-
break;
|
|
1442
|
-
}
|
|
1443
|
-
default: {
|
|
1444
|
-
throw err;
|
|
1445
|
-
}
|
|
1446
|
-
};
|
|
1447
|
-
};
|
|
1448
|
-
};
|
|
1449
|
-
return isSucceed;
|
|
1450
|
-
};
|
|
1451
|
-
|
|
1452
|
-
/**
|
|
1453
|
-
* Inserts an elements into a given object.
|
|
1454
|
-
* @function insertXObjElements
|
|
1455
|
-
* @param {object} obj - some object
|
|
1456
|
-
* @param {...string} name - some child element name
|
|
1457
|
-
* @param {OPT_inselops_L} [opt]
|
|
1458
|
-
* @returns {number}
|
|
1459
|
-
* @throws {TypeError} if `obj` param is not an object
|
|
1460
|
-
* @experimental
|
|
1461
|
-
*/
|
|
1462
|
-
function insertXObjElements(obj, ...args) {
|
|
1463
|
-
if (!isPlainObject(obj)) {
|
|
1464
|
-
throw new EvalKeyNameError(
|
|
1465
|
-
XOBJ_TE_NPOBJ_EMSG,
|
|
1466
|
-
{ code: XOBJ_TE_NPOBJ_ECODE },
|
|
1467
|
-
);
|
|
1468
|
-
};
|
|
1469
|
-
let count = 0;
|
|
1470
|
-
let len = args.length;
|
|
1471
|
-
const opt = (
|
|
1472
|
-
len > 0 && isPlainObject(args[len - 1])
|
|
1473
|
-
? (len--, args.pop())
|
|
1474
|
-
: {}
|
|
1475
|
-
);
|
|
1476
|
-
for (let key of args) {
|
|
1477
|
-
if (insertXObjElement(obj, key, opt) !== null) count++;
|
|
1478
|
-
};
|
|
1479
|
-
return count;
|
|
1480
|
-
};
|
|
1481
|
-
|
|
1482
|
-
/**
|
|
1483
|
-
* An options for an element insertion ops
|
|
1484
|
-
* @typedef {Object} OPT_inselops_S
|
|
1485
|
-
* @property {boolean} [force=false]
|
|
1486
|
-
* @property {boolean} [ripOldies=false]
|
|
1487
|
-
*/
|
|
1488
|
-
|
|
1489
|
-
/**
|
|
1490
|
-
* Inserts a list elements into a given object.
|
|
1491
|
-
* @function insertXObjEList
|
|
1492
|
-
* @param {object} obj - some object
|
|
1493
|
-
* @param {string} name - some child element name
|
|
1494
|
-
* @param {OPT_inselops_S} [opt] - options
|
|
1495
|
-
* @returns {?(object|object[])}
|
|
1496
|
-
* @throws {TypeError} if `obj` param is not an object
|
|
1497
|
-
*/
|
|
1498
|
-
function insertXObjEList(...args) {
|
|
1499
|
-
let item = null;
|
|
1500
|
-
try {
|
|
1501
|
-
({ item } = insertXObjEListEx(...args));
|
|
1502
|
-
} catch (err) {
|
|
1503
|
-
switch (err.code) {
|
|
1504
|
-
case XOBJ_TE_NSTR_ECODE :
|
|
1505
|
-
case XOBJ_TE_KNES_ECODE : {
|
|
1506
|
-
break;
|
|
1507
|
-
}
|
|
1508
|
-
default: {
|
|
1509
|
-
throw err;
|
|
1510
|
-
}
|
|
1511
|
-
};
|
|
1512
|
-
};
|
|
1513
|
-
return item;
|
|
1514
|
-
};
|
|
1515
|
-
|
|
1516
|
-
/**
|
|
1517
|
-
* Inserts a list elements into a given object.
|
|
1518
|
-
* @since 0.2.0
|
|
1519
|
-
* @function insertXObjEListEx
|
|
1520
|
-
* @param {object} obj - some object
|
|
1521
|
-
* @param {string} name - some child element name
|
|
1522
|
-
* @param {OPT_inselops_S} [opt] - options
|
|
1523
|
-
* @returns {RVAL_emodif}
|
|
1524
|
-
* @throws {TypeError} if `obj` param is not an object
|
|
1525
|
-
* @throws {EvalKeyNameError} if `nmae` param is not valid identifier
|
|
1526
|
-
*/
|
|
1527
|
-
function insertXObjEListEx(obj, name, opt) {
|
|
1528
|
-
if (!isPlainObject(obj)) {
|
|
1529
|
-
throw new EvalKeyNameError(
|
|
1530
|
-
XOBJ_TE_NPOBJ_EMSG,
|
|
1531
|
-
{ code: XOBJ_TE_NPOBJ_ECODE },
|
|
1532
|
-
);
|
|
1533
|
-
};
|
|
1534
|
-
const { isSucceed, value: key } = evalKeyName(name);
|
|
1535
|
-
if (isSucceed) {
|
|
1536
|
-
/** @type {OPT_inselops_S} */
|
|
1537
|
-
let {
|
|
1538
|
-
force,
|
|
1539
|
-
ripOldies,
|
|
1540
|
-
} = isPlainObject(opt) ? opt : {};
|
|
1541
|
-
if (typeof force !== 'boolean') force = false;
|
|
1542
|
-
if (typeof ripOldies !== 'boolean') ripOldies = false;
|
|
1543
|
-
let prop = obj[key];
|
|
1544
|
-
let isSucceed = false;
|
|
1545
|
-
if (
|
|
1546
|
-
isNullOrUndef(prop)
|
|
1547
|
-
|| (force && (ripOldies || !isObject(prop)))
|
|
1548
|
-
) {
|
|
1549
|
-
obj[key] = prop = [];
|
|
1550
|
-
isSucceed = true;
|
|
1551
|
-
} else {
|
|
1552
|
-
if (isObject(prop)) {
|
|
1553
|
-
if (!isArray(prop)) prop = [ prop ];
|
|
1554
|
-
obj[key] = prop;
|
|
1555
|
-
isSucceed = true;
|
|
1556
|
-
};
|
|
1557
|
-
};
|
|
1558
|
-
return {
|
|
1559
|
-
isSucceed,
|
|
1560
|
-
item: isSucceed ? prop : null,
|
|
1561
|
-
};
|
|
1562
|
-
} else {
|
|
1563
|
-
const { code, msg } = key;
|
|
1564
|
-
throw new EvalKeyNameError(msg, { code });
|
|
1565
|
-
};
|
|
1566
|
-
};
|
|
1567
|
-
|
|
1568
|
-
/**
|
|
1569
|
-
* Inserts a chain of an elements into a given object.
|
|
1570
|
-
* @function insertXObjEChain
|
|
1571
|
-
* @param {object} obj - some object
|
|
1572
|
-
* @param {...string} name - some child element name
|
|
1573
|
-
* @param {object} [opt] - options
|
|
1574
|
-
* @returns {?any}
|
|
1575
|
-
* @throws {TypeError} if `obj` param is not an object
|
|
1576
|
-
* @experimental
|
|
1577
|
-
*/
|
|
1578
|
-
function insertXObjEChain(obj, ...args) {
|
|
1579
|
-
if (!isPlainObject(obj)) {
|
|
1580
|
-
throw new EvalKeyNameError(
|
|
1581
|
-
XOBJ_TE_NPOBJ_EMSG,
|
|
1582
|
-
{ code: XOBJ_TE_NPOBJ_ECODE },
|
|
1583
|
-
);
|
|
1584
|
-
};
|
|
1585
|
-
let result = null;
|
|
1586
|
-
let len = args.length;
|
|
1587
|
-
const opt = (
|
|
1588
|
-
len > 0 && isPlainObject(args[len - 1])
|
|
1589
|
-
? (len--, args.pop())
|
|
1590
|
-
: {}
|
|
1591
|
-
);
|
|
1592
|
-
if (len > 0) {
|
|
1593
|
-
let parent = obj;
|
|
1594
|
-
let child = null;
|
|
1595
|
-
let isSucceed = false;
|
|
1596
|
-
for (let key of args) {
|
|
1597
|
-
child = insertXObjElement(parent, key, opt);
|
|
1598
|
-
isSucceed = isPlainObject(child);
|
|
1599
|
-
if (!isSucceed) break;
|
|
1600
|
-
parent = child;
|
|
1601
|
-
};
|
|
1602
|
-
result = isSucceed ? parent : null;
|
|
1603
|
-
};
|
|
1604
|
-
return result;
|
|
1605
|
-
};
|
|
1606
|
-
|
|
1607
|
-
/**
|
|
1608
|
-
* Clears a given node
|
|
1609
|
-
* @param {(object|object[])} node - some node
|
|
1610
|
-
* @returns {boolean}
|
|
1611
|
-
* @throws {TypeError} if `node`-param is not an object or array
|
|
1612
|
-
*/
|
|
1613
|
-
function clearXObjNode(node) {
|
|
1614
|
-
if (isObject(node)) {
|
|
1615
|
-
if (isArray(node)) {
|
|
1616
|
-
node.length = 0;
|
|
1617
|
-
return true;
|
|
1618
|
-
};
|
|
1619
|
-
let isSucceed = false;
|
|
1620
|
-
for (const key in node) {
|
|
1621
|
-
// // TODO: catch errors in strict mode
|
|
1622
|
-
isSucceed = delete node[key];
|
|
1623
|
-
};
|
|
1624
|
-
return isSucceed;
|
|
1625
|
-
} else {
|
|
1626
|
-
throw new EvalKeyNameError(
|
|
1627
|
-
XOBJ_TE_NPOBJ_EMSG,
|
|
1628
|
-
{ code: XOBJ_TE_NPOBJ_ECODE },
|
|
1629
|
-
);
|
|
1630
|
-
};
|
|
1631
|
-
};
|
|
1632
|
-
module.exports.clearXObjNode = clearXObjNode;
|
|
797
|
+
module.exports.clearXObjNode = clearXObjNode;
|
|
1633
798
|
|
|
1634
799
|
// === module exports block ===
|
|
1635
800
|
|
|
1636
|
-
module.exports.readXObjParamRaw = readXObjParamRaw;
|
|
1637
|
-
module.exports.readXObjParam = readXObjParam;
|
|
1638
|
-
module.exports.readXObjParamEx = readXObjParamEx;
|
|
1639
|
-
module.exports.writeXObjParamRaw = writeXObjParamRaw;
|
|
1640
|
-
module.exports.writeXObjParam = writeXObjParam;
|
|
1641
|
-
module.exports.writeXObjParamEx = writeXObjParamEx;
|
|
1642
|
-
|
|
1643
|
-
module.exports.readXObjAttrRaw = readXObjAttrRaw;
|
|
1644
|
-
module.exports.readXObjAttr = readXObjAttr;
|
|
1645
|
-
module.exports.readXObjAttrEx = readXObjAttrEx;
|
|
1646
|
-
module.exports.writeXObjAttrRaw = writeXObjAttrRaw;
|
|
1647
|
-
module.exports.writeXObjAttr = writeXObjAttr;
|
|
1648
|
-
module.exports.writeXObjAttrEx = writeXObjAttrEx;
|
|
1649
|
-
|
|
1650
|
-
module.exports.getXObjAttributes = getXObjAttributes;
|
|
1651
|
-
module.exports.checkXObjAttribute = checkXObjAttribute;
|
|
1652
|
-
module.exports.deleteXObjAttribute = deleteXObjAttribute;
|
|
1653
|
-
module.exports.renameXObjAttribute = renameXObjAttribute;
|
|
1654
|
-
|
|
1655
|
-
module.exports.getXObjElement = getXObjElement;
|
|
1656
|
-
module.exports.addXObjElement = addXObjElement;
|
|
1657
|
-
module.exports.insertXObjElement = insertXObjElement;
|
|
1658
|
-
module.exports.insertXObjElementEx = insertXObjElementEx;
|
|
1659
|
-
module.exports.deleteXObjElement = deleteXObjElement;
|
|
1660
|
-
module.exports.deleteXObjElementEx = deleteXObjElementEx;
|
|
1661
|
-
module.exports.renameXObjElement = renameXObjElement;
|
|
1662
|
-
|
|
1663
801
|
module.exports.evalXObjEName = evalXObjEName;
|
|
1664
802
|
module.exports.insertXObjEList = insertXObjEList;
|
|
1665
803
|
module.exports.insertXObjEListEx = insertXObjEListEx;
|
|
1666
804
|
|
|
1667
|
-
// * inner *
|
|
1668
|
-
module.exports.evalKeyName = evalKeyName;
|
|
1669
|
-
|
|
1670
805
|
// * experimental *
|
|
1671
806
|
module.exports.genXObjENameDescr = genXObjENameDescr;
|
|
1672
807
|
module.exports.insertXObjElements = insertXObjElements;
|