@ygracs/chn-alias-list 0.0.3 → 0.0.4
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 +6 -0
- package/LICENSE +1 -1
- package/lib/file-helper.js +211 -14
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
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/lib/file-helper.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
// [v0.1.
|
|
1
|
+
// [v0.1.029-20250218]
|
|
2
2
|
|
|
3
3
|
// === module init block ===
|
|
4
4
|
|
|
5
5
|
const fs = require('fs');
|
|
6
|
+
const fse = require('fs').promises;
|
|
6
7
|
|
|
7
8
|
const {
|
|
8
9
|
isPlainObject,
|
|
@@ -57,14 +58,24 @@ function checkFsError(descr, err) {
|
|
|
57
58
|
return { isSucceed, descr };
|
|
58
59
|
};
|
|
59
60
|
|
|
61
|
+
// === module main block ===
|
|
62
|
+
|
|
63
|
+
/***
|
|
64
|
+
* (* constant definitions *)
|
|
65
|
+
*/
|
|
66
|
+
|
|
67
|
+
/***
|
|
68
|
+
* (* function definitions *)
|
|
69
|
+
*/
|
|
70
|
+
|
|
60
71
|
/**
|
|
61
|
-
* @function
|
|
72
|
+
* @function loadFromFileSync
|
|
62
73
|
* @param {string} src - a path to some file
|
|
63
74
|
* @returns {fsoDescr}
|
|
64
75
|
* @inner
|
|
65
76
|
* @description Loads file by a given path
|
|
66
77
|
*/
|
|
67
|
-
function
|
|
78
|
+
function loadFromFileSync(src) {
|
|
68
79
|
/** @type {fsoDescr} */
|
|
69
80
|
const data = {
|
|
70
81
|
isERR: false,
|
|
@@ -90,7 +101,7 @@ function loadFileSync(src) {
|
|
|
90
101
|
const { isSucceed } = checkFsError(data, err);
|
|
91
102
|
if (!isSucceed) {
|
|
92
103
|
// // TODO: [?] consider check others
|
|
93
|
-
//console.log(`TEST_ERR-MSG:
|
|
104
|
+
//console.log(`TEST_ERR-MSG:loadFromFileSync: ${err}`);
|
|
94
105
|
//throw err;
|
|
95
106
|
};
|
|
96
107
|
};
|
|
@@ -99,6 +110,55 @@ function loadFileSync(src) {
|
|
|
99
110
|
return data;
|
|
100
111
|
};
|
|
101
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
|
+
|
|
102
162
|
/**
|
|
103
163
|
* @function saveToFileSync
|
|
104
164
|
* @param {string} src - a path to some file
|
|
@@ -149,15 +209,61 @@ function saveToFileSync(src, content = '', opt) {
|
|
|
149
209
|
return data;
|
|
150
210
|
};
|
|
151
211
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
*
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
*
|
|
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
|
|
160
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
|
+
};
|
|
161
267
|
|
|
162
268
|
/**
|
|
163
269
|
* @typedef {Object} RVAL_loadjsonff
|
|
@@ -166,6 +272,48 @@ function saveToFileSync(src, content = '', opt) {
|
|
|
166
272
|
* @description A result of `loadJSONFromFileSync`
|
|
167
273
|
*/
|
|
168
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
|
+
|
|
169
317
|
/**
|
|
170
318
|
* @function loadJSONFromFileSync
|
|
171
319
|
* @param {string} src - a path to some file
|
|
@@ -175,7 +323,7 @@ function saveToFileSync(src, content = '', opt) {
|
|
|
175
323
|
* @description Loads a JSON-object from a file
|
|
176
324
|
*/
|
|
177
325
|
function loadJSONFromFileSync(src, opt) {
|
|
178
|
-
const data =
|
|
326
|
+
const data = loadFromFileSync(src);
|
|
179
327
|
let obj = null;
|
|
180
328
|
if (!data.isERR) {
|
|
181
329
|
if (data.content !== '') {
|
|
@@ -199,6 +347,51 @@ function loadJSONFromFileSync(src, opt) {
|
|
|
199
347
|
return { descr: data, obj };
|
|
200
348
|
};
|
|
201
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
|
+
|
|
202
395
|
/**
|
|
203
396
|
* @function saveJSONToFileSync
|
|
204
397
|
* @param {string} src - a path to some file
|
|
@@ -239,7 +432,11 @@ function saveJSONToFileSync(src, obj, opt) {
|
|
|
239
432
|
// === module exports block ===
|
|
240
433
|
|
|
241
434
|
module.exports.checkFsError = checkFsError;
|
|
242
|
-
module.exports.
|
|
435
|
+
module.exports.loadFromFile = loadFromFile;
|
|
436
|
+
module.exports.loadFromFileSync = loadFromFileSync;
|
|
437
|
+
module.exports.saveToFile = saveToFile;
|
|
243
438
|
module.exports.saveToFileSync = saveToFileSync;
|
|
439
|
+
module.exports.loadJSONFromFile = loadJSONFromFile;
|
|
244
440
|
module.exports.loadJSONFromFileSync = loadJSONFromFileSync;
|
|
441
|
+
module.exports.saveJSONToFile = saveJSONToFile;
|
|
245
442
|
module.exports.saveJSONToFileSync = saveJSONToFileSync;
|
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.4",
|
|
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",
|
|
@@ -26,11 +26,11 @@
|
|
|
26
26
|
"#test-dir/*": "./__test__/*"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@ygracs/bsfoc-lib-js": "^0.2.
|
|
29
|
+
"@ygracs/bsfoc-lib-js": "^0.2.2"
|
|
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
|
}
|