@ygracs/chn-alias-list 0.0.3 → 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 +21 -0
- package/LICENSE +1 -1
- package/doc/chn-alias-list.md +53 -3
- package/lib/chn-alias-list.js +112 -23
- package/package.json +4 -4
- package/lib/file-helper.js +0 -245
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,24 @@
|
|
|
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
|
+
|
|
16
|
+
#### *v0.0.4*
|
|
17
|
+
|
|
18
|
+
Pre-release version.
|
|
19
|
+
|
|
20
|
+
> - update dependency on `@ygracs/bsfoc-lib-js` module to v0.2.2.
|
|
21
|
+
|
|
1
22
|
#### *v0.0.1-0.0.3*
|
|
2
23
|
|
|
3
24
|
Pre-release version.
|
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
The MIT License (MIT)
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2024-
|
|
3
|
+
Copyright (c) 2024-2025 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/doc/chn-alias-list.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
|
|
2
|
-
>|***rev.*:**|0.0.
|
|
2
|
+
>|***rev.*:**|0.0.5|
|
|
3
3
|
>|:---|---:|
|
|
4
|
-
>|date:|
|
|
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
|
|
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>
|
package/lib/chn-alias-list.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// [v0.1.
|
|
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('
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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.
|
|
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.
|
|
529
|
+
* @see TChnNamesList.getItem
|
|
458
530
|
*/
|
|
459
531
|
getName(value) {
|
|
460
|
-
return this.#_names.
|
|
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.
|
|
539
|
+
* @see TChnNamesList.addItem
|
|
468
540
|
*/
|
|
469
541
|
addName(data) {
|
|
470
|
-
return this.#_names.
|
|
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.
|
|
550
|
+
* @see TChnNamesList.loadItems
|
|
479
551
|
*/
|
|
480
552
|
loadNames(...args) {
|
|
481
|
-
return this.#_names.
|
|
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.
|
|
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.
|
|
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,11 +25,12 @@
|
|
|
26
25
|
"#test-dir/*": "./__test__/*"
|
|
27
26
|
},
|
|
28
27
|
"dependencies": {
|
|
29
|
-
"@
|
|
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",
|
|
33
|
-
"jsdoc-to-markdown": "^9.
|
|
33
|
+
"jsdoc-to-markdown": "^9.1.1",
|
|
34
34
|
"minimist": "^1.2.8"
|
|
35
35
|
}
|
|
36
36
|
}
|
package/lib/file-helper.js
DELETED
|
@@ -1,245 +0,0 @@
|
|
|
1
|
-
// [v0.1.026-20241021]
|
|
2
|
-
|
|
3
|
-
// === module init block ===
|
|
4
|
-
|
|
5
|
-
const fs = require('fs');
|
|
6
|
-
|
|
7
|
-
const {
|
|
8
|
-
isPlainObject,
|
|
9
|
-
} = require('@ygracs/bsfoc-lib-js');
|
|
10
|
-
|
|
11
|
-
// === module extra block (helper functions) ===
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* @typedef {Object} fsoDescr
|
|
15
|
-
* @property {boolean} isERR - flag
|
|
16
|
-
* @property {number|undefined} errCode - error code
|
|
17
|
-
* @property {string} errEvent - event ID
|
|
18
|
-
* @property {string} errMsg - event message
|
|
19
|
-
* @property {string} source - path to file
|
|
20
|
-
* @property {any} content - file content
|
|
21
|
-
* @description A fs ops description.
|
|
22
|
-
*/
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* @typedef {Object} VCOR_evalerrfs
|
|
26
|
-
* @property {boolean} isSucceed - ops flag
|
|
27
|
-
* @property {fsoDescr} descr - description
|
|
28
|
-
* @description A result of an error check ops.
|
|
29
|
-
*/
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* @function checkFsError
|
|
33
|
-
* @param {fsoDescr} descr
|
|
34
|
-
* @param {Error} err
|
|
35
|
-
* @returns {VCOR_evalerrfs}
|
|
36
|
-
* @inner
|
|
37
|
-
* @description Checks an error code of a fs ops and sets descr info if succeed
|
|
38
|
-
*/
|
|
39
|
-
function checkFsError(descr, err) {
|
|
40
|
-
let isSucceed = false;
|
|
41
|
-
if (isPlainObject(err) && isPlainObject(descr)) {
|
|
42
|
-
switch (err.code) {
|
|
43
|
-
case 'ENOENT':
|
|
44
|
-
case 'EISDIR':
|
|
45
|
-
case 'ENOTDIR':
|
|
46
|
-
case 'EPERM': {
|
|
47
|
-
descr.errCode = err.errno;
|
|
48
|
-
descr.errEvent = err.code;
|
|
49
|
-
descr.errMsg = `ERR_FILE_${err.code}`;
|
|
50
|
-
isSucceed = true;
|
|
51
|
-
break;
|
|
52
|
-
}
|
|
53
|
-
default: {}
|
|
54
|
-
};
|
|
55
|
-
if (isSucceed) descr.isERR = true;
|
|
56
|
-
};
|
|
57
|
-
return { isSucceed, descr };
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* @function loadFileSync
|
|
62
|
-
* @param {string} src - a path to some file
|
|
63
|
-
* @returns {fsoDescr}
|
|
64
|
-
* @inner
|
|
65
|
-
* @description Loads file by a given path
|
|
66
|
-
*/
|
|
67
|
-
function loadFileSync(src) {
|
|
68
|
-
/** @type {fsoDescr} */
|
|
69
|
-
const data = {
|
|
70
|
-
isERR: false,
|
|
71
|
-
errCode: 0,
|
|
72
|
-
errEvent: '',
|
|
73
|
-
errMsg: '',
|
|
74
|
-
source: '',
|
|
75
|
-
content: '',
|
|
76
|
-
};
|
|
77
|
-
if (typeof src !== 'string') {
|
|
78
|
-
if (src !== undefined) {
|
|
79
|
-
data.isERR = true;
|
|
80
|
-
data.errEvent = 'ERR_INVARG';
|
|
81
|
-
data.errMsg = 'The "source" argument must be a string';
|
|
82
|
-
};
|
|
83
|
-
} else {
|
|
84
|
-
const srcPath = src.trim();
|
|
85
|
-
if (srcPath !== '') {
|
|
86
|
-
data.source = srcPath;
|
|
87
|
-
try {
|
|
88
|
-
data.content = fs.readFileSync(srcPath, 'utf8');
|
|
89
|
-
} catch (err) {
|
|
90
|
-
const { isSucceed } = checkFsError(data, err);
|
|
91
|
-
if (!isSucceed) {
|
|
92
|
-
// // TODO: [?] consider check others
|
|
93
|
-
//console.log(`TEST_ERR-MSG:loadFileSync: ${err}`);
|
|
94
|
-
//throw err;
|
|
95
|
-
};
|
|
96
|
-
};
|
|
97
|
-
};
|
|
98
|
-
};
|
|
99
|
-
return data;
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* @function saveToFileSync
|
|
104
|
-
* @param {string} src - a path to some file
|
|
105
|
-
* @param {string} content - some file content
|
|
106
|
-
* @param {any} [opt] - <reserved>
|
|
107
|
-
* @returns {fsoDescr}
|
|
108
|
-
* @inner
|
|
109
|
-
* @description Saves a content to a file
|
|
110
|
-
*/
|
|
111
|
-
function saveToFileSync(src, content = '', opt) {
|
|
112
|
-
/** @type {fsoDescr} */
|
|
113
|
-
const data = {
|
|
114
|
-
isERR: false,
|
|
115
|
-
errCode: 0,
|
|
116
|
-
errEvent: '',
|
|
117
|
-
errMsg: '',
|
|
118
|
-
source: '',
|
|
119
|
-
content: '',
|
|
120
|
-
};
|
|
121
|
-
if (typeof src !== 'string') {
|
|
122
|
-
data.isERR = true;
|
|
123
|
-
data.errEvent = src === undefined ? 'ERR_NOSRC' : 'ERR_INVARG';
|
|
124
|
-
data.errMsg = 'The "source" argument must be a string';
|
|
125
|
-
} else if (typeof content === 'string') {
|
|
126
|
-
const srcPath = src.trim();
|
|
127
|
-
if (srcPath === '') {
|
|
128
|
-
data.isERR = true;
|
|
129
|
-
data.errEvent = 'ERR_NOSRC';
|
|
130
|
-
data.errMsg = 'No "source" path given';
|
|
131
|
-
} else {
|
|
132
|
-
data.source = srcPath;
|
|
133
|
-
try {
|
|
134
|
-
fs.writeFileSync(srcPath, content, 'utf8');
|
|
135
|
-
} catch (err) {
|
|
136
|
-
const { isSucceed } = checkFsError(data, err);
|
|
137
|
-
if (!isSucceed) {
|
|
138
|
-
// // TODO: [?] consider check others
|
|
139
|
-
//console.log(`TEST_ERR-MSG:saveToFileSync: ${err}`);
|
|
140
|
-
//throw err;
|
|
141
|
-
};
|
|
142
|
-
};
|
|
143
|
-
};
|
|
144
|
-
} else {
|
|
145
|
-
data.isERR = true;
|
|
146
|
-
data.errEvent = 'ERR_INVARG';
|
|
147
|
-
data.errMsg = 'The "content" argument must be a string';
|
|
148
|
-
};
|
|
149
|
-
return data;
|
|
150
|
-
};
|
|
151
|
-
|
|
152
|
-
// === module main block ===
|
|
153
|
-
|
|
154
|
-
/***
|
|
155
|
-
* (* constant definitions *)
|
|
156
|
-
*/
|
|
157
|
-
|
|
158
|
-
/***
|
|
159
|
-
* (* function definitions *)
|
|
160
|
-
*/
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* @typedef {Object} RVAL_loadjsonff
|
|
164
|
-
* @property {fsoDescr} descr - ops description
|
|
165
|
-
* @property {any} obj - loaded content
|
|
166
|
-
* @description A result of `loadJSONFromFileSync`
|
|
167
|
-
*/
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* @function loadJSONFromFileSync
|
|
171
|
-
* @param {string} src - a path to some file
|
|
172
|
-
* @param {any} [opt] - <reserved>
|
|
173
|
-
* @returns {RVAL_loadjsonff}
|
|
174
|
-
* @inner
|
|
175
|
-
* @description Loads a JSON-object from a file
|
|
176
|
-
*/
|
|
177
|
-
function loadJSONFromFileSync(src, opt) {
|
|
178
|
-
const data = loadFileSync(src);
|
|
179
|
-
let obj = null;
|
|
180
|
-
if (!data.isERR) {
|
|
181
|
-
if (data.content !== '') {
|
|
182
|
-
try {
|
|
183
|
-
obj = JSON.parse(data.content);
|
|
184
|
-
// if succeed clear <data.content>
|
|
185
|
-
data.content = '';
|
|
186
|
-
} catch (err) {
|
|
187
|
-
data.isERR = true;
|
|
188
|
-
//console.log(`TEST_ERR-CODE: [${err.code}](${err.errno})`);
|
|
189
|
-
//console.log(`TEST_ERR-MSG: ${err}`);
|
|
190
|
-
if (err instanceof SyntaxError) {
|
|
191
|
-
data.errEvent = 'ERR_JSON_BADDATA';
|
|
192
|
-
data.errMsg = err.message;
|
|
193
|
-
} else {
|
|
194
|
-
data.errEvent = 'ERR_JSON_UNKNOWN';
|
|
195
|
-
};
|
|
196
|
-
};
|
|
197
|
-
};
|
|
198
|
-
};
|
|
199
|
-
return { descr: data, obj };
|
|
200
|
-
};
|
|
201
|
-
|
|
202
|
-
/**
|
|
203
|
-
* @function saveJSONToFileSync
|
|
204
|
-
* @param {string} src - a path to some file
|
|
205
|
-
* @param {object} obj - some content
|
|
206
|
-
* @param {any} [opt] - <reserved>
|
|
207
|
-
* @returns {fsoDescr}
|
|
208
|
-
* @inner
|
|
209
|
-
* @description Saves a JSON-object to a file
|
|
210
|
-
*/
|
|
211
|
-
function saveJSONToFileSync(src, obj, opt) {
|
|
212
|
-
/** @type {fsoDescr} */
|
|
213
|
-
let data = {
|
|
214
|
-
isERR: false,
|
|
215
|
-
errCode: 0,
|
|
216
|
-
errEvent: '',
|
|
217
|
-
errMsg: '',
|
|
218
|
-
source: '',
|
|
219
|
-
content: '',
|
|
220
|
-
};
|
|
221
|
-
try {
|
|
222
|
-
data.content = JSON.stringify(obj, null, 2);
|
|
223
|
-
} catch (err) {
|
|
224
|
-
data.isERR = true;
|
|
225
|
-
//console.log(`TEST_ERR-CODE: [${err.code}](${err.errno})`);
|
|
226
|
-
//console.log(`TEST_ERR-MSG: ${err}`);
|
|
227
|
-
data.errEvent = 'ERR_JSON_UNKNOWN';
|
|
228
|
-
};
|
|
229
|
-
if (!data.isERR) {
|
|
230
|
-
data = saveToFileSync(src, data.content, opt);
|
|
231
|
-
};
|
|
232
|
-
return data;
|
|
233
|
-
};
|
|
234
|
-
|
|
235
|
-
/***
|
|
236
|
-
* (* class definitions *)
|
|
237
|
-
*/
|
|
238
|
-
|
|
239
|
-
// === module exports block ===
|
|
240
|
-
|
|
241
|
-
module.exports.checkFsError = checkFsError;
|
|
242
|
-
module.exports.loadFileSync = loadFileSync;
|
|
243
|
-
module.exports.saveToFileSync = saveToFileSync;
|
|
244
|
-
module.exports.loadJSONFromFileSync = loadJSONFromFileSync;
|
|
245
|
-
module.exports.saveJSONToFileSync = saveJSONToFileSync;
|