@cntwg/xml-lib-js 0.0.19 → 0.0.20

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,11 @@
1
+ #### *v0.0.20*
2
+
3
+ Pre-release version.
4
+
5
+ > - `xmldoc-lib.md` updated;
6
+ > - changed behavior of `TXmlAttributesMapper` class constructor: if `obj` parameter is not an object, the `TypeError` will be thrown;
7
+ > - some fixes in `xmldoc-lib.js` module.
8
+
1
9
  #### *v0.0.19*
2
10
 
3
11
  Pre-release version.
package/doc/xmldoc-lib.md CHANGED
@@ -1,8 +1,8 @@
1
- >|***rev.*:**|0.1.18|
1
+ >|***rev.*:**|0.1.19|
2
2
  >|:---|---:|
3
- >|date:|2022-09-06|
3
+ >|date:|2023-02-14|
4
4
 
5
- ## Intoduction
5
+ ## Introduction
6
6
 
7
7
  This paper describes a constants and an object classes provided by `xmldoc-lib.js` module.
8
8
 
@@ -41,6 +41,8 @@ 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
+ > WARNING: The constant `DEF\_XML\_ROOT\_ETAG_NAME` will be deprecated. Use `XML\_DEF\_ROOT\_ETAG_NAME` instead.
45
+
44
46
  ## Module classes
45
47
 
46
48
  ### **TXmlContentParseOptions**
@@ -91,6 +93,8 @@ The `options` structure is listed below:
91
93
  |:---|---|---:|:---|
92
94
  |`parseOptions`|`object`|`EMPTY_OBJECT`|contains a XML-parse options (*see description for `XML_DEF_PARSE_OPTIONS`*)|
93
95
 
96
+ > NOTE: if `object` parameter is not a plain object the `TypeError` thrown.
97
+
94
98
  #### class properties
95
99
 
96
100
  |name|read only|description|
@@ -295,7 +299,7 @@ This method deletes an element addressed by `index`. If succeed `true` is return
295
299
 
296
300
  ##### **loadItems(items, options)**
297
301
 
298
- This method loaded a list of elements listed by `items` and returns a quantity of a successfuly added elements.
302
+ This method loaded a list of elements listed by `items` and returns a quantity of a successfully added elements.
299
303
 
300
304
  #### class methods (*special*)
301
305
 
@@ -382,7 +386,7 @@ The `options` structure is listed below:
382
386
 
383
387
  This method returns a host for an element as an object. If failed `null` is returned.
384
388
 
385
- The `options` parameter, if given, change a behavoir for the method. If set to 'true' the given instance checks aganst inheritance from a `TXmlContentRootElement`, if else, from a `TXmlElementController` class.
389
+ The `options` parameter, if given, change a behavior for the method. If set to 'true' the given instance checks against inheritance from a `TXmlContentRootElement`, if else, from a `TXmlElementController` class.
386
390
 
387
391
  #### class methods (*special*)
388
392
 
package/lib/xmldoc-lib.js CHANGED
@@ -1,4 +1,4 @@
1
- // [v0.1.048-20220908]
1
+ // [v0.1.053-20230214]
2
2
 
3
3
  // === module init block ===
4
4
 
@@ -36,16 +36,17 @@ class TXmlAttributesMapper {
36
36
 
37
37
  constructor(obj, opt){
38
38
  // init an elements content
39
- this.#_host = isPlainObject(obj) ? obj : {};
39
+ if (!isPlainObject(obj)) throw new TypeError(XML_TE_NOBJ_EMSG);
40
+ this.#_host = obj;
40
41
  // load options
41
- let _options = (
42
- isPlainObject(opt) && isPlainObject(opt.parseOptions)
43
- ? opt.parseOptions
44
- : {}
45
- );
46
- let { settings: _settings } = _options;
47
- if (!isPlainObject(_settings)) _options.settings = _settings = {};
48
- let { attributesKey } = _settings;
42
+ const _options = isPlainObject(opt) ? opt : {};
43
+ let { parseOptions } = _options;
44
+ if (!isPlainObject(parseOptions)) {
45
+ _options.parseOptions = parseOptions = {};
46
+ };
47
+ let { settings } = parseOptions;
48
+ if (!isPlainObject(settings)) parseOptions.settings = settings = {};
49
+ let { attributesKey } = settings;
49
50
  if (
50
51
  typeof attributesKey !== 'string'
51
52
  || ((attributesKey = attributesKey.trim()) === '')
@@ -53,8 +54,8 @@ class TXmlAttributesMapper {
53
54
  attributesKey = XML_DEF_PARSE_OPTIONS.attributesKey;
54
55
  };
55
56
  // save options
56
- _settings.attributesKey = attributesKey;
57
- this.#_options = _options;
57
+ settings.attributesKey = attributesKey;
58
+ this.#_options = parseOptions;
58
59
  }
59
60
 
60
61
  get entries(){
@@ -66,48 +67,49 @@ class TXmlAttributesMapper {
66
67
  }
67
68
 
68
69
  hasAttribute(value){
69
- let name = '';
70
- if (
71
- typeof value !== 'string' || ((name = value.trim()) === '')
72
- ) {
73
- return false;
70
+ let result = false;
71
+ try {
72
+ result = xObj.checkXObjAttribute(
73
+ this.#_host,
74
+ value,
75
+ this.#_options.settings.attributesKey,
76
+ );
77
+ } catch (err) {
78
+ // // TODO: verify what error is thrown
79
+ result = false;
74
80
  };
75
- // // TODO: await implementation for class method provided by xObj
76
- let item = xObj.getXObjAttributes(
77
- this.#_host,
78
- this.#_options.settings.attributesKey,
79
- );
80
- return isPlainObject(item) && item[name] !== undefined;
81
+ return result;
81
82
  }
82
83
 
83
84
  getAttribute(value){
84
- let name = '';
85
- if (
86
- typeof value !== 'string' || ((name = value.trim()) === '')
87
- ) {
88
- return '';
85
+ let result = '';
86
+ try {
87
+ result = xObj.readXObjAttr(
88
+ this.#_host,
89
+ value,
90
+ this.#_options.settings.attributesKey,
91
+ );
92
+ } catch (err) {
93
+ // // TODO: verify what error is thrown
94
+ result = '';
89
95
  };
90
- return xObj.readXObjAttr(
91
- this.#_host,
92
- name,
93
- this.#_options.settings.attributesKey,
94
- );
96
+ return result;
95
97
  }
96
98
 
97
- setAttribute(attr, value){
98
- let name = '';
99
- if (
100
- typeof attr !== 'string' || ((name = attr.trim()) === '')
101
- || value === undefined
102
- ) {
103
- return false;
99
+ setAttribute(name, value){
100
+ let result = false;
101
+ try {
102
+ result = xObj.writeXObjAttr(
103
+ this.#_host,
104
+ name,
105
+ value,
106
+ this.#_options.settings.attributesKey,
107
+ );
108
+ } catch (err) {
109
+ // // TODO: verify what error is thrown
110
+ result = false;
104
111
  };
105
- return xObj.writeXObjAttr(
106
- this.#_host,
107
- name,
108
- value,
109
- this.#_options.settings.attributesKey,
110
- );
112
+ return result;
111
113
  }
112
114
 
113
115
  delAttribute(value){
@@ -136,7 +138,7 @@ class TXmlAttributesMapper {
136
138
  this.#_options.settings.attributesKey,
137
139
  {
138
140
  force: true,
139
- rip_oldies: true,
141
+ ripOldies: true,
140
142
  },
141
143
  );
142
144
  }
@@ -153,9 +155,12 @@ class TXmlElementController {
153
155
  constructor(obj, opt){
154
156
  // load options
155
157
  let _options = isPlainObject(opt) ? opt : {};
156
- let { proxyModeEnable } = _options;
158
+ let {
159
+ proxyModeEnable,
160
+ parseOptions,
161
+ } = _options;
157
162
  if (typeof proxyModeEnable !== 'boolean') proxyModeEnable = false;
158
- // init an elements content
163
+ // init an element content
159
164
  if (isObject(obj)) {
160
165
  if (isArray(obj)) return new TXmlElementsListController(obj, opt);
161
166
  // // TODO: split a plain object and class instances
@@ -169,18 +174,21 @@ class TXmlElementController {
169
174
  this.#_host = {};
170
175
  };
171
176
  // set parser options
172
- let { parseOptions: _parseOptions } = _options;
173
- if (!isPlainObject(_parseOptions)) {
174
- _options.parseOptions = _parseOptions = {};
177
+ if (!isPlainObject(parseOptions)) {
178
+ _options.parseOptions = parseOptions = {};
175
179
  };
176
- let { settings: _settings } = _parseOptions;
177
- if (!isPlainObject(_settings)) {
178
- _parseOptions.settings = _settings = {};
179
- // // TODO: set defaults
180
+ let { settings } = parseOptions;
181
+ if (!isPlainObject(settings)) parseOptions.settings = settings = {};
182
+ let { textKey } = settings;
183
+ if (
184
+ typeof textKey !== 'string'
185
+ || ((textKey = textKey.trim()) === '')
186
+ ) {
187
+ textKey = XML_DEF_PARSE_OPTIONS.textKey;
180
188
  };
181
- // save parser options
182
- this.#_parseOptions = _parseOptions;
183
189
  // save options
190
+ settings.textKey = textKey;
191
+ this.#_parseOptions = parseOptions;
184
192
  _options.proxyModeEnable = proxyModeEnable;
185
193
  this.#_options = _options;
186
194
  // bind atributes mapper
@@ -379,17 +387,19 @@ class TXmlElementsListController {
379
387
  constructor(obj, opt){
380
388
  // load options
381
389
  let _options = isPlainObject(opt) ? opt : {};
382
- let { proxyModeEnable, isNullItemsAllowed } = _options;
390
+ let {
391
+ proxyModeEnable,
392
+ isNullItemsAllowed,
393
+ parseOptions,
394
+ } = _options;
383
395
  if (typeof proxyModeEnable !== 'boolean') proxyModeEnable = false;
384
396
  if (typeof isNullItemsAllowed !== 'boolean') isNullItemsAllowed = false;
385
397
  // set parser options
386
- let { parseOptions: _parseOptions } = _options;
387
- if (!isPlainObject(_parseOptions)) _parseOptions = {};
388
- let { settings: _settings } = _parseOptions;
389
- if (!isPlainObject(_settings)) {
390
- _parseOptions.settings = _settings = {};
391
- // // TODO: set defaults
398
+ if (!isPlainObject(parseOptions)) {
399
+ _options.parseOptions = parseOptions = {};
392
400
  };
401
+ let { settings } = parseOptions;
402
+ if (!isPlainObject(settings)) parseOptions.settings = settings = {};
393
403
  // init an elements list content
394
404
  if (isArray(obj)) {
395
405
  this.#_host = obj;
@@ -403,8 +413,6 @@ class TXmlElementsListController {
403
413
  this.#_host = [];
404
414
  };
405
415
  };
406
- // save parser options
407
- _options.parseOptions = _parseOptions;
408
416
  // save options
409
417
  _options.proxyModeEnable = proxyModeEnable;
410
418
  _options.isNullItemsAllowed = isNullItemsAllowed;
@@ -570,21 +578,20 @@ class TXmlContentDeclaration {
570
578
  // load options
571
579
  let _options = isPlainObject(opt) ? opt : {};
572
580
  // set parser options
573
- let { parseOptions: _parseOptions } = _options;
574
- if (!isPlainObject(_parseOptions)) _parseOptions = {};
575
- let { settings: _settings } = _parseOptions;
576
- if (!isPlainObject(_settings)) _parseOptions.settings = _settings = {};
577
- let { declarationKey } = _settings;
581
+ let { parseOptions: parseOptions } = _options;
582
+ if (!isPlainObject(parseOptions)) {
583
+ _options.parseOptions = parseOptions = {};
584
+ };
585
+ let { settings: settings } = parseOptions;
586
+ if (!isPlainObject(settings)) parseOptions.settings = settings = {};
587
+ let { declarationKey } = settings;
578
588
  if (
579
589
  typeof declarationKey !== 'string'
580
590
  || ((declarationKey = declarationKey.trim()) === '')
581
591
  ) {
582
592
  declarationKey = XML_DEF_PARSE_OPTIONS.declarationKey;
593
+ settings.declarationKey = declarationKey;
583
594
  };
584
- // // TODO: set defaults
585
- // save parser options
586
- _options.parseOptions = _parseOptions;
587
- _settings.declarationKey = declarationKey;
588
595
  // save options
589
596
  this.#_options = _options;
590
597
  // bind a documents content
@@ -610,13 +617,13 @@ class TXmlContentDeclaration {
610
617
  }
611
618
 
612
619
  init(){
613
- let _options = this.#_options;
620
+ const _options = this.#_options;
614
621
  this.#_ctrls = new TXmlElementController(
615
622
  xObj.insertXObjElement(
616
623
  this.#_host,
617
624
  _options.parseOptions.settings.declarationKey,
618
625
  ),
619
- this._options,
626
+ _options,
620
627
  );
621
628
  // // TODO: set version & encoding
622
629
  if (!isNotEmptyString(this.version)) this.version = '1.0';
@@ -725,19 +732,6 @@ class TXmlContentRootElement extends TXmlElementController {
725
732
  ) ? value : XML_DEF_ROOT_ETAG_NAME;
726
733
  }
727
734
 
728
- //* will deprecate */
729
- /*init(name){
730
- //console.log('CHECK: TXmlContentRootElement.init() => was called...');
731
- //console.log('CHECK: TXmlContentRootElement.init() => (*1) name:['+name+']');
732
- let _options = this.#_options;
733
- name = readAsString(name, _options.rootETagName, true);
734
- // // TODO: check if name is valid
735
- //console.log('CHECK: TXmlContentRootElement.init() => (*2) name:['+name+']');
736
- if (!isPlainObject(this.#_content[name])) this.#_content[name] = {};
737
- this.#_ctrls = new TXmlElementController(this.#_content[name], _options);
738
- //console.log('CHECK: TXmlContentRootElement.init() => was left...');
739
- }*/
740
-
741
735
  static __unwrap(item, opt){
742
736
  return (
743
737
  item instanceof TXmlContentRootElement
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cntwg/xml-lib-js",
3
- "version": "0.0.19",
3
+ "version": "0.0.20",
4
4
  "description": "A library for handling an XML-documents",
5
5
  "author": "ygracs <cs70th-om@rambler.ru>",
6
6
  "license": "MIT",
@@ -34,6 +34,6 @@
34
34
  "@ygracs/xobj-lib-js": "^0.0.14-rc1"
35
35
  },
36
36
  "devDependencies": {
37
- "jest": "^27.5.1"
37
+ "jest": "^29.4.2"
38
38
  }
39
39
  }