@cntwg/xml-lib-js 0.0.26 → 0.0.28

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 CHANGED
@@ -1,3 +1,19 @@
1
+ #### *v0.0.28*
2
+
3
+ Pre-release version.
4
+
5
+ > - `xmldoc-lib.md` updated;
6
+ > - add functions: readAsTagName, `readAsAttrName`, valueToElementID;
7
+ > - some fixes in `xmldoc-lib.js` module.
8
+
9
+ #### *v0.0.27*
10
+
11
+ Pre-release version.
12
+
13
+ > - `xmldoc-lib.md` updated;
14
+ > - fix error in a `addChildByPath` function;
15
+ > - add functions (`xmlHelper` module): `extractTextValues`, `extractTextValuesLn`.
16
+
1
17
  #### *v0.0.26*
2
18
 
3
19
  Pre-release version.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
- |***rev.*:**|0.1.5|
1
+ |***rev.*:**|0.1.9|
2
2
  |:---|---:|
3
- |***date***:|2024-06-26|
3
+ |***date***:|2024-08-01|
4
4
 
5
5
  ## Introduction
6
6
 
@@ -21,6 +21,12 @@ xObj-manipulator is a set of functions (see docs for [`@ygracs/xobj-lib-js` modu
21
21
  + `XML_DEF_ROOT_ETAG_NAME`;
22
22
  + `XML_DEF_PARSE_OPTIONS`;
23
23
 
24
+ - functions:
25
+
26
+ + `readAsTagName` (*experimental*);
27
+ + `readAsAttrName` (*experimental*);
28
+ + `valueToElementID` (*experimental*);
29
+
24
30
  - classes:
25
31
 
26
32
  + `TXmlAttributesMapper`;
@@ -33,12 +39,14 @@ xObj-manipulator is a set of functions (see docs for [`@ygracs/xobj-lib-js` modu
33
39
 
34
40
  For more read the `xmldoc-lib.md` in the project `doc` directory.
35
41
 
36
- ### 3. an `xmlHelper` module extention
42
+ ### 3. an `xmlHelper` module extension
37
43
 
38
44
  - functions:
39
45
 
40
46
  + `getChildByPath`;
41
- + `addChildByPath`.
47
+ + `addChildByPath`;
48
+ + `extractTextValues`;
49
+ + `extractTextValuesLn`.
42
50
 
43
51
  For more read the `xmldoc-lib.md` in the project `doc` directory.
44
52
 
package/doc/xmldoc-lib.md CHANGED
@@ -1,10 +1,10 @@
1
- >|***rev.*:**|0.1.23|
1
+ >|***rev.*:**|0.1.27|
2
2
  >|:---|---:|
3
- >|date:|2024-06-26|
3
+ >|date:|2024-08-01|
4
4
 
5
5
  ## Introduction
6
6
 
7
- This paper describes a constants and an object classes provided by `xmldoc-lib.js` module.
7
+ This paper describes a constants, functions and an object classes provided by `xmldoc-lib.js` module.
8
8
 
9
9
  ## Module constants
10
10
 
@@ -41,6 +41,34 @@ The settings listed in the table below:
41
41
 
42
42
  This constant defines a default value for naming the root element for XML-document. Its value is `root`.
43
43
 
44
+ ### **XML\_LANG\_ATTR\_TNAME**
45
+
46
+ This constant defines a default value for naming the language attribute for XML-element. Its value is `lang`.
47
+
48
+ ## Module functions
49
+
50
+ ### Experimental functions
51
+
52
+ > Note: Purpose of those functions will be discussed and some may be deprecate and make obsolete or functionality may be altered. So use it with cautions in mind.
53
+
54
+ #### **readAsTagName(value)**
55
+
56
+ > since: `v0.0.28`
57
+
58
+ This function tries to convert a given `value` to the value of type which is suitable for an XML-element's tag name. If failed an empty string is returned.
59
+
60
+ #### **readAsAttrName(value)**
61
+
62
+ > since: `v0.0.28`
63
+
64
+ This function tries to convert a given `value` to the value of type which is suitable for an XML-element's attribute name. If failed an empty string is returned.
65
+
66
+ #### **valueToElementID(value)**
67
+
68
+ > since: `v0.0.28`
69
+
70
+ This function tries to convert a given `value` to the value of a valid identifier which is suitable for an XML-element's ID-attribute. If failed an empty string is returned.
71
+
44
72
  ## Module classes
45
73
 
46
74
  ### **TXmlContentParseOptions**
@@ -200,7 +228,7 @@ This method checks whether or not a child element addressed by `name` is exists.
200
228
 
201
229
  ##### **getChild(name)**
202
230
 
203
- This method returns a child element addressed by `name`.
231
+ This method returns a child element addressed by `name`. If failed `null` is returned.
204
232
 
205
233
  ##### **addChild(name)**
206
234
 
@@ -210,7 +238,7 @@ This method adds an element as a child element addressed by a `name`.
210
238
 
211
239
  This method deletes a child element addressed by a `name`.
212
240
 
213
- > Warning: method not fully implemented. not used for list like elements.
241
+ > Warning: A method not fully implemented yet. Not use it for list like elements. If list contains a more than one element, a method will fail.
214
242
 
215
243
  ##### **clear()**
216
244
 
@@ -526,3 +554,15 @@ The `chain` parameter contains an array of an element names.
526
554
  This function tries to add a new child element into a given object following path provided by `chain` parameter. If succeed a new element returned. If failed `null` is returned.
527
555
 
528
556
  The `chain` parameter contains an array of an element names. The last name in `chain` is a name of the element which will be added.
557
+
558
+ #### **extractTextValues(object)**
559
+
560
+ > since: \[v0.0.27]
561
+
562
+ This function tries to get a text value from a given element.
563
+
564
+ #### **extractTextValuesLn(object)**
565
+
566
+ > since: \[v0.0.27]
567
+
568
+ This function tries to get a text value with its language attribute from a given element.
package/index.js CHANGED
@@ -1,4 +1,4 @@
1
- // [v0.1.012-20240626]
1
+ // [v0.1.016-20240801]
2
2
 
3
3
  // === module init block ===
4
4
 
@@ -14,9 +14,15 @@ const xmlHelper = require('./lib/xml-helper.js');
14
14
 
15
15
  module.exports.xObj = xObj;
16
16
  module.exports.xmlHelper = xmlHelper;
17
+ module.exports.xmlDoc = xmldoc;
17
18
 
18
19
  module.exports.XML_DEF_PARSE_OPTIONS = xmldoc.XML_DEF_PARSE_OPTIONS;
19
20
  module.exports.XML_DEF_ROOT_ETAG_NAME = xmldoc.XML_DEF_ROOT_ETAG_NAME;
21
+ module.exports.XML_LANG_ATTR_TNAME = xmldoc.XML_LANG_ATTR_TNAME;
22
+
23
+ module.exports.readAsTagName = xmldoc.readAsTagName;
24
+ module.exports.readAsAttrName = xmldoc.readAsAttrName;
25
+ module.exports.valueToElementID = xmldoc.valueToElementID;
20
26
 
21
27
  module.exports.TXmlContentParseOptions = xmldoc.TXmlContentParseOptions;
22
28
 
package/lib/xml-helper.js CHANGED
@@ -1,4 +1,4 @@
1
- // [v0.0.001-20240626]
1
+ // [v0.1.004-20240702]
2
2
 
3
3
  // === module init block ===
4
4
 
@@ -8,6 +8,7 @@ const {
8
8
 
9
9
  const {
10
10
  TXmlElementController,
11
+ TXmlElementsListController,
11
12
  } = require('./xmldoc-lib');
12
13
 
13
14
  // === module extra block (helper functions) ===
@@ -60,6 +61,7 @@ function getChildByPath(obj, chain) {
60
61
  * @description Tries to get a child element.
61
62
  */
62
63
  function addChildByPath(obj, chain){
64
+ let item = obj;
63
65
  let child = null;
64
66
  if (obj instanceof TXmlElementController) {
65
67
  const list = valueToArray(chain);
@@ -71,13 +73,63 @@ function addChildByPath(obj, chain){
71
73
  if ((child instanceof TXmlElementController)) {
72
74
  item = child;
73
75
  } else {
74
- child = null; break;
76
+ child = null;
77
+ break;
75
78
  };
76
79
  };
77
80
  };
78
81
  return child;
79
82
  };
80
83
 
84
+ /**
85
+ * @function extractTextValues
86
+ * @param {(TXmlElementController|TXmlElementsListController)}
87
+ * @returns {array}
88
+ * @description Reads a text values from a given XML-element.
89
+ */
90
+ function extractTextValues(obj){
91
+ const pushValue = (result, item) => {
92
+ if (item instanceof TXmlElementController) {
93
+ let value = item.textValue;
94
+ if (value !== '') result.push(value);
95
+ };
96
+ };
97
+ let result = [];
98
+ if (obj instanceof TXmlElementsListController) {
99
+ for (let i = 0; i < obj.size; i++) {
100
+ pushValue(result, obj.getItem(i));
101
+ };
102
+ } else {
103
+ pushValue(result, obj);
104
+ };
105
+ return result;
106
+ };
107
+
108
+ /**
109
+ * @function extractTextValuesLn
110
+ * @param {(TXmlElementController|TXmlElementsListController)}
111
+ * @returns {array}
112
+ * @description Reads a text values with a `lang` attribute
113
+ * from a given XML-element.
114
+ */
115
+ function extractTextValuesLn(obj){
116
+ const pushValue = (result, item) => {
117
+ if (item instanceof TXmlElementController) {
118
+ let [ lang, value ] = item.getTextValue();
119
+ if (value !== '') result.push([ lang, value ]);
120
+ };
121
+ };
122
+ let result = [];
123
+ if (obj instanceof TXmlElementsListController) {
124
+ for (let i = 0; i < obj.size; i++) {
125
+ pushValue(result, obj.getItem(i));
126
+ };
127
+ } else {
128
+ pushValue(result, obj);
129
+ };
130
+ return result;
131
+ };
132
+
81
133
  /***
82
134
  * (* class definitions *)
83
135
  */
@@ -86,3 +138,5 @@ function addChildByPath(obj, chain){
86
138
 
87
139
  module.exports.getChildByPath = getChildByPath;
88
140
  module.exports.addChildByPath = addChildByPath;
141
+ module.exports.extractTextValues = extractTextValues;
142
+ module.exports.extractTextValuesLn = extractTextValuesLn;
package/lib/xmldoc-lib.js CHANGED
@@ -1,4 +1,4 @@
1
- // [v0.1.077-20240625]
1
+ // [v0.1.085-20240801]
2
2
 
3
3
  // === module init block ===
4
4
 
@@ -10,6 +10,7 @@ const {
10
10
  valueToIndex, readAsBool, readAsString, isNotEmptyString,
11
11
  isArray, isObject, isPlainObject,
12
12
  valueToArray, valueToEntry,
13
+ valueToIDString,
13
14
  } = require('@ygracs/bsfoc-lib-js');
14
15
 
15
16
  const xObj = require('@ygracs/xobj-lib-js');
@@ -66,6 +67,75 @@ const XML_TE_NROOT_EMSG = 'root element not found';
66
67
  * (* function definitions *)
67
68
  */
68
69
 
70
+ /**
71
+ * @function readAsTagName
72
+ * @param {any}
73
+ * @return {string}
74
+ * @since v0.0.28
75
+ * @description Tries to convert a given value to a valid tag name
76
+ * of an XML-element.
77
+ */
78
+ function readAsTagName(value) {
79
+ let tagName = valueToIDString(value, { ignoreNumbers: true });
80
+ if (tagName === null) return '';
81
+ if (tagName !== '') {
82
+ // // TODO: do extra checks
83
+ const template = /[\s\/\\\"\'<>=]/;
84
+ const trigger = tagName.match(template);
85
+ if (trigger) {
86
+ //console.log(trigger);
87
+ tagName = '';
88
+ };
89
+ };
90
+ return tagName;
91
+ };
92
+
93
+ /**
94
+ * @function readAsAttrName
95
+ * @param {any}
96
+ * @return {string}
97
+ * @since v0.0.28
98
+ * @description Tries to convert a given value to a valid name
99
+ * of an XML-attribute.
100
+ */
101
+ function readAsAttrName(value) {
102
+ let attrName = valueToIDString(value, { ignoreNumbers: true });
103
+ if (attrName === null) return '';
104
+ if (attrName !== '') {
105
+ // // TODO: do extra checks
106
+ const template = /[\s\/\\\"\'<>=]/;
107
+ const trigger = attrName.match(template);
108
+ if (trigger) {
109
+ //console.log(trigger);
110
+ attrName = '';
111
+ };
112
+ };
113
+ return attrName;
114
+ };
115
+
116
+ /**
117
+ * @function valueToElementID
118
+ * @param {any}
119
+ * @return {string}
120
+ * @since v0.0.28
121
+ * @description Tries to convert a given value to a valid identifier
122
+ * suitable as a value for an "ID-attribute" of an XML-element.
123
+ */
124
+ function valueToElementID(value) {
125
+ let id = valueToIDString(value);
126
+ if (id === null) return '';
127
+ if (id !== '') {
128
+ // // TODO: do extra checks
129
+ const template = /[\s\/\\\"\'<>=]/;
130
+ const trigger = id.match(template);
131
+ if (trigger) {
132
+ //console.log(trigger);
133
+ id = '';
134
+ };
135
+ };
136
+ return id;
137
+ };
138
+
69
139
  /***
70
140
  * (* class definitions *)
71
141
  */
@@ -166,17 +236,20 @@ class TXmlAttributesMapper {
166
236
  * @description Sets an attribute value.
167
237
  */
168
238
  setAttribute(name, value){
239
+ const attrName = readAsAttrName(name);
169
240
  let result = false;
170
- try {
171
- result = xObj.writeXObjAttr(
172
- this.#_host,
173
- name,
174
- value,
175
- this.#_options.settings.attributesKey,
176
- );
177
- } catch (err) {
178
- // // TODO: verify what error is thrown
179
- result = false;
241
+ if (attrName !== '') {
242
+ try {
243
+ result = xObj.writeXObjAttr(
244
+ this.#_host,
245
+ name,
246
+ value,
247
+ this.#_options.settings.attributesKey,
248
+ );
249
+ } catch (err) {
250
+ // // TODO: verify what error is thrown
251
+ result = false;
252
+ };
180
253
  };
181
254
  return result;
182
255
  }
@@ -394,16 +467,16 @@ class TXmlElementController {
394
467
  * @returns {bool}
395
468
  * @description Checks whether an element has a child element.
396
469
  */
397
- hasChild(name){
470
+ hasChild(name) {
398
471
  const _name = xObj.evalXObjEName(name);
472
+ if (_name === null) return false;
399
473
  let result = false;
400
- if (typeof _name === 'string' && _name !== '') {
401
- try {
402
- result = isObject(xObj.getXObjElement(this.#_host, name));
403
- } catch (err) {
404
- // // TODO: [?] verify what error is thrown
405
- throw err;
406
- };
474
+ try {
475
+ result = isObject(xObj.getXObjElement(this.#_host, _name));
476
+ } catch (err) {
477
+ // // TODO: [?] verify what error is thrown
478
+ //throw err;
479
+ result = false;
407
480
  };
408
481
  return result;
409
482
  }
@@ -413,21 +486,21 @@ class TXmlElementController {
413
486
  * @returns {(null|TXmlElementController|TXmlElementsListController)}
414
487
  * @description Returns a child element.
415
488
  */
416
- getChild(name){
417
- const _name = xObj.evalXObjEName(name);
418
- let result = null;
419
- if (typeof _name === 'string' && _name !== '') {
420
- try {
421
- const item = xObj.getXObjElement(this.#_host, name);
422
- if (isArray(item)) {
423
- result = new TXmlElementsListController(item, this.#_options);
424
- } else if (isObject(item)) {
425
- result = new TXmlElementController(item, this.#_options);
426
- };
427
- } catch (err) {
428
- // // TODO: [?] verify what error is thrown
429
- throw err;
489
+ 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 {
499
+ result = null;
430
500
  };
501
+ } catch (err) {
502
+ // // TODO: [?] verify what error is thrown
503
+ result = null;
431
504
  };
432
505
  return result;
433
506
  }
@@ -483,17 +556,17 @@ class TXmlElementController {
483
556
  * @returns {?TXmlElementController}
484
557
  * @description Adds a new child element.
485
558
  */
486
- addChild(name){
559
+ addChild(name) {
487
560
  const _name = xObj.evalXObjEName(name);
561
+ if (_name === null) return null;
488
562
  let result = null;
489
- if (typeof _name === 'string' && _name !== '') {
490
- try {
491
- let { item } = xObj.addXObjElement(this.#_host, name);
492
- if (item) result = new TXmlElementController(item, this.#_options)
493
- } catch (err) {
494
- // // TODO: [?] verify what error is thrown
495
- throw err;
496
- };
563
+ try {
564
+ const { item } = xObj.addXObjElement(this.#_host, _name);
565
+ if (item) result = new TXmlElementController(item, this.#_options)
566
+ } catch (err) {
567
+ // // TODO: [?] verify what error is thrown
568
+ //throw err;
569
+ result = null;
497
570
  };
498
571
  return result;
499
572
  }
@@ -503,33 +576,32 @@ class TXmlElementController {
503
576
  * @returns {bool}
504
577
  * @description Deletes a child element.
505
578
  */
506
- delChild(name){
579
+ delChild(name) {
507
580
  const _name = xObj.evalXObjEName(name);
508
- let isSUCCEED = false;
509
- if (typeof _name === 'string' && _name !== '') {
510
- try {
511
- const item = xObj.getXObjElement(this.#_host, name);
512
- let isACCEPTED = false;
513
- if (isArray(item)) {
514
- //console.log('CHECK: TXmlElementController.delChild() => item.typeof() => [object Array]');
515
- if (item.length < 2) {
516
- isACCEPTED = true;
517
- } else {
518
- //console.log('CHECK: TXmlElementController.delChild() => item.length() => ['+item.length+']');
519
- };
520
- } else if (isObject(item)) {
581
+ if (_name === null) return false;
582
+ let isSucceed = false;
583
+ try {
584
+ const item = xObj.getXObjElement(this.#_host, _name);
585
+ let isACCEPTED = false;
586
+ if (isArray(item)) {
587
+ //console.log('CHECK: TXmlElementController.delChild() => item.typeof() => [object Array]');
588
+ if (item.length < 2) {
521
589
  isACCEPTED = true;
522
590
  } else {
523
- //console.log('CHECK: TXmlElementController.delChild() => item.typeof() => ['+typeof item+']');
524
- //console.log('CHECK: TXmlElementController.delChild() => item.value() => ['+item+']');
591
+ //console.log('CHECK: TXmlElementController.delChild() => item.length() => ['+item.length+']');
525
592
  };
526
- if (isACCEPTED) isSUCCEED = xObj.deleteXObjElement(this.#_host, name);
527
- } catch (err) {
528
- // // TODO: [?] verify what error is thrown
529
- throw err;
593
+ } else {
594
+ isACCEPTED = item !== null;
595
+ //console.log('CHECK: TXmlElementController.delChild() => item.typeof() => ['+typeof item+']');
596
+ //console.log('CHECK: TXmlElementController.delChild() => item.value() => ['+item+']');
530
597
  };
598
+ if (isACCEPTED) isSucceed = xObj.deleteXObjElement(this.#_host, _name);
599
+ } catch (err) {
600
+ // // TODO: [?] verify what error is thrown
601
+ //throw err;
602
+ isSucceed = false;
531
603
  };
532
- return isSUCCEED;
604
+ return isSucceed;
533
605
  }
534
606
 
535
607
  /**
@@ -1440,13 +1512,18 @@ class TXmlContentContainer {
1440
1512
 
1441
1513
  // === module exports block ===
1442
1514
 
1443
- exports.XML_DEF_PARSE_OPTIONS = XML_DEF_PARSE_OPTIONS;
1444
- exports.XML_DEF_ROOT_ETAG_NAME = XML_DEF_ROOT_ETAG_NAME;
1445
-
1446
- exports.TXmlAttributesMapper = TXmlAttributesMapper;
1447
- exports.TXmlElementController = TXmlElementController;
1448
- exports.TXmlElementsListController = TXmlElementsListController;
1449
- exports.TXmlContentParseOptions = TXmlContentParseOptions;
1450
- exports.TXmlContentDeclaration = TXmlContentDeclaration;
1451
- exports.TXmlContentRootElement = TXmlContentRootElement;
1452
- exports.TXmlContentContainer = TXmlContentContainer;
1515
+ module.exports.XML_DEF_PARSE_OPTIONS = XML_DEF_PARSE_OPTIONS;
1516
+ module.exports.XML_DEF_ROOT_ETAG_NAME = XML_DEF_ROOT_ETAG_NAME;
1517
+ module.exports.XML_LANG_ATTR_TNAME = XML_LANG_ATTR_TNAME;
1518
+
1519
+ module.exports.readAsTagName = readAsTagName;
1520
+ module.exports.readAsAttrName = readAsAttrName;
1521
+ module.exports.valueToElementID = valueToElementID;
1522
+
1523
+ module.exports.TXmlAttributesMapper = TXmlAttributesMapper;
1524
+ module.exports.TXmlElementController = TXmlElementController;
1525
+ module.exports.TXmlElementsListController = TXmlElementsListController;
1526
+ module.exports.TXmlContentParseOptions = TXmlContentParseOptions;
1527
+ module.exports.TXmlContentDeclaration = TXmlContentDeclaration;
1528
+ module.exports.TXmlContentRootElement = TXmlContentRootElement;
1529
+ module.exports.TXmlContentContainer = TXmlContentContainer;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cntwg/xml-lib-js",
3
- "version": "0.0.26",
3
+ "version": "0.0.28",
4
4
  "description": "A library for handling an XML-documents",
5
5
  "author": "ygracs <cs70th-om@rambler.ru>",
6
6
  "license": "MIT",