@cntwg/xml-lib-js 0.0.26 → 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 +8 -0
- package/README.md +6 -4
- package/doc/xmldoc-lib.md +20 -4
- package/index.js +3 -1
- package/lib/xml-helper.js +56 -2
- package/lib/xmldoc-lib.js +63 -63
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
|***rev.*:**|0.1.
|
|
1
|
+
|***rev.*:**|0.1.6|
|
|
2
2
|
|:---|---:|
|
|
3
|
-
|***date***:|2024-
|
|
3
|
+
|***date***:|2024-07-02|
|
|
4
4
|
|
|
5
5
|
## Introduction
|
|
6
6
|
|
|
@@ -33,12 +33,14 @@ xObj-manipulator is a set of functions (see docs for [`@ygracs/xobj-lib-js` modu
|
|
|
33
33
|
|
|
34
34
|
For more read the `xmldoc-lib.md` in the project `doc` directory.
|
|
35
35
|
|
|
36
|
-
### 3. an `xmlHelper` module
|
|
36
|
+
### 3. an `xmlHelper` module extension
|
|
37
37
|
|
|
38
38
|
- functions:
|
|
39
39
|
|
|
40
40
|
+ `getChildByPath`;
|
|
41
|
-
+ `addChildByPath
|
|
41
|
+
+ `addChildByPath`;
|
|
42
|
+
+ `extractTextValues`;
|
|
43
|
+
+ `extractTextValuesLn`.
|
|
42
44
|
|
|
43
45
|
For more read the `xmldoc-lib.md` in the project `doc` directory.
|
|
44
46
|
|
package/doc/xmldoc-lib.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
>|***rev.*:**|0.1.
|
|
1
|
+
>|***rev.*:**|0.1.24|
|
|
2
2
|
>|:---|---:|
|
|
3
|
-
>|date:|2024-
|
|
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**
|
|
@@ -200,7 +204,7 @@ This method checks whether or not a child element addressed by `name` is exists.
|
|
|
200
204
|
|
|
201
205
|
##### **getChild(name)**
|
|
202
206
|
|
|
203
|
-
This method returns a child element addressed by `name`.
|
|
207
|
+
This method returns a child element addressed by `name`. If failed `null` is returned.
|
|
204
208
|
|
|
205
209
|
##### **addChild(name)**
|
|
206
210
|
|
|
@@ -210,7 +214,7 @@ This method adds an element as a child element addressed by a `name`.
|
|
|
210
214
|
|
|
211
215
|
This method deletes a child element addressed by a `name`.
|
|
212
216
|
|
|
213
|
-
> Warning: method not fully implemented.
|
|
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.
|
|
214
218
|
|
|
215
219
|
##### **clear()**
|
|
216
220
|
|
|
@@ -526,3 +530,15 @@ The `chain` parameter contains an array of an element names.
|
|
|
526
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.
|
|
527
531
|
|
|
528
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,4 +1,4 @@
|
|
|
1
|
-
// [v0.1.
|
|
1
|
+
// [v0.1.013-20240701]
|
|
2
2
|
|
|
3
3
|
// === module init block ===
|
|
4
4
|
|
|
@@ -14,9 +14,11 @@ 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;
|
|
20
22
|
|
|
21
23
|
module.exports.TXmlContentParseOptions = xmldoc.TXmlContentParseOptions;
|
|
22
24
|
|
package/lib/xml-helper.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// [v0.
|
|
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;
|
|
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.
|
|
1
|
+
// [v0.1.081-20240701]
|
|
2
2
|
|
|
3
3
|
// === module init block ===
|
|
4
4
|
|
|
@@ -394,16 +394,16 @@ class TXmlElementController {
|
|
|
394
394
|
* @returns {bool}
|
|
395
395
|
* @description Checks whether an element has a child element.
|
|
396
396
|
*/
|
|
397
|
-
hasChild(name){
|
|
397
|
+
hasChild(name) {
|
|
398
398
|
const _name = xObj.evalXObjEName(name);
|
|
399
|
+
if (_name === null) return false;
|
|
399
400
|
let result = false;
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
};
|
|
401
|
+
try {
|
|
402
|
+
result = isObject(xObj.getXObjElement(this.#_host, _name));
|
|
403
|
+
} catch (err) {
|
|
404
|
+
// // TODO: [?] verify what error is thrown
|
|
405
|
+
//throw err;
|
|
406
|
+
result = false;
|
|
407
407
|
};
|
|
408
408
|
return result;
|
|
409
409
|
}
|
|
@@ -413,21 +413,21 @@ class TXmlElementController {
|
|
|
413
413
|
* @returns {(null|TXmlElementController|TXmlElementsListController)}
|
|
414
414
|
* @description Returns a child element.
|
|
415
415
|
*/
|
|
416
|
-
getChild(name){
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
} catch (err) {
|
|
428
|
-
// // TODO: [?] verify what error is thrown
|
|
429
|
-
throw err;
|
|
416
|
+
getChild(name) {
|
|
417
|
+
let result = xObj.evalXObjEName(name);
|
|
418
|
+
if (result === null) return null;
|
|
419
|
+
try {
|
|
420
|
+
const item = xObj.getXObjElement(this.#_host, result);
|
|
421
|
+
if (isArray(item)) {
|
|
422
|
+
result = new TXmlElementsListController(item, this.#_options);
|
|
423
|
+
} else if (isObject(item)) {
|
|
424
|
+
result = new TXmlElementController(item, this.#_options);
|
|
425
|
+
} else {
|
|
426
|
+
result = null;
|
|
430
427
|
};
|
|
428
|
+
} catch (err) {
|
|
429
|
+
// // TODO: [?] verify what error is thrown
|
|
430
|
+
result = null;
|
|
431
431
|
};
|
|
432
432
|
return result;
|
|
433
433
|
}
|
|
@@ -483,17 +483,17 @@ class TXmlElementController {
|
|
|
483
483
|
* @returns {?TXmlElementController}
|
|
484
484
|
* @description Adds a new child element.
|
|
485
485
|
*/
|
|
486
|
-
addChild(name){
|
|
486
|
+
addChild(name) {
|
|
487
487
|
const _name = xObj.evalXObjEName(name);
|
|
488
|
+
if (_name === null) return null;
|
|
488
489
|
let result = null;
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
};
|
|
490
|
+
try {
|
|
491
|
+
const { 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
|
+
result = null;
|
|
497
497
|
};
|
|
498
498
|
return result;
|
|
499
499
|
}
|
|
@@ -503,33 +503,32 @@ class TXmlElementController {
|
|
|
503
503
|
* @returns {bool}
|
|
504
504
|
* @description Deletes a child element.
|
|
505
505
|
*/
|
|
506
|
-
delChild(name){
|
|
506
|
+
delChild(name) {
|
|
507
507
|
const _name = xObj.evalXObjEName(name);
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
isACCEPTED = true;
|
|
517
|
-
} else {
|
|
518
|
-
//console.log('CHECK: TXmlElementController.delChild() => item.length() => ['+item.length+']');
|
|
519
|
-
};
|
|
520
|
-
} else if (isObject(item)) {
|
|
508
|
+
if (_name === null) return false;
|
|
509
|
+
let isSucceed = false;
|
|
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) {
|
|
521
516
|
isACCEPTED = true;
|
|
522
517
|
} else {
|
|
523
|
-
//console.log('CHECK: TXmlElementController.delChild() => item.
|
|
524
|
-
//console.log('CHECK: TXmlElementController.delChild() => item.value() => ['+item+']');
|
|
518
|
+
//console.log('CHECK: TXmlElementController.delChild() => item.length() => ['+item.length+']');
|
|
525
519
|
};
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
//
|
|
529
|
-
|
|
520
|
+
} else {
|
|
521
|
+
isACCEPTED = item !== null;
|
|
522
|
+
//console.log('CHECK: TXmlElementController.delChild() => item.typeof() => ['+typeof item+']');
|
|
523
|
+
//console.log('CHECK: TXmlElementController.delChild() => item.value() => ['+item+']');
|
|
530
524
|
};
|
|
525
|
+
if (isACCEPTED) isSucceed = xObj.deleteXObjElement(this.#_host, _name);
|
|
526
|
+
} catch (err) {
|
|
527
|
+
// // TODO: [?] verify what error is thrown
|
|
528
|
+
//throw err;
|
|
529
|
+
isSucceed = false;
|
|
531
530
|
};
|
|
532
|
-
return
|
|
531
|
+
return isSucceed;
|
|
533
532
|
}
|
|
534
533
|
|
|
535
534
|
/**
|
|
@@ -1440,13 +1439,14 @@ class TXmlContentContainer {
|
|
|
1440
1439
|
|
|
1441
1440
|
// === module exports block ===
|
|
1442
1441
|
|
|
1443
|
-
exports.XML_DEF_PARSE_OPTIONS = XML_DEF_PARSE_OPTIONS;
|
|
1444
|
-
exports.XML_DEF_ROOT_ETAG_NAME = XML_DEF_ROOT_ETAG_NAME;
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
exports.
|
|
1448
|
-
exports.
|
|
1449
|
-
exports.
|
|
1450
|
-
exports.
|
|
1451
|
-
exports.
|
|
1452
|
-
exports.
|
|
1442
|
+
module.exports.XML_DEF_PARSE_OPTIONS = XML_DEF_PARSE_OPTIONS;
|
|
1443
|
+
module.exports.XML_DEF_ROOT_ETAG_NAME = XML_DEF_ROOT_ETAG_NAME;
|
|
1444
|
+
module.exports.XML_LANG_ATTR_TNAME = XML_LANG_ATTR_TNAME;
|
|
1445
|
+
|
|
1446
|
+
module.exports.TXmlAttributesMapper = TXmlAttributesMapper;
|
|
1447
|
+
module.exports.TXmlElementController = TXmlElementController;
|
|
1448
|
+
module.exports.TXmlElementsListController = TXmlElementsListController;
|
|
1449
|
+
module.exports.TXmlContentParseOptions = TXmlContentParseOptions;
|
|
1450
|
+
module.exports.TXmlContentDeclaration = TXmlContentDeclaration;
|
|
1451
|
+
module.exports.TXmlContentRootElement = TXmlContentRootElement;
|
|
1452
|
+
module.exports.TXmlContentContainer = TXmlContentContainer;
|