@cntwg/xml-lib-js 0.0.29 → 0.0.31

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/xmldoc-lib.js CHANGED
@@ -1,4 +1,4 @@
1
- // [v0.1.085-20240801]
1
+ // [v0.2.096-20240929]
2
2
 
3
3
  // === module init block ===
4
4
 
@@ -23,8 +23,8 @@ const {
23
23
 
24
24
  /**
25
25
  * @function checkFsError
26
- * @param {object}
27
- * @param {Error}
26
+ * @param {object} descr
27
+ * @param {Error} err
28
28
  * @returns {object}
29
29
  * @inner
30
30
  * @description Checs an error code of a fs ops and sets descr info if succeed
@@ -69,8 +69,8 @@ const XML_TE_NROOT_EMSG = 'root element not found';
69
69
 
70
70
  /**
71
71
  * @function readAsTagName
72
- * @param {any}
73
- * @return {string}
72
+ * @param {any} value
73
+ * @returns {string}
74
74
  * @since v0.0.28
75
75
  * @description Tries to convert a given value to a valid tag name
76
76
  * of an XML-element.
@@ -92,8 +92,8 @@ function readAsTagName(value) {
92
92
 
93
93
  /**
94
94
  * @function readAsAttrName
95
- * @param {any}
96
- * @return {string}
95
+ * @param {any} value
96
+ * @returns {string}
97
97
  * @since v0.0.28
98
98
  * @description Tries to convert a given value to a valid name
99
99
  * of an XML-attribute.
@@ -115,8 +115,8 @@ function readAsAttrName(value) {
115
115
 
116
116
  /**
117
117
  * @function valueToElementID
118
- * @param {any}
119
- * @return {string}
118
+ * @param {any} value
119
+ * @returns {string}
120
120
  * @since v0.0.28
121
121
  * @description Tries to convert a given value to a valid identifier
122
122
  * suitable as a value for an "ID-attribute" of an XML-element.
@@ -144,16 +144,18 @@ function valueToElementID(value) {
144
144
  * @description This class implements an attributes mapper instance
145
145
  */
146
146
  class TXmlAttributesMapper {
147
- #_host = null;
148
- #_options = null;
147
+ /** @property {?object} */
148
+ #_host;// = null;
149
+ /** @property {?object} */
150
+ #_options;// = null;
149
151
 
150
152
  /**
151
- * @param {object}
153
+ * @param {object} obj
152
154
  * @param {object} [opt]
153
155
  * @param {object} [opt.parseOptions]
154
156
  * @throws {TypeError}
155
157
  */
156
- constructor(obj, opt){
158
+ constructor(obj, opt) {
157
159
  // init an elements content
158
160
  if (!isPlainObject(obj)) throw new TypeError(XML_TE_NOBJ_EMSG);
159
161
  this.#_host = obj;
@@ -178,10 +180,10 @@ class TXmlAttributesMapper {
178
180
  }
179
181
 
180
182
  /**
181
- * @property {array}
183
+ * @property {Array}
182
184
  * @readonly
183
185
  */
184
- get entries(){
186
+ get entries() {
185
187
  let items = xObj.getXObjAttributes(
186
188
  this.#_host,
187
189
  this.#_options.settings.attributesKey,
@@ -190,11 +192,11 @@ class TXmlAttributesMapper {
190
192
  }
191
193
 
192
194
  /**
193
- * @param {ID_STRING}
194
- * @returns {bool}
195
+ * @param {string} name
196
+ * @returns {boolean}
195
197
  * @description Checks whether an attribute exists.
196
198
  */
197
- hasAttribute(name){
199
+ hasAttribute(name) {
198
200
  let result = false;
199
201
  try {
200
202
  result = xObj.checkXObjAttribute(
@@ -210,11 +212,11 @@ class TXmlAttributesMapper {
210
212
  }
211
213
 
212
214
  /**
213
- * @param {ID_STRING}
215
+ * @param {string} name
214
216
  * @returns {string}
215
217
  * @description Returns an attribute value.
216
218
  */
217
- getAttribute(name){
219
+ getAttribute(name) {
218
220
  let result = '';
219
221
  try {
220
222
  result = xObj.readXObjAttr(
@@ -230,12 +232,12 @@ class TXmlAttributesMapper {
230
232
  }
231
233
 
232
234
  /**
233
- * @param {ID_STRING}
234
- * @param {any}
235
- * @returns {bool}
235
+ * @param {string} name
236
+ * @param {any} value
237
+ * @returns {boolean}
236
238
  * @description Sets an attribute value.
237
239
  */
238
- setAttribute(name, value){
240
+ setAttribute(name, value) {
239
241
  const attrName = readAsAttrName(name);
240
242
  let result = false;
241
243
  if (attrName !== '') {
@@ -255,11 +257,11 @@ class TXmlAttributesMapper {
255
257
  }
256
258
 
257
259
  /**
258
- * @param {ID_STRING}
259
- * @returns {bool}
260
+ * @param {string} name
261
+ * @returns {boolean}
260
262
  * @description Deletes an attribute.
261
263
  */
262
- delAttribute(name){
264
+ delAttribute(name) {
263
265
  let result = false;
264
266
  try {
265
267
  result = xObj.deleteXObjAttribute(
@@ -275,10 +277,36 @@ class TXmlAttributesMapper {
275
277
  }
276
278
 
277
279
  /**
278
- * @returns {none}
280
+ * @param {string} name
281
+ * @param {string} value
282
+ * @returns {boolean}
283
+ * @since v0.0.30
284
+ * @description Renames an attribute.
285
+ */
286
+ renameAttribute(name, value) {
287
+ const newName = readAsAttrName(value);
288
+ let result = false;
289
+ if (newName !== '') {
290
+ try {
291
+ result = xObj.renameXObjAttribute(
292
+ this.#_host,
293
+ name,
294
+ newName,
295
+ this.#_options.settings.attributesKey,
296
+ );
297
+ } catch (err) {
298
+ // // TODO: verify what error is thrown
299
+ result = false;
300
+ };
301
+ };
302
+ return result;
303
+ }
304
+
305
+ /**
306
+ * @returns {void}
279
307
  * @description Deletes all attributes.
280
308
  */
281
- clear(){
309
+ clear() {
282
310
  xObj.insertXObjElement(
283
311
  this.#_host,
284
312
  this.#_options.settings.attributesKey,
@@ -295,20 +323,24 @@ class TXmlAttributesMapper {
295
323
  * @description This class implements an instance of an element controller
296
324
  */
297
325
  class TXmlElementController {
298
- #_host = null;
299
- #_options = null;
300
- #_parseOptions = null;
301
- #_attributes = null;
326
+ /** @property {?object} */
327
+ #_host;// = null;
328
+ /** @property {?object} */
329
+ #_options;// = null;
330
+ /** @property {?object} */
331
+ #_parseOptions;// = null;
332
+ /** @property {?TXmlAttributesMapper} */
333
+ #_attributes;// = null;
302
334
  #_name = null;
303
335
 
304
336
  /**
305
- * @param {object}
337
+ * @param {object} obj
306
338
  * @param {object} [opt]
307
339
  * @param {object} [opt.parseOptions]
308
- * @param {bool} [opt.proxyModeEnable=false]
340
+ * @param {boolean} [opt.proxyModeEnable=false]
309
341
  * @throws {TypeError}
310
342
  */
311
- constructor(obj, opt){
343
+ constructor(obj, opt) {
312
344
  // load options
313
345
  let _options = isPlainObject(opt) ? opt : {};
314
346
  let {
@@ -317,8 +349,7 @@ class TXmlElementController {
317
349
  } = _options;
318
350
  if (typeof proxyModeEnable !== 'boolean') proxyModeEnable = false;
319
351
  // init an element content
320
- if (isObject(obj)) {
321
- if (isArray(obj)) return new TXmlElementsListController(obj, opt);
352
+ if (isPlainObject(obj)) {
322
353
  // // TODO: split a plain object and class instances
323
354
  this.#_host = obj;
324
355
  } else {
@@ -330,20 +361,24 @@ class TXmlElementController {
330
361
  this.#_host = {};
331
362
  };
332
363
  // set parser options
333
- if (!isPlainObject(parseOptions)) {
334
- _options.parseOptions = parseOptions = {};
335
- };
336
- let { settings } = parseOptions;
337
- if (!isPlainObject(settings)) parseOptions.settings = settings = {};
338
- let { textKey } = settings;
339
- if (
340
- typeof textKey !== 'string'
341
- || ((textKey = textKey.trim()) === '')
342
- ) {
343
- textKey = XML_DEF_PARSE_OPTIONS.textKey;
364
+ if (!(parseOptions instanceof TXmlContentParseOptions)) {
365
+ parseOptions = new TXmlContentParseOptions(parseOptions);
366
+ _options.parseOptions = parseOptions;
344
367
  };
368
+ //if (!isPlainObject(parseOptions)) {
369
+ // _options.parseOptions = parseOptions = {};
370
+ //};
371
+ //let { settings } = parseOptions;
372
+ //if (!isPlainObject(settings)) parseOptions.settings = settings = {};
373
+ //let { textKey } = settings;
374
+ //if (
375
+ // typeof textKey !== 'string'
376
+ // || ((textKey = textKey.trim()) === '')
377
+ //) {
378
+ // textKey = XML_DEF_PARSE_OPTIONS.textKey;
379
+ //};
345
380
  // save options
346
- settings.textKey = textKey;
381
+ //settings.textKey = textKey;
347
382
  this.#_parseOptions = parseOptions;
348
383
  _options.proxyModeEnable = proxyModeEnable;
349
384
  this.#_options = _options;
@@ -355,7 +390,7 @@ class TXmlElementController {
355
390
  * @property {TXmlAttributesMapper}
356
391
  * @readonly
357
392
  */
358
- get attributes(){
393
+ get attributes() {
359
394
  return this.#_attributes;
360
395
  }
361
396
 
@@ -364,7 +399,7 @@ class TXmlElementController {
364
399
  * @readonly
365
400
  * @experimental
366
401
  */
367
- get name(){
402
+ get name() {
368
403
  let result = '';
369
404
  if (this.#_parseOptions.settings.compact) {
370
405
  result = xObj.readXObjParam(
@@ -380,53 +415,69 @@ class TXmlElementController {
380
415
  /**
381
416
  * @property {string}
382
417
  */
383
- get textValue(){
418
+ get textValue() {
384
419
  return xObj.readXObjParam(this.#_host, this.#_parseOptions.settings.textKey);
385
420
  }
386
421
 
387
- set textValue(value){
422
+ set textValue(value) {
388
423
  xObj.writeXObjParam(this.#_host, value, this.#_parseOptions.settings.textKey);
389
424
  }
390
425
 
391
426
  /**
392
- * @param {ID_STRING}
393
- * @returns {bool}
427
+ * @param {string} name
428
+ * @returns {boolean}
394
429
  * @description Checks whether an attribute exists.
395
430
  */
396
- hasAttribute(name){
431
+ hasAttribute(name) {
397
432
  return this.#_attributes.hasAttribute(name);
398
433
  }
399
434
 
400
435
  /**
401
- * @param {ID_STRING}
436
+ * @param {string} name
402
437
  * @returns {string}
403
438
  * @description Returns an attribute value.
404
439
  */
405
- getAttribute(name){
440
+ getAttribute(name) {
406
441
  return this.#_attributes.getAttribute(name);
407
442
  }
408
443
 
409
444
  /**
445
+ * @param {string} name
446
+ * @param {any} value
447
+ * @returns {boolean}
448
+ * @description Sets an attribute value.
410
449
  * @see TXmlAttributesMapper.setAttribute
411
450
  */
412
- setAttribute(...args){
451
+ setAttribute(...args) {
413
452
  return this.#_attributes.setAttribute(...args);
414
453
  }
415
454
 
416
455
  /**
417
- * @param {ID_STRING}
418
- * @returns {bool}
456
+ * @param {string} name
457
+ * @returns {boolean}
419
458
  * @description Deletes an attribute.
420
459
  */
421
- delAttribute(name){
460
+ delAttribute(name) {
422
461
  return this.#_attributes.delAttribute(name);
423
462
  }
424
463
 
425
464
  /**
426
- * @returns {array}
465
+ * @param {string} name
466
+ * @param {string} value
467
+ * @returns {boolean}
468
+ * @since v0.0.30
469
+ * @description Renames an attribute.
470
+ * @see TXmlAttributesMapper.renameAttribute
471
+ */
472
+ renameAttribute(...args) {
473
+ return this.#_attributes.renameAttribute(...args);
474
+ }
475
+
476
+ /**
477
+ * @returns {string[]}
427
478
  * @description Returns a text value in format of a `[ <lang>, <text> ]` pair.
428
479
  */
429
- getTextValue(){
480
+ getTextValue() {
430
481
  return [
431
482
  this.#_attributes.getAttribute(XML_LANG_ATTR_TNAME),
432
483
  this.textValue,
@@ -434,11 +485,11 @@ class TXmlElementController {
434
485
  }
435
486
 
436
487
  /**
437
- * @param {(string|entry)}
438
- * @returns {bool}
488
+ * @param {(string|string[])} value
489
+ * @returns {boolean}
439
490
  * @description Sets a text value.
440
491
  */
441
- setTextValue(value){
492
+ setTextValue(value) {
442
493
  const _value = valueToEntry(value);
443
494
  if (_value !== null) {
444
495
  const [ lang, param ] = _value;
@@ -463,8 +514,8 @@ class TXmlElementController {
463
514
  }
464
515
 
465
516
  /**
466
- * @param {ID_STRING}
467
- * @returns {bool}
517
+ * @param {string} name
518
+ * @returns {boolean}
468
519
  * @description Checks whether an element has a child element.
469
520
  */
470
521
  hasChild(name) {
@@ -482,77 +533,67 @@ class TXmlElementController {
482
533
  }
483
534
 
484
535
  /**
485
- * @param {ID_STRING}
536
+ * @param {string} name
486
537
  * @returns {(null|TXmlElementController|TXmlElementsListController)}
487
538
  * @description Returns a child element.
488
539
  */
489
540
  getChild(name) {
490
- let result = xObj.evalXObjEName(name);
491
- if (result === null) return null;
492
- try {
493
- const item = xObj.getXObjElement(this.#_host, result);
494
- if (isArray(item)) {
495
- result = new TXmlElementsListController(item, this.#_options);
496
- } else if (isObject(item)) {
497
- result = new TXmlElementController(item, this.#_options);
498
- } else {
541
+ const _name = xObj.evalXObjEName(name);
542
+ let result = null;
543
+ if (_name !== null) {
544
+ try {
545
+ const item = xObj.getXObjElement(this.#_host, _name);
546
+ result = TXmlElementController.create(item, this.#_options);
547
+ } catch (err) {
548
+ // // TODO: [?] verify what error is thrown
499
549
  result = null;
500
550
  };
501
- } catch (err) {
502
- // // TODO: [?] verify what error is thrown
503
- result = null;
504
551
  };
505
552
  return result;
506
553
  }
507
554
 
508
555
  /**
509
- * @param {ID_STRING}
510
- * @returns {object}
556
+ * @param {string} name
557
+ * @returns {?any}
511
558
  * @protected
559
+ * @deprecated
512
560
  * @description Returns a child element.
561
+ * @see TXmlElementController.__getChildRaw
513
562
  */
514
- _getChildRaw(name){
563
+ _getChildRaw(name) {
515
564
  //console.log('CHECK: TXmlElementController._getChildRaw() => was called...');
516
- //console.log('CHECK: TXmlElementController._getChildRaw() => _host => '+JSON.stringify(this.#_host, null, 2));
517
- let item = xObj.getXObjElement(this.#_host, name);
518
- //console.log('CHECK: TXmlElementController._getChildRaw() => item => '+item);
519
- //console.log('CHECK: TXmlElementController._getChildRaw() => item.name() => ['+name+']');
520
- //console.log('CHECK: TXmlElementController._getChildRaw() => item.typeof() => '+typeof item);
521
- //console.log('CHECK: TXmlElementController._getChildRaw() => item => '+JSON.stringify(item, null, 2));
522
- //console.log('CHECK: TXmlElementController._getChildRaw() => was left...');
523
- return item !== undefined ? item : null;
565
+ return TXmlElementController.__getChildRaw(this, name);
524
566
  }
525
567
 
526
568
  /**
527
- * @see TXmlElementController.__setChildRaw
569
+ * @param {string} name
570
+ * @param {object} obj
571
+ * @returns {boolean}
528
572
  * @protected
573
+ * @deprecated
529
574
  * @description Sets a child element.
575
+ * @see TXmlElementController.__setChildRaw
530
576
  */
531
- _setChildRaw(...args){
577
+ _setChildRaw(...args) {
532
578
  //console.log('CHECK: TXmlElementController._setChildRaw() => was called...');
533
579
  return TXmlElementController.__setChildRaw(this, ...args);
534
580
  }
535
581
 
536
582
  /**
537
- * @param {ID_STRING}
538
- * @returns {?object}
583
+ * @param {string} name
584
+ * @returns {?Object}
539
585
  * @protected
586
+ * @deprecated
540
587
  * @description Adds a new child element.
588
+ * @see TXmlElementController.__addChildRaw
541
589
  */
542
- _addChildRaw(name){//}, obj){
590
+ _addChildRaw(name) {
543
591
  //console.log('CHECK: TXmlElementController._addChild() => was called...');
544
- //console.log('CHECK: TXmlElementController._addChildRaw() => _host => '+JSON.stringify(this.#_host, null, 2));
545
- let result = xObj.addXObjElement(this.#_host, name);
546
- //console.log('CHECK: TXmlElementController._addChildRaw() => result => '+result);
547
- //console.log('CHECK: TXmlElementController._addChildRaw() => item.name() => ['+name+']');
548
- //console.log('CHECK: TXmlElementController._addChildRaw() => item.typeof() => '+typeof result.item);
549
- //console.log('CHECK: TXmlElementController._addChildRaw() => item => '+JSON.stringify(result.item, null, 2));
550
- //console.log('CHECK: TXmlElementController._addChildRaw() => was left...');
551
- return result.isSucceed ? result.item : null;
592
+ return TXmlElementController.__addChildRaw(this, name);
552
593
  }
553
594
 
554
595
  /**
555
- * @param {ID_STRING}
596
+ * @param {string} name
556
597
  * @returns {?TXmlElementController}
557
598
  * @description Adds a new child element.
558
599
  */
@@ -572,8 +613,8 @@ class TXmlElementController {
572
613
  }
573
614
 
574
615
  /**
575
- * @param {ID_STRING}
576
- * @returns {bool}
616
+ * @param {string} name
617
+ * @returns {boolean}
577
618
  * @description Deletes a child element.
578
619
  */
579
620
  delChild(name) {
@@ -605,62 +646,155 @@ class TXmlElementController {
605
646
  }
606
647
 
607
648
  /**
608
- * @returns {none}
649
+ * @returns {void}
609
650
  * @description Deletes all child elements and attributes.
610
651
  */
611
- clear(){
652
+ clear() {
612
653
  TXmlElementController.clear(this);
613
654
  }
614
655
 
615
656
  /**
616
- * @param {object}
657
+ * @param {string} loadFromXMLString
658
+ * @returns {boolean}
659
+ * @throws {Error}
660
+ * @experimental
661
+ * @since v0.0.30
662
+ * @description Loads a element content from a string.
663
+ */
664
+ loadFromXMLString(xmlString) {
665
+ let result = false;
666
+ if (typeof xmlString === 'string' && xmlString !== '') {
667
+ const _options = this.#_options;
668
+ let {
669
+ proxyModeEnable,
670
+ parseOptions,
671
+ } = _options;
672
+ parseOptions = parseOptions.xml2js;
673
+ parseOptions.ignoreDocType = true;
674
+ parseOptions.ignoreDeclaration = true;
675
+ try {
676
+ let obj = xmlParser.xml2js(xmlString, parseOptions);
677
+ if (isPlainObject(obj)) {
678
+ let list = Object.entries(obj);
679
+ let item = list[0];
680
+ obj = item[1];
681
+ if (isPlainObject(obj)) {
682
+ if (proxyModeEnable) {
683
+ let list = Object.entries(obj);
684
+ const target = this.#_host;
685
+ this.clear();
686
+ list.forEach((entry) => {
687
+ const [key, value] = entry;
688
+ target[key] = value;
689
+ });
690
+ } else {
691
+ this.#_host = obj;
692
+ };
693
+ this.#_attributes = new TXmlAttributesMapper(obj, _options);
694
+ result = true;
695
+ };
696
+ };
697
+ } catch (err) {
698
+ //console.log('CHECK: TXmlContentContainer.loadFromXMLString() => Error => '+err);
699
+ //console.log('CHECK: TXmlContentContainer.loadFromXMLString() => Error => '+err.code);
700
+ throw err;
701
+ };
702
+ };
703
+ return result;
704
+ }
705
+
706
+ /**
707
+ * @param {object} item
617
708
  * @returns {?object}
618
709
  * @protected
619
710
  * @static
620
711
  * @description Tries to unwraps a given object.
621
712
  */
622
- static __unwrap(item){
713
+ static __unwrap(item) {
623
714
  return (item instanceof TXmlElementController) ? item.#_host : null;
624
715
  }
625
716
 
626
717
  /**
627
- * @param {TXmlElementController}
628
- * @param {ID_STRING}
629
- * @param {object}
630
- * @returns {bool}
718
+ * @param {TXmlElementController} node
719
+ * @param {string} name
720
+ * @returns {?any}
631
721
  * @protected
632
722
  * @static
633
- * @description Sets a child element.
723
+ * @since v0.0.31
724
+ * @description Returns a child element.
634
725
  */
635
- static __setChildRaw(node, name, obj){
636
- //console.log('CHECK: TXmlElementController.__setChildRaw() => was called...');
637
- let isSUCCEED = false;
726
+ static __getChildRaw(node, name) {
727
+ let item = null;
638
728
  if (node instanceof TXmlElementController) {
639
- let item = null;
640
- if (isArray(obj)) {
641
- //console.log('CHECK: TXmlElementController.__setChildRaw() => <obj> is array...');
642
- item = xObj.insertXObjEList(node.#_host, name, { force: true });
643
- // load elements
644
- for (let element of obj) {
645
- if (isPlainObject(element)) item.push(element);
646
- };
647
- } else if (isObject(obj)) {
648
- //console.log('CHECK: TXmlElementController.__setChildRaw() => <obj> is object...');
649
- item = xObj.insertXObjElement(node.#_host, name, { force: true });
650
- // // TODO: set element
729
+ try {
730
+ const obj = node.#_host;
731
+ item = xObj.getXObjElement(obj, name);
732
+ if (item === undefined) item = null;
733
+ } catch (err) {
734
+ // // TODO: [?] check what kind of error thrown
735
+ item = null;
651
736
  };
652
- if (item) isSUCCEED = true;
653
737
  };
654
- return isSUCCEED;
738
+ return item;
739
+ }
740
+
741
+ /**
742
+ * @param {TXmlElementController} node
743
+ * @param {string} name
744
+ * @returns {?Object}
745
+ * @protected
746
+ * @static
747
+ * @since v0.0.31
748
+ * @description Adds a new child element.
749
+ */
750
+ static __addChildRaw(node, name) {
751
+ const childName = readAsTagName(name);
752
+ let item = null;
753
+ if (
754
+ node instanceof TXmlElementController
755
+ && childName !== ''
756
+ ) {
757
+ try {
758
+ const obj = node.#_host;
759
+ ({ item } = xObj.addXObjElement(obj, childName));
760
+ } catch (err) {
761
+ // // TODO: [?] check what kind of error thrown
762
+ item = null;
763
+ };
764
+ };
765
+ return item;
655
766
  }
656
767
 
657
768
  /**
658
- * @param {object}
659
- * @returns {none}
769
+ * @param {TXmlElementController} node
770
+ * @param {string} name
771
+ * @param {object} obj
772
+ * @returns {boolean}
773
+ * @protected
774
+ * @static
775
+ * @description Sets a child element.
776
+ */
777
+ static __setChildRaw(node, name, obj) {
778
+ const childName = readAsTagName(name);
779
+ let isSucceed = false;
780
+ if (
781
+ node instanceof TXmlElementController
782
+ && childName !== ''
783
+ && isObject(obj)
784
+ ) {
785
+ node.#_host[childName] = obj;
786
+ isSucceed = true;
787
+ };
788
+ return isSucceed;
789
+ }
790
+
791
+ /**
792
+ * @param {object} obj
793
+ * @returns {void}
660
794
  * @static
661
795
  * @description Tries to clean a given object.
662
796
  */
663
- static clear(obj){
797
+ static clear(obj) {
664
798
  let item = TXmlElementController.__unwrap(obj);
665
799
  if (item) {
666
800
  for (let prop in item) {
@@ -670,25 +804,52 @@ class TXmlElementController {
670
804
  };
671
805
  }
672
806
 
807
+ /**
808
+ * @param {object} obj
809
+ * @param {object} [opt]
810
+ * @returns {(null|TXmlElementController|TXmlElementsListController)}
811
+ * @static
812
+ * @since v0.0.30
813
+ * @description Creates new controller element.
814
+ */
815
+ static create(obj, opt) {
816
+ let result = null;
817
+ try {
818
+ if (isObject(obj)) {
819
+ if (isArray(obj)) {
820
+ result = new TXmlElementsListController(obj, opt);
821
+ } else {
822
+ // // TODO: [?] check what kind of object passed in
823
+ result = new TXmlElementController(obj, opt);
824
+ };
825
+ };
826
+ } catch (err) {
827
+ result = null;
828
+ };
829
+ return result;
830
+ }
831
+
673
832
  };
674
833
 
675
834
  /**
676
835
  * @description This class implements an instance of a list controller
677
836
  */
678
837
  class TXmlElementsListController {
679
- #_host = null;
680
- #_options = null;
838
+ /** @property {?Array} */
839
+ #_host;// = null;
840
+ /** @property {?object} */
841
+ #_options;// = null;
681
842
  #_count = null;
682
843
 
683
844
  /**
684
- * @param {array}
845
+ * @param {Array} obj
685
846
  * @param {object} [opt]
686
847
  * @param {object} [opt.parseOptions]
687
- * @param {bool} [opt.proxyModeEnable=false]
688
- * @param {bool} [opt.isNullItemsAllowed=false] - <reserved>
848
+ * @param {boolean} [opt.proxyModeEnable=false]
849
+ * @param {boolean} [opt.isNullItemsAllowed=false] - <reserved>
689
850
  * @throws {TypeError}
690
851
  */
691
- constructor(obj, opt){
852
+ constructor(obj, opt) {
692
853
  // load options
693
854
  let _options = isPlainObject(opt) ? opt : {};
694
855
  let {
@@ -727,7 +888,7 @@ class TXmlElementsListController {
727
888
  * @property {number}
728
889
  * @readonly
729
890
  */
730
- get count(){
891
+ get count() {
731
892
  return this.#_host.length;
732
893
  }
733
894
 
@@ -735,67 +896,67 @@ class TXmlElementsListController {
735
896
  * @property {number}
736
897
  * @readonly
737
898
  */
738
- get size(){
899
+ get size() {
739
900
  return this.#_host.length;
740
901
  }
741
902
 
742
903
  /**
743
- * @property {index}
904
+ * @property {number}
744
905
  * @readonly
745
906
  */
746
- get maxIndex(){
907
+ get maxIndex() {
747
908
  return this.#_host.length - 1;
748
909
  }
749
910
 
750
911
  /**
751
- * @property {bool}
912
+ * @property {boolean}
752
913
  * @readonly
753
914
  */
754
- get isNullItemsAllowed(){
915
+ get isNullItemsAllowed() {
755
916
  return readAsBool(this.#_options.isNullItemsAllowed);
756
917
  }
757
918
 
758
919
  /**
759
- * @returns {none}
920
+ * @returns {void}
760
921
  * @description Deletes all child elements and attributes.
761
922
  */
762
- clear(){
923
+ clear() {
763
924
  this.#_host.length = 0;
764
925
  }
765
926
 
766
927
  /**
767
- * @returns {bool}
928
+ * @returns {boolean}
768
929
  * @description Checks whether an instance holds none element.
769
930
  */
770
- isEmpty(){
931
+ isEmpty() {
771
932
  return this.count === 0;
772
933
  }
773
934
 
774
935
  /**
775
- * @returns {bool}
936
+ * @returns {boolean}
776
937
  * @description Checks whether an instance holds at least one element.
777
938
  */
778
- isNotEmpty(){
939
+ isNotEmpty() {
779
940
  return this.count > 0;
780
941
  }
781
942
 
782
943
  /**
783
- * @param {any}
784
- * @returns {bool}
944
+ * @param {any} value
945
+ * @returns {boolean}
785
946
  * @description Checks whether a given value is a valid index value
786
947
  * and it is not exceeds an index range within the instance.
787
948
  */
788
- chkIndex(value){
949
+ chkIndex(value) {
789
950
  const index = valueToIndex(value);
790
951
  return index !== -1 && index < this.size;
791
952
  }
792
953
 
793
954
  /**
794
- * @param {any}
795
- * @returns {bool}
955
+ * @param {any} obj
956
+ * @returns {boolean}
796
957
  * @description Checks whether or not a given element is valid.
797
958
  */
798
- isValidItem(obj){
959
+ isValidItem(obj) {
799
960
  return (
800
961
  (this.#_options.isNullItemsAllowed && obj === null)
801
962
  || isPlainObject(obj)
@@ -803,22 +964,22 @@ class TXmlElementsListController {
803
964
  }
804
965
 
805
966
  /**
806
- * @param {index}
967
+ * @param {number} index
807
968
  * @returns {?object}
808
969
  * @protected
809
970
  * @description Returns an element.
810
971
  */
811
- _getItemRaw(index){
972
+ _getItemRaw(index) {
812
973
  let item = this.#_host[valueToIndex(index)];
813
974
  return item !== undefined ? item : null;
814
975
  }
815
976
 
816
977
  /**
817
- * @param {index}
978
+ * @param {number} index
818
979
  * @returns {?TXmlElementController}
819
980
  * @description Returns an element.
820
981
  */
821
- getItem(index){
982
+ getItem(index) {
822
983
  let item = this.#_host[valueToIndex(index)];
823
984
  // wrap item in container
824
985
  return (
@@ -829,12 +990,12 @@ class TXmlElementsListController {
829
990
  }
830
991
 
831
992
  /**
832
- * @param {any}
833
- * @returns {index}
993
+ * @param {any} item
994
+ * @returns {number}
834
995
  * @protected
835
996
  * @description Adds a new element.
836
997
  */
837
- _addItemRaw(item){
998
+ _addItemRaw(item) {
838
999
  let index = -1;
839
1000
  if (this.isValidItem(item)) {
840
1001
  index = this.size;
@@ -844,11 +1005,11 @@ class TXmlElementsListController {
844
1005
  }
845
1006
 
846
1007
  /**
847
- * @param {any}
848
- * @returns {index}
1008
+ * @param {any} item
1009
+ * @returns {number}
849
1010
  * @description Adds a new element.
850
1011
  */
851
- addItem(item){
1012
+ addItem(item) {
852
1013
  // unwrap item from container
853
1014
  if (item instanceof TXmlElementController) {
854
1015
  item = TXmlElementController.__unwrap(item);
@@ -857,25 +1018,25 @@ class TXmlElementsListController {
857
1018
  }
858
1019
 
859
1020
  /**
860
- * @param {index}
861
- * @param {any}
862
- * @returns {bool}
1021
+ * @param {number} index
1022
+ * @param {any} item
1023
+ * @returns {boolean}
863
1024
  * @protected
864
1025
  * @description Sets a new element.
865
1026
  */
866
- _setItemRaw(index, item){
1027
+ _setItemRaw(index, item) {
867
1028
  let isSUCCEED = this.chkIndex(index) && this.isValidItem(item);
868
1029
  if (isSUCCEED) this.#_host[Number(index)] = item; // // TODO: correct count
869
1030
  return isSUCCEED;
870
1031
  }
871
1032
 
872
1033
  /**
873
- * @param {index}
874
- * @param {any}
875
- * @returns {bool}
1034
+ * @param {string} index
1035
+ * @param {any} item
1036
+ * @returns {boolean}
876
1037
  * @description Sets a new element.
877
1038
  */
878
- setItem(index, item){
1039
+ setItem(index, item) {
879
1040
  // unwrap item from container
880
1041
  if (item instanceof TXmlElementController) {
881
1042
  item = TXmlElementController.__unwrap(item);
@@ -884,13 +1045,13 @@ class TXmlElementsListController {
884
1045
  }
885
1046
 
886
1047
  /**
887
- * @param {index}
888
- * @param {any}
889
- * @returns {bool}
1048
+ * @param {number} index
1049
+ * @param {any} item
1050
+ * @returns {boolean}
890
1051
  * @protected
891
1052
  * @description Insertes a new element.
892
1053
  */
893
- _insItemRaw(index, item){
1054
+ _insItemRaw(index, item) {
894
1055
  index = valueToIndex(index);
895
1056
  let size = this.size;
896
1057
  let isSUCCEED = (
@@ -911,12 +1072,12 @@ class TXmlElementsListController {
911
1072
  }
912
1073
 
913
1074
  /**
914
- * @param {index}
915
- * @param {any}
916
- * @returns {bool}
1075
+ * @param {number} index
1076
+ * @param {any} item
1077
+ * @returns {boolean}
917
1078
  * @description Insertes a new element.
918
1079
  */
919
- insItem(index, item){
1080
+ insItem(index, item) {
920
1081
  // unwrap item from container
921
1082
  if (item instanceof TXmlElementController) {
922
1083
  item = TXmlElementController.__unwrap(item);
@@ -925,11 +1086,11 @@ class TXmlElementsListController {
925
1086
  }
926
1087
 
927
1088
  /**
928
- * @param {index}
929
- * @returns {bool}
1089
+ * @param {number} index
1090
+ * @returns {boolean}
930
1091
  * @description Deletes an element.
931
1092
  */
932
- delItem(value){
1093
+ delItem(value) {
933
1094
  let isSUCCEED = this.chkIndex(value);
934
1095
  if (isSUCCEED) {
935
1096
  let index = Number(value);
@@ -949,13 +1110,13 @@ class TXmlElementsListController {
949
1110
  }
950
1111
 
951
1112
  /**
952
- * @param {any}
1113
+ * @param {any} items
953
1114
  * @param {object} [opt]
954
- * @param {bool} [opt.useClear=true]
1115
+ * @param {boolean} [opt.useClear=true]
955
1116
  * @returns {number}
956
1117
  * @description Loads a list of an elements.
957
1118
  */
958
- loadItems(items, opt){
1119
+ loadItems(items, opt) {
959
1120
  const _options = isPlainObject(opt) ? opt : {};
960
1121
  let {
961
1122
  useClear,
@@ -974,17 +1135,20 @@ class TXmlElementsListController {
974
1135
  * that managea content of a document declaration
975
1136
  */
976
1137
  class TXmlContentDeclaration {
977
- #_host = null;
978
- #_options = null;
979
- #_ctrls = null;
1138
+ /** @property {?object} */
1139
+ #_host;// = null;
1140
+ /** @property {?object} */
1141
+ #_options;// = null;
1142
+ /** @property {?TXmlElementController} */
1143
+ #_ctrls;// = null;
980
1144
 
981
1145
  /**
982
- * @param {object}
1146
+ * @param {object} obj
983
1147
  * @param {object} [opt]
984
1148
  * @param {object} [opt.parseOptions]
985
1149
  * @throws {TypeError}
986
1150
  */
987
- constructor(obj, opt){
1151
+ constructor(obj, opt) {
988
1152
  // check <obj>-param
989
1153
  if (!isPlainObject(obj)) {
990
1154
  throw new TypeError(`TXmlContentDeclaration : ${XML_TE_NOBJ_EMSG}`);
@@ -1017,30 +1181,30 @@ class TXmlContentDeclaration {
1017
1181
  /**
1018
1182
  * @property {string}
1019
1183
  */
1020
- get version(){
1184
+ get version() {
1021
1185
  return this.#_ctrls.getAttribute('version');
1022
1186
  }
1023
1187
 
1024
- set version(value){
1188
+ set version(value) {
1025
1189
  this.#_ctrls.setAttribute('version', value);
1026
1190
  }
1027
1191
 
1028
1192
  /**
1029
1193
  * @property {string}
1030
1194
  */
1031
- get encoding(){
1195
+ get encoding() {
1032
1196
  return this.#_ctrls.getAttribute('encoding');
1033
1197
  }
1034
1198
 
1035
- set encoding(value){
1199
+ set encoding(value) {
1036
1200
  this.#_ctrls.setAttribute('encoding', value);
1037
1201
  }
1038
1202
 
1039
1203
  /**
1040
- * @returns {bool}
1204
+ * @returns {boolean}
1041
1205
  * @description Initializes an instance content.
1042
1206
  */
1043
- init(){
1207
+ init() {
1044
1208
  const _options = this.#_options;
1045
1209
  this.#_ctrls = new TXmlElementController(
1046
1210
  xObj.insertXObjElement(
@@ -1061,18 +1225,20 @@ class TXmlContentDeclaration {
1061
1225
  * @description This class implements an instance of a root element
1062
1226
  */
1063
1227
  class TXmlContentRootElement extends TXmlElementController {
1064
- #_content = null;
1065
- #_options = null;
1228
+ /** @property {?object} */
1229
+ #_content;// = null;
1230
+ /** @property {?object} */
1231
+ #_options;// = null;
1066
1232
 
1067
1233
  /**
1068
- * @param {object}
1234
+ * @param {object} obj
1069
1235
  * @param {object} [opt]
1070
1236
  * @param {object} [opt.parseOptions]
1071
- * @param {ID_STRING} [opt.rootETagName='']
1072
- * @param {bool} [opt.autoBindRoot=true]
1237
+ * @param {string} [opt.rootETagName='']
1238
+ * @param {boolean} [opt.autoBindRoot=true]
1073
1239
  * @throws {TypeError}
1074
1240
  */
1075
- constructor(obj, opt){
1241
+ constructor(obj, opt) {
1076
1242
  // check <obj>-param
1077
1243
  if (!isPlainObject(obj)) {
1078
1244
  throw new TypeError(`TXmlContentRootElement : ${XML_TE_NOBJ_EMSG}`);
@@ -1160,11 +1326,11 @@ class TXmlContentRootElement extends TXmlElementController {
1160
1326
  /**
1161
1327
  * @property {string}
1162
1328
  */
1163
- get tagName(){
1329
+ get tagName() {
1164
1330
  return this.#_options.rootETagName;
1165
1331
  }
1166
1332
 
1167
- set tagName(value){
1333
+ set tagName(value) {
1168
1334
  value = readAsString(value, true);
1169
1335
  this.#_options.rootETagName = isNotEmptyString(
1170
1336
  value
@@ -1172,14 +1338,14 @@ class TXmlContentRootElement extends TXmlElementController {
1172
1338
  }
1173
1339
 
1174
1340
  /**
1175
- * @param {object}
1176
- * @param {bool} [opt=false]
1341
+ * @param {object} item
1342
+ * @param {boolean} [opt=false]
1177
1343
  * @returns {?object}
1178
1344
  * @protected
1179
1345
  * @static
1180
1346
  * @description Tries to unwraps a given object.
1181
1347
  */
1182
- static __unwrap(item, opt){
1348
+ static __unwrap(item, opt) {
1183
1349
  return (
1184
1350
  item instanceof TXmlContentRootElement
1185
1351
  && (typeof opt === 'boolean' ? opt : false)
@@ -1195,17 +1361,22 @@ class TXmlContentRootElement extends TXmlElementController {
1195
1361
  * that managea content of a document
1196
1362
  */
1197
1363
  class TXmlContentContainer {
1198
- #_content = null;
1199
- #_options = null;
1200
- #_parseOptions = null;
1201
- #_docDecl = null;
1202
- #_docRoot = null;
1364
+ /** @property {?object} */
1365
+ #_content;// = null;
1366
+ /** @property {?object} */
1367
+ #_options;// = null;
1368
+ /** @property {?object} */
1369
+ #_parseOptions;// = null;
1370
+ /** @property {?TXmlContentDeclaration} */
1371
+ #_docDecl;// = null;
1372
+ /** @property {?TXmlContentRootElement} */
1373
+ #_docRoot;// = null;
1203
1374
 
1204
1375
  /**
1205
1376
  * @param {object} [opt]
1206
1377
  * @param {object} [opt.parseOptions]
1207
1378
  */
1208
- constructor(opt){
1379
+ constructor(opt) {
1209
1380
  // load options
1210
1381
  const _options = isPlainObject(opt) ? opt : {};
1211
1382
  let { parseOptions } = _options;
@@ -1230,26 +1401,27 @@ class TXmlContentContainer {
1230
1401
  * @property {TXmlContentRootElement}
1231
1402
  * @readonly
1232
1403
  */
1233
- get rootElement(){
1404
+ get rootElement() {
1234
1405
  return this.#_docRoot;
1235
1406
  }
1236
1407
 
1237
1408
  /**
1238
- * @returns {none}
1409
+ * @returns {void}
1239
1410
  * @description Cleans a document content.
1240
1411
  */
1241
- clear(){
1412
+ clear() {
1242
1413
  this.#_docDecl.init();
1243
1414
  this.rootElement.clear();
1244
1415
  }
1245
1416
 
1246
1417
  /**
1247
- * @param {object}
1248
- * @returns {bool}
1418
+ * @param {object} obj
1419
+ * @param {object} [opt]
1420
+ * @returns {boolean}
1249
1421
  * @protected
1250
1422
  * @description Initializes an instance with a given content.
1251
1423
  */
1252
- _bindContent(obj, opt){
1424
+ _bindContent(obj, opt) {
1253
1425
  const _options = isPlainObject(opt) ? opt : this.#_options;
1254
1426
  let isSUCCEED = false;
1255
1427
  if (isPlainObject(obj)) {
@@ -1281,7 +1453,7 @@ class TXmlContentContainer {
1281
1453
  * @returns {string}
1282
1454
  * @description Returns a document content as a string in an XML-format.
1283
1455
  */
1284
- saveToXMLString(){
1456
+ saveToXMLString() {
1285
1457
  let result = '';
1286
1458
  try {
1287
1459
  result = xmlParser.js2xml(this.#_content, this.#_parseOptions.js2xml);
@@ -1293,13 +1465,13 @@ class TXmlContentContainer {
1293
1465
  }
1294
1466
 
1295
1467
  /**
1296
- * @param {string}
1468
+ * @param {string} source
1297
1469
  * @returns {object}
1298
1470
  * @throws {Error}
1299
1471
  * @async
1300
1472
  * @description Saves a document content to a file.
1301
1473
  */
1302
- saveToFile(source){
1474
+ saveToFile(source) {
1303
1475
  /**/// main part that return promise as a result
1304
1476
  return new Promise((resolve, reject) => {
1305
1477
  let data = {
@@ -1337,12 +1509,12 @@ class TXmlContentContainer {
1337
1509
  }
1338
1510
 
1339
1511
  /**
1340
- * @param {string}
1512
+ * @param {string} source
1341
1513
  * @returns {object}
1342
1514
  * @throws {Error}
1343
1515
  * @description Saves a document content to a file.
1344
1516
  */
1345
- saveToFileSync(source){
1517
+ saveToFileSync(source) {
1346
1518
  let data = {
1347
1519
  isSucceed: false,
1348
1520
  source: typeof source === 'string' ? source.trim() : '',
@@ -1374,12 +1546,12 @@ class TXmlContentContainer {
1374
1546
  }
1375
1547
 
1376
1548
  /**
1377
- * @param {string}
1378
- * @returns {bool}
1549
+ * @param {string} loadFromXMLString
1550
+ * @returns {boolean}
1379
1551
  * @throws {Error}
1380
1552
  * @description Loads a document content from a string.
1381
1553
  */
1382
- loadFromXMLString(xmlString){
1554
+ loadFromXMLString(xmlString) {
1383
1555
  let isSUCCEED = false;
1384
1556
  if (typeof xmlString === 'string') {
1385
1557
  let obj = null;
@@ -1398,13 +1570,13 @@ class TXmlContentContainer {
1398
1570
  }
1399
1571
 
1400
1572
  /**
1401
- * @param {string}
1573
+ * @param {string} source
1402
1574
  * @returns {object}
1403
1575
  * @throws {Error}
1404
1576
  * @async
1405
1577
  * @description Loads a document content from a file.
1406
1578
  */
1407
- loadFromFile(source){
1579
+ loadFromFile(source) {
1408
1580
  /**/// main part that return promise as a result
1409
1581
  return new Promise((resolve, reject) => {
1410
1582
  let data = {
@@ -1452,12 +1624,12 @@ class TXmlContentContainer {
1452
1624
  }
1453
1625
 
1454
1626
  /**
1455
- * @param {string}
1627
+ * @param {string} source
1456
1628
  * @returns {object}
1457
1629
  * @throws {Error}
1458
1630
  * @description Loads a document content from a file.
1459
1631
  */
1460
- loadFromFileSync(source){
1632
+ loadFromFileSync(source) {
1461
1633
  let data = {
1462
1634
  isLoaded: false,
1463
1635
  source: typeof source === 'string' ? source.trim() : '',
@@ -1504,7 +1676,7 @@ class TXmlContentContainer {
1504
1676
  * @protected
1505
1677
  * @description Returns a document content a string in a JSON-format.
1506
1678
  */
1507
- asJSON(){
1679
+ asJSON() {
1508
1680
  return JSON.stringify(this.#_content, null, 2);
1509
1681
  }
1510
1682