@cntwg/xml-lib-js 0.0.33 → 0.0.35

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,24 @@
1
+ #### *v0.0.35*
2
+
3
+ Pre-release version.
4
+
5
+ > - update dependency on `@ygracs/bsfoc-lib-js` module to v0.3.0;
6
+ > - update dependency on `@ygracs/xobj-lib-js` module to v0.2.4;
7
+ > - move some functions from `$lib/xmldoc-lib.js` into new `$lib/xml-base.js` module;
8
+ > - add function: `readAsAttrValue`.
9
+
10
+ #### *v0.0.34*
11
+
12
+ Pre-release version.
13
+
14
+ > - update `xmldoc-lib.md`;
15
+ > - update dependency on `@ygracs/xobj-lib-js` module to v0.2.3;
16
+ > - (`TXmlElementController`) remove methods: `_getChildRaw`, `_addChildRaw`, `_setChildRaw`;
17
+ > - (`TXmlElementsListController`) add special static method: `__getItemRaw`;
18
+ > - (`TXmlElementsListController`) add special static method: `__setItemRaw`;
19
+ > - (`TXmlElementsListController`) deprecate methods: `_getItemRaw`, `_setItemRaw`;
20
+ > - (`TXmlElementsListController`) add iterator method.
21
+
1
22
  #### *v0.0.33*
2
23
 
3
24
  Pre-release version.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
- |***rev.*:**|0.1.9|
1
+ |***rev.*:**|0.1.10|
2
2
  |:---|---:|
3
- |***date***:|2024-08-01|
3
+ |***date***:|2024-08-23|
4
4
 
5
5
  ## Introduction
6
6
 
@@ -25,6 +25,7 @@ xObj-manipulator is a set of functions (see docs for [`@ygracs/xobj-lib-js` modu
25
25
 
26
26
  + `readAsTagName` (*experimental*);
27
27
  + `readAsAttrName` (*experimental*);
28
+ + `readAsAttrValue` (*experimental*);
28
29
  + `valueToElementID` (*experimental*);
29
30
 
30
31
  - classes:
package/doc/xmldoc-lib.md CHANGED
@@ -1,6 +1,6 @@
1
- >|***rev.*:**|0.1.37|
1
+ >|***rev.*:**|0.1.41|
2
2
  >|:---|---:|
3
- >|date:|2025-02-06|
3
+ >|date:|2025-08-23|
4
4
 
5
5
  ## Introduction
6
6
 
@@ -91,6 +91,17 @@ This function tries to convert a given `value` to the value of type which is sui
91
91
  |:---|---|---:|:---|
92
92
  | `value` | `any` | --- | some value to read |
93
93
 
94
+ <a name="readAsAttrValue"></a>
95
+ #### **readAsAttrValue(value)** => `string`
96
+
97
+ > since: \[`v0.0.35`]
98
+
99
+ This function tries to convert a given `value` to the value of type which is suitable for an XML-element's attribute. If failed a `null` is returned.
100
+
101
+ | parameter name | value type | default value | description |
102
+ |:---|---|---:|:---|
103
+ | `value` | `any` | --- | some value |
104
+
94
105
  <a name="valueToElementID"></a>
95
106
  #### **valueToElementID(value)**
96
107
 
@@ -409,20 +420,6 @@ This method loads an element content from a string given by a `str` parameter.
409
420
 
410
421
  This methods throws `Error` if something goes wrong through a parsing process of a string.
411
422
 
412
- #### class methods (*special*)
413
-
414
- ##### **_getChildRaw(name)** => `?any`
415
-
416
- > WARNING: **\[since `v0.0.31`]** this method deprecated (*use static `__getChildRaw` method of the class*).
417
-
418
- ##### **_addChildRaw(name)** => `?object`
419
-
420
- > WARNING: **\[since `v0.0.31`]** this method deprecated (*use static `__addChildRaw` method of the class*).
421
-
422
- ##### **_setChildRaw(name, obj)** => `boolean`
423
-
424
- > WARNING: **\[since `v0.0.31`]** this method deprecated (*use static `__setChildRaw` method of the class*).
425
-
426
423
  #### class methods (*static*)
427
424
 
428
425
  ##### **clear(item)** => `void`
@@ -455,6 +452,8 @@ This method adds an element addressed by `name` as a child element.
455
452
 
456
453
  ##### **__setChildRaw(node, name, obj)** => `boolean`
457
454
 
455
+ > since: \[`v0.0.31`]
456
+
458
457
  This method inserts a given object as a child element addressed by `name`.
459
458
 
460
459
  <a name="TXmlElementsListController"></a>
@@ -619,7 +618,9 @@ The `options` structure is listed below:
619
618
 
620
619
  #### class methods (*special*)
621
620
 
622
- ##### **_getItemRaw(name)**
621
+ ##### **_getItemRaw(index)**
622
+
623
+ > WARNING: \[since `v0.0.34`] this method deprecated (*use `__getItemRaw`, a static method of the class, instead*).
623
624
 
624
625
  This method returns an element addressed by `index` as object. If failed `null` is returned.
625
626
 
@@ -629,12 +630,30 @@ This method adds an element to a list members as object. If succeed an index of
629
630
 
630
631
  ##### **_setItemRaw(index, obj)**
631
632
 
633
+ > WARNING: \[since `v0.0.34`] this method deprecated (*use `__setItemRaw`, a static method of the class, instead*).
634
+
632
635
  This method replaces an element addressed by `index` with a new element which is saved as raw object. If succeed `true` is returned.
633
636
 
634
637
  ##### **_insItemRaw(index, obj)**
635
638
 
636
639
  This method inserts an element as a raw object in the position addressed by `index`. If succeed `true` is returned.
637
640
 
641
+ #### class methods (*static, special*)
642
+
643
+ <a name="TXmlElementsListController.__getItemRaw"></a>
644
+ ##### **__getItemRaw(node, index)** => `?any`
645
+
646
+ > since: \[`v0.0.34`]
647
+
648
+ This method returns an element addressed by `index` as object. If failed `null` is returned.
649
+
650
+ <a name="TXmlElementsListController.__setItemRaw"></a>
651
+ ##### **__setItemRaw(node, index, item)** => `?any`
652
+
653
+ > since: \[`v0.0.34`]
654
+
655
+ This method replaces an element addressed by `index` with a new element which is saved as raw object. If succeed `true` is returned.
656
+
638
657
  <a name="TXmlContentDeclaration"></a>
639
658
  ### **TXmlContentDeclaration**
640
659
 
package/index.js CHANGED
@@ -1,12 +1,13 @@
1
- // [v0.1.016-20240801]
1
+ // [v0.2.018-20250823]
2
2
 
3
3
  // === module init block ===
4
4
 
5
5
  const xObj = require('@ygracs/xobj-lib-js');
6
- const xmldoc = require('./lib/xmldoc-lib.js');
7
- const xmlHelper = require('./lib/xml-helper.js');
6
+ const xmldoc = require('./lib/xmldoc-lib');
7
+ const xmlHelper = require('./lib/xml-helper');
8
+ const xmlBase = require('./lib/xml-base');
8
9
 
9
- // === module extra block (helper functions) ===
10
+ // === module inner block ===
10
11
 
11
12
  // === module main block ===
12
13
 
@@ -14,16 +15,17 @@ const xmlHelper = require('./lib/xml-helper.js');
14
15
 
15
16
  module.exports.xObj = xObj;
16
17
  module.exports.xmlHelper = xmlHelper;
17
- module.exports.xmlDoc = xmldoc;
18
+ module.exports.xmlDoc = xmldoc; // // TODO: [?]
19
+
20
+ module.exports.readAsTagName = xmlBase.readAsTagName;
21
+ module.exports.readAsAttrName = xmlBase.readAsAttrName;
22
+ module.exports.readAsAttrValue = xmlBase.readAsAttrValue;
23
+ module.exports.valueToElementID = xmlBase.valueToElementID;
18
24
 
19
25
  module.exports.XML_DEF_PARSE_OPTIONS = xmldoc.XML_DEF_PARSE_OPTIONS;
20
26
  module.exports.XML_DEF_ROOT_ETAG_NAME = xmldoc.XML_DEF_ROOT_ETAG_NAME;
21
27
  module.exports.XML_LANG_ATTR_TNAME = xmldoc.XML_LANG_ATTR_TNAME;
22
28
 
23
- module.exports.readAsTagName = xmldoc.readAsTagName;
24
- module.exports.readAsAttrName = xmldoc.readAsAttrName;
25
- module.exports.valueToElementID = xmldoc.valueToElementID;
26
-
27
29
  module.exports.TXmlContentParseOptions = xmldoc.TXmlContentParseOptions;
28
30
 
29
31
  module.exports.TXmlAttributesMapper = xmldoc.TXmlAttributesMapper;
@@ -0,0 +1,129 @@
1
+ // [v0.1.001-20250823]
2
+
3
+ // === module init block ===
4
+
5
+ const {
6
+ //valueToIndex, readAsBool, readAsString, isNotEmptyString,
7
+ //isArray, isObject, isPlainObject,
8
+ //valueToArray, valueToEntry,
9
+ valueToIDString,
10
+ } = require('@ygracs/bsfoc-lib-js');
11
+
12
+ // === module inner block ===
13
+
14
+ // === module main block ===
15
+
16
+ /***
17
+ * (* constant definitions *)
18
+ */
19
+
20
+ /***
21
+ * (* function definitions *)
22
+ */
23
+
24
+ /**
25
+ * Tries to convert a given value to a valid tag name of an XML-element.
26
+ * @since v0.0.28
27
+ * @function readAsTagName
28
+ * @param {any} value - some value
29
+ * @returns {string}
30
+ */
31
+ function readAsTagName(value) {
32
+ let tagName = valueToIDString(value, { ignoreNumbers: true });
33
+ if (tagName === null) return '';
34
+ if (tagName !== '') {
35
+ // // TODO: do extra checks
36
+ const template = /[\s\/\\\"\'<>=]/;
37
+ const trigger = tagName.match(template);
38
+ if (trigger) {
39
+ //console.log(trigger);
40
+ tagName = '';
41
+ };
42
+ };
43
+ return tagName;
44
+ };
45
+
46
+ /**
47
+ * Tries to convert a given value to a valid name of an XML-attribute.
48
+ * @since v0.0.28
49
+ * @function readAsAttrName
50
+ * @param {any} value - some value
51
+ * @returns {string}
52
+ */
53
+ function readAsAttrName(value) {
54
+ let attrName = valueToIDString(value, { ignoreNumbers: true });
55
+ if (attrName === null) return '';
56
+ if (attrName !== '') {
57
+ // // TODO: do extra checks
58
+ const template = /[\s\/\\\"\'<>=]/;
59
+ const trigger = attrName.match(template);
60
+ if (trigger) {
61
+ //console.log(trigger);
62
+ attrName = '';
63
+ };
64
+ };
65
+ return attrName;
66
+ };
67
+
68
+ /**
69
+ * Tries to convert a given value to a valid "attribute value".
70
+ * @since v0.0.35
71
+ * @function readAsAttrValue
72
+ * @param {any} value - some value
73
+ * @return {?string}
74
+ */
75
+ function readAsAttrValue(value) {
76
+ let result = null;
77
+ switch (typeof value) {
78
+ case 'boolean': {
79
+ result = value.toString();
80
+ break;
81
+ }
82
+ case 'number': {
83
+ if (Number.isNaN(value)) break;
84
+ result = value.toString();
85
+ break;
86
+ }
87
+ case 'string': {
88
+ result = value.trim();
89
+ break;
90
+ }
91
+ default: {}
92
+ };
93
+ return result;
94
+ };
95
+
96
+ /**
97
+ * Tries to convert a given value to a valid identifier
98
+ * suitable as a value for an "ID-attribute" of an XML-element.
99
+ * @since v0.0.28
100
+ * @function valueToElementID
101
+ * @param {any} value - some value
102
+ * @returns {string}
103
+ * @see valueToIDString
104
+ */
105
+ function valueToElementID(value) {
106
+ let id = valueToIDString(value);
107
+ if (id === null) return '';
108
+ if (id !== '') {
109
+ // // TODO: do extra checks
110
+ const template = /[\s\/\\\"\'<>=]/;
111
+ const trigger = id.match(template);
112
+ if (trigger) {
113
+ //console.log(trigger);
114
+ id = '';
115
+ };
116
+ };
117
+ return id;
118
+ };
119
+
120
+ /***
121
+ * (* class definitions *)
122
+ */
123
+
124
+ // === module exports block ===
125
+
126
+ module.exports.readAsTagName = readAsTagName;
127
+ module.exports.readAsAttrName = readAsAttrName;
128
+ module.exports.readAsAttrValue = readAsAttrValue;
129
+ module.exports.valueToElementID = valueToElementID;
package/lib/xml-helper.js CHANGED
@@ -1,4 +1,4 @@
1
- // [v0.1.005-20250206]
1
+ // [v0.1.006-20250822]
2
2
 
3
3
  // === module init block ===
4
4
 
@@ -24,11 +24,11 @@ const {
24
24
  */
25
25
 
26
26
  /**
27
+ * Tries to get a child element.
27
28
  * @function getChildByPath
28
29
  * @param {TXmlElementController} obj - some element
29
30
  * @param {string[]} chain - path to a target child element
30
31
  * @returns {?TXmlElementController}
31
- * @description Tries to get a child element.
32
32
  */
33
33
  function getChildByPath(obj, chain) {
34
34
  let result = null;
@@ -54,11 +54,11 @@ function getChildByPath(obj, chain) {
54
54
  };
55
55
 
56
56
  /**
57
+ * Tries to get a child element.
57
58
  * @function addChildByPath
58
59
  * @param {TXmlElementController} obj - some element
59
60
  * @param {string[]} chain - path to a target child element
60
61
  * @returns {?TXmlElementController}
61
- * @description Tries to get a child element.
62
62
  */
63
63
  function addChildByPath(obj, chain) {
64
64
  let item = obj;
@@ -82,19 +82,19 @@ function addChildByPath(obj, chain) {
82
82
  };
83
83
 
84
84
  /**
85
+ * Reads a text values from a given XML-element.
85
86
  * @function extractTextValues
86
87
  * @param {(TXmlElementController|TXmlElementsListController)} obj - some element
87
88
  * @returns {string[]}
88
- * @description Reads a text values from a given XML-element.
89
89
  */
90
90
  function extractTextValues(obj) {
91
91
  /**
92
+ * Reads element value and adds it to the list
92
93
  * @function pushValue
93
94
  * @param {string[]} result - accumulator array
94
95
  * @param {any} item - some element
95
96
  * @returns {void}
96
97
  * @protected
97
- * @description Reads element value and adds it to the list
98
98
  */
99
99
  const pushValue = (result, item) => {
100
100
  if (item instanceof TXmlElementController) {
@@ -115,20 +115,19 @@ function extractTextValues(obj) {
115
115
  };
116
116
 
117
117
  /**
118
+ * Reads a text values with a `lang` attribute from a given XML-element.
118
119
  * @function extractTextValuesLn
119
120
  * @param {(TXmlElementController|TXmlElementsListController)} obj - some element
120
121
  * @returns {any[]}
121
- * @description Reads a text values with a `lang` attribute
122
- * from a given XML-element.
123
122
  */
124
123
  function extractTextValuesLn(obj) {
125
124
  /**
125
+ * Reads element value and adds it to the list
126
126
  * @function pushValue
127
127
  * @param {any[]} result - accumulator array
128
128
  * @param {any} item - some element
129
129
  * @returns {void}
130
130
  * @protected
131
- * @description Reads element value and adds it to the list
132
131
  */
133
132
  const pushValue = (result, item) => {
134
133
  if (item instanceof TXmlElementController) {