@ygracs/xepg-lib-js 0.0.16 → 0.0.18

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,9 +1,30 @@
1
+ #### *v0.0.18*
2
+
3
+ Pre-release version.
4
+
5
+ > - `xepgdoc-lib.md` updated;
6
+ > - updated dependency on `@ygracs/bsfoc-lib-js` module to v0.2.1;
7
+ > - updated dependency on `@ygracs/dtf-lib-js` module to v0.0.25;
8
+ > - updated dependency on `@ygracs/xobj-lib-js` module to v0.1.2;
9
+ > - add method `loadFromXMLString` of `TXEpgContentProvider` class;
10
+ > - deprecate method `loadFromString` of `TXEpgContentProvider` class;
11
+ > - other fixes.
12
+
13
+ #### *v0.0.17*
14
+
15
+ Pre-release version.
16
+
17
+ > - updated dependency on `@ygracs/bsfoc-lib-js` module to v0.1.3;
18
+ > - updated dependency on `@ygracs/dtf-lib-js` module to v0.0.22;
19
+ > - updated dependency on `@ygracs/xobj-lib-js` module to v0.0.14-rc4;
20
+ > - some fixes in `dts-helper.js` lib-module.
21
+
1
22
  #### *v0.0.16*
2
23
 
3
24
  Pre-release version.
4
25
 
5
26
  > - updated dependency on `@ygracs/xobj-lib-js` module to v0.0.14-rc3;
6
- > - updated dependency on `@ygracs/dtf-lib-js` module to v0.0.21.
27
+ > - updated dependency on `@ygracs/dtf-lib-js` module to v0.0.21;
7
28
  > - added `getDTSysDescr` function into `dts-helper.js` lib-module;
8
29
  > - some fixes in `dts-helper.js` lib-module;
9
30
  > - some fixes in `xepgdoc-lib.js` module.
package/LICENSE CHANGED
@@ -1,9 +1,9 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2019-2023 Yuri Grachev
4
-
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
-
7
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
-
9
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019-2024 Yuri Grachev
4
+
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
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,6 +1,6 @@
1
- |***rev.*:**|0.0.13|
1
+ |***rev.*:**|0.0.14|
2
2
  |:---|---:|
3
- |***date***:|2022-12-06|
3
+ |***date***:|2024-07-17|
4
4
 
5
5
  ## Introduction
6
6
 
@@ -251,6 +251,12 @@ The table below describes a properties of a `TXEpgContentProvider` class:
251
251
 
252
252
  ##### **loadFromString(xmlString, options)**
253
253
 
254
+ > WARNING: This method deprecated (*since `v0.0.18`*). Use `loadFromXMLString` instead.
255
+
256
+ ##### **loadFromXMLString(xmlString, options)**
257
+
258
+ > since: `v0.0.18`
259
+
254
260
  This method loads a document content from string containing text in XML format.
255
261
 
256
262
  ##### **loadFromFile(src)**
@@ -277,7 +283,7 @@ This method saves a document content to a file. A `src` provided a string that i
277
283
 
278
284
  This method saves a document content to a string.
279
285
 
280
- > Note: will deprecate.
286
+ > Note: will deprecate. Use `saveToXMLString` instead.
281
287
 
282
288
  #### class methods (*special*)
283
289
 
package/lib/dts-helper.js CHANGED
@@ -1,4 +1,4 @@
1
- // [v0.1.042-20230326]
1
+ // [v0.1.043-20230711]
2
2
 
3
3
  // === module init block ===
4
4
 
@@ -56,43 +56,49 @@ function convStringToDTValue(value, dts_id, tz){
56
56
  let dt_str = typeof value === 'string' ? value : '';
57
57
  if (chkDTSysID(dtstype)) {
58
58
  switch (getDTSysName(dtstype)) {
59
- case 'default':
59
+ case 'default': {
60
60
  // TODO: check TZ
61
61
  //console.log('convStringToDTValue => dtstype:[default]');
62
62
  result = (new Date(dt_str)).getTime();
63
63
  //console.log('convStringToDTValue => value:['+value+']');
64
64
  //console.log('convStringToDTValue => result:['+result+']');
65
65
  break;
66
- case 'iso8601':
66
+ }
67
+ case 'iso8601': {
67
68
  // TODO: check TZ
68
69
  //console.log('convStringToDTValue => dtstype:[iso8601]');
69
70
  result = (new Date(dt_str)).getTime();
70
71
  //console.log('convStringToDTValue => value:['+value+']');
71
72
  //console.log('convStringToDTValue => result:['+result+']');
72
73
  break;
73
- case 'xmltv':
74
+ }
75
+ case 'xmltv': {
74
76
  // TODO: check TZ
75
77
  //console.log('convStringToDTValue => dtstype:[xmltv]');
76
78
  result = (new Date(convXMLTVToDTValue(dt_str))).getTime();
77
79
  //console.log('convStringToDTValue => value:['+value+']');
78
80
  //console.log('convStringToDTValue => result:['+result+']');
79
81
  break;
80
- case 'timestamp':
82
+ }
83
+ case 'timestamp': {
81
84
  // TODO: check TZ
82
85
  //console.log('convStringToDTValue => dtstype:[timestamp]');
83
86
  result = Number(dt_str);
84
87
  //console.log('convStringToDTValue => value:['+value+']');
85
88
  //console.log('convStringToDTValue => result:['+result+']');
86
89
  break;
87
- case 'utc':
90
+ }
91
+ case 'utc': {
88
92
  // TODO: check TZ
89
93
  //console.log('convStringToDTValue => dtstype:[utc]');
90
94
  result = (new Date(Date.UTC(dt_str))).getTime();
91
95
  //console.log('convStringToDTValue => value:['+value+']');
92
96
  //console.log('convStringToDTValue => result:['+result+']');
93
97
  break;
94
- default:
98
+ }
99
+ default: {
95
100
  break;
101
+ }
96
102
  };
97
103
  if (Number.isNaN(result)) result = 0;
98
104
  };
@@ -110,7 +116,7 @@ function convDTValueToString(value, dts_id, tz){
110
116
  let dtstype = valueToIndex(dts_id);
111
117
  if (!chkDTSysID(dtstype)) dtstype = 0;
112
118
  switch (getDTSysName(dtstype)) {
113
- case 'default':
119
+ case 'default': {
114
120
  // TODO: check TZ
115
121
  //console.log('convDTValueToString => dtstype:[default]');
116
122
  result = convDTValueToDateString(dt_val, {
@@ -119,17 +125,20 @@ function convDTValueToString(value, dts_id, tz){
119
125
  //console.log('convDTValueToString => value:['+value+']');
120
126
  //console.log('convDTValueToString => result:['+result+']');
121
127
  break;
122
- case 'iso8601':
128
+ }
129
+ case 'iso8601': {
123
130
  // TODO: check TZ
124
131
  //console.log('convDTValueToString => dtstype:[iso8601]');
125
132
  result = convDTValueToISO8601(dt_val, {
126
- use_bs_format: false,
133
+ //use_bs_format: false,
134
+ useBaseFormat: false,
127
135
  src_in_utc: false,
128
136
  });
129
137
  //console.log('convDTValueToString => value:['+value+']');
130
138
  //console.log('convDTValueToString => result:['+result+']');
131
139
  break;
132
- case 'xmltv':
140
+ }
141
+ case 'xmltv': {
133
142
  // TODO: check TZ
134
143
  //console.log('convDTValueToString => dtstype:[xmltv]');
135
144
  result = convDTValueToXMLTV(dt_val, {
@@ -138,15 +147,18 @@ function convDTValueToString(value, dts_id, tz){
138
147
  //console.log('convDTValueToString => value:['+value+']');
139
148
  //console.log('convDTValueToString => result:['+result+']');
140
149
  break;
141
- case 'timestamp':
150
+ }
151
+ case 'timestamp': {
142
152
  // TODO: check TZ
143
153
  //console.log('convDTValueToString => dtstype:[timestamp]');
144
154
  result = dt_val.toString();
145
155
  //console.log('convDTValueToString => value:['+value+']');
146
156
  //console.log('convDTValueToString => result:['+result+']');
147
157
  break;
148
- default:
158
+ }
159
+ default: {
149
160
  break;
161
+ }
150
162
  };
151
163
  return result;
152
164
  };
@@ -1,4 +1,4 @@
1
- // [v0.1.054-20230326]
1
+ // [v0.1.056-20240717]
2
2
 
3
3
  // === module init block ===
4
4
 
@@ -28,6 +28,84 @@ const TXmlContentParseOptions = xObj.TXmlContentParseOptions;
28
28
  const insertXObjElement = xObj.insertXObjElement;
29
29
  const getXObjElement = xObj.getXObjElement;
30
30
 
31
+ // === module extra block (helper functions) ===
32
+
33
+ /*
34
+ * === xObj-function wrappers (start) ===
35
+ */
36
+
37
+ function readXObjParam(obj){
38
+ return xObj.readXObjParam(obj, defParseOptions.settings.textKey);
39
+ };
40
+
41
+ function writeXObjParam(obj, value){
42
+ return xObj.writeXObjParam(obj, value, defParseOptions.settings.textKey);
43
+ };
44
+
45
+ function writeXObjAttr(obj, name, value) {
46
+ return xObj.writeXObjAttr(
47
+ obj, name, value, defParseOptions.settings.attributesKey
48
+ );
49
+ };
50
+
51
+ /*
52
+ * === xObj-function wrappers (end) ===
53
+ */
54
+
55
+ function _getXmlChildElementParam(obj, elem){
56
+ const child = obj ? getXObjElement(obj, elem) : null;
57
+ return child ? readXObjParam(child) : '';
58
+ };
59
+
60
+ function writeXEpgDocDeclSection(obj, opt){
61
+ let xml_ver = XEPG_XML_DOC_VERSION;
62
+ let doc_def_enc = XEPG_DEF_ENCODING_VALUE;
63
+ let key_name = defParseOptions.settings.declarationKey;
64
+ if (isPlainObject(opt)) {
65
+ xml_ver = readAsString(opt.version, xml_ver);
66
+ doc_def_enc = readAsString(opt.encoding, doc_def_enc);
67
+ };
68
+ if (isPlainObject(obj) && isNotEmptyString(key_name)) {
69
+ obj[key_name] = {};
70
+ writeXObjAttr(obj[key_name], 'version', xml_ver);
71
+ writeXObjAttr(obj[key_name], 'encoding', doc_def_enc);
72
+ };
73
+ };
74
+
75
+ /**
76
+ * @function checkFsError
77
+ * @param {object}
78
+ * @param {Error}
79
+ * @returns {object}
80
+ * @inner
81
+ * @description Checs an error code of a fs ops and sets descr info if succeed
82
+ */
83
+ function checkFsError(descr, err) {
84
+ let isSucceed = false;
85
+ if (isPlainObject(err) && isPlainObject(descr)) {
86
+ switch (err.code) {
87
+ case 'ENOENT':
88
+ case 'EISDIR':
89
+ case 'EPERM': {
90
+ descr.errCode = err.errno;
91
+ descr.errEvent = err.code;
92
+ descr.errMsg = `ERR_FILE_${err.code}`;
93
+ isSucceed = true;
94
+ break;
95
+ }
96
+ default: {}
97
+ };
98
+ if (isSucceed) descr.isERR = true;
99
+ };
100
+ return { isSucceed, descr };
101
+ };
102
+
103
+ // === module main block ===
104
+
105
+ /***
106
+ * (* constant definitions *)
107
+ */
108
+
31
109
  const XEPG_ROOT_ETAG_NAME = 'epgdoc';
32
110
  const XEPG_DEF_NS_NAME = 'tvp';
33
111
  const XEPG_DEF_NS_PATH = 'urn:e-doc:epg-doc:xepg';
@@ -56,6 +134,7 @@ const XEPG_DEF_DOCUMENT_TYPE = 'xepg';
56
134
  const XEPG_DEF_DOCUMENT_SCHEMA = '0.2';
57
135
  const XEPG_DEF_PROVIDER_NAME = 'xepg-lib-js';
58
136
  const XEPG_DEF_PROVIDER_VER = '0.0.15';
137
+
59
138
  const defXEpgDocOptions = {
60
139
  xmlns_xepg_pref: XEPG_DEF_NS_NAME,
61
140
  xmlns_xepg_path: XEPG_DEF_NS_PATH,
@@ -77,50 +156,17 @@ const defXEpgDocOptions = {
77
156
  const defParseOptions = new TXmlContentParseOptions();
78
157
  //console.log('CHECK: defParseOptions => '+JSON.stringify(defParseOptions, null, 2));
79
158
 
80
- // === module extra block (helper functions) ===
159
+ /***
160
+ * (* function definitions *)
161
+ */
81
162
 
82
- // === xObj-function wrappers (start) ===
83
-
84
- function readXObjParam(obj){
85
- return xObj.readXObjParam(obj, defParseOptions.settings.textKey);
86
- };
87
-
88
- function writeXObjParam(obj, value){
89
- return xObj.writeXObjParam(obj, value, defParseOptions.settings.textKey);
90
- };
91
-
92
- function writeXObjAttr(obj, name, value) {
93
- return xObj.writeXObjAttr(
94
- obj, name, value, defParseOptions.settings.attributesKey
95
- );
96
- };
97
-
98
- // === xObj-function wrappers (end) ===
99
-
100
- function _getXmlChildElementParam(obj, elem){
101
- const child = obj ? getXObjElement(obj, elem) : null;
102
- return child ? readXObjParam(child) : '';
103
- };
104
-
105
- function writeXEpgDocDeclSection(obj, opt){
106
- let xml_ver = XEPG_XML_DOC_VERSION;
107
- let doc_def_enc = XEPG_DEF_ENCODING_VALUE;
108
- let key_name = defParseOptions.settings.declarationKey;
109
- if (isPlainObject(opt)) {
110
- xml_ver = readAsString(opt.version, xml_ver);
111
- doc_def_enc = readAsString(opt.encoding, doc_def_enc);
112
- };
113
- if (isPlainObject(obj) && isNotEmptyString(key_name)) {
114
- obj[key_name] = {};
115
- writeXObjAttr(obj[key_name], 'version', xml_ver);
116
- writeXObjAttr(obj[key_name], 'encoding', doc_def_enc);
117
- };
118
- };
119
-
120
- // === module main block (function definitions) ===
121
-
122
- // === module main block (class definitions) ===
163
+ /***
164
+ * (* class definitions *)
165
+ */
123
166
 
167
+ /**
168
+ * @description This class implements an instance of a EPG-schedules list
169
+ */
124
170
  class TXEpgSchedulesList {
125
171
  #_content = null;
126
172
  #_options = null;
@@ -159,7 +205,7 @@ class TXEpgSchedulesList {
159
205
  let result = valueToIndex(
160
206
  _getXmlChildElementParam(this.#_dts_src, 'value')
161
207
  );
162
- return chkDTSysID(result) ? result : -1;
208
+ return chkDTSysID(result) ? result : 0; //-1; // // TODO: [?]
163
209
  }
164
210
 
165
211
  get curDocTZ(){
@@ -226,6 +272,9 @@ class TXEpgSchedulesList {
226
272
 
227
273
  };
228
274
 
275
+ /**
276
+ * @description This class implements an instance of a EPG-provider element
277
+ */
229
278
  class TXEpgProviderContainer {
230
279
  #_content = null;
231
280
  #_options = null;
@@ -374,10 +423,10 @@ class TXEpgProviderContainer {
374
423
  } else {
375
424
  ({ name, version, schema, doctype } = _options);
376
425
  };
377
- name = readAsString(name, prov_opt.name, true);
378
- version = readAsString(version, prov_opt.version, true);
379
- doctype = readAsString(doctype, prov_opt.doctype, true);
380
- schema = readAsString(schema, prov_opt.schema, true);
426
+ name = readAsString(name, { defValue: prov_opt.name, useTrim: true });
427
+ version = readAsString(version, { defValue: prov_opt.version, useTrim: true });
428
+ doctype = readAsString(doctype, { defValue: prov_opt.doctype, useTrim: true });
429
+ schema = readAsString(schema, { defValue: prov_opt.schema, useTrim: true });
381
430
  this.name = name;
382
431
  this.version = version;
383
432
  this.schema = schema;
@@ -386,6 +435,9 @@ class TXEpgProviderContainer {
386
435
 
387
436
  };
388
437
 
438
+ /**
439
+ * @description This class implements an instance of a DTS-provider element
440
+ */
389
441
  class TXepgDTSProvider {
390
442
  #_content = null;
391
443
  #_options = null;
@@ -416,10 +468,10 @@ class TXepgDTSProvider {
416
468
 
417
469
  get dtstype(){
418
470
  const item = getXObjElement(this.#_content, XEPG_DTSINFO_ETAG_NAME);
419
- let result = -1;
471
+ let result = 0; //-1; // // TODO: [?]
420
472
  if (isPlainObject(item)) {
421
473
  result = valueToIndex(_getXmlChildElementParam(item, 'value'));
422
- if (!chkDTSysID(result)) result = -1;
474
+ if (!chkDTSysID(result)) result = 0; //-1; // // TODO: [?]
423
475
  };
424
476
  return result;
425
477
  }
@@ -465,6 +517,9 @@ class TXepgDTSProvider {
465
517
 
466
518
  };
467
519
 
520
+ /**
521
+ * @description This class implements an instance of a EPG-header element
522
+ */
468
523
  class TXEpgHeaderContainer {
469
524
  #_content = null;
470
525
  #_options = null;
@@ -645,16 +700,21 @@ class TXEpgHeaderContainer {
645
700
 
646
701
  };
647
702
 
703
+ /**
704
+ * @description This class implements an instance of a container
705
+ * that managea content of a document
706
+ */
648
707
  class TXEpgContentProvider {
649
708
  #_content = null;
650
709
  #_options = null;
710
+ #_parseOptions = null;
651
711
 
652
- constructor(opt){
712
+ constructor(opt) {
653
713
  // load options
654
714
  const _options = isPlainObject(opt) ? opt : {};
655
715
  let {
656
716
  parseOptions,
657
- autoBindRoot,
717
+ autoBindRoot, // <*reserved*> ([?] - deprecate)
658
718
  } = _options;
659
719
  if (!(parseOptions instanceof TXmlContentParseOptions)) {
660
720
  parseOptions = new TXmlContentParseOptions(parseOptions);
@@ -664,6 +724,8 @@ class TXEpgContentProvider {
664
724
  // save options
665
725
  //_options.autoBindRoot = autoBindRoot; //* // TODO:
666
726
  this.#_options = _options;
727
+ // save parser options
728
+ this.#_parseOptions = parseOptions;
667
729
  // init document content
668
730
  this.#_content = {};
669
731
  this.init();
@@ -802,41 +864,72 @@ class TXEpgContentProvider {
802
864
  };
803
865
  }
804
866
 
805
- saveToXMLString(){
806
- return xmlParser.js2xml(
807
- this.#_content,
808
- this.#_options.parseOptions.js2xml,
809
- );
867
+ /**
868
+ * @returns {string}
869
+ * @description Returns a document content as a string in an XML-format.
870
+ */
871
+ saveToXMLString() {
872
+ let result = '';
873
+ try {
874
+ result = xmlParser.js2xml(this.#_content, this.#_parseOptions.js2xml);
875
+ } catch (err) {
876
+ //console.log('CHECK: TXEpgContentProvider.saveToXMLString() => Error => '+err);
877
+ throw err;
878
+ };
879
+ return result;
810
880
  }
811
881
 
812
- saveToFile(src_link){
813
- let saveFile = function(obj){
814
- return new Promise(function(resolve, reject) {
815
- let { src_link } = obj;
816
- if (
817
- typeof src_link !== 'string'
818
- || ((src_link = src_link.trim()) === '')
819
- ) {
820
- resolve(false);
821
- };
822
- fs.writeFile(src_link, obj.content, (err) => {
823
- if (err) reject(err);
824
- resolve(true);
882
+ /**
883
+ * @param {string}
884
+ * @returns {object}
885
+ * @throws {Error}
886
+ * @async
887
+ * @description Saves a document content to a file.
888
+ */
889
+ saveToFile(source) {
890
+ /**/// main part that return promise as a result
891
+ return new Promise((resolve, reject) => {
892
+ let data = {
893
+ isSucceed: false,
894
+ source: typeof source === 'string' ? source.trim() : '',
895
+ content: '',
896
+ isERR: false,
897
+ errEvent: '',
898
+ };
899
+ if (data.source === '') {
900
+ data.isERR = true;
901
+ data.errEvent = (
902
+ typeof source === 'string' ? 'EMPTY_SRCLINK' : 'NO_SRCLINK'
903
+ );
904
+ resolve(data);
905
+ } else {
906
+ data.content = this.saveToXMLString();
907
+ fse.writeFile(data.source, data.content, 'utf8').then(result => {
908
+ data.isSucceed = true;
909
+ data.errEvent = 'OPS_IS_SUCCEED';
910
+ resolve(data);
911
+ }).catch(err => {
912
+ let { isSucceed } = checkFsError(data, err);
913
+ if (isSucceed) {
914
+ data.content = '';
915
+ resolve(data);
916
+ } else {
917
+ //console.log('CHECK: TXEpgContentProvider.saveToFile() => Error => '+err);
918
+ //console.log('CHECK: TXEpgContentProvider.saveToFile() => Error => '+err.code);
919
+ reject(err);
920
+ };
825
921
  });
826
- });
827
- };
828
- let loadContent = (opt) => { return this.asXML(opt); };
829
- return new Promise(function(resolve, reject){
830
- let tmpObj = { src_link: src_link };
831
- tmpObj.content = loadContent();
832
- saveFile(tmpObj).then(result => {
833
- resolve(result);
834
- }).catch(err => { reject(err); });
922
+ };
835
923
  });
836
924
  }
837
925
 
838
- saveToFileSync(source){
839
- //console.log('CHECK: TXEpgContentProvider.saveToFileSync() => was called...');
926
+ /**
927
+ * @param {string}
928
+ * @returns {object}
929
+ * @throws {Error}
930
+ * @description Saves a document content to a file.
931
+ */
932
+ saveToFileSync(source) {
840
933
  let data = {
841
934
  isSucceed: false,
842
935
  source: typeof source === 'string' ? source.trim() : '',
@@ -844,24 +937,30 @@ class TXEpgContentProvider {
844
937
  isERR: false,
845
938
  errEvent: '',
846
939
  };
847
- if (data.source !== '') {
940
+ if (data.source === '') {
941
+ data.isERR = true;
942
+ data.errEvent = (
943
+ typeof source === 'string' ? 'EMPTY_SRCLINK' : 'NO_SRCLINK'
944
+ );
945
+ } else {
848
946
  try {
849
- data.content = this.asXML();
947
+ data.content = this.saveToXMLString();
850
948
  fs.writeFileSync(data.source, data.content, 'utf8');
851
949
  } catch (err) {
852
- console.log('CHECK: TXEpgContentProvider.saveToFileSync() => Error => '+err);
853
- console.log('CHECK: TXEpgContentProvider.saveToFileSync() => Error => '+err.code);
854
- throw err;
950
+ let { isSucceed } = checkFsError(data, err);
951
+ if (isSucceed) {
952
+ data.content = '';
953
+ } else {
954
+ //console.log('CHECK: TXEpgContentProvider.saveToFileSync() => Error => '+err);
955
+ //console.log('CHECK: TXEpgContentProvider.saveToFileSync() => Error => '+err.code);
956
+ throw err;
957
+ };
855
958
  };
856
- } else {
857
- data.isERR = true;
858
- data.errEvent = 'EMPTY_SRCLINK';
859
959
  };
860
- //console.log('CHECK: TXEpgContentProvider.saveToFileSync() => was left...');
861
960
  return data;
862
961
  }
863
962
 
864
- loadFromString(xmlString, opt){
963
+ loadFromXMLString(xmlString, opt) {
865
964
  let isSUCCEED = false;
866
965
  if (typeof xmlString === 'string') {
867
966
  let _options = this.#_options;
@@ -891,83 +990,27 @@ class TXEpgContentProvider {
891
990
  isSUCCEED = true;
892
991
  };
893
992
  } catch (err) {
894
- console.log('CHECK: TXEpgContentProvider.loadFromString() => err => '+err);
993
+ //console.log('CHECK: TXEpgContentProvider.loadFromString() => err => '+err);
895
994
  throw err;
896
995
  };
897
996
  };
898
997
  return isSUCCEED;
899
998
  }
900
999
 
901
- loadFromFile(source){
902
- /**/// extend part that return promise as a result
903
- const loadFile = function(source){
904
- return new Promise((resolve, reject) => {
905
- let data = {
906
- isLoaded: false,
907
- source: typeof source === 'string' ? source.trim() : '',
908
- content: '',
909
- isERR: false,
910
- errEvent: '',
911
- };
912
- if (data.source === '') {
913
- data.isERR = true;
914
- data.errEvent = (
915
- typeof source === 'string' ? 'EMPTY_SRCLINK' : 'NO_SRCLINK'
916
- );
917
- resolve(data);
918
- } else {
919
- fse.readFile(data.source, 'utf8').then(result => {
920
- data.content = result;
921
- if (result != '') {
922
- data.isLoaded = true;
923
- data.errEvent = 'OPS_IS_SUCCEED';
924
- } else {
925
- data.isERR = true;
926
- data.errEvent = 'NO_CONTENT';
927
- };
928
- resolve(data);
929
- }).catch(err => {
930
- switch (err.code) {
931
- case 'ENOENT':
932
- case 'EISDIR':
933
- data.isERR = true;
934
- data.errEvent = err.code;
935
- resolve(data);
936
- break;
937
- default:
938
- console.log('CHECK: TXEpgContentProvider.loadFromFile() => [1] loadFile() => Error => '+err.code);
939
- reject(err);
940
- break;
941
- };
942
- });
943
- };
944
- });
945
- };
946
- /**/// main part that return promise as a result
947
- return new Promise((resolve, reject) => {
948
- loadFile(source).then(data => {
949
- if (data.isLoaded) {
950
- if (this.loadFromString(data.content)) {
951
- data.isLoaded = true;
952
- data.errEvent = 'OPS_IS_SUCCEED';
953
- } else {
954
- data.isERR = true;
955
- data.errEvent = 'OPS_IS_FAILED';
956
- };
957
- } else {
958
- //console.log('CHECK: TXEpgContentProvider.loadFromFile() => result.isERR:['+data.isERR+']');
959
- //console.log('CHECK: TXEpgContentProvider.loadFromFile() => result.errEvent:['+data.errEvent+']');
960
- };
961
- resolve(data);
962
- }).catch(err => {
963
- console.log('CHECK: TXEpgContentProvider.loadFromFile() => loadFile().err() => '+err);
964
- reject(err);
965
- });
966
- });
1000
+ loadFromString(...args){
1001
+ return this.loadFromXMLString(...args);
967
1002
  }
968
1003
 
969
- loadFromFileSync(source){
970
- const loadFile = function(source){
1004
+ /**
1005
+ * @param {string}
1006
+ * @returns {object}
1007
+ * @throws {Error}
1008
+ * @async
1009
+ * @description Loads a document content from a file.
1010
+ */
1011
+ loadFromFile(source) {
1012
+ /**/// main part that return promise as a result
1013
+ return new Promise((resolve, reject) => {
971
1014
  let data = {
972
1015
  isLoaded: false,
973
1016
  source: typeof source === 'string' ? source.trim() : '',
@@ -980,56 +1023,84 @@ class TXEpgContentProvider {
980
1023
  data.errEvent = (
981
1024
  typeof source === 'string' ? 'EMPTY_SRCLINK' : 'NO_SRCLINK'
982
1025
  );
1026
+ resolve(data);
983
1027
  } else {
984
- try {
985
- data.content = fs.readFileSync(data.source, 'utf8');
986
- if (data.content !== '') {
987
- data.isLoaded = true;
988
- data.errEvent = 'OPS_IS_SUCCEED';
1028
+ fse.readFile(data.source, 'utf8').then(result => {
1029
+ data.content = result;
1030
+ if (result !== '') {
1031
+ if (this.loadFromXMLString(result)) {
1032
+ data.isLoaded = true;
1033
+ data.errEvent = 'OPS_IS_SUCCEED';
1034
+ } else {
1035
+ data.isERR = true;
1036
+ data.errEvent = 'OPS_IS_FAILED';
1037
+ };
989
1038
  } else {
990
1039
  data.isERR = true;
991
1040
  data.errEvent = 'NO_CONTENT';
992
1041
  };
993
- } catch (err) {
994
- switch (err.code) {
995
- case 'ENOENT':
996
- case 'EISDIR':
997
- data.isERR = true;
998
- data.errEvent = err.code;
999
- break;
1000
- default:
1001
- console.log('CHECK: TXEpgContentProvider.loadFromFileSync() => [1] loadFile() => Error => '+err.code);
1002
- throw err;
1003
- break;
1042
+ resolve(data);
1043
+ }).catch(err => {
1044
+ let { isSucceed } = checkFsError(data, err);
1045
+ if (isSucceed) {
1046
+ data.content = '';
1047
+ resolve(data);
1048
+ } else {
1049
+ //console.log('CHECK: TXEpgContentProvider.loadFromFile() => Error => '+err);
1050
+ //console.log('CHECK: TXEpgContentProvider.loadFromFile() => Error => '+err.code);
1051
+ reject(err);
1004
1052
  };
1005
- };
1053
+ });
1006
1054
  };
1007
- return data;
1008
- };
1009
- let result = {
1055
+ });
1056
+ }
1057
+
1058
+ /**
1059
+ * @param {string}
1060
+ * @returns {object}
1061
+ * @throws {Error}
1062
+ * @description Loads a document content from a file.
1063
+ */
1064
+ loadFromFileSync(source) {
1065
+ let data = {
1010
1066
  isLoaded: false,
1067
+ source: typeof source === 'string' ? source.trim() : '',
1068
+ content: '',
1011
1069
  isERR: false,
1012
1070
  errEvent: '',
1013
1071
  };
1014
- try {
1015
- result = loadFile(source);
1016
- if (result.isLoaded) {
1017
- if (this.loadFromString(result.content)) {
1018
- result.isLoaded = true;
1019
- result.errEvent = 'OPS_IS_SUCCEED';
1072
+ if (data.source === '') {
1073
+ data.isERR = true;
1074
+ data.errEvent = (
1075
+ typeof source === 'string' ? 'EMPTY_SRCLINK' : 'NO_SRCLINK'
1076
+ );
1077
+ } else {
1078
+ try {
1079
+ data.content = fs.readFileSync(data.source, 'utf8');
1080
+ if (data.content !== '') {
1081
+ if (this.loadFromXMLString(data.content)) {
1082
+ data.isLoaded = true;
1083
+ data.errEvent = 'OPS_IS_SUCCEED';
1084
+ } else {
1085
+ data.isERR = true;
1086
+ data.errEvent = 'OPS_IS_FAILED';
1087
+ };
1020
1088
  } else {
1021
- result.isERR = true;
1022
- result.errEvent = 'OPS_IS_FAILED';
1089
+ data.isERR = true;
1090
+ data.errEvent = 'NO_CONTENT';
1091
+ };
1092
+ } catch (err) {
1093
+ let { isSucceed } = checkFsError(data, err);
1094
+ if (isSucceed) {
1095
+ data.content = '';
1096
+ } else {
1097
+ //console.log('CHECK: TXEpgContentProvider.loadFromFileSync() => Error => '+err);
1098
+ //console.log('CHECK: TXEpgContentProvider.loadFromFileSync() => Error => '+err.code);
1099
+ throw err;
1023
1100
  };
1024
- } else {
1025
- //console.log('CHECK: TXEpgContentProvider.loadFromFileSync() => result.isERR:['+result.isERR+']');
1026
- //console.log('CHECK: TXEpgContentProvider.loadFromFileSync() => result.errEvent:['+result.errEvent+']');
1027
1101
  };
1028
- } catch (err) {
1029
- console.log('CHECK: TXEpgContentProvider.loadFromFileSync() => loadFile().err() => '+err);
1030
- throw err;
1031
1102
  };
1032
- return result;
1103
+ return data;
1033
1104
  }
1034
1105
 
1035
1106
  // will deprecate
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ygracs/xepg-lib-js",
3
- "version": "0.0.16",
3
+ "version": "0.0.18",
4
4
  "description": "A library for handling an EPG-sources based on an XEPG-format",
5
5
  "author": "ygracs <cs70th-om@rambler.ru>",
6
6
  "license": "MIT",
@@ -19,7 +19,7 @@
19
19
  "CHANGELOG.md"
20
20
  ],
21
21
  "scripts": {
22
- "test": "TZ='Etc/UTC' jest",
22
+ "test": "cross-env TZ='Etc/UTC' jest",
23
23
  "test-xepg": "TZ='Etc/UTC' jest ./test",
24
24
  "test-xepg:sl": "jest TXEpgSchedulesList",
25
25
  "test-xepg:hc": "TZ='Etc/UTC' jest TXEpgHeader",
@@ -28,15 +28,24 @@
28
28
  "test-xepg:cp": "jest TXEpgContentProvider",
29
29
  "win:test": "set TZ='Etc/UTC' && jest",
30
30
  "win:test-xepg": "set TZ='Etc/UTC' && jest ./test",
31
- "win:test-xepg:hc": "set TZ='Etc/UTC' && jest TXEpgHeader"
32
- },
31
+ "win:test-xepg:hc": "set TZ='Etc/UTC' && jest TXEpgHeader",
32
+ "build-doc-md": "jsdoc2md",
33
+ "build-doc-html": "jsdoc"
34
+ },
35
+ "imports": {
36
+ "#lib/*": "./lib/*",
37
+ "#test-dir/*": "./__test__/*"
38
+ },
33
39
  "dependencies": {
34
- "@ygracs/bsfoc-lib-js": "^0.1.2",
35
- "@ygracs/dtf-lib-js": "^0.0.21",
40
+ "@ygracs/bsfoc-lib-js": "^0.2.1",
41
+ "@ygracs/dtf-lib-js": "^0.0.25",
36
42
  "@ygracs/xml-js6": "^0.0.3-b",
37
- "@ygracs/xobj-lib-js": "^0.0.14-rc3"
43
+ "@ygracs/xobj-lib-js": "^0.1.2"
38
44
  },
39
45
  "devDependencies": {
40
- "jest": "^29.3.1"
46
+ "cross-env": "^7.0.3",
47
+ "jest": "^29.6.1",
48
+ "jsdoc-to-markdown": "^8.0.1",
49
+ "minimist": "^1.2.8"
41
50
  }
42
51
  }