@ygracs/xobj-lib-js 0.3.3 → 0.4.0

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/lib/xObj-lib.js CHANGED
@@ -1,14 +1,16 @@
1
- // [v0.3.140-20260606]
1
+ // [v0.3.144-20260704]
2
2
 
3
3
  // === module init block ===
4
4
 
5
5
  const {
6
- valueToIndex,
7
6
  isNullOrUndef,
8
- isInteger,
9
7
  isArray, isObject, isPlainObject,
10
8
  } = require('@ygracs/bsfoc-lib-js');
11
9
 
10
+ const {
11
+ evalKeyName,
12
+ } = require('./xObj-names');
13
+
12
14
  const {
13
15
  EvalKeyNameError,
14
16
  XOBJ_ECODE_TABLE,
@@ -17,229 +19,16 @@ const {
17
19
  // === module inner block ===
18
20
 
19
21
  const {
20
- XOBJ_TE_NSTR_EMSG,
21
22
  XOBJ_TE_NSTR_ECODE,
22
23
  XOBJ_TE_NPOBJ_EMSG,
23
24
  XOBJ_TE_NPOBJ_ECODE,
24
- XOBJ_TE_KNES_EMSG,
25
25
  XOBJ_TE_KNES_ECODE,
26
26
  } = XOBJ_ECODE_TABLE;
27
27
 
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
- */
36
-
37
- /**
38
- * An error description of a value check ops on fail.
39
- * @since 0.3.0
40
- * @typedef {Object} IEvalKeyNameReasonOnFail
41
- * @property {string} code - message ID
42
- * @property {string} msg - message text
43
- */
44
-
45
- /**
46
- * A result of a value check ops.
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.
52
- */
53
-
54
- /**
55
- * An options setting for `evalKeyName` function
56
- * @since 0.3.0
57
- * @typedef {Object} IEvalKeyNameOptions
58
- * @property {boolean} [onlyNES] - a flag to accept only a non-empty string values
59
- */
60
-
61
- /**
62
- * A result of `evalKeyName` function if succeed
63
- * @since 0.3.0
64
- * @typedef {Object} IEvalKeyNameResultOnSuccess
65
- * @property {true} isSucceed - ops flag
66
- * @property {string} value - result value
67
- */
68
-
69
- /**
70
- * A result of `evalKeyName` function if failed
71
- * @since 0.3.0
72
- * @typedef {Object} IEvalKeyNameResultOnFail
73
- * @property {false} isSucceed - ops flag
74
- * @property {IEvalKeyNameReasonOnFail} value - reason of fail
75
- */
76
-
77
- /**
78
- * A result of `evalKeyName` function
79
- * @since 0.3.0
80
- * @typedef {IEvalKeyNameResultOnSuccess|IEvalKeyNameResultOnFail} IEvalKeyNameResult
81
- */
82
- /**
83
- * A virtual constant meant for support jsdoc notation:
84
- * @type {IEvalKeyNameResult}
85
- */
86
- module.exports.IEvalKeyNameResult = { isSucceed: true, value: '' };
87
-
88
- /**
89
- * Tries to convert a value into an ID.
90
- * @function evalKeyName
91
- * @param {any} value - key to validate
92
- * @param {boolean|IEvalKeyNameOptions} [opt=true] - some options
93
- * @returns {IEvalKeyNameResult}
94
- * @inner
95
- */
96
- function evalKeyName(value, opt = true) {
97
- let {
98
- onlyNES,
99
- } = isPlainObject(opt) ? opt : { onlyNES: opt };
100
- if (typeof value !== 'string') {
101
- return {
102
- isSucceed: false,
103
- value: {
104
- code: XOBJ_TE_NSTR_ECODE,
105
- msg: XOBJ_TE_NSTR_EMSG,
106
- },
107
- };
108
- };
109
- const key = value.trim();
110
- if (onlyNES && key === '') {
111
- return {
112
- isSucceed: false,
113
- value: {
114
- code: XOBJ_TE_KNES_ECODE,
115
- msg: XOBJ_TE_KNES_EMSG,
116
- },
117
- };
118
- };
119
- return {
120
- isSucceed: true,
121
- value: key,
122
- };
123
- };
124
- module.exports.evalKeyName = evalKeyName;
125
-
126
28
  // === module main block ===
127
29
 
128
30
  /**
129
- * Tries to convert a value into an ID.
130
- * @function evalXObjEName
131
- * @param {any} value - some value to evaluate
132
- * @returns {null|number|string}
133
- */
134
- function evalXObjEName(value) {
135
- //return valueToIDString(value); // // TODO: [?]
136
- let name = value;
137
- switch (typeof name) {
138
- case 'number' : {
139
- if (valueToIndex(name) === -1) name = null;
140
- break;
141
- }
142
- case 'string' : {
143
- name = name.trim();
144
- if (name !== '') {
145
- let value = Number(name);
146
- if (!Number.isNaN(value)) {
147
- name = (
148
- value < 0 || !isInteger(value)
149
- ? null
150
- : value
151
- );
152
- };
153
- } else {
154
- name = null;
155
- };
156
- break;
157
- }
158
- default: {
159
- name = null;
160
- }
161
- };
162
- return name;
163
- };
164
-
165
- /**
166
- * Some conditions applied to an element as a result of an element name pattern
167
- * evaluation ops.
168
- * @since 0.3.0
169
- * @typedef {Object} XObjENameDescrConditions
170
- * @property {any} name - element name
171
- * @property {any} type - element type
172
- * @property {any} [value] - some value
173
- */
174
-
175
- /**
176
- * A result of an element name pattern evaluation ops.
177
- * @typedef {Object} XObjENameDescr
178
- * @property {boolean} isERR - ops flag
179
- * @property {null|number|string} name - element name
180
- * @property {?XObjENameDescrConditions} conditions - some conditions applied to an element
181
- */
182
-
183
- /**
184
- * Tries to convert a value into an element name description.
185
- * @function genXObjENameDescr
186
- * @param {any} value - element name pattern
187
- * @returns {XObjENameDescr}
188
- * @experimental
189
- */
190
- function genXObjENameDescr(value) {
191
- let result = null;
192
- let name = null;
193
- let conditions = null;
194
- let isERR = false;
195
- let tail = null;
196
- if (typeof value === 'string') {
197
- const re = /^\s*([^\[\]\s]+)?(\s*)(\[.*])?\s*$/;
198
- tail = value.match(re);
199
- //console.log('CHECK: '+JSON.stringify(tail, null, 2));
200
- if (tail) {
201
- if (tail[1]) {
202
- name = tail[1];
203
- if (name) {
204
- if (tail[2] === '') {
205
- if (tail[3]) {
206
- const re = /\[(@{0,1})(?:([^\[=]*?)(?:(?=[=])(=)((?![\"\'])[^\]]*|(?=([\"\']))\5(.*)\5))?)](\s*.*)/;
207
- tail = tail[3].match(re);
208
- //console.log('CHECK: '+JSON.stringify(tail, null, 2));
209
- if (tail) {
210
- let name = evalXObjEName(tail[2]);
211
- let type = tail[1] === '@' ? 'attribute' : 'child';
212
- let value = undefined;
213
- if (tail[3] === '=') value = tail[5] ? tail[6] : tail[4];
214
- if (tail[7]) isERR = true;
215
- conditions = {
216
- name,
217
- type,
218
- value,
219
- };
220
- };
221
- };
222
- } else if (tail[3]) {
223
- name = null;
224
- };
225
- };
226
- };
227
- };
228
- } else {
229
- name = value;
230
- };
231
- name = evalXObjEName(name);
232
- if (name === null) isERR = true;
233
- result = {
234
- name,
235
- conditions,
236
- isERR,
237
- };
238
- return result;
239
- };
240
-
241
- /**
242
- *
31
+ * A `Node`-element
243
32
  * @since 0.3.2
244
33
  * @typedef {Object<string, any>} INode
245
34
  */
@@ -249,7 +38,7 @@ function genXObjENameDescr(value) {
249
38
  */
250
39
  module.exports.INode = {};
251
40
  /**
252
- * A list of a `INode`-elements
41
+ * A list of a `Node`-elements
253
42
  * @since 0.3.2
254
43
  * @typedef {INode[]} INodeList
255
44
  */
@@ -280,15 +69,6 @@ function getXObjElement(obj, name) {
280
69
  };
281
70
  module.exports.getXObjElement = getXObjElement;
282
71
 
283
- /**
284
- * A result of an xObj modification ops
285
- * @typedef {Object} RVAL_emodif
286
- * @property {boolean} isSucceed - flag that indicates whether an ops is succeed or not
287
- * @property {?(object|object[])} item - some element as a result
288
- * @deprecated
289
- * @todo \[from v0.3.2] make obsolete. Use `INodeModifResult` instead.
290
- */
291
-
292
72
  /**
293
73
  * A result of an xObj modification ops if failed
294
74
  * @since 0.3.2
@@ -296,33 +76,18 @@ module.exports.getXObjElement = getXObjElement;
296
76
  * @property {false} isSucceed - flag that indicates whether an ops is succeed or not
297
77
  * @property {null} item - some element as a result
298
78
  */
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
79
  /**
315
80
  * A virtual constant meant for support jsdoc notation:
316
- * @type {INodeModifResult}
81
+ * @type {INodeModifResultOnFail}
317
82
  */
318
- module.exports.INodeModifResult = { isSucceed: false, item: null };
83
+ module.exports.INodeModifResultOnFail = { isSucceed: false, item: null };
319
84
 
320
85
  /**
321
- *
86
+ * Function interface for adding some child element to `INode`
322
87
  * @callback addXObjElementFn
323
- * @param {object} obj - some object
88
+ * @param {INode} obj - some object
324
89
  * @param {string} name - some child element
325
- * @returns {?object}
90
+ * @returns {?INode}
326
91
  * @see {@link addXObjElementEx} for param details
327
92
  */
328
93
  /**
@@ -349,13 +114,32 @@ function addXObjElement(...args) {
349
114
  };
350
115
  module.exports.addXObjElement = addXObjElement;
351
116
 
117
+ /**
118
+ * A result of an xObj modification ops if succeed (when child element returned)
119
+ * @since 0.4.0
120
+ * @typedef {Object} INodeModifResultOnSuccess_S1
121
+ * @property {true} isSucceed - flag that indicates whether an ops is succeed or not
122
+ * @property {INode} item - some element as a result
123
+ */
124
+ /**
125
+ * A virtual constant meant for support jsdoc notation:
126
+ * @type {INodeModifResultOnSuccess_S1}
127
+ */
128
+ module.exports.INodeModifResultOnSuccess_S1 = { isSucceed: true, item: {} };
129
+
130
+ /**
131
+ * A result of an xObj modification ops
132
+ * @since 0.4.0
133
+ * @typedef {INodeModifResultOnFail|INodeModifResultOnSuccess_S1} IAddNodeResult
134
+ */
135
+
352
136
  /**
353
137
  * Adds an element addressed by its key to a given object.
354
138
  * @since 0.3.0
355
139
  * @function addXObjElementEx
356
140
  * @param {INode} obj - some object
357
141
  * @param {string} name - some child element
358
- * @returns {INodeModifResult}
142
+ * @returns {IAddNodeResult}
359
143
  * @throws {TypeError} if `obj` param is not an object
360
144
  * @throws {EvalKeyNameError} if `name` param is not valid identifier
361
145
  */
@@ -405,12 +189,12 @@ module.exports.addXObjElementEx = addXObjElementEx;
405
189
  */
406
190
 
407
191
  /**
408
- *
192
+ * Function interface for inserting some child element into `INode`
409
193
  * @callback insertXObjElementFn
410
- * @param {object} obj - some object
194
+ * @param {INode} obj - some object
411
195
  * @param {string} name - some child element
412
196
  * @param {OPT_inselops_L} [opt] - options
413
- * @returns {?object}
197
+ * @returns {?INode|INode[]}
414
198
  * @see {@link insertXObjElementEx} for param details
415
199
  */
416
200
  /**
@@ -437,6 +221,25 @@ function insertXObjElement(...args) {
437
221
  };
438
222
  module.exports.insertXObjElement = insertXObjElement;
439
223
 
224
+ /**
225
+ * A result of an xObj modification ops if succeed (when child element returned)
226
+ * @since 0.4.0
227
+ * @typedef {Object} INodeModifResultOnSuccess_S2
228
+ * @property {true} isSucceed - flag that indicates whether an ops is succeed or not
229
+ * @property {INode[]} item - some element as a result
230
+ */
231
+ /**
232
+ * A virtual constant meant for support jsdoc notation:
233
+ * @type {INodeModifResultOnSuccess_S2}
234
+ */
235
+ module.exports.INodeModifResultOnSuccess_S2 = { isSucceed: true, item: [] };
236
+
237
+ /**
238
+ * A result of an xObj modification ops
239
+ * @since 0.4.0
240
+ * @typedef {INodeModifResultOnFail|INodeModifResultOnSuccess_S1|INodeModifResultOnSuccess_S2} IInsertNodeResult
241
+ */
242
+
440
243
  /**
441
244
  * Inserts an element addressed by its key into a given object.
442
245
  * @since 0.2.0
@@ -444,7 +247,7 @@ module.exports.insertXObjElement = insertXObjElement;
444
247
  * @param {INode} obj - some object
445
248
  * @param {string} name - some child element
446
249
  * @param {OPT_inselops_L} [opt] - options
447
- * @returns {INodeModifResult}
250
+ * @returns {IInsertNodeResult}
448
251
  * @throws {TypeError} if `obj` param is not an object
449
252
  * @throws {EvalKeyNameError} if `name` param is not valid identifier
450
253
  */
@@ -493,9 +296,9 @@ function insertXObjElementEx(obj, name, opt) {
493
296
  module.exports.insertXObjElementEx = insertXObjElementEx;
494
297
 
495
298
  /**
496
- *
299
+ * Function interface for deleting some child element from `INode`
497
300
  * @callback deleteXObjElementFn
498
- * @param {object} obj - some object
301
+ * @param {INode} obj - some object
499
302
  * @param {string} name - some child element
500
303
  * @returns {boolean}
501
304
  * @see {@link deleteXObjElementEx} for param details
@@ -524,12 +327,31 @@ function deleteXObjElement(...args) {
524
327
  };
525
328
  module.exports.deleteXObjElement = deleteXObjElement;
526
329
 
330
+ /**
331
+ * A result of an xObj modification ops if succeed (when child element not returned)
332
+ * @since 0.4.0
333
+ * @typedef {Object} INodeModifResultOnSuccess_S0
334
+ * @property {true} isSucceed - flag that indicates whether an ops is succeed or not
335
+ * @property {null} item - some element as a result
336
+ */
337
+ /**
338
+ * A virtual constant meant for support jsdoc notation:
339
+ * @type {INodeModifResultOnSuccess_S0}
340
+ */
341
+ module.exports.INodeModifResultOnSuccess_S0 = { isSucceed: true, item: null };
342
+
343
+ /**
344
+ * A result of an xObj modification ops
345
+ * @since 0.4.0
346
+ * @typedef {INodeModifResultOnFail|INodeModifResultOnSuccess_S0|INodeModifResultOnSuccess_S1|INodeModifResultOnSuccess_S2} IDeleteNodeResult
347
+ */
348
+
527
349
  /**
528
350
  * Deletes an element addressed by its key from a given object.
529
351
  * @function deleteXObjElementEx
530
352
  * @param {INode} obj - some object
531
353
  * @param {string} name - some child element
532
- * @returns {INodeModifResult}
354
+ * @returns {IDeleteNodeResult}
533
355
  * @throws {TypeError} if `obj` param is not an object
534
356
  * @throws {EvalKeyNameError} if `name` param is not valid identifier
535
357
  */
@@ -546,9 +368,16 @@ function deleteXObjElementEx(obj, name) {
546
368
  let isSucceed = false;
547
369
  // // TODO: catch errors in strict mode
548
370
  isSucceed = delete obj[key];
549
- return {
550
- isSucceed,
551
- item: isSucceed && isObject(prop) ? prop : null,
371
+ if (isSucceed) {
372
+ return {
373
+ isSucceed,
374
+ item: isObject(prop) ? prop : null,
375
+ };
376
+ } else {
377
+ return {
378
+ isSucceed,
379
+ item: null,
380
+ };
552
381
  };
553
382
  } else {
554
383
  const { code, msg } = key;
@@ -606,7 +435,7 @@ module.exports.renameXObjElement = renameXObjElement;
606
435
  /**
607
436
  * Inserts an elements into a given object.
608
437
  * @function insertXObjElements
609
- * @param {object} obj - some object
438
+ * @param {INode} obj - some object
610
439
  * @param {...string} name - some child element name
611
440
  * @param {OPT_inselops_L} [opt]
612
441
  * @returns {number}
@@ -641,15 +470,21 @@ function insertXObjElements(obj, ...args) {
641
470
  */
642
471
 
643
472
  /**
644
- * Inserts a list elements into a given object.
645
- * @function insertXObjEList
646
- * @param {object} obj - some object
473
+ *
474
+ * @callback insertXObjEListFn
475
+ * @param {INode} obj - some object
647
476
  * @param {string} name - some child element name
648
477
  * @param {OPT_inselops_S} [opt] - options
649
- * @returns {?(object|object[])}
478
+ * @returns {?INode[]}
479
+ * @see {@link insertXObjEListEx} for param details
480
+ */
481
+ /**
482
+ * Inserts a list elements into a given object.
483
+ * @type {insertXObjEListFn}
650
484
  * @throws {TypeError} if `obj` param is not an object
651
485
  */
652
486
  function insertXObjEList(...args) {
487
+ /** @type {?INode[]} */
653
488
  let item = null;
654
489
  try {
655
490
  ({ item } = insertXObjEListEx(...args));
@@ -666,6 +501,7 @@ function insertXObjEList(...args) {
666
501
  };
667
502
  return item;
668
503
  };
504
+ module.exports.insertXObjEList = insertXObjEList;
669
505
 
670
506
  /**
671
507
  * Inserts a list elements into a given object.
@@ -674,7 +510,7 @@ function insertXObjEList(...args) {
674
510
  * @param {INode} obj - some object
675
511
  * @param {string} name - some child element name
676
512
  * @param {OPT_inselops_S} [opt] - options
677
- * @returns {INodeModifResult}
513
+ * @returns {INodeModifResultOnFail|INodeModifResultOnSuccess_S2}
678
514
  * @throws {TypeError} if `obj` param is not an object
679
515
  * @throws {EvalKeyNameError} if `name` param is not valid identifier
680
516
  */
@@ -718,13 +554,14 @@ function insertXObjEListEx(obj, name, opt) {
718
554
  throw new EvalKeyNameError(msg, { code });
719
555
  };
720
556
  };
557
+ module.exports.insertXObjEListEx = insertXObjEListEx;
721
558
 
722
559
  /**
723
560
  * Inserts a chain of an elements into a given object.
724
561
  * @function insertXObjEChain
725
- * @param {object} obj - some object
562
+ * @param {INode} obj - some object
726
563
  * @param {...string} name - some child element name
727
- * @param {object} [opt] - options
564
+ * @param {OPT_inselops_L} [opt] - options
728
565
  * @returns {?any}
729
566
  * @throws {TypeError} if `obj` param is not an object
730
567
  * @experimental
@@ -749,14 +586,19 @@ function insertXObjEChain(obj, ...args) {
749
586
  let isSucceed = false;
750
587
  for (let key of args) {
751
588
  child = insertXObjElement(parent, key, opt);
752
- isSucceed = isPlainObject(child);
753
- if (!isSucceed) break;
754
- parent = child;
589
+ if (isPlainObject(child)) {
590
+ parent = child;
591
+ isSucceed = true;
592
+ } else {
593
+ isSucceed = false;
594
+ break;
595
+ };
755
596
  };
756
597
  result = isSucceed ? parent : null;
757
598
  };
758
599
  return result;
759
600
  };
601
+ module.exports.insertXObjEChain = insertXObjEChain;
760
602
 
761
603
  /**
762
604
  * Clears a given node
@@ -787,11 +629,5 @@ module.exports.clearXObjNode = clearXObjNode;
787
629
 
788
630
  // === module exports block ===
789
631
 
790
- module.exports.evalXObjEName = evalXObjEName;
791
- module.exports.insertXObjEList = insertXObjEList;
792
- module.exports.insertXObjEListEx = insertXObjEListEx;
793
-
794
632
  // * experimental *
795
- module.exports.genXObjENameDescr = genXObjENameDescr;
796
633
  module.exports.insertXObjElements = insertXObjElements;
797
- module.exports.insertXObjEChain = insertXObjEChain;
@@ -0,0 +1,102 @@
1
+ /**
2
+ * A result of `evalKeyName` function
3
+ */
4
+ export type IEvalKeyNameResult = IEvalKeyNameResultOnSuccess | IEvalKeyNameResultOnFail;
5
+ export const IEvalKeyNameResult: IEvalKeyNameResult;
6
+ /**
7
+ * Some conditions applied to an element as a result of an element name pattern
8
+ * evaluation ops.
9
+ */
10
+ export type IXObjENameDescrConditions = {
11
+ /**
12
+ * - element name
13
+ */
14
+ name: any;
15
+ /**
16
+ * - element type
17
+ */
18
+ type: any;
19
+ /**
20
+ * - some value
21
+ */
22
+ value?: any;
23
+ };
24
+ /**
25
+ * A result of an element name pattern evaluation ops.
26
+ */
27
+ export type IXObjENameDescr = {
28
+ /**
29
+ * - ops flag
30
+ */
31
+ isERR: boolean;
32
+ /**
33
+ * - element name
34
+ */
35
+ name: null | number | string;
36
+ /**
37
+ * - some conditions applied to an element
38
+ */
39
+ conditions: IXObjENameDescrConditions | null;
40
+ };
41
+ /**
42
+ * An error description of a value check ops on fail.
43
+ */
44
+ export type IEvalKeyNameReasonOnFail = {
45
+ /**
46
+ * - message ID
47
+ */
48
+ code: string;
49
+ /**
50
+ * - message text
51
+ */
52
+ msg: string;
53
+ };
54
+ /**
55
+ * An options setting for `evalKeyName` function
56
+ */
57
+ export type IEvalKeyNameOptions = {
58
+ /**
59
+ * - a flag to accept only a non-empty string values
60
+ */
61
+ onlyNES?: boolean;
62
+ };
63
+ /**
64
+ * A result of `evalKeyName` function if succeed
65
+ */
66
+ export type IEvalKeyNameResultOnSuccess = {
67
+ /**
68
+ * - ops flag
69
+ */
70
+ isSucceed: true;
71
+ /**
72
+ * - result value
73
+ */
74
+ value: string;
75
+ };
76
+ /**
77
+ * A result of `evalKeyName` function if failed
78
+ */
79
+ export type IEvalKeyNameResultOnFail = {
80
+ /**
81
+ * - ops flag
82
+ */
83
+ isSucceed: false;
84
+ /**
85
+ * - reason of fail
86
+ */
87
+ value: IEvalKeyNameReasonOnFail;
88
+ };
89
+
90
+ /**
91
+ * Tries to convert a value into an ID.
92
+ * @inner
93
+ */
94
+ export function evalKeyName(value: any, opt?: boolean | IEvalKeyNameOptions): IEvalKeyNameResult;
95
+ /**
96
+ * Tries to convert a value into an ID.
97
+ */
98
+ export function evalXObjEName(value: any): null | number | string;
99
+ /**
100
+ * Tries to convert a value into an element name description.
101
+ */
102
+ export function genXObjENameDescr(value: any): IXObjENameDescr;