@ygracs/xobj-lib-js 0.1.1 → 0.1.2

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.1.2*
2
+
3
+ Release version.
4
+
5
+ > - updated dependency on `@ygracs/bsfoc-lib-js` module to v0.2.1;
6
+ > - move some definitions from `xObj-lib.js` module into `xObj-defs.js`;
7
+ > - fix behavior for function: `evalXObjEName`.
8
+
1
9
  #### *v0.1.1*
2
10
 
3
11
  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/index.js CHANGED
@@ -1,11 +1,18 @@
1
- // [v0.1.010-20230730]
1
+ // [v0.1.012-20240624]
2
2
 
3
3
  // === module init block ===
4
4
 
5
+ const {
6
+ TXmlContentParseOptions,
7
+ DEF_XML_PARSE_OPTIONS,
8
+ } = require('./lib/xObj-defs');
9
+
5
10
  // === module extra block (helper functions) ===
6
11
 
7
12
  // === module main block ===
8
13
 
9
14
  // === module exports block ===
10
15
 
11
- module.exports = require('./lib/xObj-lib.js');
16
+ module.exports = require('./lib/xObj-lib');
17
+ module.exports.TXmlContentParseOptions = TXmlContentParseOptions;
18
+ module.exports.DEF_XML_PARSE_OPTIONS = DEF_XML_PARSE_OPTIONS;
@@ -0,0 +1,166 @@
1
+ // [v0.1.060-20240623]
2
+
3
+ // === module init block ===
4
+
5
+ const {
6
+ //valueToIndex,
7
+ //readAsString, readAsBoolEx, readAsNumberEx,
8
+ //isNullOrUndef,
9
+ //isArray, isObject,
10
+ isPlainObject, //readAsListS,
11
+ } = require('@ygracs/bsfoc-lib-js');
12
+
13
+ // === module extra block (helper functions) ===
14
+
15
+ // === module main block ===
16
+
17
+ /***
18
+ * (* constant definitions *)
19
+ */
20
+
21
+ const XOBJ_DEF_PARAM_TNAME = '__text';
22
+ const XOBJ_DEF_ATTR_TNAME = '__attr';
23
+
24
+ const XOBJ_TE_INVARG_EMSG = 'invalid argument';
25
+ const XOBJ_TE_NOBJ_EMSG = `${XOBJ_TE_INVARG_EMSG} (an object expected)`;
26
+ const XOBJ_TE_NOBJ_ECODE = 'ERR_XOBJ_NOBJ';
27
+ const XOBJ_TE_NARR_EMSG = `${XOBJ_TE_INVARG_EMSG} (an array expected)`;
28
+ const XOBJ_TE_NARR_ECODE = 'ERR_XOBJ_NARR';
29
+ const XOBJ_TE_NSTR_EMSG = `${XOBJ_TE_INVARG_EMSG} (a string expected)`;
30
+ const XOBJ_TE_NSTR_ECODE = 'ERR_XOBJ_NSTR';
31
+ const XOBJ_TE_NPOBJ_EMSG = `${XOBJ_TE_INVARG_EMSG} (a plain object expected)`;
32
+ const XOBJ_TE_NPOBJ_ECODE = 'ERR_XOBJ_NPOBJ';
33
+ const XOBJ_TE_ANES_EMSG = '<attr_name> must be a non-empty string';
34
+ const XOBJ_TE_ANES_ECODE = 'ERR_XOBJ_INVARG_ATTR';
35
+ const XOBJ_TE_KNES_EMSG = '<key_name> must be a non-empty string';
36
+ const XOBJ_TE_KNES_ECODE = 'ERR_XOBJ_INVARG_KEY';
37
+
38
+ const DEF_XML_PARSE_OPTIONS = {
39
+ compact: true,
40
+ declarationKey: '__decl',
41
+ attributesKey: XOBJ_DEF_ATTR_TNAME,
42
+ textKey: XOBJ_DEF_PARAM_TNAME,
43
+ commentKey: '__note',
44
+ cdataKey: '__cdata',
45
+ nameKey: '__name',
46
+ typeKey: '__type',
47
+ parentKey: 'parent',
48
+ elementsKey: 'items',
49
+ ignoreDeclaration: false,
50
+ ignoreDocType: false,
51
+ ignoreInstractions: false,
52
+ ignoreText: false,
53
+ ignoreComments: false,
54
+ ignoreCData: false,
55
+ fullTagEmptyElement: true,
56
+ addParent: false,
57
+ trim: true,
58
+ spaces: 2,
59
+ };
60
+
61
+ /***
62
+ * (* function definitions *)
63
+ */
64
+
65
+ /***
66
+ * (* class definitions *)
67
+ */
68
+
69
+ class TXmlContentParseOptions {
70
+ #_options = null;
71
+
72
+ constructor(param){
73
+ this.#_options = TXmlContentParseOptions.createNewOptionsSet(param);
74
+ }
75
+
76
+ get settings(){ return this.#_options }
77
+
78
+ get xml2js(){
79
+ let _settings = this.#_options;
80
+ return {
81
+ compact: _settings.compact,
82
+ declarationKey: _settings.declarationKey,
83
+ attributesKey: _settings.attributesKey,
84
+ textKey: _settings.textKey,
85
+ commentKey: _settings.commentKey,
86
+ cdataKey: _settings.cdataKey,
87
+ nameKey: _settings.nameKey,
88
+ typeKey: _settings.typeKey,
89
+ parentKey: _settings.parentKey,
90
+ elementsKey: _settings.elementsKey,
91
+ ignoreDeclaration: _settings.ignoreDeclaration,
92
+ ignoreDocType: _settings.ignoreDocType,
93
+ ignoreInstraction: _settings.ignoreInstractions,
94
+ ignoreText: _settings.ignoreText,
95
+ ignoreComment: _settings.ignoreComments,
96
+ ignoreCData: _settings.ignoreCData,
97
+ addParent: _settings.addParent,
98
+ trim: _settings.trim,
99
+ };
100
+ }
101
+
102
+ get js2xml(){
103
+ let _settings = this.#_options;
104
+ return {
105
+ compact: _settings.compact,
106
+ declarationKey: _settings.declarationKey,
107
+ attributesKey: _settings.attributesKey,
108
+ textKey: _settings.textKey,
109
+ commentKey: _settings.commentKey,
110
+ cdataKey: _settings.cdataKey,
111
+ nameKey: _settings.nameKey,
112
+ typeKey: _settings.typeKey,
113
+ parentKey: _settings.parentKey,
114
+ elementsKey: _settings.elementsKey,
115
+ ignoreDeclaration: _settings.ignoreDeclaration,
116
+ ignoreDocType: _settings.ignoreDocType,
117
+ ignoreInstraction: _settings.ignoreInstractions,
118
+ ignoreText: _settings.ignoreText,
119
+ ignoreComment: _settings.ignoreComments,
120
+ ignoreCData: _settings.ignoreCData,
121
+ fullTagEmptyElement: _settings.fullTagEmptyElement,
122
+ spaces: _settings.spaces,
123
+ };
124
+ }
125
+
126
+ static createNewOptionsSet(opt){
127
+ if (opt instanceof TXmlContentParseOptions) {
128
+ opt = opt.settings;
129
+ } else if (isPlainObject(opt)) {
130
+ opt = isPlainObject(opt.settings) ? opt.settings : opt;
131
+ } else {
132
+ opt = DEF_XML_PARSE_OPTIONS;
133
+ };
134
+ return {
135
+ compact: opt.compact,
136
+ declarationKey: opt.declarationKey,
137
+ attributesKey: opt.attributesKey,
138
+ textKey: opt.textKey,
139
+ commentKey: opt.commentKey,
140
+ cdataKey: opt.cdataKey,
141
+ nameKey: opt.nameKey,
142
+ typeKey: opt.typeKey,
143
+ parentKey: opt.parentKey,
144
+ elementsKey: opt.elementsKey,
145
+ ignoreDeclaration: opt.ignoreDeclaration,
146
+ ignoreDocType: opt.ignoreDocType,
147
+ ignoreInstractions: opt.ignoreInstractions,
148
+ ignoreText: opt.ignoreText,
149
+ ignoreComments: opt.ignoreComments,
150
+ ignoreCData: opt.ignoreCData,
151
+ fullTagEmptyElement: opt.fullTagEmptyElement,
152
+ addParent: opt.addParent,
153
+ trim: opt.trim,
154
+ spaces: opt.spaces,
155
+ };
156
+ }
157
+
158
+ }
159
+
160
+ // === module exports block ===
161
+
162
+ module.exports.XOBJ_DEF_PARAM_TNAME = XOBJ_DEF_PARAM_TNAME;
163
+ module.exports.XOBJ_DEF_ATTR_TNAME = XOBJ_DEF_ATTR_TNAME;
164
+ module.exports.DEF_XML_PARSE_OPTIONS = DEF_XML_PARSE_OPTIONS;
165
+
166
+ module.exports.TXmlContentParseOptions = TXmlContentParseOptions;
package/lib/xObj-lib.js CHANGED
@@ -1,14 +1,24 @@
1
- // [v0.1.059-20230918]
1
+ // [v0.1.063-20240624]
2
2
 
3
3
  // === module init block ===
4
4
 
5
5
  const {
6
- valueToIndex,
6
+ valueToIndex, valueToIDString,
7
7
  readAsString, readAsBoolEx, readAsNumberEx,
8
8
  isNullOrUndef,
9
- isArray, isObject, isPlainObject, readAsListS,
9
+ isInteger,
10
+ isArray, isObject, isPlainObject,
11
+ readAsListS,
10
12
  } = require('@ygracs/bsfoc-lib-js');
11
13
 
14
+ // === module extra block (helper functions) ===
15
+
16
+ // === module main block ===
17
+
18
+ /***
19
+ * (* constant definitions *)
20
+ */
21
+
12
22
  const XOBJ_DEF_PARAM_TNAME = '__text';
13
23
  const XOBJ_DEF_ATTR_TNAME = '__attr';
14
24
 
@@ -26,56 +36,37 @@ const XOBJ_TE_ANES_ECODE = 'ERR_XOBJ_INVARG_ATTR';
26
36
  const XOBJ_TE_KNES_EMSG = '<key_name> must be a non-empty string';
27
37
  const XOBJ_TE_KNES_ECODE = 'ERR_XOBJ_INVARG_KEY';
28
38
 
29
- const DEF_XML_PARSE_OPTIONS = {
30
- compact: true,
31
- declarationKey: '__decl',
32
- attributesKey: XOBJ_DEF_ATTR_TNAME,
33
- textKey: XOBJ_DEF_PARAM_TNAME,
34
- commentKey: '__note',
35
- cdataKey: '__cdata',
36
- nameKey: '__name',
37
- typeKey: '__type',
38
- parentKey: 'parent',
39
- elementsKey: 'items',
40
- ignoreDeclaration: false,
41
- ignoreDocType: false,
42
- ignoreInstractions: false,
43
- ignoreText: false,
44
- ignoreComments: false,
45
- ignoreCData: false,
46
- fullTagEmptyElement: true,
47
- addParent: false,
48
- trim: true,
49
- spaces: 2,
50
- };
51
-
52
- // === module extra block (helper functions) ===
53
-
54
- // === module main block (function definitions) ===
39
+ /***
40
+ * (* function definitions *)
41
+ */
55
42
 
56
43
  function evalXObjEName(value){
44
+ //return valueToIDString(value); // // TODO: [?]
57
45
  let name = value;
58
- if (!isNullOrUndef(name)) {
59
- switch (typeof name) {
60
- case 'number' : {
61
- if (Number.isNaN(name) || name < 0) name = null;
62
- break;
63
- }
64
- case 'string' : {
65
- name = name.trim();
66
- if (name !== '') {
67
- let value = Number(name);
68
- if (!Number.isNaN(value)) {
69
- name = value < 0 ? null : value;
70
- };
46
+ switch (typeof name) {
47
+ case 'number' : {
48
+ if (valueToIndex(name) === -1) name = null;
49
+ break;
50
+ }
51
+ case 'string' : {
52
+ name = name.trim();
53
+ if (name !== '') {
54
+ let value = Number(name);
55
+ if (!Number.isNaN(value)) {
56
+ name = (
57
+ value < 0 || !isInteger(value)
58
+ ? null
59
+ : value
60
+ );
71
61
  };
72
- break;
73
- }
74
- default: {
62
+ } else {
75
63
  name = null;
76
- break;
77
- }
78
- };
64
+ };
65
+ break;
66
+ }
67
+ default: {
68
+ name = null;
69
+ }
79
70
  };
80
71
  return name;
81
72
  };
@@ -131,6 +122,14 @@ function genXObjENameDescr(value){
131
122
  return result;
132
123
  };
133
124
 
125
+ /**
126
+ * @function getXObjElement
127
+ * @param {object}
128
+ * @param {ID_STRING}
129
+ * @returns {?object}
130
+ * @throws {TypeError}
131
+ * @description Extracts an element from a given object by its key.
132
+ */
134
133
  function getXObjElement(obj, name){
135
134
  let err = null;
136
135
  if (!isPlainObject(obj)) {
@@ -153,6 +152,14 @@ function getXObjElement(obj, name){
153
152
  return obj[key] !== undefined ? obj[key] : null;
154
153
  };
155
154
 
155
+ /**
156
+ * @function getXObjAttributes
157
+ * @param {object}
158
+ * @param {ID_STRING} [key]
159
+ * @returns {?object}
160
+ * @throws {TypeError}
161
+ * @description Extracts an attributes from a given object by its key.
162
+ */
156
163
  function getXObjAttributes(obj, key = XOBJ_DEF_ATTR_TNAME){
157
164
  let result = null;
158
165
  try {
@@ -172,6 +179,14 @@ function getXObjAttributes(obj, key = XOBJ_DEF_ATTR_TNAME){
172
179
  return isPlainObject(result) ? result : null;
173
180
  };
174
181
 
182
+ /**
183
+ * @function addXObjElement
184
+ * @param {object}
185
+ * @param {ID_STRING}
186
+ * @returns {?object}
187
+ * @throws {TypeError}
188
+ * @description Adds an element addressed by its key to a given object.
189
+ */
175
190
  function addXObjElement(obj, name){
176
191
  let err = null;
177
192
  if (!isPlainObject(obj)) {
@@ -212,6 +227,17 @@ function addXObjElement(obj, name){
212
227
  };
213
228
  };
214
229
 
230
+ /**
231
+ * @function insertXObjElement
232
+ * @param {object}
233
+ * @param {ID_STRING}
234
+ * @param {object} [opt]
235
+ * @param {bool} [opt.force=false]
236
+ * @param {bool} [opt.ripOldies=false]
237
+ * @returns {?object}
238
+ * @throws {TypeError}
239
+ * @description Inserts an element addressed by its key into a given object.
240
+ */
215
241
  function insertXObjElement(obj, name, opt){
216
242
  let err = null;
217
243
  if (!isPlainObject(obj)) {
@@ -247,6 +273,14 @@ function insertXObjElement(obj, name, opt){
247
273
  return isACCEPTED ? prop : null;
248
274
  };
249
275
 
276
+ /**
277
+ * @function deleteXObjElement
278
+ * @param {object}
279
+ * @param {ID_STRING}
280
+ * @returns {bool}
281
+ * @throws {TypeError}
282
+ * @description Deletes an element addressed by its key from a given object.
283
+ */
250
284
  function deleteXObjElement(obj, name){
251
285
  let err = null;
252
286
  if (!isPlainObject(obj)) {
@@ -271,6 +305,14 @@ function deleteXObjElement(obj, name){
271
305
  return result;
272
306
  };
273
307
 
308
+ /**
309
+ * @function deleteXObjElementEx
310
+ * @param {object}
311
+ * @param {ID_STRING}
312
+ * @returns {object}
313
+ * @throws {TypeError}
314
+ * @description Deletes an element addressed by its key from a given object.
315
+ */
274
316
  function deleteXObjElementEx(obj, name){
275
317
  let err = null;
276
318
  if (!isPlainObject(obj)) {
@@ -299,6 +341,15 @@ function deleteXObjElementEx(obj, name){
299
341
  };
300
342
  };
301
343
 
344
+ /**
345
+ * @function renameXObjElement
346
+ * @param {object}
347
+ * @param {ID_STRING} - old key
348
+ * @param {ID_STRING} - new key
349
+ * @returns {bool}
350
+ * @throws {TypeError}
351
+ * @description Renames an element addressed by its key.
352
+ */
302
353
  function renameXObjElement(obj, name, newName){
303
354
  let err = null;
304
355
  if (!isPlainObject(obj)) {
@@ -332,6 +383,15 @@ function renameXObjElement(obj, name, newName){
332
383
  return result;
333
384
  };
334
385
 
386
+ /**
387
+ * @function checkXObjAttribute
388
+ * @param {object}
389
+ * @param {ID_STRING}
390
+ * @param {ID_STRING} [key]
391
+ * @returns {bool}
392
+ * @throws {TypeError}
393
+ * @description Checks wheter an attribute is exists.
394
+ */
335
395
  function checkXObjAttribute(obj, attr = '', key){
336
396
  if (typeof attr !== 'string') {
337
397
  let err = new TypeError(XOBJ_TE_NSTR_EMSG);
@@ -356,6 +416,15 @@ function checkXObjAttribute(obj, attr = '', key){
356
416
  return result;
357
417
  };
358
418
 
419
+ /**
420
+ * @function deleteXObjAttribute
421
+ * @param {object}
422
+ * @param {ID_STRING}
423
+ * @param {ID_STRING} [key]
424
+ * @returns {bool}
425
+ * @throws {TypeError}
426
+ * @description Deletes an attribute addressed by a given key.
427
+ */
359
428
  function deleteXObjAttribute(obj, attr = '', key){
360
429
  if (typeof attr !== 'string') {
361
430
  let err = new TypeError(XOBJ_TE_NSTR_EMSG);
@@ -377,6 +446,14 @@ function deleteXObjAttribute(obj, attr = '', key){
377
446
  return result;
378
447
  };
379
448
 
449
+ /**
450
+ * @function readXObjParamRaw
451
+ * @param {object}
452
+ * @param {ID_STRING} [key]
453
+ * @returns {any}
454
+ * @throws {TypeError}
455
+ * @description Extracts a parameter from a given object.
456
+ */
380
457
  function readXObjParamRaw(obj, key = XOBJ_DEF_PARAM_TNAME){
381
458
  let err = null;
382
459
  if (!isPlainObject(obj)) {
@@ -393,6 +470,15 @@ function readXObjParamRaw(obj, key = XOBJ_DEF_PARAM_TNAME){
393
470
  return _key !== '' ? obj[_key] : undefined;
394
471
  };
395
472
 
473
+ /**
474
+ * @function writeXObjParamRaw
475
+ * @param {object}
476
+ * @param {any}
477
+ * @param {ID_STRING} [key]
478
+ * @returns {bool}
479
+ * @throws {TypeError}
480
+ * @description Writes a parameter into a given object.
481
+ */
396
482
  function writeXObjParamRaw(obj, value, key = XOBJ_DEF_PARAM_TNAME){
397
483
  let err = null;
398
484
  if (!isPlainObject(obj)) {
@@ -414,6 +500,15 @@ function writeXObjParamRaw(obj, value, key = XOBJ_DEF_PARAM_TNAME){
414
500
  return isSUCCEED;
415
501
  };
416
502
 
503
+ /**
504
+ * @function readXObjAttrRaw
505
+ * @param {object}
506
+ * @param {ID_STRING}
507
+ * @param {ID_STRING} [key]
508
+ * @returns {any}
509
+ * @throws {TypeError}
510
+ * @description Extracts an attribute from a given object.
511
+ */
417
512
  function readXObjAttrRaw(obj, attr = '', key){
418
513
  if (typeof attr !== 'string') {
419
514
  let err = new TypeError(XOBJ_TE_NSTR_EMSG);
@@ -430,6 +525,16 @@ function readXObjAttrRaw(obj, attr = '', key){
430
525
  if (objAttr !== null && attrName !== '') return objAttr[attrName];
431
526
  };
432
527
 
528
+ /**
529
+ * @function writeXObjAttrRaw
530
+ * @param {object}
531
+ * @param {ID_STRING}
532
+ * @param {any}
533
+ * @param {ID_STRING} [key]
534
+ * @returns {bool}
535
+ * @throws {TypeError}
536
+ * @description Writes a parameter into a given object.
537
+ */
433
538
  function writeXObjAttrRaw(obj, attr = '', value, key = XOBJ_DEF_ATTR_TNAME){
434
539
  if (typeof attr !== 'string') {
435
540
  let err = new TypeError(XOBJ_TE_NSTR_EMSG);
@@ -462,6 +567,14 @@ function writeXObjAttrRaw(obj, attr = '', value, key = XOBJ_DEF_ATTR_TNAME){
462
567
  return isSUCCEED;
463
568
  };
464
569
 
570
+ /**
571
+ * @function readXObjParam
572
+ * @param {object}
573
+ * @param {ID_STRING} [key]
574
+ * @returns {string}
575
+ * @throws {TypeError}
576
+ * @description Extracts a parameter from a given object.
577
+ */
465
578
  function readXObjParam(obj, key){
466
579
  let result = undefined;
467
580
  try {
@@ -484,6 +597,15 @@ function readXObjParam(obj, key){
484
597
  });
485
598
  };
486
599
 
600
+ /**
601
+ * @function readXObjParam
602
+ * @param {object}
603
+ * @param {bool}
604
+ * @param {ID_STRING} [key]
605
+ * @returns {bool}
606
+ * @throws {TypeError}
607
+ * @description Extracts a parameter from a given object.
608
+ */
487
609
  function readXObjParamAsBool(obj, defValue, key){
488
610
  let result = undefined;
489
611
  try {
@@ -502,6 +624,15 @@ function readXObjParamAsBool(obj, defValue, key){
502
624
  return readAsBoolEx(result, defValue);
503
625
  };
504
626
 
627
+ /**
628
+ * @function readXObjParam
629
+ * @param {object}
630
+ * @param {number}
631
+ * @param {ID_STRING} [key]
632
+ * @returns {number}
633
+ * @throws {TypeError}
634
+ * @description Extracts a parameter from a given object.
635
+ */
505
636
  function readXObjParamAsNum(obj, defValue, key){
506
637
  let result = undefined;
507
638
  try {
@@ -520,6 +651,15 @@ function readXObjParamAsNum(obj, defValue, key){
520
651
  return readAsNumberEx(result, defValue);
521
652
  };
522
653
 
654
+ /**
655
+ * @function readXObjParam
656
+ * @param {object}
657
+ * @param {string}
658
+ * @param {ID_STRING} [key]
659
+ * @returns {string}
660
+ * @throws {TypeError}
661
+ * @description Extracts a parameter from a given object.
662
+ */
523
663
  function readXObjParamAsStr(obj, defValue, key){
524
664
  let result = undefined;
525
665
  try {
@@ -543,6 +683,14 @@ function readXObjParamAsStr(obj, defValue, key){
543
683
  });
544
684
  };
545
685
 
686
+ /**
687
+ * @function readXObjParam
688
+ * @param {object}
689
+ * @param {ID_STRING} [key]
690
+ * @returns {index}
691
+ * @throws {TypeError}
692
+ * @description Extracts a parameter from a given object.
693
+ */
546
694
  function readXObjParamAsIndex(obj, key){
547
695
  let result = undefined;
548
696
  try {
@@ -561,6 +709,15 @@ function readXObjParamAsIndex(obj, key){
561
709
  return valueToIndex(result);
562
710
  };
563
711
 
712
+ /**
713
+ * @function writeXObjParam
714
+ * @param {object}
715
+ * @param {any}
716
+ * @param {ID_STRING} [key]
717
+ * @returns {bool}
718
+ * @throws {TypeError}
719
+ * @description Writes a parameter into a given object.
720
+ */
564
721
  function writeXObjParam(obj, value, key){
565
722
  let isSUCCEED = false;
566
723
  if (value !== undefined) {
@@ -586,6 +743,16 @@ function writeXObjParam(obj, value, key){
586
743
  return isSUCCEED;
587
744
  };
588
745
 
746
+ /**
747
+ * @function writeXObjParamAsBool
748
+ * @param {object}
749
+ * @param {any}
750
+ * @param {bool}
751
+ * @param {ID_STRING} [key]
752
+ * @returns {bool}
753
+ * @throws {TypeError}
754
+ * @description Writes a parameter into a given object.
755
+ */
589
756
  function writeXObjParamAsBool(obj, value, defValue, key){
590
757
  let isSUCCEED = false;
591
758
  if (value !== undefined) {
@@ -607,6 +774,16 @@ function writeXObjParamAsBool(obj, value, defValue, key){
607
774
  return isSUCCEED;
608
775
  };
609
776
 
777
+ /**
778
+ * @function writeXObjParamAsNum
779
+ * @param {object}
780
+ * @param {any}
781
+ * @param {number}
782
+ * @param {ID_STRING} [key]
783
+ * @returns {bool}
784
+ * @throws {TypeError}
785
+ * @description Writes a parameter into a given object.
786
+ */
610
787
  function writeXObjParamAsNum(obj, value, defValue, key){
611
788
  let isSUCCEED = false;
612
789
  if (value !== undefined) {
@@ -628,6 +805,15 @@ function writeXObjParamAsNum(obj, value, defValue, key){
628
805
  return isSUCCEED;
629
806
  };
630
807
 
808
+ /**
809
+ * @function writeXObjParamAsIndex
810
+ * @param {object}
811
+ * @param {any}
812
+ * @param {ID_STRING} [key]
813
+ * @returns {bool}
814
+ * @throws {TypeError}
815
+ * @description Writes a parameter into a given object.
816
+ */
631
817
  function writeXObjParamAsIndex(obj, value, key){
632
818
  let isSUCCEED = false;
633
819
  if (value !== undefined) {
@@ -649,6 +835,16 @@ function writeXObjParamAsIndex(obj, value, key){
649
835
  return isSUCCEED;
650
836
  };
651
837
 
838
+ /**
839
+ * @function writeXObjParamEx
840
+ * @param {object}
841
+ * @param {any}
842
+ * @param {any}
843
+ * @param {ID_STRING} [key]
844
+ * @returns {bool}
845
+ * @throws {TypeError}
846
+ * @description Writes a parameter into a given object.
847
+ */
652
848
  function writeXObjParamEx(obj, value, defValue, key){
653
849
  let isSUCCEED = false;
654
850
  if (value !== undefined) {
@@ -680,6 +876,15 @@ function writeXObjParamEx(obj, value, defValue, key){
680
876
  return isSUCCEED;
681
877
  };
682
878
 
879
+ /**
880
+ * @function readXObjAttr
881
+ * @param {object}
882
+ * @param {ID_STRING}
883
+ * @param {ID_STRING} [key]
884
+ * @returns {string}
885
+ * @throws {TypeError}
886
+ * @description Extracts an attribute from a given object.
887
+ */
683
888
  function readXObjAttr(obj, attr, key){
684
889
  let result = undefined;
685
890
  try {
@@ -702,6 +907,16 @@ function readXObjAttr(obj, attr, key){
702
907
  });
703
908
  };
704
909
 
910
+ /**
911
+ * @function readXObjAttrAsBool
912
+ * @param {object}
913
+ * @param {ID_STRING}
914
+ * @param {bool}
915
+ * @param {ID_STRING} [key]
916
+ * @returns {bool}
917
+ * @throws {TypeError}
918
+ * @description Extracts an attribute from a given object.
919
+ */
705
920
  function readXObjAttrAsBool(obj, attr, defValue, key){
706
921
  let result = undefined;
707
922
  try {
@@ -720,6 +935,16 @@ function readXObjAttrAsBool(obj, attr, defValue, key){
720
935
  return readAsBoolEx(result, defValue);
721
936
  };
722
937
 
938
+ /**
939
+ * @function readXObjAttrAsNum
940
+ * @param {object}
941
+ * @param {ID_STRING}
942
+ * @param {number}
943
+ * @param {ID_STRING} [key]
944
+ * @returns {number}
945
+ * @throws {TypeError}
946
+ * @description Extracts an attribute from a given object.
947
+ */
723
948
  function readXObjAttrAsNum(obj, attr, defValue, key){
724
949
  let result = undefined;
725
950
  try {
@@ -738,6 +963,16 @@ function readXObjAttrAsNum(obj, attr, defValue, key){
738
963
  return readAsNumberEx(result, defValue);
739
964
  };
740
965
 
966
+ /**
967
+ * @function readXObjAttrAsStr
968
+ * @param {object}
969
+ * @param {ID_STRING}
970
+ * @param {string}
971
+ * @param {ID_STRING} [key]
972
+ * @returns {string}
973
+ * @throws {TypeError}
974
+ * @description Extracts an attribute from a given object.
975
+ */
741
976
  function readXObjAttrAsStr(obj, attr, defValue, key){
742
977
  let result = undefined;
743
978
  try {
@@ -761,6 +996,15 @@ function readXObjAttrAsStr(obj, attr, defValue, key){
761
996
  });
762
997
  };
763
998
 
999
+ /**
1000
+ * @function readXObjAttrAsIndex
1001
+ * @param {object}
1002
+ * @param {ID_STRING}
1003
+ * @param {ID_STRING} [key]
1004
+ * @returns {index}
1005
+ * @throws {TypeError}
1006
+ * @description Extracts an attribute from a given object.
1007
+ */
764
1008
  function readXObjAttrAsIndex(obj, attr, key){
765
1009
  let result = undefined;
766
1010
  try {
@@ -779,6 +1023,16 @@ function readXObjAttrAsIndex(obj, attr, key){
779
1023
  return valueToIndex(result);
780
1024
  };
781
1025
 
1026
+ /**
1027
+ * @function writeXObjAttr
1028
+ * @param {object}
1029
+ * @param {ID_STRING}
1030
+ * @param {any}
1031
+ * @param {ID_STRING} [key]
1032
+ * @returns {bool}
1033
+ * @throws {TypeError}
1034
+ * @description Writes a parameter into a given object.
1035
+ */
782
1036
  function writeXObjAttr(obj, attr, value, key){
783
1037
  let isSUCCEED = false;
784
1038
  if (value !== undefined) {
@@ -804,6 +1058,17 @@ function writeXObjAttr(obj, attr, value, key){
804
1058
  return isSUCCEED;
805
1059
  };
806
1060
 
1061
+ /**
1062
+ * @function writeXObjAttrAsBool
1063
+ * @param {object}
1064
+ * @param {ID_STRING}
1065
+ * @param {any}
1066
+ * @param {bool}
1067
+ * @param {ID_STRING} [key]
1068
+ * @returns {bool}
1069
+ * @throws {TypeError}
1070
+ * @description Writes a parameter into a given object.
1071
+ */
807
1072
  function writeXObjAttrAsBool(obj, attr, value, defValue, key){
808
1073
  let isSUCCEED = false;
809
1074
  if (value !== undefined) {
@@ -825,6 +1090,17 @@ function writeXObjAttrAsBool(obj, attr, value, defValue, key){
825
1090
  return isSUCCEED;
826
1091
  };
827
1092
 
1093
+ /**
1094
+ * @function writeXObjAttrAsNum
1095
+ * @param {object}
1096
+ * @param {ID_STRING}
1097
+ * @param {any}
1098
+ * @param {number}
1099
+ * @param {ID_STRING} [key]
1100
+ * @returns {bool}
1101
+ * @throws {TypeError}
1102
+ * @description Writes a parameter into a given object.
1103
+ */
828
1104
  function writeXObjAttrAsNum(obj, attr, value, defValue, key){
829
1105
  let isSUCCEED = false;
830
1106
  if (value !== undefined) {
@@ -846,6 +1122,16 @@ function writeXObjAttrAsNum(obj, attr, value, defValue, key){
846
1122
  return isSUCCEED;
847
1123
  };
848
1124
 
1125
+ /**
1126
+ * @function writeXObjAttrAsIndex
1127
+ * @param {object}
1128
+ * @param {ID_STRING}
1129
+ * @param {any}
1130
+ * @param {ID_STRING} [key]
1131
+ * @returns {bool}
1132
+ * @throws {TypeError}
1133
+ * @description Writes a parameter into a given object.
1134
+ */
849
1135
  function writeXObjAttrAsIndex(obj, attr, value, key){
850
1136
  let isSUCCEED = false;
851
1137
  if (value !== undefined) {
@@ -867,6 +1153,17 @@ function writeXObjAttrAsIndex(obj, attr, value, key){
867
1153
  return isSUCCEED;
868
1154
  };
869
1155
 
1156
+ /**
1157
+ * @function writeXObjAttrEx
1158
+ * @param {object}
1159
+ * @param {ID_STRING}
1160
+ * @param {any}
1161
+ * @param {any}
1162
+ * @param {ID_STRING} [key]
1163
+ * @returns {bool}
1164
+ * @throws {TypeError}
1165
+ * @description Writes a parameter into a given object.
1166
+ */
870
1167
  function writeXObjAttrEx(obj, attr, value, defValue, key){
871
1168
  let isSUCCEED = false;
872
1169
  if (value !== undefined) {
@@ -898,6 +1195,15 @@ function writeXObjAttrEx(obj, attr, value, defValue, key){
898
1195
  return isSUCCEED;
899
1196
  };
900
1197
 
1198
+ /**
1199
+ * @function insertXObjElements
1200
+ * @param {object}
1201
+ * @param {...ID_STRING} args - list of a keys
1202
+ * @param {object} [opt]
1203
+ * @returns {number}
1204
+ * @throws {TypeError}
1205
+ * @description Inserts an elements into a given object.
1206
+ */
901
1207
  function insertXObjElements(obj, ...args){
902
1208
  if (!isPlainObject(obj)) {
903
1209
  let err = new TypeError(XOBJ_TE_NPOBJ_EMSG);
@@ -918,6 +1224,17 @@ function insertXObjElements(obj, ...args){
918
1224
  return count;
919
1225
  };
920
1226
 
1227
+ /**
1228
+ * @function insertXObjEList
1229
+ * @param {object}
1230
+ * @param {ID_STRING}
1231
+ * @param {object} [opt]
1232
+ * @param {bool} [opt.force=false]
1233
+ * @param {bool} [opt.ripOldies=false]
1234
+ * @returns {?any}
1235
+ * @throws {TypeError}
1236
+ * @description Inserts a list elements into a given object.
1237
+ */
921
1238
  function insertXObjEList(obj, name, opt){
922
1239
  let err = null;
923
1240
  if (!isPlainObject(obj)) {
@@ -957,6 +1274,16 @@ function insertXObjEList(obj, name, opt){
957
1274
  return isACCEPTED ? item : null;
958
1275
  };
959
1276
 
1277
+ /**
1278
+ * @function insertXObjEChain
1279
+ * @param {object}
1280
+ * @param {ID_STRING}
1281
+ * @param {...ID_STRING} args - a list of keys
1282
+ * @param {object} [opt]
1283
+ * @returns {?any}
1284
+ * @throws {TypeError}
1285
+ * @description Inserts a chain of an elements into a given object.
1286
+ */
960
1287
  function insertXObjEChain(obj, ...args){
961
1288
  if (!isPlainObject(obj)) {
962
1289
  let err = new TypeError(XOBJ_TE_NPOBJ_EMSG);
@@ -981,106 +1308,14 @@ function insertXObjEChain(obj, ...args){
981
1308
  return isSUCCEED ? curObj : null;
982
1309
  };
983
1310
 
984
- // === module main block (class definitions) ===
985
-
986
- class TXmlContentParseOptions {
987
- #_options = null;
988
-
989
- constructor(param){
990
- this.#_options = TXmlContentParseOptions.createNewOptionsSet(param);
991
- }
992
-
993
- get settings(){ return this.#_options }
994
-
995
- get xml2js(){
996
- let _settings = this.#_options;
997
- return {
998
- compact: _settings.compact,
999
- declarationKey: _settings.declarationKey,
1000
- attributesKey: _settings.attributesKey,
1001
- textKey: _settings.textKey,
1002
- commentKey: _settings.commentKey,
1003
- cdataKey: _settings.cdataKey,
1004
- nameKey: _settings.nameKey,
1005
- typeKey: _settings.typeKey,
1006
- parentKey: _settings.parentKey,
1007
- elementsKey: _settings.elementsKey,
1008
- ignoreDeclaration: _settings.ignoreDeclaration,
1009
- ignoreDocType: _settings.ignoreDocType,
1010
- ignoreInstraction: _settings.ignoreInstractions,
1011
- ignoreText: _settings.ignoreText,
1012
- ignoreComment: _settings.ignoreComments,
1013
- ignoreCData: _settings.ignoreCData,
1014
- addParent: _settings.addParent,
1015
- trim: _settings.trim,
1016
- };
1017
- }
1018
-
1019
- get js2xml(){
1020
- let _settings = this.#_options;
1021
- return {
1022
- compact: _settings.compact,
1023
- declarationKey: _settings.declarationKey,
1024
- attributesKey: _settings.attributesKey,
1025
- textKey: _settings.textKey,
1026
- commentKey: _settings.commentKey,
1027
- cdataKey: _settings.cdataKey,
1028
- nameKey: _settings.nameKey,
1029
- typeKey: _settings.typeKey,
1030
- parentKey: _settings.parentKey,
1031
- elementsKey: _settings.elementsKey,
1032
- ignoreDeclaration: _settings.ignoreDeclaration,
1033
- ignoreDocType: _settings.ignoreDocType,
1034
- ignoreInstraction: _settings.ignoreInstractions,
1035
- ignoreText: _settings.ignoreText,
1036
- ignoreComment: _settings.ignoreComments,
1037
- ignoreCData: _settings.ignoreCData,
1038
- fullTagEmptyElement: _settings.fullTagEmptyElement,
1039
- spaces: _settings.spaces,
1040
- };
1041
- }
1042
-
1043
- static createNewOptionsSet(opt){
1044
- if (opt instanceof TXmlContentParseOptions) {
1045
- opt = opt.settings;
1046
- } else if (isPlainObject(opt)) {
1047
- opt = isPlainObject(opt.settings) ? opt.settings : opt;
1048
- } else {
1049
- opt = DEF_XML_PARSE_OPTIONS;
1050
- };
1051
- return {
1052
- compact: opt.compact,
1053
- declarationKey: opt.declarationKey,
1054
- attributesKey: opt.attributesKey,
1055
- textKey: opt.textKey,
1056
- commentKey: opt.commentKey,
1057
- cdataKey: opt.cdataKey,
1058
- nameKey: opt.nameKey,
1059
- typeKey: opt.typeKey,
1060
- parentKey: opt.parentKey,
1061
- elementsKey: opt.elementsKey,
1062
- ignoreDeclaration: opt.ignoreDeclaration,
1063
- ignoreDocType: opt.ignoreDocType,
1064
- ignoreInstractions: opt.ignoreInstractions,
1065
- ignoreText: opt.ignoreText,
1066
- ignoreComments: opt.ignoreComments,
1067
- ignoreCData: opt.ignoreCData,
1068
- fullTagEmptyElement: opt.fullTagEmptyElement,
1069
- addParent: opt.addParent,
1070
- trim: opt.trim,
1071
- spaces: opt.spaces,
1072
- };
1073
- }
1074
-
1075
- }
1311
+ /***
1312
+ * (* class definitions *)
1313
+ */
1076
1314
 
1077
1315
  // === module exports block ===
1078
1316
 
1079
1317
  exports.XOBJ_DEF_PARAM_TNAME = XOBJ_DEF_PARAM_TNAME;
1080
1318
  exports.XOBJ_DEF_ATTR_TNAME = XOBJ_DEF_ATTR_TNAME;
1081
- exports.DEF_XML_PARSE_OPTIONS = DEF_XML_PARSE_OPTIONS;
1082
-
1083
- exports.TXmlContentParseOptions = TXmlContentParseOptions;
1084
1319
 
1085
1320
  exports.evalXObjEName = evalXObjEName;
1086
1321
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ygracs/xobj-lib-js",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "A helper library to work with xml-js module",
5
5
  "author": "ygracs <cs70th-om@rambler.ru>",
6
6
  "license": "MIT",
@@ -12,6 +12,7 @@
12
12
  "files": [
13
13
  "doc/xObj.md",
14
14
  "lib/xObj-lib.js",
15
+ "lib/xObj-defs.js",
15
16
  "index.js",
16
17
  "CHANGELOG.md"
17
18
  ],
@@ -21,16 +22,20 @@
21
22
  "test-xobj:c1": "jest xObj-lib.1",
22
23
  "test-xobj:c2": "jest xObj-lib.2",
23
24
  "test-xobj:c3": "jest xObj-lib.3",
24
- "test-xobj:c4": "jest xObj-lib.4"
25
+ "test-xobj:c4": "jest xObj-lib.4",
26
+ "build-doc-md": "jsdoc2md",
27
+ "build-doc-html": "jsdoc"
25
28
  },
26
29
  "imports": {
27
- "#lib/*": "./lib/*"
30
+ "#lib/*": "./lib/*",
31
+ "#test-dir/*": "./__test__/*"
28
32
  },
29
33
  "dependencies": {
30
- "@ygracs/bsfoc-lib-js": "^0.2.0"
34
+ "@ygracs/bsfoc-lib-js": "^0.2.1"
31
35
  },
32
36
  "devDependencies": {
33
37
  "jest": "^29.7.0",
38
+ "jsdoc-to-markdown": "^8.0.1",
34
39
  "minimist": "^1.2.8"
35
40
  }
36
41
  }