@cntwg/xml-lib-js 0.0.25 → 0.0.27

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,22 @@
1
+ #### *v0.0.27*
2
+
3
+ Pre-release version.
4
+
5
+ > - `xmldoc-lib.md` updated;
6
+ > - fix error in a `addChildByPath` function;
7
+ > - add functions (`xmlHelper` module): `extractTextValues`, `extractTextValuesLn`.
8
+
9
+ #### *v0.0.26*
10
+
11
+ Pre-release version.
12
+
13
+ > - `xmldoc-lib.md` updated;
14
+ > - updated dependency on `@ygracs/bsfoc-lib-js` module to v0.2.1;
15
+ > - updated dependency on `@ygracs/xobj-lib-js` module to v0.1.2;
16
+ > - improve error handling on file load/save ops for `TXmlContentContainer`;
17
+ > - fix behavior for a `setTextValue` method of a `TXmlElementController`;
18
+ > - add `xml-helper.js` module.
19
+
1
20
  #### *v0.0.25*
2
21
 
3
22
  Pre-release version.
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2019-2023 Yuri Grachev
3
+ Copyright (c) 2019-2024 Yuri Grachev
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
6
 
package/README.md CHANGED
@@ -1,17 +1,11 @@
1
- |***rev.*:**|0.1.4|
1
+ |***rev.*:**|0.1.6|
2
2
  |:---|---:|
3
- |***date***:|2023-09-19|
3
+ |***date***:|2024-07-02|
4
4
 
5
5
  ## Introduction
6
6
 
7
7
  This module provide a base functionality for handling an XML-documents.
8
8
 
9
- ## Use cases
10
-
11
- ### Installation
12
-
13
- `npm install @cntwg/xml-lib-js`
14
-
15
9
  ## Description
16
10
 
17
11
  This module contains the following components:
@@ -38,3 +32,20 @@ xObj-manipulator is a set of functions (see docs for [`@ygracs/xobj-lib-js` modu
38
32
  + `TXmlContentContainer`.
39
33
 
40
34
  For more read the `xmldoc-lib.md` in the project `doc` directory.
35
+
36
+ ### 3. an `xmlHelper` module extension
37
+
38
+ - functions:
39
+
40
+ + `getChildByPath`;
41
+ + `addChildByPath`;
42
+ + `extractTextValues`;
43
+ + `extractTextValuesLn`.
44
+
45
+ For more read the `xmldoc-lib.md` in the project `doc` directory.
46
+
47
+ ## Use cases
48
+
49
+ ### Installation
50
+
51
+ `npm install @cntwg/xml-lib-js`
package/doc/xmldoc-lib.md CHANGED
@@ -1,6 +1,6 @@
1
- >|***rev.*:**|0.1.22|
1
+ >|***rev.*:**|0.1.24|
2
2
  >|:---|---:|
3
- >|date:|2023-09-19|
3
+ >|date:|2024-07-02|
4
4
 
5
5
  ## Introduction
6
6
 
@@ -41,6 +41,10 @@ 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
+
44
48
  ## Module classes
45
49
 
46
50
  ### **TXmlContentParseOptions**
@@ -189,6 +193,7 @@ This method returns an array witch contain a value of a `textValue` property and
189
193
  This method sets the value of a `textValue` property and its `lang` attribute and if succeed returns `true`.
190
194
 
191
195
  The method received value that can be a string or an array. If value is an array it must be in the following format:
196
+
192
197
  `[ <textValue> ]`
193
198
  or
194
199
  `[ <lang>, <textValue> ]`.
@@ -199,7 +204,7 @@ This method checks whether or not a child element addressed by `name` is exists.
199
204
 
200
205
  ##### **getChild(name)**
201
206
 
202
- This method returns a child element addressed by `name`.
207
+ This method returns a child element addressed by `name`. If failed `null` is returned.
203
208
 
204
209
  ##### **addChild(name)**
205
210
 
@@ -209,7 +214,7 @@ This method adds an element as a child element addressed by a `name`.
209
214
 
210
215
  This method deletes a child element addressed by a `name`.
211
216
 
212
- > Warning: method not fully implemented. not used for list like elements.
217
+ > 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.
213
218
 
214
219
  ##### **clear()**
215
220
 
@@ -501,3 +506,39 @@ This method returns a document content as a string in the JSON-format.
501
506
  This method binds a given object as a document content.
502
507
 
503
508
  For `options` parameter details see the class constructor description.
509
+
510
+ ## Module `xmlHelper`
511
+
512
+ This section contains functions provided by `xmlHelper` module.
513
+
514
+ ### Experimental functions
515
+
516
+ > 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.
517
+
518
+ #### **getChildByPath(object, chain)**
519
+
520
+ > since: \[v0.0.26]
521
+
522
+ This function tries to get a child element from a given object following path provided by `chain` parameter. If failed `null` is returned.
523
+
524
+ The `chain` parameter contains an array of an element names.
525
+
526
+ #### **addChildByPath(object, chain)**
527
+
528
+ > since: \[v0.0.26]
529
+
530
+ 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.
531
+
532
+ 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.
533
+
534
+ #### **extractTextValues(object)**
535
+
536
+ > since: \[v0.0.27]
537
+
538
+ This function tries to get a text value from a given element.
539
+
540
+ #### **extractTextValuesLn(object)**
541
+
542
+ > since: \[v0.0.27]
543
+
544
+ This function tries to get a text value with its language attribute from a given element.
package/index.js CHANGED
@@ -1,9 +1,10 @@
1
- // [v0.1.011-20230919]
1
+ // [v0.1.013-20240701]
2
2
 
3
3
  // === module init block ===
4
4
 
5
5
  const xObj = require('@ygracs/xobj-lib-js');
6
6
  const xmldoc = require('./lib/xmldoc-lib.js');
7
+ const xmlHelper = require('./lib/xml-helper.js');
7
8
 
8
9
  // === module extra block (helper functions) ===
9
10
 
@@ -11,16 +12,19 @@ const xmldoc = require('./lib/xmldoc-lib.js');
11
12
 
12
13
  // === module exports block ===
13
14
 
14
- exports.xObj = xObj;
15
+ module.exports.xObj = xObj;
16
+ module.exports.xmlHelper = xmlHelper;
17
+ module.exports.xmlDoc = xmldoc;
15
18
 
16
- exports.XML_DEF_PARSE_OPTIONS = xmldoc.XML_DEF_PARSE_OPTIONS;
17
- exports.XML_DEF_ROOT_ETAG_NAME = xmldoc.XML_DEF_ROOT_ETAG_NAME;
19
+ module.exports.XML_DEF_PARSE_OPTIONS = xmldoc.XML_DEF_PARSE_OPTIONS;
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;
18
22
 
19
- exports.TXmlContentParseOptions = xmldoc.TXmlContentParseOptions;
23
+ module.exports.TXmlContentParseOptions = xmldoc.TXmlContentParseOptions;
20
24
 
21
- exports.TXmlAttributesMapper = xmldoc.TXmlAttributesMapper;
22
- exports.TXmlElementController = xmldoc.TXmlElementController;
23
- exports.TXmlElementsListController = xmldoc.TXmlElementsListController;
24
- exports.TXmlContentDeclaration = xmldoc.TXmlContentDeclaration;
25
- exports.TXmlContentRootElement = xmldoc.TXmlContentRootElement;
26
- exports.TXmlContentContainer = xmldoc.TXmlContentContainer;
25
+ module.exports.TXmlAttributesMapper = xmldoc.TXmlAttributesMapper;
26
+ module.exports.TXmlElementController = xmldoc.TXmlElementController;
27
+ module.exports.TXmlElementsListController = xmldoc.TXmlElementsListController;
28
+ module.exports.TXmlContentDeclaration = xmldoc.TXmlContentDeclaration;
29
+ module.exports.TXmlContentRootElement = xmldoc.TXmlContentRootElement;
30
+ module.exports.TXmlContentContainer = xmldoc.TXmlContentContainer;
@@ -0,0 +1,142 @@
1
+ // [v0.1.004-20240702]
2
+
3
+ // === module init block ===
4
+
5
+ const {
6
+ valueToArray,
7
+ } = require('@ygracs/bsfoc-lib-js');
8
+
9
+ const {
10
+ TXmlElementController,
11
+ TXmlElementsListController,
12
+ } = require('./xmldoc-lib');
13
+
14
+ // === module extra block (helper functions) ===
15
+
16
+ // === module main block ===
17
+
18
+ /***
19
+ * (* constant definitions *)
20
+ */
21
+
22
+ /***
23
+ * (* function definitions *)
24
+ */
25
+
26
+ /**
27
+ * @function getChildByPath
28
+ * @param {TXmlElementController}
29
+ * @param {array}
30
+ * @returns {?TXmlElementController}
31
+ * @description Tries to get a child element.
32
+ */
33
+ function getChildByPath(obj, chain) {
34
+ let result = null;
35
+ if (obj instanceof TXmlElementController) {
36
+ const list = valueToArray(chain);
37
+ let child = obj;
38
+ let isFound = false;
39
+ for (let name of list) {
40
+ child = child.getChild(name);
41
+ if (
42
+ child !== null
43
+ && (child instanceof TXmlElementController)
44
+ ) {
45
+ isFound = true;
46
+ } else {
47
+ isFound = false;
48
+ break;
49
+ };
50
+ };
51
+ if (isFound) result = child;
52
+ };
53
+ return result;
54
+ };
55
+
56
+ /**
57
+ * @function addChildByPath
58
+ * @param {TXmlElementController}
59
+ * @param {array}
60
+ * @returns {?TXmlElementController}
61
+ * @description Tries to get a child element.
62
+ */
63
+ function addChildByPath(obj, chain){
64
+ let item = obj;
65
+ let child = null;
66
+ if (obj instanceof TXmlElementController) {
67
+ const list = valueToArray(chain);
68
+ for (let name of list) {
69
+ child = item.getChild(name);
70
+ if (child === null) {
71
+ child = item.addChild(name);
72
+ };
73
+ if ((child instanceof TXmlElementController)) {
74
+ item = child;
75
+ } else {
76
+ child = null;
77
+ break;
78
+ };
79
+ };
80
+ };
81
+ return child;
82
+ };
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
+
133
+ /***
134
+ * (* class definitions *)
135
+ */
136
+
137
+ // === module exports block ===
138
+
139
+ module.exports.getChildByPath = getChildByPath;
140
+ module.exports.addChildByPath = addChildByPath;
141
+ module.exports.extractTextValues = extractTextValues;
142
+ module.exports.extractTextValuesLn = extractTextValuesLn;