@ygracs/xobj-lib-js 0.3.1 → 0.3.3
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 +13 -0
- package/doc/xObj.md +44 -38
- package/index.d.ts +16 -8
- package/index.js +37 -33
- package/lib/xObj-attr.d.ts +109 -0
- package/lib/xObj-attr.js +614 -0
- package/lib/xObj-lib.d.ts +126 -247
- package/lib/xObj-lib.js +231 -1107
- package/lib/xObj-param.d.ts +83 -0
- package/lib/xObj-param.js +435 -0
- package/package.json +4 -2
package/lib/xObj-lib.js
CHANGED
|
@@ -1,15 +1,12 @@
|
|
|
1
|
-
// [v0.3.
|
|
1
|
+
// [v0.3.140-20260606]
|
|
2
2
|
|
|
3
3
|
// === module init block ===
|
|
4
4
|
|
|
5
5
|
const {
|
|
6
|
-
valueToIndex,
|
|
7
|
-
readAsString, readAsBoolEx, readAsNumberEx,
|
|
6
|
+
valueToIndex,
|
|
8
7
|
isNullOrUndef,
|
|
9
8
|
isInteger,
|
|
10
9
|
isArray, isObject, isPlainObject,
|
|
11
|
-
// * import types definitions *
|
|
12
|
-
OPT_valtostr,
|
|
13
10
|
} = require('@ygracs/bsfoc-lib-js');
|
|
14
11
|
|
|
15
12
|
const {
|
|
@@ -17,9 +14,7 @@ const {
|
|
|
17
14
|
XOBJ_ECODE_TABLE,
|
|
18
15
|
} = require('./xObj-errors');
|
|
19
16
|
|
|
20
|
-
|
|
21
|
-
XOBJ_DEF_TNAMES,
|
|
22
|
-
} = require('./xObj-defs');
|
|
17
|
+
// === module inner block ===
|
|
23
18
|
|
|
24
19
|
const {
|
|
25
20
|
XOBJ_TE_NSTR_EMSG,
|
|
@@ -30,12 +25,14 @@ const {
|
|
|
30
25
|
XOBJ_TE_KNES_ECODE,
|
|
31
26
|
} = XOBJ_ECODE_TABLE;
|
|
32
27
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
|
|
28
|
+
/**
|
|
29
|
+
* A result of a value check ops.
|
|
30
|
+
* @typedef {Object} RVAL_reason
|
|
31
|
+
* @property {string} code - message ID
|
|
32
|
+
* @property {string} msg - message text
|
|
33
|
+
* @deprecated
|
|
34
|
+
* @todo \[from v0.3.0] make obsolete. Use `IEvalKeyNameReasonOnFail` instead.
|
|
35
|
+
*/
|
|
39
36
|
|
|
40
37
|
/**
|
|
41
38
|
* An error description of a value check ops on fail.
|
|
@@ -47,10 +44,11 @@ const {
|
|
|
47
44
|
|
|
48
45
|
/**
|
|
49
46
|
* A result of a value check ops.
|
|
50
|
-
* @typedef {Object}
|
|
51
|
-
* @property {
|
|
52
|
-
* @property {string}
|
|
53
|
-
* @
|
|
47
|
+
* @typedef {Object} VCOR_evalkname
|
|
48
|
+
* @property {boolean} isSucceed - ops flag
|
|
49
|
+
* @property {string|IEvalKeyNameReasonOnFail} value - result value or reson if failed
|
|
50
|
+
* @deprecated
|
|
51
|
+
* @todo \[from v0.3.0] make obsolete. Use `IEvalKeyNameResult` instead.
|
|
54
52
|
*/
|
|
55
53
|
|
|
56
54
|
/**
|
|
@@ -79,22 +77,19 @@ const {
|
|
|
79
77
|
/**
|
|
80
78
|
* A result of `evalKeyName` function
|
|
81
79
|
* @since 0.3.0
|
|
82
|
-
* @typedef {
|
|
80
|
+
* @typedef {IEvalKeyNameResultOnSuccess|IEvalKeyNameResultOnFail} IEvalKeyNameResult
|
|
83
81
|
*/
|
|
84
|
-
|
|
85
82
|
/**
|
|
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.
|
|
83
|
+
* A virtual constant meant for support jsdoc notation:
|
|
84
|
+
* @type {IEvalKeyNameResult}
|
|
91
85
|
*/
|
|
86
|
+
module.exports.IEvalKeyNameResult = { isSucceed: true, value: '' };
|
|
92
87
|
|
|
93
88
|
/**
|
|
94
89
|
* Tries to convert a value into an ID.
|
|
95
90
|
* @function evalKeyName
|
|
96
91
|
* @param {any} value - key to validate
|
|
97
|
-
* @param {
|
|
92
|
+
* @param {boolean|IEvalKeyNameOptions} [opt=true] - some options
|
|
98
93
|
* @returns {IEvalKeyNameResult}
|
|
99
94
|
* @inner
|
|
100
95
|
*/
|
|
@@ -126,22 +121,15 @@ function evalKeyName(value, opt = true) {
|
|
|
126
121
|
value: key,
|
|
127
122
|
};
|
|
128
123
|
};
|
|
124
|
+
module.exports.evalKeyName = evalKeyName;
|
|
129
125
|
|
|
130
126
|
// === module main block ===
|
|
131
127
|
|
|
132
|
-
/***
|
|
133
|
-
* (* constant definitions *)
|
|
134
|
-
*/
|
|
135
|
-
|
|
136
|
-
/***
|
|
137
|
-
* (* function definitions *)
|
|
138
|
-
*/
|
|
139
|
-
|
|
140
128
|
/**
|
|
141
129
|
* Tries to convert a value into an ID.
|
|
142
130
|
* @function evalXObjEName
|
|
143
131
|
* @param {any} value - some value to evaluate
|
|
144
|
-
* @returns {
|
|
132
|
+
* @returns {null|number|string}
|
|
145
133
|
*/
|
|
146
134
|
function evalXObjEName(value) {
|
|
147
135
|
//return valueToIDString(value); // // TODO: [?]
|
|
@@ -188,7 +176,7 @@ function evalXObjEName(value) {
|
|
|
188
176
|
* A result of an element name pattern evaluation ops.
|
|
189
177
|
* @typedef {Object} XObjENameDescr
|
|
190
178
|
* @property {boolean} isERR - ops flag
|
|
191
|
-
* @property {
|
|
179
|
+
* @property {null|number|string} name - element name
|
|
192
180
|
* @property {?XObjENameDescrConditions} conditions - some conditions applied to an element
|
|
193
181
|
*/
|
|
194
182
|
|
|
@@ -250,10 +238,26 @@ function genXObjENameDescr(value) {
|
|
|
250
238
|
return result;
|
|
251
239
|
};
|
|
252
240
|
|
|
241
|
+
/**
|
|
242
|
+
*
|
|
243
|
+
* @since 0.3.2
|
|
244
|
+
* @typedef {Object<string, any>} INode
|
|
245
|
+
*/
|
|
246
|
+
/**
|
|
247
|
+
* A virtual constant meant for support jsdoc notation:
|
|
248
|
+
* @type {INode}
|
|
249
|
+
*/
|
|
250
|
+
module.exports.INode = {};
|
|
251
|
+
/**
|
|
252
|
+
* A list of a `INode`-elements
|
|
253
|
+
* @since 0.3.2
|
|
254
|
+
* @typedef {INode[]} INodeList
|
|
255
|
+
*/
|
|
256
|
+
|
|
253
257
|
/**
|
|
254
258
|
* Extracts an element from a given object by its key.
|
|
255
259
|
* @function getXObjElement
|
|
256
|
-
* @param {
|
|
260
|
+
* @param {INode} obj - some object
|
|
257
261
|
* @param {string} name - some child element
|
|
258
262
|
* @returns {?any}
|
|
259
263
|
* @throws {TypeError} if `obj` param is not an object
|
|
@@ -274,49 +278,58 @@ function getXObjElement(obj, name) {
|
|
|
274
278
|
const { code, msg } = key;
|
|
275
279
|
throw new EvalKeyNameError(msg, { code });
|
|
276
280
|
};
|
|
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
|
-
};
|
|
281
|
+
module.exports.getXObjElement = getXObjElement;
|
|
303
282
|
|
|
304
283
|
/**
|
|
305
284
|
* A result of an xObj modification ops
|
|
306
285
|
* @typedef {Object} RVAL_emodif
|
|
307
286
|
* @property {boolean} isSucceed - flag that indicates whether an ops is succeed or not
|
|
308
287
|
* @property {?(object|object[])} item - some element as a result
|
|
288
|
+
* @deprecated
|
|
289
|
+
* @todo \[from v0.3.2] make obsolete. Use `INodeModifResult` instead.
|
|
309
290
|
*/
|
|
310
291
|
|
|
311
292
|
/**
|
|
312
|
-
*
|
|
313
|
-
* @
|
|
293
|
+
* A result of an xObj modification ops if failed
|
|
294
|
+
* @since 0.3.2
|
|
295
|
+
* @typedef {Object} INodeModifResultOnFail
|
|
296
|
+
* @property {false} isSucceed - flag that indicates whether an ops is succeed or not
|
|
297
|
+
* @property {null} item - some element as a result
|
|
298
|
+
*/
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* A result of an xObj modification ops if succeed
|
|
302
|
+
* @since 0.3.2
|
|
303
|
+
* @typedef {Object} INodeModifResultOnSuccess
|
|
304
|
+
* @property {true} isSucceed - flag that indicates whether an ops is succeed or not
|
|
305
|
+
* @property {object|object[]} item - some element as a result
|
|
306
|
+
*/
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* A result of an xObj modification ops
|
|
310
|
+
* @since 0.3.2
|
|
311
|
+
* @typedef {INodeModifResultOnFail|INodeModifResultOnSuccess} INodeModifResult
|
|
312
|
+
*/
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* A virtual constant meant for support jsdoc notation:
|
|
316
|
+
* @type {INodeModifResult}
|
|
317
|
+
*/
|
|
318
|
+
module.exports.INodeModifResult = { isSucceed: false, item: null };
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
*
|
|
322
|
+
* @callback addXObjElementFn
|
|
314
323
|
* @param {object} obj - some object
|
|
315
324
|
* @param {string} name - some child element
|
|
316
325
|
* @returns {?object}
|
|
317
|
-
* @throws {TypeError} if `obj` param is not an object
|
|
318
326
|
* @see {@link addXObjElementEx} for param details
|
|
319
327
|
*/
|
|
328
|
+
/**
|
|
329
|
+
* Adds an element addressed by its key to a given object.
|
|
330
|
+
* @type {addXObjElementFn}
|
|
331
|
+
* @throws {TypeError} if `obj` param is not an object
|
|
332
|
+
*/
|
|
320
333
|
function addXObjElement(...args) {
|
|
321
334
|
let item = null;
|
|
322
335
|
try {
|
|
@@ -334,14 +347,15 @@ function addXObjElement(...args) {
|
|
|
334
347
|
};
|
|
335
348
|
return item;
|
|
336
349
|
};
|
|
350
|
+
module.exports.addXObjElement = addXObjElement;
|
|
337
351
|
|
|
338
352
|
/**
|
|
339
353
|
* Adds an element addressed by its key to a given object.
|
|
340
354
|
* @since 0.3.0
|
|
341
355
|
* @function addXObjElementEx
|
|
342
|
-
* @param {
|
|
356
|
+
* @param {INode} obj - some object
|
|
343
357
|
* @param {string} name - some child element
|
|
344
|
-
* @returns {
|
|
358
|
+
* @returns {INodeModifResult}
|
|
345
359
|
* @throws {TypeError} if `obj` param is not an object
|
|
346
360
|
* @throws {EvalKeyNameError} if `name` param is not valid identifier
|
|
347
361
|
*/
|
|
@@ -354,6 +368,7 @@ function addXObjElementEx(obj, name) {
|
|
|
354
368
|
};
|
|
355
369
|
const { isSucceed, value: key } = evalKeyName(name);
|
|
356
370
|
if (isSucceed) {
|
|
371
|
+
/** @type {INode} */
|
|
357
372
|
const item = {};
|
|
358
373
|
let prop = obj[key];
|
|
359
374
|
let isSucceed = false;
|
|
@@ -390,15 +405,19 @@ module.exports.addXObjElementEx = addXObjElementEx;
|
|
|
390
405
|
*/
|
|
391
406
|
|
|
392
407
|
/**
|
|
393
|
-
*
|
|
394
|
-
* @
|
|
408
|
+
*
|
|
409
|
+
* @callback insertXObjElementFn
|
|
395
410
|
* @param {object} obj - some object
|
|
396
411
|
* @param {string} name - some child element
|
|
397
412
|
* @param {OPT_inselops_L} [opt] - options
|
|
398
413
|
* @returns {?object}
|
|
399
|
-
* @throws {TypeError} if `obj` param is not an object
|
|
400
414
|
* @see {@link insertXObjElementEx} for param details
|
|
401
415
|
*/
|
|
416
|
+
/**
|
|
417
|
+
* Inserts an element addressed by its key into a given object.
|
|
418
|
+
* @type {insertXObjElementFn}
|
|
419
|
+
* @throws {TypeError} if `obj` param is not an object
|
|
420
|
+
*/
|
|
402
421
|
function insertXObjElement(...args) {
|
|
403
422
|
let item = null;
|
|
404
423
|
try {
|
|
@@ -416,15 +435,16 @@ function insertXObjElement(...args) {
|
|
|
416
435
|
};
|
|
417
436
|
return item;
|
|
418
437
|
};
|
|
438
|
+
module.exports.insertXObjElement = insertXObjElement;
|
|
419
439
|
|
|
420
440
|
/**
|
|
421
441
|
* Inserts an element addressed by its key into a given object.
|
|
422
442
|
* @since 0.2.0
|
|
423
443
|
* @function insertXObjElementEx
|
|
424
|
-
* @param {
|
|
444
|
+
* @param {INode} obj - some object
|
|
425
445
|
* @param {string} name - some child element
|
|
426
446
|
* @param {OPT_inselops_L} [opt] - options
|
|
427
|
-
* @returns {
|
|
447
|
+
* @returns {INodeModifResult}
|
|
428
448
|
* @throws {TypeError} if `obj` param is not an object
|
|
429
449
|
* @throws {EvalKeyNameError} if `name` param is not valid identifier
|
|
430
450
|
*/
|
|
@@ -437,6 +457,7 @@ function insertXObjElementEx(obj, name, opt) {
|
|
|
437
457
|
};
|
|
438
458
|
const { isSucceed, value: key } = evalKeyName(name);
|
|
439
459
|
if (isSucceed) {
|
|
460
|
+
/** @type {INode} */
|
|
440
461
|
const item = {};
|
|
441
462
|
let prop = obj[key];
|
|
442
463
|
let isSucceed = false;
|
|
@@ -469,16 +490,21 @@ function insertXObjElementEx(obj, name, opt) {
|
|
|
469
490
|
throw new EvalKeyNameError(msg, { code });
|
|
470
491
|
};
|
|
471
492
|
};
|
|
493
|
+
module.exports.insertXObjElementEx = insertXObjElementEx;
|
|
472
494
|
|
|
473
495
|
/**
|
|
474
|
-
*
|
|
475
|
-
* @
|
|
496
|
+
*
|
|
497
|
+
* @callback deleteXObjElementFn
|
|
476
498
|
* @param {object} obj - some object
|
|
477
499
|
* @param {string} name - some child element
|
|
478
500
|
* @returns {boolean}
|
|
479
|
-
* @throws {TypeError} if `obj` param is not an object
|
|
480
501
|
* @see {@link deleteXObjElementEx} for param details
|
|
481
502
|
*/
|
|
503
|
+
/**
|
|
504
|
+
* Deletes an element addressed by its key from a given object.
|
|
505
|
+
* @type {deleteXObjElementFn}
|
|
506
|
+
* @throws {TypeError} if `obj` param is not an object
|
|
507
|
+
*/
|
|
482
508
|
function deleteXObjElement(...args) {
|
|
483
509
|
let isSucceed = false;
|
|
484
510
|
try {
|
|
@@ -496,13 +522,14 @@ function deleteXObjElement(...args) {
|
|
|
496
522
|
};
|
|
497
523
|
return isSucceed;
|
|
498
524
|
};
|
|
525
|
+
module.exports.deleteXObjElement = deleteXObjElement;
|
|
499
526
|
|
|
500
527
|
/**
|
|
501
528
|
* Deletes an element addressed by its key from a given object.
|
|
502
529
|
* @function deleteXObjElementEx
|
|
503
|
-
* @param {
|
|
530
|
+
* @param {INode} obj - some object
|
|
504
531
|
* @param {string} name - some child element
|
|
505
|
-
* @returns {
|
|
532
|
+
* @returns {INodeModifResult}
|
|
506
533
|
* @throws {TypeError} if `obj` param is not an object
|
|
507
534
|
* @throws {EvalKeyNameError} if `name` param is not valid identifier
|
|
508
535
|
*/
|
|
@@ -528,11 +555,12 @@ function deleteXObjElementEx(obj, name) {
|
|
|
528
555
|
throw new EvalKeyNameError(msg, { code });
|
|
529
556
|
};
|
|
530
557
|
};
|
|
558
|
+
module.exports.deleteXObjElementEx = deleteXObjElementEx;
|
|
531
559
|
|
|
532
560
|
/**
|
|
533
561
|
* Renames an element addressed by its key.
|
|
534
562
|
* @function renameXObjElement
|
|
535
|
-
* @param {
|
|
563
|
+
* @param {INode} obj - some object
|
|
536
564
|
* @param {string} name - some child element
|
|
537
565
|
* @param {string} value - new element ID
|
|
538
566
|
* @returns {boolean}
|
|
@@ -573,1100 +601,196 @@ function renameXObjElement(obj, name = '', value = '') {
|
|
|
573
601
|
};
|
|
574
602
|
return isSucceed;
|
|
575
603
|
};
|
|
604
|
+
module.exports.renameXObjElement = renameXObjElement;
|
|
576
605
|
|
|
577
606
|
/**
|
|
578
|
-
*
|
|
579
|
-
* @function
|
|
580
|
-
* @param {object} obj
|
|
581
|
-
* @param {string}
|
|
582
|
-
* @param {
|
|
583
|
-
* @returns {
|
|
607
|
+
* Inserts an elements into a given object.
|
|
608
|
+
* @function insertXObjElements
|
|
609
|
+
* @param {object} obj - some object
|
|
610
|
+
* @param {...string} name - some child element name
|
|
611
|
+
* @param {OPT_inselops_L} [opt]
|
|
612
|
+
* @returns {number}
|
|
584
613
|
* @throws {TypeError} if `obj` param is not an object
|
|
585
|
-
* @
|
|
614
|
+
* @experimental
|
|
586
615
|
*/
|
|
587
|
-
function
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
616
|
+
function insertXObjElements(obj, ...args) {
|
|
617
|
+
if (!isPlainObject(obj)) {
|
|
618
|
+
throw new EvalKeyNameError(
|
|
619
|
+
XOBJ_TE_NPOBJ_EMSG,
|
|
620
|
+
{ code: XOBJ_TE_NPOBJ_ECODE },
|
|
621
|
+
);
|
|
593
622
|
};
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
623
|
+
let count = 0;
|
|
624
|
+
let len = args.length;
|
|
625
|
+
const opt = (
|
|
626
|
+
len > 0 && isPlainObject(args[len - 1])
|
|
627
|
+
? (len--, args.pop())
|
|
628
|
+
: {}
|
|
629
|
+
);
|
|
630
|
+
for (let key of args) {
|
|
631
|
+
if (insertXObjElement(obj, key, opt) !== null) count++;
|
|
601
632
|
};
|
|
602
|
-
return
|
|
633
|
+
return count;
|
|
603
634
|
};
|
|
604
635
|
|
|
605
636
|
/**
|
|
606
|
-
*
|
|
607
|
-
* @
|
|
637
|
+
* An options for an element insertion ops
|
|
638
|
+
* @typedef {Object} OPT_inselops_S
|
|
639
|
+
* @property {boolean} [force=false]
|
|
640
|
+
* @property {boolean} [ripOldies=false]
|
|
641
|
+
*/
|
|
642
|
+
|
|
643
|
+
/**
|
|
644
|
+
* Inserts a list elements into a given object.
|
|
645
|
+
* @function insertXObjEList
|
|
608
646
|
* @param {object} obj - some object
|
|
609
|
-
* @param {string}
|
|
610
|
-
* @param {
|
|
611
|
-
* @returns {
|
|
647
|
+
* @param {string} name - some child element name
|
|
648
|
+
* @param {OPT_inselops_S} [opt] - options
|
|
649
|
+
* @returns {?(object|object[])}
|
|
612
650
|
* @throws {TypeError} if `obj` param is not an object
|
|
613
|
-
* @throws {EvalKeyNameError} if `attr` param is not valid identifier
|
|
614
651
|
*/
|
|
615
|
-
function
|
|
616
|
-
let
|
|
652
|
+
function insertXObjEList(...args) {
|
|
653
|
+
let item = null;
|
|
617
654
|
try {
|
|
618
|
-
|
|
655
|
+
({ item } = insertXObjEListEx(...args));
|
|
619
656
|
} catch (err) {
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
isSucceed = delete _obj[name];
|
|
657
|
+
switch (err.code) {
|
|
658
|
+
case XOBJ_TE_NSTR_ECODE :
|
|
659
|
+
case XOBJ_TE_KNES_ECODE : {
|
|
660
|
+
break;
|
|
661
|
+
}
|
|
662
|
+
default: {
|
|
663
|
+
throw err;
|
|
664
|
+
}
|
|
629
665
|
};
|
|
630
|
-
return isSucceed;
|
|
631
|
-
} else {
|
|
632
|
-
const { code, msg } = name;
|
|
633
|
-
throw new EvalKeyNameError(msg, { code });
|
|
634
666
|
};
|
|
667
|
+
return item;
|
|
635
668
|
};
|
|
636
669
|
|
|
637
670
|
/**
|
|
638
|
-
*
|
|
671
|
+
* Inserts a list elements into a given object.
|
|
639
672
|
* @since 0.2.0
|
|
640
|
-
* @function
|
|
641
|
-
* @param {
|
|
642
|
-
* @param {string}
|
|
643
|
-
* @param {
|
|
644
|
-
* @
|
|
645
|
-
* @returns {boolean}
|
|
673
|
+
* @function insertXObjEListEx
|
|
674
|
+
* @param {INode} obj - some object
|
|
675
|
+
* @param {string} name - some child element name
|
|
676
|
+
* @param {OPT_inselops_S} [opt] - options
|
|
677
|
+
* @returns {INodeModifResult}
|
|
646
678
|
* @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
|
|
679
|
+
* @throws {EvalKeyNameError} if `name` param is not valid identifier
|
|
650
680
|
*/
|
|
651
|
-
function
|
|
681
|
+
function insertXObjEListEx(obj, name, opt) {
|
|
652
682
|
if (!isPlainObject(obj)) {
|
|
653
683
|
throw new EvalKeyNameError(
|
|
654
684
|
XOBJ_TE_NPOBJ_EMSG,
|
|
655
685
|
{ code: XOBJ_TE_NPOBJ_ECODE },
|
|
656
686
|
);
|
|
657
687
|
};
|
|
658
|
-
const
|
|
659
|
-
const { isSucceed, value: _key } = evalKeyName(key, opt);
|
|
688
|
+
const { isSucceed, value: key } = evalKeyName(name);
|
|
660
689
|
if (isSucceed) {
|
|
690
|
+
/** @type {OPT_inselops_S} */
|
|
691
|
+
let {
|
|
692
|
+
force,
|
|
693
|
+
ripOldies,
|
|
694
|
+
} = isPlainObject(opt) ? opt : {};
|
|
695
|
+
if (typeof force !== 'boolean') force = false;
|
|
696
|
+
if (typeof ripOldies !== 'boolean') ripOldies = false;
|
|
697
|
+
let prop = obj[key];
|
|
661
698
|
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
|
-
};
|
|
699
|
+
if (
|
|
700
|
+
isNullOrUndef(prop)
|
|
701
|
+
|| (force && (ripOldies || !isObject(prop)))
|
|
702
|
+
) {
|
|
703
|
+
obj[key] = prop = [];
|
|
704
|
+
isSucceed = true;
|
|
705
|
+
} else {
|
|
706
|
+
if (isObject(prop)) {
|
|
707
|
+
if (!isArray(prop)) prop = [ prop ];
|
|
708
|
+
obj[key] = prop;
|
|
709
|
+
isSucceed = true;
|
|
684
710
|
};
|
|
685
711
|
};
|
|
686
|
-
return
|
|
712
|
+
return {
|
|
713
|
+
isSucceed,
|
|
714
|
+
item: isSucceed ? prop : null,
|
|
715
|
+
};
|
|
687
716
|
} else {
|
|
688
|
-
const { code, msg } =
|
|
717
|
+
const { code, msg } = key;
|
|
689
718
|
throw new EvalKeyNameError(msg, { code });
|
|
690
719
|
};
|
|
691
720
|
};
|
|
692
721
|
|
|
693
722
|
/**
|
|
694
|
-
*
|
|
695
|
-
* @function
|
|
723
|
+
* Inserts a chain of an elements into a given object.
|
|
724
|
+
* @function insertXObjEChain
|
|
696
725
|
* @param {object} obj - some object
|
|
697
|
-
* @param {string}
|
|
698
|
-
* @
|
|
726
|
+
* @param {...string} name - some child element name
|
|
727
|
+
* @param {object} [opt] - options
|
|
728
|
+
* @returns {?any}
|
|
699
729
|
* @throws {TypeError} if `obj` param is not an object
|
|
700
|
-
* @
|
|
730
|
+
* @experimental
|
|
701
731
|
*/
|
|
702
|
-
function
|
|
732
|
+
function insertXObjEChain(obj, ...args) {
|
|
703
733
|
if (!isPlainObject(obj)) {
|
|
704
734
|
throw new EvalKeyNameError(
|
|
705
735
|
XOBJ_TE_NPOBJ_EMSG,
|
|
706
736
|
{ code: XOBJ_TE_NPOBJ_ECODE },
|
|
707
737
|
);
|
|
708
738
|
};
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
739
|
+
let result = null;
|
|
740
|
+
let len = args.length;
|
|
741
|
+
const opt = (
|
|
742
|
+
len > 0 && isPlainObject(args[len - 1])
|
|
743
|
+
? (len--, args.pop())
|
|
744
|
+
: {}
|
|
745
|
+
);
|
|
746
|
+
if (len > 0) {
|
|
747
|
+
let parent = obj;
|
|
748
|
+
let child = null;
|
|
749
|
+
let isSucceed = false;
|
|
750
|
+
for (let key of args) {
|
|
751
|
+
child = insertXObjElement(parent, key, opt);
|
|
752
|
+
isSucceed = isPlainObject(child);
|
|
753
|
+
if (!isSucceed) break;
|
|
754
|
+
parent = child;
|
|
755
|
+
};
|
|
756
|
+
result = isSucceed ? parent : null;
|
|
713
757
|
};
|
|
714
|
-
return
|
|
758
|
+
return result;
|
|
715
759
|
};
|
|
716
760
|
|
|
717
761
|
/**
|
|
718
|
-
*
|
|
719
|
-
* @
|
|
720
|
-
* @param {object} obj - some object
|
|
721
|
-
* @param {any} value - some value
|
|
722
|
-
* @param {string} [key=XOBJ_DEF_PARAM_TNAME] - some key
|
|
762
|
+
* Clears a given node
|
|
763
|
+
* @param {INode|INode[]} node - some node
|
|
723
764
|
* @returns {boolean}
|
|
724
|
-
* @throws {TypeError} if `
|
|
725
|
-
* @throws {EvalKeyNameError} if `key` param is not valid identifier
|
|
765
|
+
* @throws {TypeError} if `node`-param is not an object or array
|
|
726
766
|
*/
|
|
727
|
-
function
|
|
728
|
-
if (
|
|
767
|
+
function clearXObjNode(node) {
|
|
768
|
+
if (isObject(node)) {
|
|
769
|
+
if (isArray(node)) {
|
|
770
|
+
node.length = 0;
|
|
771
|
+
return true;
|
|
772
|
+
};
|
|
773
|
+
let isSucceed = false;
|
|
774
|
+
for (const key in node) {
|
|
775
|
+
// // TODO: catch errors in strict mode
|
|
776
|
+
isSucceed = delete node[key];
|
|
777
|
+
};
|
|
778
|
+
return isSucceed;
|
|
779
|
+
} else {
|
|
729
780
|
throw new EvalKeyNameError(
|
|
730
781
|
XOBJ_TE_NPOBJ_EMSG,
|
|
731
782
|
{ code: XOBJ_TE_NPOBJ_ECODE },
|
|
732
783
|
);
|
|
733
784
|
};
|
|
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
785
|
};
|
|
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;
|
|
786
|
+
module.exports.clearXObjNode = clearXObjNode;
|
|
1633
787
|
|
|
1634
788
|
// === module exports block ===
|
|
1635
789
|
|
|
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
790
|
module.exports.evalXObjEName = evalXObjEName;
|
|
1664
791
|
module.exports.insertXObjEList = insertXObjEList;
|
|
1665
792
|
module.exports.insertXObjEListEx = insertXObjEListEx;
|
|
1666
793
|
|
|
1667
|
-
// * inner *
|
|
1668
|
-
module.exports.evalKeyName = evalKeyName;
|
|
1669
|
-
|
|
1670
794
|
// * experimental *
|
|
1671
795
|
module.exports.genXObjENameDescr = genXObjENameDescr;
|
|
1672
796
|
module.exports.insertXObjElements = insertXObjElements;
|