@ygracs/chn-alias-list 0.0.4 → 0.0.5

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,18 @@
1
+ #### *v0.0.5*
2
+
3
+ Pre-release version.
4
+
5
+ > - update `chn-alias-list.md`;
6
+ > - update dependency on `@ygracs/bsfoc-lib-js` module to v0.2.3;
7
+ > - add dependency on `@cntwg/file-helper` module;
8
+ > - add 'filter'-method to a `TChnNamesList`-class;
9
+ > - change name of a method `getName` into `getItem` for a `TChnNamesList`-class (*old name deprecated*);
10
+ > - change name of a method `addName` into `addItem` for a `TChnNamesList`-class (*old name deprecated*);
11
+ > - change name of a method `delName` into `delItem` for a `TChnNamesList`-class (*old name deprecated*);
12
+ > - change name of a method `loadNames` into `loadItems` for a `TChnNamesList`-class (*old name deprecated*);
13
+ > - add 'filter'-method to a `TChnAliasList`-class;
14
+ > - other fixes.
15
+
1
16
  #### *v0.0.4*
2
17
 
3
18
  Pre-release version.
@@ -1,7 +1,7 @@
1
1
 
2
- >|***rev.*:**|0.0.3|
2
+ >|***rev.*:**|0.0.5|
3
3
  >|:---|---:|
4
- >|date:|2024-10-21|
4
+ >|date:|2025-04-14|
5
5
 
6
6
  ## Introduction
7
7
 
@@ -138,6 +138,13 @@ Searches an index of a given name in the list.
138
138
  <a name="TChnNamesList+getName"></a>
139
139
  ##### **getName(value)** => `?TChnNameRecord`
140
140
 
141
+ > \[!] NOTE: `[since v0.0.5]` deprecated. Use `getItem` instead.
142
+
143
+ <a name="TChnNamesList+getItem"></a>
144
+ ##### **getItem(value)** => `?TChnNameRecord`
145
+
146
+ > since: \[v0.0.5]
147
+
141
148
  Returns a name record addressed by a given index.
142
149
 
143
150
  | parameter name | value type | default value | description |
@@ -147,6 +154,13 @@ Returns a name record addressed by a given index.
147
154
  <a name="TChnNamesList+addName"></a>
148
155
  #### **addName(data)** => `boolean`
149
156
 
157
+ > \[!] NOTE: `[since v0.0.5]` deprecated. Use `addItem` instead.
158
+
159
+ <a name="TChnNamesList+addItem"></a>
160
+ #### **addItem(data)** => `boolean`
161
+
162
+ > since: \[v0.0.5]
163
+
150
164
  Adds a new name record to a list members.
151
165
 
152
166
  | parameter name | value type | default value | description |
@@ -156,6 +170,13 @@ Adds a new name record to a list members.
156
170
  <a name="TChnNamesList+delName"></a>
157
171
  ##### **delName(value)** => `boolean`
158
172
 
173
+ > \[!] NOTE: `[since v0.0.5]` deprecated. Use `delItem` instead.
174
+
175
+ <a name="TChnNamesList+delItem"></a>
176
+ ##### **delItem(value)** => `boolean`
177
+
178
+ > since: \[v0.0.5]
179
+
159
180
  Tries to delete a name record addressed by a given index.
160
181
 
161
182
  | parameter name | value type | default value | description |
@@ -165,6 +186,13 @@ Tries to delete a name record addressed by a given index.
165
186
  <a name="TChnNamesList+loadNames"></a>
166
187
  ##### **loadNames(list, \[opt])** => `number`
167
188
 
189
+ > \[!] NOTE: `[since v0.0.5]` deprecated. Use `loadItems` instead.
190
+
191
+ <a name="TChnNamesList+loadItems"></a>
192
+ ##### **loadItems(list, \[opt])** => `number`
193
+
194
+ > since: \[v0.0.5]
195
+
168
196
  Loads a list of a new name records.
169
197
 
170
198
  | parameter name | value type | default value | description |
@@ -180,7 +208,18 @@ Removes all of the instance members.
180
208
  <a name="TChnNamesList+forEach"></a>
181
209
  ##### **forEach(cb)** => `void`
182
210
 
183
- Calls given function for each name record in the list.
211
+ Calls a given function upon each name record in the list.
212
+
213
+ | parameter name | value type | default value | description |
214
+ |:---|---|---:|:---|
215
+ | `cb` | `callback` | --- | a callback function defined by a user |
216
+
217
+ <a name="TChnNamesList+filter"></a>
218
+ ##### **filter(cb)** => `TChnNameRecord[]`
219
+
220
+ > since: \[v0.0.5]
221
+
222
+ Returns an array of a name records picked up by a given function.
184
223
 
185
224
  | parameter name | value type | default value | description |
186
225
  |:---|---|---:|:---|
@@ -419,6 +458,17 @@ Calls given function for each alias element in the list.
419
458
  |:---|---|---:|:---|
420
459
  | `cb` | `callback` | --- | a callback function defined by a user |
421
460
 
461
+ <a name="TChnAliasList+filter"></a>
462
+ ##### **filter(cb)** => `TChnAliasItem[]`
463
+
464
+ > since: \[v0.0.5]
465
+
466
+ Returns an array of a channel items picked up by a given function.
467
+
468
+ | parameter name | value type | default value | description |
469
+ |:---|---|---:|:---|
470
+ | `cb` | `callback` | --- | a callback function defined by a user |
471
+
422
472
  ## Module functions
423
473
 
424
474
  <a name="loadAliasFromFileSync"></a>
@@ -1,4 +1,4 @@
1
- // [v0.1.033-20241021]
1
+ // [v0.1.044-20250414]
2
2
 
3
3
  // === module init block ===
4
4
 
@@ -10,7 +10,7 @@ const {
10
10
 
11
11
  const {
12
12
  loadJSONFromFileSync, saveJSONToFileSync,
13
- } = require('#lib/file-helper.js');
13
+ } = require('@cntwg/file-helper');
14
14
 
15
15
  // === module extra block (helper functions) ===
16
16
 
@@ -215,6 +215,15 @@ class TChnNameRecord {
215
215
  * @description user defined procedure to process an array elements
216
216
  */
217
217
 
218
+ /**
219
+ * @callback cbArrECheck
220
+ * @param {any} item - some element
221
+ * @param {number} [index] - element index
222
+ * @param {any[]} [arr] - array a callback was called upon
223
+ * @returns {any}
224
+ * @description user defined procedure to process an array elements
225
+ */
226
+
218
227
  /**
219
228
  * @classdesc This class implements an interface of the name records list
220
229
  */
@@ -229,6 +238,22 @@ class TChnNamesList {
229
238
  this.#_items = [];
230
239
  }
231
240
 
241
+ [Symbol.iterator]() {
242
+ let index = 0;
243
+ return {
244
+ next: () => {
245
+ if (index < this.count) {
246
+ return { done: false, value: this.getItem(index++) };
247
+ } else {
248
+ return { done: true };
249
+ };
250
+ },
251
+ return() {
252
+ return { done: true };
253
+ },
254
+ };
255
+ }
256
+
232
257
  /**
233
258
  * @property {number} - contains a quantity of a name records
234
259
  * @readonly
@@ -291,21 +316,39 @@ class TChnNamesList {
291
316
  return index;
292
317
  }
293
318
 
319
+ /**
320
+ * @deprecated
321
+ * @see TChnNamesList.getItem
322
+ * @todo [from v0.0.5] make obsolete
323
+ */
324
+ getName(value) {
325
+ return this.getItem(value);
326
+ }
327
+
294
328
  /**
295
329
  * @param {number} value - an element index
296
330
  * @returns {?TChnNameRecord}
297
331
  * @description Returns a name record
298
332
  */
299
- getName(value) {
333
+ getItem(value) {
300
334
  return this.chkIndex(value) ? this.#_items[Number(value)] : null;
301
335
  }
302
336
 
337
+ /**
338
+ * @deprecated
339
+ * @see TChnNamesList.addItem
340
+ * @todo [from v0.0.5] make obsolete
341
+ */
342
+ addName(data) {
343
+ return this.addItem(data);
344
+ }
345
+
303
346
  /**
304
347
  * @param {any} data - a value of a name record
305
348
  * @returns {boolean}
306
349
  * @description Adds a new name record to a list members
307
350
  */
308
- addName(data) {
351
+ addItem(data) {
309
352
  const _data = data instanceof TChnNameRecord ? data.value : data;
310
353
  const item = new TChnNameRecord();
311
354
  const isSucceed = item.setValue(_data) && item.text !== '';
@@ -313,30 +356,49 @@ class TChnNamesList {
313
356
  return isSucceed;
314
357
  }
315
358
 
359
+ /**
360
+ * @deprecated
361
+ * @see TChnNamesList.delItem
362
+ * @todo [from v0.0.5] make obsolete
363
+ */
364
+ delName(value) {
365
+ return this.delItem(value);
366
+ }
367
+
316
368
  /**
317
369
  * @param {number} value - an element index
318
370
  * @returns {boolean}
319
371
  * @description Tries to delete a name record addressed by a given index
320
372
  */
321
- delName(value) {
373
+ delItem(value) {
322
374
  let isSucceed = this.chkIndex(value)
323
375
  if (isSucceed) this.#_items.splice(Number(value), 1);
324
376
  return isSucceed;
325
377
  }
326
378
 
379
+ //
380
+ /**
381
+ * @deprecated
382
+ * @see TChnNamesList.loadItems
383
+ * @todo [from v0.0.5] make obsolete
384
+ */
385
+ loadNames(...args) {
386
+ return this.loadItems(...args);
387
+ }
388
+
327
389
  /**
328
390
  * @param {any} list - an element or a list of an elements
329
391
  * @param {boolean} [opt=true] - defines whether to clear the list before load a new one
330
392
  * @returns {number}
331
393
  * @description Loads a new name records
332
394
  */
333
- loadNames(list, opt) {
395
+ loadItems(list, opt) {
334
396
  const useClear = typeof opt === 'boolean' ? opt : true;
335
397
  const items = valueToArray(list);
336
398
  let count = 0;
337
399
  if (items.length) {
338
400
  if (useClear) this.clear();
339
- items.forEach((item) => { if (this.addName(item)) count++; });
401
+ items.forEach((item) => { if (this.addItem(item)) count++; });
340
402
  };
341
403
  return count;
342
404
  };
@@ -358,6 +420,16 @@ class TChnNamesList {
358
420
  if (typeof cb === 'function') this.#_items.forEach(cb);
359
421
  }
360
422
 
423
+ /**
424
+ * @param {cbArrECheck} cb - a callback function
425
+ * @returns {TChnNameRecord[]}
426
+ * @description Returns an array of a name records picked up
427
+ * by a given function
428
+ */
429
+ filter(cb) {
430
+ return typeof cb === 'function' ? this.#_items.filter(cb) : [];
431
+ }
432
+
361
433
  };
362
434
 
363
435
  /**
@@ -454,20 +526,20 @@ class TChnAliasItem {
454
526
  * @returns {?TChnNameRecord}
455
527
  * @deprecated
456
528
  * @description Returns a name record
457
- * @see TChnNamesList.getName
529
+ * @see TChnNamesList.getItem
458
530
  */
459
531
  getName(value) {
460
- return this.#_names.getName(value);
532
+ return this.#_names.getItem(value);
461
533
  }
462
534
 
463
535
  /**
464
536
  * @param {any} data - a name record
465
537
  * @returns {boolean}
466
538
  * @description Adds a new name record
467
- * @see TChnNamesList.addName
539
+ * @see TChnNamesList.addItem
468
540
  */
469
541
  addName(data) {
470
- return this.#_names.addName(data);
542
+ return this.#_names.addItem(data);
471
543
  }
472
544
 
473
545
  /**
@@ -475,10 +547,10 @@ class TChnAliasItem {
475
547
  * @param {boolean} [opt=true] - defines whether to clear the list before load a new one
476
548
  * @returns {number}
477
549
  * @description Loads a list of a new name records
478
- * @see TChnNamesList.loadNames
550
+ * @see TChnNamesList.loadItems
479
551
  */
480
552
  loadNames(...args) {
481
- return this.#_names.loadNames(...args);
553
+ return this.#_names.loadItems(...args);
482
554
  }
483
555
 
484
556
  /**
@@ -605,6 +677,22 @@ class TChnAliasList {
605
677
  this.#_items = [];
606
678
  }
607
679
 
680
+ [Symbol.iterator]() {
681
+ let index = 0;
682
+ return {
683
+ next: () => {
684
+ if (index < this.count) {
685
+ return { done: false, value: this.getItem(index++) };
686
+ } else {
687
+ return { done: true };
688
+ };
689
+ },
690
+ return() {
691
+ return { done: true };
692
+ },
693
+ };
694
+ }
695
+
608
696
  /**
609
697
  * @property {number} - contains a quantity of the elements
610
698
  * @readonly
@@ -613,15 +701,6 @@ class TChnAliasList {
613
701
  return this.#_items.length;
614
702
  }
615
703
 
616
- /**
617
- * @property {boolean} - defines if the instance has any items
618
- * @readonly
619
- * @deprecated
620
- */
621
- get hasItems() {
622
- return this.#_items.length > 0;
623
- }
624
-
625
704
  /**
626
705
  * @returns {boolean}
627
706
  * @description Returns a flag whether a list is empty or not
@@ -657,7 +736,7 @@ class TChnAliasList {
657
736
  searchIndexByID(value) {
658
737
  const id = valueToIDString(value);
659
738
  let index = -1;
660
- if (id !== null && this.hasItems) {
739
+ if (id !== null && this.isNotEmpty()) {
661
740
  index = this.#_items.findIndex((item) => id === item.id);
662
741
  };
663
742
  return index;
@@ -746,6 +825,16 @@ class TChnAliasList {
746
825
  if (typeof cb === 'function') this.#_items.forEach(cb);
747
826
  }
748
827
 
828
+ /**
829
+ * @param {cbArrECheck} cb - a callback function
830
+ * @returns {TChnAliasItem[]}
831
+ * @description Returns an array of a name records picked up
832
+ * by a given function
833
+ */
834
+ filter(cb) {
835
+ return typeof cb === 'function' ? this.#_items.filter(cb) : [];
836
+ }
837
+
749
838
  };
750
839
 
751
840
  // === module exports block ===
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ygracs/chn-alias-list",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "A small library which provides some helper classes for EPG-tools",
5
5
  "author": "ygracs <cs70th-om@rambler.ru>",
6
6
  "license": "MIT",
@@ -12,7 +12,6 @@
12
12
  "files": [
13
13
  "doc/chn-alias-list.md",
14
14
  "lib/chn-alias-list.js",
15
- "lib/file-helper.js",
16
15
  "index.js",
17
16
  "CHANGELOG.md"
18
17
  ],
@@ -26,7 +25,8 @@
26
25
  "#test-dir/*": "./__test__/*"
27
26
  },
28
27
  "dependencies": {
29
- "@ygracs/bsfoc-lib-js": "^0.2.2"
28
+ "@cntwg/file-helper": "^0.0.1",
29
+ "@ygracs/bsfoc-lib-js": "^0.2.3"
30
30
  },
31
31
  "devDependencies": {
32
32
  "jest": "^29.7.0",
@@ -1,442 +0,0 @@
1
- // [v0.1.029-20250218]
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 loadFromFileSync
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 loadFromFileSync(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:loadFromFileSync: ${err}`);
105
- //throw err;
106
- };
107
- };
108
- };
109
- };
110
- return data;
111
- };
112
-
113
- /**
114
- * @function loadFromFile
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 loadFromFile(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: loadFromFile() => Error => '+err);
154
- //console.log('CHECK: loadFromFile() => 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
- * @typedef {Object} RVAL_loadjsonff
270
- * @property {fsoDescr} descr - ops description
271
- * @property {any} obj - loaded content
272
- * @description A result of `loadJSONFromFileSync`
273
- */
274
-
275
- /**
276
- * @function loadJSONFromFile
277
- * @param {string} src - a path to some file
278
- * @param {any} [opt] - <reserved>
279
- * @returns {Promise<RVAL_loadjsonff, Error>}
280
- * @async
281
- * @inner
282
- * @description Loads a JSON-object from a file
283
- */
284
- function loadJSONFromFile(src, opt) {
285
- /**/// main part that return promise as a result
286
- return new Promise((resolve, reject) => {
287
- loadFromFile(src).then(data => {
288
- let obj = null;
289
- if (!data.isERR) {
290
- if (data.content !== '') {
291
- try {
292
- obj = JSON.parse(data.content);
293
- // if succeed clear <data.content>
294
- data.content = '';
295
- } catch (err) {
296
- data.isERR = true;
297
- //console.log(`TEST_ERR-CODE: [${err.code}](${err.errno})`);
298
- //console.log(`TEST_ERR-MSG: ${err}`);
299
- if (err instanceof SyntaxError) {
300
- data.errEvent = 'ERR_JSON_BADDATA';
301
- data.errMsg = err.message;
302
- } else {
303
- data.errEvent = 'ERR_JSON_UNKNOWN';
304
- };
305
- };
306
- };
307
- };
308
- resolve({ descr: data, obj });
309
- }).catch(err => {
310
- //console.log('CHECK: TXEpgContentProvider.loadFromFile() => Error => '+err);
311
- //console.log('CHECK: TXEpgContentProvider.loadFromFile() => Error => '+err.code);
312
- reject(err);
313
- });
314
- });
315
- };
316
-
317
- /**
318
- * @function loadJSONFromFileSync
319
- * @param {string} src - a path to some file
320
- * @param {any} [opt] - <reserved>
321
- * @returns {RVAL_loadjsonff}
322
- * @inner
323
- * @description Loads a JSON-object from a file
324
- */
325
- function loadJSONFromFileSync(src, opt) {
326
- const data = loadFromFileSync(src);
327
- let obj = null;
328
- if (!data.isERR) {
329
- if (data.content !== '') {
330
- try {
331
- obj = JSON.parse(data.content);
332
- // if succeed clear <data.content>
333
- data.content = '';
334
- } catch (err) {
335
- data.isERR = true;
336
- //console.log(`TEST_ERR-CODE: [${err.code}](${err.errno})`);
337
- //console.log(`TEST_ERR-MSG: ${err}`);
338
- if (err instanceof SyntaxError) {
339
- data.errEvent = 'ERR_JSON_BADDATA';
340
- data.errMsg = err.message;
341
- } else {
342
- data.errEvent = 'ERR_JSON_UNKNOWN';
343
- };
344
- };
345
- };
346
- };
347
- return { descr: data, obj };
348
- };
349
-
350
- /**
351
- * @function saveJSONToFile
352
- * @param {string} src - a path to some file
353
- * @param {object} obj - some content
354
- * @param {any} [opt] - <reserved>
355
- * @returns {Promise<fsoDescr, Error>}
356
- * @throws {Error}
357
- * @async
358
- * @inner
359
- * @description Saves a JSON-object to a file
360
- */
361
- function saveJSONToFile(src, obj, opt) {
362
- /**/// main part that return promise as a result
363
- return new Promise((resolve, reject) => {
364
- /** @type {fsoDescr} */
365
- let data = {
366
- isERR: false,
367
- errCode: 0,
368
- errEvent: '',
369
- errMsg: '',
370
- source: '',
371
- content: '',
372
- };
373
- (new Promise((resolve, reject) => {
374
- data.content = JSON.stringify(obj, null, 2);
375
- resolve(true);
376
- })).then(result => {
377
- saveToFile(src, data.content, null).then(data => {
378
- resolve(data);
379
- }).catch(err => {
380
- // // TODO: [?] consider check others
381
- //console.log('CHECK: TXEpgContentProvider.saveToFile() => Error => '+err);
382
- //console.log('CHECK: TXEpgContentProvider.saveToFile() => Error => '+err.code);
383
- reject(err);
384
- });
385
- }).catch(err => {
386
- data.isERR = true;
387
- //console.log(`TEST_ERR-CODE: [${err.code}](${err.errno})`);
388
- //console.log(`TEST_ERR-MSG: ${err}`);
389
- data.errEvent = 'ERR_JSON_UNKNOWN';
390
- resolve(data);
391
- });
392
- });
393
- };
394
-
395
- /**
396
- * @function saveJSONToFileSync
397
- * @param {string} src - a path to some file
398
- * @param {object} obj - some content
399
- * @param {any} [opt] - <reserved>
400
- * @returns {fsoDescr}
401
- * @inner
402
- * @description Saves a JSON-object to a file
403
- */
404
- function saveJSONToFileSync(src, obj, opt) {
405
- /** @type {fsoDescr} */
406
- let data = {
407
- isERR: false,
408
- errCode: 0,
409
- errEvent: '',
410
- errMsg: '',
411
- source: '',
412
- content: '',
413
- };
414
- try {
415
- data.content = JSON.stringify(obj, null, 2);
416
- } catch (err) {
417
- data.isERR = true;
418
- //console.log(`TEST_ERR-CODE: [${err.code}](${err.errno})`);
419
- //console.log(`TEST_ERR-MSG: ${err}`);
420
- data.errEvent = 'ERR_JSON_UNKNOWN';
421
- };
422
- if (!data.isERR) {
423
- data = saveToFileSync(src, data.content, opt);
424
- };
425
- return data;
426
- };
427
-
428
- /***
429
- * (* class definitions *)
430
- */
431
-
432
- // === module exports block ===
433
-
434
- module.exports.checkFsError = checkFsError;
435
- module.exports.loadFromFile = loadFromFile;
436
- module.exports.loadFromFileSync = loadFromFileSync;
437
- module.exports.saveToFile = saveToFile;
438
- module.exports.saveToFileSync = saveToFileSync;
439
- module.exports.loadJSONFromFile = loadJSONFromFile;
440
- module.exports.loadJSONFromFileSync = loadJSONFromFileSync;
441
- module.exports.saveJSONToFile = saveJSONToFile;
442
- module.exports.saveJSONToFileSync = saveJSONToFileSync;