@cntwg/xml-lib-js 0.0.32 → 0.0.33

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,14 @@
1
+ #### *v0.0.33*
2
+
3
+ Pre-release version.
4
+
5
+ > - update dependency on `@ygracs/bsfoc-lib-js` module to v0.2.3;
6
+ > - update dependency on `@ygracs/xobj-lib-js` module to v0.2.2;
7
+ > - update dependency on `@ygracs/xml-js6` module to v0.0.5-b;
8
+ > - add dependency on `@cntwg/file-helper` module;
9
+ > - remove `file-helper.js` module;
10
+ > - some other fixes.
11
+
1
12
  #### *v0.0.32*
2
13
 
3
14
  Pre-release version.
package/lib/xmldoc-lib.js CHANGED
@@ -1,4 +1,4 @@
1
- // [v0.2.107-20250208]
1
+ // [v0.2.111-20250415]
2
2
 
3
3
  // === module init block ===
4
4
 
@@ -12,12 +12,15 @@ const {
12
12
  } = require('@ygracs/bsfoc-lib-js');
13
13
 
14
14
  const {
15
- loadFileSync, loadFile,
15
+ loadFromFileSync, loadFromFile,
16
16
  saveToFileSync, saveToFile,
17
- } = require('./file-helper.js');
17
+ } = require('@cntwg/file-helper');
18
18
 
19
19
  const xObj = require('@ygracs/xobj-lib-js');
20
20
  const {
21
+ evalXObjEName,
22
+ getXObjElement, addXObjElement, insertXObjElement,
23
+ readXObjParam, writeXObjParam,
21
24
  TXmlContentParseOptions,
22
25
  DEF_XML_PARSE_OPTIONS: XML_DEF_PARSE_OPTIONS,
23
26
  } = xObj;
@@ -26,9 +29,9 @@ const {
26
29
 
27
30
  /**
28
31
  * @typedef {Object} OPT_xmlelemsett
29
- * @property {object} [parseOptions] - parser options
30
- * @property {boolean} [opt.proxyModeEnable=false]
31
- * @property {boolean} [opt.isNullItemsAllowed=false] - <reserved>
32
+ * @property {TXmlContentParseOptions} [parseOptions] - parser options
33
+ * @property {boolean} [proxyModeEnable=false] - mode flag
34
+ * @property {boolean} [isNullItemsAllowed=false] - <reserved>
32
35
  * @description An XML-element controller settings.
33
36
  */
34
37
 
@@ -40,6 +43,7 @@ const {
40
43
  * @description Evaluates an XML-element settings
41
44
  */
42
45
  function __evalXMLElementSettings(value) {
46
+ /** @type {OPT_xmlelemsett} */
43
47
  const settings = isPlainObject(value) ? value : {};
44
48
  let {
45
49
  proxyModeEnable,
@@ -56,10 +60,13 @@ function __evalXMLElementSettings(value) {
56
60
 
57
61
  /**
58
62
  * @typedef {Object} OPT_xmlcontsett
59
- * @augments OPT_xmlelemsett
60
- * @property {string} [rootETagName]
61
- * @property {boolean} [autoBindRoot=true]
63
+ * @property {TXmlContentParseOptions} [parseOptions] - parser options
64
+ * @property {boolean} [proxyModeEnable=false] - mode flag
65
+ * @property {boolean} [isNullItemsAllowed=false] - <reserved>
66
+ * @property {string} [rootETagName] - tag name of the root element
67
+ * @property {boolean} [autoBindRoot=true] - mode flag
62
68
  * @description An XML-content container settings.
69
+ * @see OPT_xmlelemsett
63
70
  */
64
71
 
65
72
  /**
@@ -70,6 +77,7 @@ function __evalXMLElementSettings(value) {
70
77
  * @description Evaluates an XML-container settings
71
78
  */
72
79
  function __evalXMLContainerSettings(value) {
80
+ /** @type {OPT_xmlcontsett} */
73
81
  const settings = __evalXMLElementSettings(value);
74
82
  let { rootETagName, autoBindRoot } = settings;
75
83
  if (
@@ -178,9 +186,9 @@ function valueToElementID(value) {
178
186
  * @classdesc This class implements an attributes mapper instance
179
187
  */
180
188
  class TXmlAttributesMapper {
181
- /** @property {?object} */
189
+ /** @type {object} */
182
190
  #_host;
183
- /** @property {OPT_xmlelemsett} */
191
+ /** @type {OPT_xmlelemsett} */
184
192
  #_options;
185
193
 
186
194
  /**
@@ -211,7 +219,7 @@ class TXmlAttributesMapper {
211
219
 
212
220
  /**
213
221
  * Contains a list of all instance attributes as a `<name>-<value>` pairs
214
- * @property {any[]}
222
+ * @type {string[][]}
215
223
  * @readonly
216
224
  */
217
225
  get entries() {
@@ -338,7 +346,7 @@ class TXmlAttributesMapper {
338
346
  * @description Deletes all attributes.
339
347
  */
340
348
  clear() {
341
- xObj.insertXObjElement(
349
+ insertXObjElement(
342
350
  this.#_host,
343
351
  this.#_options.settings.attributesKey,
344
352
  {
@@ -354,13 +362,13 @@ class TXmlAttributesMapper {
354
362
  * @classdesc This class implements an instance of an element controller
355
363
  */
356
364
  class TXmlElementController {
357
- /** @property {?object} */
365
+ /** @type {object} */
358
366
  #_host;
359
- /** @property {OPT_xmlelemsett} */
367
+ /** @type {OPT_xmlelemsett} */
360
368
  #_options;
361
- /** @property {?object} */
369
+ /** @type {object} */
362
370
  #_parseOptions;
363
- /** @property {?TXmlAttributesMapper} */
371
+ /** @type {TXmlAttributesMapper} */
364
372
  #_attributes;
365
373
  #_name = null;
366
374
 
@@ -399,7 +407,7 @@ class TXmlElementController {
399
407
 
400
408
  /**
401
409
  * Contains an element attribute mapper instance
402
- * @property {TXmlAttributesMapper}
410
+ * @type {TXmlAttributesMapper}
403
411
  * @readonly
404
412
  */
405
413
  get attributes() {
@@ -407,14 +415,14 @@ class TXmlElementController {
407
415
  }
408
416
 
409
417
  /**
410
- * @property {string}
418
+ * @type {string}
411
419
  * @readonly
412
420
  * @experimental
413
421
  */
414
422
  get name() {
415
423
  let result = '';
416
424
  if (this.#_parseOptions.settings.compact) {
417
- result = xObj.readXObjParam(
425
+ result = readXObjParam(
418
426
  this.#_host,
419
427
  this.#_parseOptions.settings.nameKey
420
428
  );
@@ -426,14 +434,14 @@ class TXmlElementController {
426
434
 
427
435
  /**
428
436
  * Contains an element text value
429
- * @property {string}
437
+ * @type {string}
430
438
  */
431
439
  get textValue() {
432
- return xObj.readXObjParam(this.#_host, this.#_parseOptions.settings.textKey);
440
+ return readXObjParam(this.#_host, this.#_parseOptions.settings.textKey);
433
441
  }
434
442
 
435
443
  set textValue(value) {
436
- xObj.writeXObjParam(this.#_host, value, this.#_parseOptions.settings.textKey);
444
+ writeXObjParam(this.#_host, value, this.#_parseOptions.settings.textKey);
437
445
  }
438
446
 
439
447
  /**
@@ -509,7 +517,7 @@ class TXmlElementController {
509
517
  const _value = valueToEntry(value);
510
518
  if (_value !== null) {
511
519
  const [ lang, param ] = _value;
512
- if (xObj.writeXObjParam(
520
+ if (writeXObjParam(
513
521
  this.#_host,
514
522
  param,
515
523
  this.#_parseOptions.settings.textKey,
@@ -535,11 +543,11 @@ class TXmlElementController {
535
543
  * @description Checks whether an element has a child element.
536
544
  */
537
545
  hasChild(name) {
538
- const _name = xObj.evalXObjEName(name);
546
+ const _name = evalXObjEName(name);
539
547
  if (_name === null) return false;
540
548
  let result = false;
541
549
  try {
542
- result = isObject(xObj.getXObjElement(this.#_host, _name));
550
+ result = isObject(getXObjElement(this.#_host, _name));
543
551
  } catch (err) {
544
552
  // // TODO: [?] verify what error is thrown
545
553
  //throw err;
@@ -554,11 +562,11 @@ class TXmlElementController {
554
562
  * @description Returns a child element.
555
563
  */
556
564
  getChild(name) {
557
- const _name = xObj.evalXObjEName(name);
565
+ const _name = evalXObjEName(name);
558
566
  let result = null;
559
567
  if (_name !== null) {
560
568
  try {
561
- const item = xObj.getXObjElement(this.#_host, _name);
569
+ const item = getXObjElement(this.#_host, _name);
562
570
  result = TXmlElementController.create(item, this.#_options);
563
571
  } catch (err) {
564
572
  // // TODO: [?] verify what error is thrown
@@ -614,11 +622,11 @@ class TXmlElementController {
614
622
  * @description Adds a new child element.
615
623
  */
616
624
  addChild(name) {
617
- const _name = xObj.evalXObjEName(name);
625
+ const _name = evalXObjEName(name);
618
626
  if (_name === null) return null;
619
627
  let result = null;
620
628
  try {
621
- const { item } = xObj.addXObjElement(this.#_host, _name);
629
+ const { item } = addXObjElement(this.#_host, _name);
622
630
  if (item) result = new TXmlElementController(item, this.#_options)
623
631
  } catch (err) {
624
632
  // // TODO: [?] verify what error is thrown
@@ -634,11 +642,11 @@ class TXmlElementController {
634
642
  * @description Deletes a child element.
635
643
  */
636
644
  delChild(name) {
637
- const _name = xObj.evalXObjEName(name);
645
+ const _name = evalXObjEName(name);
638
646
  if (_name === null) return false;
639
647
  let isSucceed = false;
640
648
  try {
641
- const item = xObj.getXObjElement(this.#_host, _name);
649
+ const item = getXObjElement(this.#_host, _name);
642
650
  let isACCEPTED = false;
643
651
  if (isArray(item)) {
644
652
  //console.log('CHECK: TXmlElementController.delChild() => item.typeof() => [object Array]');
@@ -744,7 +752,7 @@ class TXmlElementController {
744
752
  if (node instanceof TXmlElementController) {
745
753
  try {
746
754
  const obj = node.#_host;
747
- item = xObj.getXObjElement(obj, name);
755
+ item = getXObjElement(obj, name);
748
756
  if (item === undefined) item = null;
749
757
  } catch (err) {
750
758
  // // TODO: [?] check what kind of error thrown
@@ -772,7 +780,7 @@ class TXmlElementController {
772
780
  ) {
773
781
  try {
774
782
  const obj = node.#_host;
775
- ({ item } = xObj.addXObjElement(obj, childName));
783
+ ({ item } = addXObjElement(obj, childName));
776
784
  } catch (err) {
777
785
  // // TODO: [?] check what kind of error thrown
778
786
  item = null;
@@ -851,9 +859,9 @@ class TXmlElementController {
851
859
  * @classdesc This class implements an instance of a list controller
852
860
  */
853
861
  class TXmlElementsListController {
854
- /** @property {?object[]} */
862
+ /** @type {object[]} */
855
863
  #_host;
856
- /** @property {OPT_xmlelemsett} */
864
+ /** @type {OPT_xmlelemsett} */
857
865
  #_options;
858
866
  #_count = null;
859
867
 
@@ -888,7 +896,7 @@ class TXmlElementsListController {
888
896
 
889
897
  /**
890
898
  * Contains a quantity of an elements
891
- * @property {number}
899
+ * @type {number}
892
900
  * @readonly
893
901
  */
894
902
  get count() {
@@ -897,7 +905,7 @@ class TXmlElementsListController {
897
905
 
898
906
  /**
899
907
  * Contains a quantity of an elements
900
- * @property {number}
908
+ * @type {number}
901
909
  * @readonly
902
910
  */
903
911
  get size() {
@@ -906,7 +914,7 @@ class TXmlElementsListController {
906
914
 
907
915
  /**
908
916
  * Contains a maximum possible element index in the list
909
- * @property {number}
917
+ * @type {number}
910
918
  * @readonly
911
919
  */
912
920
  get maxIndex() {
@@ -915,7 +923,7 @@ class TXmlElementsListController {
915
923
 
916
924
  /**
917
925
  * Indicates whether a `null`-element is allowed
918
- * @property {boolean}
926
+ * @type {boolean}
919
927
  * @readonly
920
928
  */
921
929
  get isNullItemsAllowed() {
@@ -1146,11 +1154,11 @@ class TXmlElementsListController {
1146
1154
  * that managea content of a document declaration
1147
1155
  */
1148
1156
  class TXmlContentDeclaration {
1149
- /** @property {?object} */
1157
+ /** @type {object} */
1150
1158
  #_host;
1151
- /** @property {OPT_xmlcontsett} */
1159
+ /** @type {OPT_xmlcontsett} */
1152
1160
  #_options;
1153
- /** @property {?TXmlElementController} */
1161
+ /** @type {TXmlElementController} */
1154
1162
  #_ctrls;
1155
1163
 
1156
1164
  /**
@@ -1188,7 +1196,7 @@ class TXmlContentDeclaration {
1188
1196
 
1189
1197
  /**
1190
1198
  * Contains a version of an XML
1191
- * @property {string}
1199
+ * @type {string}
1192
1200
  */
1193
1201
  get version() {
1194
1202
  return this.#_ctrls.getAttribute('version');
@@ -1200,7 +1208,7 @@ class TXmlContentDeclaration {
1200
1208
 
1201
1209
  /**
1202
1210
  * Contains a document character set
1203
- * @property {string}
1211
+ * @type {string}
1204
1212
  */
1205
1213
  get encoding() {
1206
1214
  return this.#_ctrls.getAttribute('encoding');
@@ -1217,7 +1225,7 @@ class TXmlContentDeclaration {
1217
1225
  init() {
1218
1226
  const _options = this.#_options;
1219
1227
  this.#_ctrls = new TXmlElementController(
1220
- xObj.insertXObjElement(
1228
+ insertXObjElement(
1221
1229
  this.#_host,
1222
1230
  _options.parseOptions.settings.declarationKey,
1223
1231
  ),
@@ -1235,9 +1243,9 @@ class TXmlContentDeclaration {
1235
1243
  * @description This class implements an instance of a root element
1236
1244
  */
1237
1245
  class TXmlContentRootElement extends TXmlElementController {
1238
- /** @property {?object} */
1246
+ /** @type {?object} */
1239
1247
  #_content;
1240
- /** @property {OPT_xmlcontsett} */
1248
+ /** @type {OPT_xmlcontsett} */
1241
1249
  #_options;
1242
1250
 
1243
1251
  /**
@@ -1259,7 +1267,7 @@ class TXmlContentRootElement extends TXmlElementController {
1259
1267
  const entries = Object.keys(obj);
1260
1268
  const len = entries.length;
1261
1269
  if (len === 0) {
1262
- rootItem = xObj.insertXObjElement(obj, rootETagName);
1270
+ rootItem = insertXObjElement(obj, rootETagName);
1263
1271
  } else {
1264
1272
  const {
1265
1273
  declarationKey,
@@ -1291,13 +1299,13 @@ class TXmlContentRootElement extends TXmlElementController {
1291
1299
  };
1292
1300
  };
1293
1301
  if (targetKey === '') {
1294
- rootItem = xObj.insertXObjElement(obj, rootETagName);
1302
+ rootItem = insertXObjElement(obj, rootETagName);
1295
1303
  } else {
1296
1304
  if (autoBindRoot) {
1297
1305
  rootItem = obj[targetKey];
1298
1306
  rootETagName = targetKey;
1299
1307
  if (!isPlainObject(rootItem)) {
1300
- rootItem = xObj.insertXObjElement(obj, rootETagName);
1308
+ rootItem = insertXObjElement(obj, rootETagName);
1301
1309
  };
1302
1310
  } else {
1303
1311
  throw new TypeError(`TXmlContentRootElement : ${XML_TE_NROOT_EMSG}`);
@@ -1305,7 +1313,7 @@ class TXmlContentRootElement extends TXmlElementController {
1305
1313
  };
1306
1314
  };
1307
1315
  } else if (!isPlainObject(rootItem)) {
1308
- rootItem = xObj.insertXObjElement(obj, rootETagName);
1316
+ rootItem = insertXObjElement(obj, rootETagName);
1309
1317
  };
1310
1318
  // init parent class instance
1311
1319
  super(rootItem, _options);
@@ -1317,7 +1325,7 @@ class TXmlContentRootElement extends TXmlElementController {
1317
1325
  }
1318
1326
 
1319
1327
  /**
1320
- * @property {string}
1328
+ * @type {string}
1321
1329
  */
1322
1330
  get tagName() {
1323
1331
  return this.#_options.rootETagName;
@@ -1354,9 +1362,9 @@ class TXmlContentRootElement extends TXmlElementController {
1354
1362
  * @property {boolean} isERR - flag
1355
1363
  * @property {number} [errCode] - error code
1356
1364
  * @property {string} errEvent - event ID
1357
- * @property {string} errMsg - event message
1358
- * @property {string} source - path to file
1359
- * @property {any} content - file content
1365
+ * @property {string} [errMsg] - event message
1366
+ * @property {string} [source] - path to file
1367
+ * @property {any} [content] - file content
1360
1368
  * @description A fs ops description.
1361
1369
  */
1362
1370
 
@@ -1365,15 +1373,15 @@ class TXmlContentRootElement extends TXmlElementController {
1365
1373
  * that managea content of a document
1366
1374
  */
1367
1375
  class TXmlContentContainer {
1368
- /** @property {?object} */
1376
+ /** @type {?object} */
1369
1377
  #_content;
1370
- /** @property {OPT_xmlcontsett} */
1378
+ /** @type {OPT_xmlcontsett} */
1371
1379
  #_options;
1372
- /** @property {?object} */
1380
+ /** @type {?object} */
1373
1381
  #_parseOptions;
1374
- /** @property {?TXmlContentDeclaration} */
1382
+ /** @type {TXmlContentDeclaration} */
1375
1383
  #_docDecl;
1376
- /** @property {?TXmlContentRootElement} */
1384
+ /** @type {TXmlContentRootElement} */
1377
1385
  #_docRoot;
1378
1386
 
1379
1387
  /**
@@ -1397,7 +1405,8 @@ class TXmlContentContainer {
1397
1405
  }
1398
1406
 
1399
1407
  /**
1400
- * @property {TXmlContentRootElement}
1408
+ * Returns a root element
1409
+ * @type {TXmlContentRootElement}
1401
1410
  * @readonly
1402
1411
  */
1403
1412
  get rootElement() {
@@ -1450,6 +1459,7 @@ class TXmlContentContainer {
1450
1459
 
1451
1460
  /**
1452
1461
  * @returns {string}
1462
+ * @throws {Error}
1453
1463
  * @description Returns a document content as a string in an XML-format.
1454
1464
  */
1455
1465
  saveToXMLString() {
@@ -1505,7 +1515,7 @@ class TXmlContentContainer {
1505
1515
  }
1506
1516
 
1507
1517
  /**
1508
- * @param {string} source source - path to a file
1518
+ * @param {string} source - path to a file
1509
1519
  * @returns {fsoDescr}
1510
1520
  * @description Saves a document content to a file.
1511
1521
  */
@@ -1534,7 +1544,7 @@ class TXmlContentContainer {
1534
1544
  }
1535
1545
 
1536
1546
  /**
1537
- * @param {string} loadFromXMLString
1547
+ * @param {string} xmlString - some content
1538
1548
  * @returns {boolean}
1539
1549
  * @throws {Error}
1540
1550
  * @description Loads a document content from a string.
@@ -1567,7 +1577,7 @@ class TXmlContentContainer {
1567
1577
  loadFromFile(source) {
1568
1578
  /**/// main part that return promise as a result
1569
1579
  return new Promise((resolve, reject) => {
1570
- loadFile(source).then(data => {
1580
+ loadFromFile(source).then(data => {
1571
1581
  if (!data.isERR) {
1572
1582
  if (data.content !== '') {
1573
1583
  if (this.loadFromXMLString(data.content)) {
@@ -1599,7 +1609,7 @@ class TXmlContentContainer {
1599
1609
  * @description Loads a document content from a file.
1600
1610
  */
1601
1611
  loadFromFileSync(source) {
1602
- const data = loadFileSync(source);
1612
+ const data = loadFromFileSync(source);
1603
1613
  if (!data.isERR) {
1604
1614
  if (data.content !== '') {
1605
1615
  if (this.loadFromXMLString(data.content)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cntwg/xml-lib-js",
3
- "version": "0.0.32",
3
+ "version": "0.0.33",
4
4
  "description": "A library for handling an XML-documents",
5
5
  "author": "ygracs <cs70th-om@rambler.ru>",
6
6
  "license": "MIT",
@@ -16,7 +16,6 @@
16
16
  "doc/xmldoc-lib.md",
17
17
  "lib/xmldoc-lib.js",
18
18
  "lib/xml-helper.js",
19
- "lib/file-helper.js",
20
19
  "index.js",
21
20
  "CHANGELOG.md"
22
21
  ],
@@ -37,9 +36,10 @@
37
36
  "#test-dir/*": "./__test__/*"
38
37
  },
39
38
  "dependencies": {
40
- "@ygracs/bsfoc-lib-js": "^0.2.2",
41
- "@ygracs/xml-js6": "^0.0.4-b",
42
- "@ygracs/xobj-lib-js": "^0.2.1"
39
+ "@cntwg/file-helper": "^0.0.1",
40
+ "@ygracs/bsfoc-lib-js": "^0.2.3",
41
+ "@ygracs/xml-js6": "^0.0.5-b",
42
+ "@ygracs/xobj-lib-js": "^0.2.2"
43
43
  },
44
44
  "devDependencies": {
45
45
  "jest": "^29.7.0",
@@ -1,278 +0,0 @@
1
- // [v0.1.030-20241029]
2
-
3
- // === module init block ===
4
-
5
- const fs = require('fs');
6
- const fse = require('fs').promises;
7
-
8
- const {
9
- isPlainObject,
10
- } = require('@ygracs/bsfoc-lib-js');
11
-
12
- // === module extra block (helper functions) ===
13
-
14
- /**
15
- * @typedef {Object} fsoDescr
16
- * @property {boolean} isERR - flag
17
- * @property {number|undefined} errCode - error code
18
- * @property {string} errEvent - event ID
19
- * @property {string} errMsg - event message
20
- * @property {string} source - path to file
21
- * @property {any} content - file content
22
- * @description A fs ops description.
23
- */
24
-
25
- /**
26
- * @typedef {Object} VCOR_evalerrfs
27
- * @property {boolean} isSucceed - ops flag
28
- * @property {fsoDescr} descr - description
29
- * @description A result of an error check ops.
30
- */
31
-
32
- /**
33
- * @function checkFsError
34
- * @param {fsoDescr} descr
35
- * @param {Error} err
36
- * @returns {VCOR_evalerrfs}
37
- * @inner
38
- * @description Checks an error code of a fs ops and sets descr info if succeed
39
- */
40
- function checkFsError(descr, err) {
41
- let isSucceed = false;
42
- if (isPlainObject(err) && isPlainObject(descr)) {
43
- switch (err.code) {
44
- case 'ENOENT':
45
- case 'EISDIR':
46
- case 'ENOTDIR':
47
- case 'EPERM': {
48
- descr.errCode = err.errno;
49
- descr.errEvent = err.code;
50
- descr.errMsg = `ERR_FILE_${err.code}`;
51
- isSucceed = true;
52
- break;
53
- }
54
- default: {}
55
- };
56
- if (isSucceed) descr.isERR = true;
57
- };
58
- return { isSucceed, descr };
59
- };
60
-
61
- // === module main block ===
62
-
63
- /***
64
- * (* constant definitions *)
65
- */
66
-
67
- /***
68
- * (* function definitions *)
69
- */
70
-
71
- /**
72
- * @function loadFileSync
73
- * @param {string} src - a path to some file
74
- * @returns {fsoDescr}
75
- * @inner
76
- * @description Loads file by a given path
77
- */
78
- function loadFileSync(src) {
79
- /** @type {fsoDescr} */
80
- const data = {
81
- isERR: false,
82
- errCode: 0,
83
- errEvent: '',
84
- errMsg: '',
85
- source: '',
86
- content: '',
87
- };
88
- if (typeof src !== 'string') {
89
- if (src !== undefined) {
90
- data.isERR = true;
91
- data.errEvent = 'ERR_INVARG';
92
- data.errMsg = 'The "source" argument must be a string';
93
- };
94
- } else {
95
- const srcPath = src.trim();
96
- if (srcPath !== '') {
97
- data.source = srcPath;
98
- try {
99
- data.content = fs.readFileSync(srcPath, 'utf8');
100
- } catch (err) {
101
- const { isSucceed } = checkFsError(data, err);
102
- if (!isSucceed) {
103
- // // TODO: [?] consider check others
104
- //console.log(`TEST_ERR-MSG:loadFileSync: ${err}`);
105
- //throw err;
106
- };
107
- };
108
- };
109
- };
110
- return data;
111
- };
112
-
113
- /**
114
- * @function loadFile
115
- * @param {string} src - a path to some file
116
- * @returns {Promise<fsoDescr, Error>}
117
- * @async
118
- * @inner
119
- * @description Loads file by a given path
120
- */
121
- function loadFile(src) {
122
- /**/// main part that return promise as a result
123
- return new Promise((resolve, reject) => {
124
- /** @type {fsoDescr} */
125
- const data = {
126
- isERR: false,
127
- errCode: 0,
128
- errEvent: '',
129
- errMsg: '',
130
- source: '',
131
- content: '',
132
- };
133
- if (typeof src !== 'string') {
134
- if (src !== undefined) {
135
- data.isERR = true;
136
- data.errEvent = 'ERR_INVARG';
137
- data.errMsg = 'The "source" argument must be a string';
138
- };
139
- resolve(data);
140
- } else {
141
- const srcPath = src.trim();
142
- if (srcPath === '') resolve(data);
143
- data.source = srcPath;
144
- fse.readFile(srcPath, 'utf8').then(result => {
145
- data.content = result;
146
- resolve(data);
147
- }).catch(err => {
148
- let { isSucceed } = checkFsError(data, err);
149
- if (isSucceed) {
150
- data.content = '';
151
- resolve(data);
152
- } else {
153
- //console.log('CHECK: loadFile() => Error => '+err);
154
- //console.log('CHECK: loadFile() => Error => '+err.code);
155
- reject(err);
156
- };
157
- });
158
- };
159
- });
160
- };
161
-
162
- /**
163
- * @function saveToFileSync
164
- * @param {string} src - a path to some file
165
- * @param {string} content - some file content
166
- * @param {any} [opt] - <reserved>
167
- * @returns {fsoDescr}
168
- * @inner
169
- * @description Saves a content to a file
170
- */
171
- function saveToFileSync(src, content = '', opt) {
172
- /** @type {fsoDescr} */
173
- const data = {
174
- isERR: false,
175
- errCode: 0,
176
- errEvent: '',
177
- errMsg: '',
178
- source: '',
179
- content: '',
180
- };
181
- if (typeof src !== 'string') {
182
- data.isERR = true;
183
- data.errEvent = src === undefined ? 'ERR_NOSRC' : 'ERR_INVARG';
184
- data.errMsg = 'The "source" argument must be a string';
185
- } else if (typeof content === 'string') {
186
- const srcPath = src.trim();
187
- if (srcPath === '') {
188
- data.isERR = true;
189
- data.errEvent = 'ERR_NOSRC';
190
- data.errMsg = 'No "source" path given';
191
- } else {
192
- data.source = srcPath;
193
- try {
194
- fs.writeFileSync(srcPath, content, 'utf8');
195
- } catch (err) {
196
- const { isSucceed } = checkFsError(data, err);
197
- if (!isSucceed) {
198
- // // TODO: [?] consider check others
199
- //console.log(`TEST_ERR-MSG:saveToFileSync: ${err}`);
200
- //throw err;
201
- };
202
- };
203
- };
204
- } else {
205
- data.isERR = true;
206
- data.errEvent = 'ERR_INVARG';
207
- data.errMsg = 'The "content" argument must be a string';
208
- };
209
- return data;
210
- };
211
-
212
- /**
213
- * @function saveToFile
214
- * @param {string} src - a path to some file
215
- * @param {string} content - some file content
216
- * @param {any} [opt] - <reserved>
217
- * @returns {Promise<fsoDescr, Error>}
218
- * @async
219
- * @inner
220
- * @description Saves a content to a file
221
- */
222
- function saveToFile(src, content = '', opt) {
223
- /**/// main part that return promise as a result
224
- return new Promise((resolve, reject) => {
225
- /** @type {fsoDescr} */
226
- const data = {
227
- isERR: false,
228
- errCode: 0,
229
- errEvent: '',
230
- errMsg: '',
231
- source: '',
232
- content: '',
233
- };
234
- if (typeof src !== 'string') {
235
- data.isERR = true;
236
- data.errEvent = src === undefined ? 'ERR_NOSRC' : 'ERR_INVARG';
237
- data.errMsg = 'The "source" argument must be a string';
238
- resolve(data);
239
- } else if (typeof content === 'string') {
240
- const srcPath = src.trim();
241
- if (srcPath === '') {
242
- data.isERR = true;
243
- data.errEvent = 'ERR_NOSRC';
244
- data.errMsg = 'No "source" path given';
245
- resolve(data);
246
- } else {
247
- data.source = srcPath;
248
- fse.writeFile(srcPath, content, 'utf8').then(result => {
249
- resolve(data);
250
- }).catch(err => {
251
- const { isSucceed } = checkFsError(data, err);
252
- if (isSucceed) resolve(data);
253
- // // TODO: [?] consider check others
254
- //console.log('CHECK: TXEpgContentProvider.saveToFile() => Error => '+err);
255
- //console.log('CHECK: TXEpgContentProvider.saveToFile() => Error => '+err.code);
256
- reject(err);
257
- });
258
- };
259
- } else {
260
- data.isERR = true;
261
- data.errEvent = 'ERR_INVARG';
262
- data.errMsg = 'The "content" argument must be a string';
263
- resolve(data);
264
- };
265
- });
266
- };
267
-
268
- /***
269
- * (* class definitions *)
270
- */
271
-
272
- // === module exports block ===
273
-
274
- module.exports.checkFsError = checkFsError;
275
- module.exports.loadFile = loadFile;
276
- module.exports.loadFileSync = loadFileSync;
277
- module.exports.saveToFile = saveToFile;
278
- module.exports.saveToFileSync = saveToFileSync;