@ygracs/chn-alias-list 0.0.4 → 0.0.6

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,24 @@
1
- #### *v0.0.4*
1
+ #### *v0.0.6*
2
2
 
3
3
  Pre-release version.
4
4
 
5
- > - update dependency on `@ygracs/bsfoc-lib-js` module to v0.2.2.
5
+ > - update dependency on `@ygracs/bsfoc-lib-js` module to v0.3.0.
6
6
 
7
- #### *v0.0.1-0.0.3*
7
+ #### *v0.0.5*
8
+
9
+ Pre-release version.
10
+
11
+ > - update `chn-alias-list.md`;
12
+ > - update dependency on `@ygracs/bsfoc-lib-js` module to v0.2.3;
13
+ > - add dependency on `@cntwg/file-helper` module;
14
+ > - add 'filter'-method to a `TChnNamesList`-class;
15
+ > - change name of a method `getName` into `getItem` for a `TChnNamesList`-class (*old name deprecated*);
16
+ > - change name of a method `addName` into `addItem` for a `TChnNamesList`-class (*old name deprecated*);
17
+ > - change name of a method `delName` into `delItem` for a `TChnNamesList`-class (*old name deprecated*);
18
+ > - change name of a method `loadNames` into `loadItems` for a `TChnNamesList`-class (*old name deprecated*);
19
+ > - add 'filter'-method to a `TChnAliasList`-class;
20
+ > - other fixes.
21
+
22
+ #### *v0.0.1-0.0.4*
8
23
 
9
24
  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.045-20250820]
2
2
 
3
3
  // === module init block ===
4
4
 
@@ -10,17 +10,21 @@ const {
10
10
 
11
11
  const {
12
12
  loadJSONFromFileSync, saveJSONToFileSync,
13
- } = require('#lib/file-helper.js');
13
+ } = require('@cntwg/file-helper');
14
14
 
15
- // === module extra block (helper functions) ===
15
+ // === module inner block ===
16
+
17
+ /***
18
+ * (* helper function definitions *)
19
+ */
16
20
 
17
21
  /**
22
+ * Converts a <Lang, Text> key-pair to a string
18
23
  * @function convertLangTextValueToString
19
24
  * @param {array} data - contains a data to be printed
20
25
  * @param {object} [opt]
21
26
  * @returns {string}
22
27
  * @inner
23
- * @description Converts a <Lang, Text> key-pair to string
24
28
  */
25
29
  function convertLangTextValueToString(data, opt) {
26
30
  let name = [ '', '' ];
@@ -46,29 +50,30 @@ function convertLangTextValueToString(data, opt) {
46
50
  */
47
51
 
48
52
  /**
53
+ * An 'FS'-ops descriptor.
49
54
  * @typedef {Object} fsoDescr
50
55
  * @property {boolean} isERR - flag
51
- * @property {number|undefined} errCode - error code
56
+ * @property {number} [errCode] - error code
52
57
  * @property {string} errEvent - event ID
53
- * @property {string} errMsg - event message
54
- * @property {string} source - path to file
55
- * @property {any} content - file content
56
- * @description A fs ops description.
58
+ * @property {string} [errMsg] - event message
59
+ * @property {string} [source] - path to file
60
+ * @property {any} [content] - file content
57
61
  */
58
62
 
59
63
  /**
64
+ * A result of `loadAliasFromFile...`
60
65
  * @typedef {Object} RVAL_loadaliasff
61
66
  * @property {fsoDescr} descr - ops description
62
67
  * @property {(null|TChnAliasItem|TChnAliasList)} result - loaded content
63
- * @description A result of `loadAliasFromFile...`
64
68
  */
65
69
 
66
70
  /**
71
+ * Loads an alias from a file
67
72
  * @function loadAliasFromFileSync
68
73
  * @param {string} src - a path to some file
69
74
  * @param {any} [opt] - <reserved>
70
75
  * @returns {RVAL_loadaliasff}
71
- * @description Loads an alias from a file
76
+ * @see {@link loadJSONFromFileSync} for details of an `opt` param
72
77
  */
73
78
  function loadAliasFromFileSync(src, opt) {
74
79
  let { descr, obj } = loadJSONFromFileSync(src, opt);
@@ -86,12 +91,13 @@ function loadAliasFromFileSync(src, opt) {
86
91
  };
87
92
 
88
93
  /**
94
+ * Saves an alias to a file
89
95
  * @function saveAliasToFileSync
90
96
  * @param {string} src - a path to some file
91
97
  * @param {Array} content - content
92
98
  * @param {any} [opt] - <reserved>
93
99
  * @returns {fsoDescr}
94
- * @description Saves an alias to a file
100
+ * @see {@link saveJSONToFileSync} for details of an `opt` param
95
101
  */
96
102
  function saveAliasToFileSync(src, content, opt) {
97
103
  const obj = isArray(content) ? content : null;
@@ -109,20 +115,21 @@ function saveAliasToFileSync(src, content, opt) {
109
115
  * @classdesc This class implements an interface of the name record
110
116
  */
111
117
  class TChnNameRecord {
112
- /** @property {string} */
118
+ /** @type {string} */
113
119
  #_lang;
114
- /** @property {string} */
120
+ /** @type {string} */
115
121
  #_value;
116
122
 
117
123
  /**
118
- * @description Creates an instance of the name record
124
+ * Creates an instance of the name record
119
125
  */
120
126
  constructor() {
121
127
  this.reset();
122
128
  }
123
129
 
124
130
  /**
125
- * @property {string} - defines a language
131
+ * Contains a language
132
+ * @type {string}
126
133
  * @readonly
127
134
  */
128
135
  get lang() {
@@ -130,7 +137,8 @@ class TChnNameRecord {
130
137
  }
131
138
 
132
139
  /**
133
- * @property {string} - defines a value of the record
140
+ * Contains a value of the record
141
+ * @type {string}
134
142
  * @readonly
135
143
  */
136
144
  get text() {
@@ -138,7 +146,8 @@ class TChnNameRecord {
138
146
  }
139
147
 
140
148
  /**
141
- * @property {array} - a value of the record
149
+ * Contains a value of the record
150
+ * @type {string[]}
142
151
  */
143
152
  get value() {
144
153
  return [ this.#_lang, this.#_value ];
@@ -149,9 +158,9 @@ class TChnNameRecord {
149
158
  }
150
159
 
151
160
  /**
161
+ * Sets the record value
152
162
  * @param {any} data - a value of the record
153
163
  * @returns {boolean}
154
- * @description Sets the record value
155
164
  */
156
165
  setValue(data) {
157
166
  let _data = data;
@@ -187,17 +196,17 @@ class TChnNameRecord {
187
196
  }
188
197
 
189
198
  /**
199
+ * Returns value as a formated string
190
200
  * @param {any} opt - <reserved>
191
201
  * @returns {string}
192
- * @description Returns value as a formated string
193
202
  */
194
203
  toFormatString(opt) {
195
204
  return convertLangTextValueToString(this.value, opt);
196
205
  }
197
206
 
198
207
  /**
208
+ * Clears the record
199
209
  * @returns {void}
200
- * @description Clears the record
201
210
  */
202
211
  reset() {
203
212
  this.#_lang = '';
@@ -207,30 +216,56 @@ class TChnNameRecord {
207
216
  };
208
217
 
209
218
  /**
219
+ * A user defined procedure to process an array elements
210
220
  * @callback forEachProcEx
211
- * @param {any} item
212
- * @param {number} [index]
221
+ * @param {any} item - some element
222
+ * @param {number} [index] - element index
213
223
  * @param {any[]} [arr]
214
224
  * @returns {void}
215
- * @description user defined procedure to process an array elements
216
225
  */
217
226
 
227
+ /**
228
+ * A user defined procedure to process an array elements
229
+ * @callback cbArrECheck
230
+ * @param {any} item - some element
231
+ * @param {number} [index] - element index
232
+ * @param {any[]} [arr] - array a callback was called upon
233
+ * @returns {any}
234
+ */
235
+
218
236
  /**
219
237
  * @classdesc This class implements an interface of the name records list
220
238
  */
221
239
  class TChnNamesList {
222
- /** @property {TChnNameRecord[]} */
240
+ /** @type {TChnNameRecord[]} */
223
241
  #_items;
224
242
 
225
243
  /**
226
- * @description Creates an instance of the name records list
244
+ * Creates an instance of the name records list
227
245
  */
228
246
  constructor() {
229
247
  this.#_items = [];
230
248
  }
231
249
 
250
+ [Symbol.iterator]() {
251
+ let index = 0;
252
+ return {
253
+ next: () => {
254
+ if (index < this.count) {
255
+ return { done: false, value: this.getItem(index++) };
256
+ } else {
257
+ return { done: true };
258
+ };
259
+ },
260
+ return() {
261
+ return { done: true };
262
+ },
263
+ };
264
+ }
265
+
232
266
  /**
233
- * @property {number} - contains a quantity of a name records
267
+ * Contains a quantity of a name records
268
+ * @type {number}
234
269
  * @readonly
235
270
  */
236
271
  get count() {
@@ -238,7 +273,8 @@ class TChnNamesList {
238
273
  }
239
274
 
240
275
  /**
241
- * @property {TChnNameRecord[]} - contains a list of a name records
276
+ * Contains a list of a name records
277
+ * @type {TChnNameRecord[]}
242
278
  * @readonly
243
279
  */
244
280
  get value() {
@@ -250,26 +286,26 @@ class TChnNamesList {
250
286
  }
251
287
 
252
288
  /**
289
+ * Returns a flag whether a list is empty or not
253
290
  * @returns {boolean}
254
- * @description Returns a flag whether a list is empty or not
255
291
  */
256
292
  isEmpty() {
257
293
  return this.count === 0;
258
294
  }
259
295
 
260
296
  /**
297
+ * Returns a flag whether a list has any members
261
298
  * @returns {boolean}
262
- * @description Returns a flag whether a list has any members
263
299
  */
264
300
  isNotEmpty() {
265
301
  return this.count > 0;
266
302
  }
267
303
 
268
304
  /**
305
+ * Checks whether a given value is an index and fits an index range
306
+ * within the instance
269
307
  * @param {any} value - a value to be verified
270
308
  * @returns {boolean}
271
- * @description Checks whether a given value is an index and fits
272
- * an Index range within the instance
273
309
  */
274
310
  chkIndex(value) {
275
311
  const index = valueToIndex(value);
@@ -277,9 +313,9 @@ class TChnNamesList {
277
313
  }
278
314
 
279
315
  /**
316
+ * Searches an index of a given name
280
317
  * @param {string} value - a name
281
318
  * @returns {number}
282
- * @description Searches an index of a given name
283
319
  */
284
320
  getIndex(value) {
285
321
  const opt = { numberToString: true };
@@ -292,20 +328,38 @@ class TChnNamesList {
292
328
  }
293
329
 
294
330
  /**
331
+ * @deprecated
332
+ * @see TChnNamesList.getItem
333
+ * @todo [from v0.0.5] make obsolete
334
+ */
335
+ getName(value) {
336
+ return this.getItem(value);
337
+ }
338
+
339
+ /**
340
+ * Returns a name record
295
341
  * @param {number} value - an element index
296
342
  * @returns {?TChnNameRecord}
297
- * @description Returns a name record
298
343
  */
299
- getName(value) {
344
+ getItem(value) {
300
345
  return this.chkIndex(value) ? this.#_items[Number(value)] : null;
301
346
  }
302
347
 
303
348
  /**
349
+ * @deprecated
350
+ * @see TChnNamesList.addItem
351
+ * @todo [from v0.0.5] make obsolete
352
+ */
353
+ addName(data) {
354
+ return this.addItem(data);
355
+ }
356
+
357
+ /**
358
+ * Adds a new name record to a list members
304
359
  * @param {any} data - a value of a name record
305
360
  * @returns {boolean}
306
- * @description Adds a new name record to a list members
307
361
  */
308
- addName(data) {
362
+ addItem(data) {
309
363
  const _data = data instanceof TChnNameRecord ? data.value : data;
310
364
  const item = new TChnNameRecord();
311
365
  const isSucceed = item.setValue(_data) && item.text !== '';
@@ -314,63 +368,96 @@ class TChnNamesList {
314
368
  }
315
369
 
316
370
  /**
371
+ * @deprecated
372
+ * @see TChnNamesList.delItem
373
+ * @todo [from v0.0.5] make obsolete
374
+ */
375
+ delName(value) {
376
+ return this.delItem(value);
377
+ }
378
+
379
+ /**
380
+ * Tries to delete a name record addressed by a given index
317
381
  * @param {number} value - an element index
318
382
  * @returns {boolean}
319
- * @description Tries to delete a name record addressed by a given index
320
383
  */
321
- delName(value) {
384
+ delItem(value) {
322
385
  let isSucceed = this.chkIndex(value)
323
386
  if (isSucceed) this.#_items.splice(Number(value), 1);
324
387
  return isSucceed;
325
388
  }
326
389
 
327
390
  /**
391
+ * @deprecated
392
+ * @see TChnNamesList.loadItems
393
+ * @todo [from v0.0.5] make obsolete
394
+ */
395
+ loadNames(...args) {
396
+ return this.loadItems(...args);
397
+ }
398
+
399
+ /**
400
+ * Loads a new name records
328
401
  * @param {any} list - an element or a list of an elements
329
402
  * @param {boolean} [opt=true] - defines whether to clear the list before load a new one
330
403
  * @returns {number}
331
- * @description Loads a new name records
332
404
  */
333
- loadNames(list, opt) {
405
+ loadItems(list, opt) {
334
406
  const useClear = typeof opt === 'boolean' ? opt : true;
335
407
  const items = valueToArray(list);
336
408
  let count = 0;
337
409
  if (items.length) {
338
410
  if (useClear) this.clear();
339
- items.forEach((item) => { if (this.addName(item)) count++; });
411
+ items.forEach((item) => { if (this.addItem(item)) count++; });
340
412
  };
341
413
  return count;
342
414
  };
343
415
 
344
416
  /**
417
+ * Removes all of the instance members
345
418
  * @returns {void}
346
- * @description Removes all of the instance members
347
419
  */
348
420
  clear() {
349
421
  this.#_items.length = 0;
350
422
  }
351
423
 
352
424
  /**
425
+ * Calls given function for each name record
353
426
  * @param {forEachProcEx} cb - a callback function
354
427
  * @returns {void}
355
- * @description Calls given function for each name record
356
428
  */
357
429
  forEach(cb) {
358
430
  if (typeof cb === 'function') this.#_items.forEach(cb);
359
431
  }
360
432
 
433
+ /**
434
+ * Returns an array of a name records picked up by a given function
435
+ * @param {cbArrECheck} cb - a callback function
436
+ * @returns {TChnNameRecord[]}
437
+ */
438
+ filter(cb) {
439
+ return typeof cb === 'function' ? this.#_items.filter(cb) : [];
440
+ }
441
+
361
442
  };
362
443
 
363
444
  /**
364
445
  * @classdesc This class implements an interface of the channel item
365
446
  */
366
447
  class TChnAliasItem {
367
- /** @property {string} */
448
+ /** @type {string} */
368
449
  #_id = '';
369
- /** @property {string} */
450
+ /** @type {string} */
370
451
  #_alias = '';
371
- /** @property {TChnNamesList} */
452
+ /** @type {TChnNamesList} */
372
453
  #_names;
373
- /** @property {object} */
454
+ /**
455
+ * A channel status
456
+ * @typedef {Object} chnStat
457
+ * @property {boolean} isEnabled
458
+ * @property {boolean} isActive
459
+ */
460
+ /** @type {chnStat} */
374
461
  #_status;
375
462
 
376
463
  /**
@@ -386,7 +473,8 @@ class TChnAliasItem {
386
473
  }
387
474
 
388
475
  /**
389
- * @property {string} - defines a channel ID
476
+ * Contains a channel ID
477
+ * @type {string}
390
478
  */
391
479
  get id() {
392
480
  return this.#_id;
@@ -397,7 +485,8 @@ class TChnAliasItem {
397
485
  }
398
486
 
399
487
  /**
400
- * @property {string} - defines a channel alias
488
+ * Contains a channel alias
489
+ * @type {string}
401
490
  */
402
491
  get alias() {
403
492
  return this.#_alias;
@@ -408,7 +497,8 @@ class TChnAliasItem {
408
497
  }
409
498
 
410
499
  /**
411
- * @property {TChnNamesList} - contains a list of a name records
500
+ * Contains a list of a name records
501
+ * @type {TChnNamesList}
412
502
  * @readonly
413
503
  */
414
504
  get names() {
@@ -416,7 +506,8 @@ class TChnAliasItem {
416
506
  }
417
507
 
418
508
  /**
419
- * @property {string} - defines an instance status
509
+ * Contains an instance status
510
+ * @type {string}
420
511
  * @readonly
421
512
  */
422
513
  get status() {
@@ -426,9 +517,10 @@ class TChnAliasItem {
426
517
  }
427
518
 
428
519
  /**
520
+ * Sets a channel ID
429
521
  * @param {string} value - a channel ID
430
522
  * @returns {boolean}
431
- * @description Sets a channel ID
523
+ * @see valueToIDString
432
524
  */
433
525
  setID(value) {
434
526
  const id = valueToIDString(value);
@@ -438,9 +530,10 @@ class TChnAliasItem {
438
530
  }
439
531
 
440
532
  /**
533
+ * Sets a channel alias
441
534
  * @param {string} value - a channel alias
442
535
  * @returns {boolean}
443
- * @description Sets a channel alias
536
+ * @see valueToIDString
444
537
  */
445
538
  setAlias(value) {
446
539
  const alias = valueToIDString(value);
@@ -450,73 +543,73 @@ class TChnAliasItem {
450
543
  }
451
544
 
452
545
  /**
546
+ * Returns a name record
453
547
  * @param {number} value
454
548
  * @returns {?TChnNameRecord}
455
549
  * @deprecated
456
- * @description Returns a name record
457
- * @see TChnNamesList.getName
550
+ * @see TChnNamesList.getItem
458
551
  */
459
552
  getName(value) {
460
- return this.#_names.getName(value);
553
+ return this.#_names.getItem(value);
461
554
  }
462
555
 
463
556
  /**
557
+ * Adds a new name record
464
558
  * @param {any} data - a name record
465
559
  * @returns {boolean}
466
- * @description Adds a new name record
467
- * @see TChnNamesList.addName
560
+ * @see TChnNamesList.addItem
468
561
  */
469
562
  addName(data) {
470
- return this.#_names.addName(data);
563
+ return this.#_names.addItem(data);
471
564
  }
472
565
 
473
566
  /**
567
+ * Loads a list of a new name records
474
568
  * @param {array} list - a list of an elements
475
569
  * @param {boolean} [opt=true] - defines whether to clear the list before load a new one
476
570
  * @returns {number}
477
- * @description Loads a list of a new name records
478
- * @see TChnNamesList.loadNames
571
+ * @see TChnNamesList.loadItems
479
572
  */
480
573
  loadNames(...args) {
481
- return this.#_names.loadNames(...args);
574
+ return this.#_names.loadItems(...args);
482
575
  }
483
576
 
484
577
  /**
578
+ * Sets item status to enabled
485
579
  * @returns {void}
486
- * @description Sets item status to enabled
487
580
  */
488
581
  enable() {
489
582
  this.#_status.isEnabled = true;
490
583
  }
491
584
 
492
585
  /**
586
+ * Sets item status to disabled
493
587
  * @returns {void}
494
- * @description Sets item status to disabled
495
588
  */
496
589
  disable() {
497
590
  this.#_status.isEnabled = false;
498
591
  }
499
592
 
500
593
  /**
594
+ * Sets item status to active/running
501
595
  * @returns {void}
502
- * @description Sets item status to active/running
503
596
  */
504
597
  up() {
505
598
  this.#_status.isActive = true;
506
599
  }
507
600
 
508
601
  /**
602
+ * Sets item status to inactive/stopped
509
603
  * @returns {void}
510
- * @description Sets item status to inactive/stopped
511
604
  */
512
605
  down() {
513
606
  this.#_status.isActive = false;
514
607
  }
515
608
 
516
609
  /**
610
+ * Sets item status
517
611
  * @param {string} value - a status value
518
612
  * @returns {boolean}
519
- * @description Sets item status
520
613
  */
521
614
  setStatus(value) {
522
615
  const status = readAsString(value);
@@ -546,10 +639,9 @@ class TChnAliasItem {
546
639
  }
547
640
 
548
641
  /**
642
+ * Provides an interface to an instance representation for JSON.stringify()
549
643
  * @returns {Object}
550
644
  * @protected
551
- * @description Provides an interface to an instance representation
552
- * for JSON.stringify()
553
645
  */
554
646
  toJSON() {
555
647
  const { id, alias, names, status } = this;
@@ -562,10 +654,10 @@ class TChnAliasItem {
562
654
  }
563
655
 
564
656
  /**
657
+ * Creates a new alias element
565
658
  * @param {object} obj - an init data
566
659
  * @returns {?TChnAliasItem}
567
660
  * @static
568
- * @description Creates a new alias element
569
661
  */
570
662
  static create(obj) {
571
663
  let result = null;
@@ -595,54 +687,62 @@ class TChnAliasItem {
595
687
  * @classdesc This class implements an interface of the channel items list
596
688
  */
597
689
  class TChnAliasList {
598
- /** @property {TChnAliasItem[]} */
690
+ /** @type {TChnAliasItem[]} */
599
691
  #_items;
600
692
 
601
693
  /**
602
- * @description Creates an instance of the channel items list
694
+ * Creates an instance of the channel items list
603
695
  */
604
696
  constructor() {
605
697
  this.#_items = [];
606
698
  }
607
699
 
608
- /**
609
- * @property {number} - contains a quantity of the elements
610
- * @readonly
611
- */
612
- get count() {
613
- return this.#_items.length;
700
+ [Symbol.iterator]() {
701
+ let index = 0;
702
+ return {
703
+ next: () => {
704
+ if (index < this.count) {
705
+ return { done: false, value: this.getItem(index++) };
706
+ } else {
707
+ return { done: true };
708
+ };
709
+ },
710
+ return() {
711
+ return { done: true };
712
+ },
713
+ };
614
714
  }
615
715
 
616
716
  /**
617
- * @property {boolean} - defines if the instance has any items
717
+ * Contains a quantity of the elements
718
+ * @type {number}
618
719
  * @readonly
619
- * @deprecated
620
720
  */
621
- get hasItems() {
622
- return this.#_items.length > 0;
721
+ get count() {
722
+ return this.#_items.length;
623
723
  }
624
724
 
625
725
  /**
726
+ * Returns a flag whether a list is empty or not
626
727
  * @returns {boolean}
627
- * @description Returns a flag whether a list is empty or not
628
728
  */
629
729
  isEmpty() {
630
730
  return this.count === 0;
631
731
  }
632
732
 
633
733
  /**
734
+ * Returns a flag whether a list has any members
634
735
  * @returns {boolean}
635
- * @description Returns a flag whether a list has any members
636
736
  */
637
737
  isNotEmpty() {
638
738
  return this.count > 0;
639
739
  }
640
740
 
641
741
  /**
742
+ * Checks whether a given value is an index and fits an index range
743
+ * within the instance
642
744
  * @param {any} value - a value to be verified
643
745
  * @returns {boolean}
644
- * @description Checks whether a given value is an index and fits
645
- * an Index range within the instance
646
746
  */
647
747
  chkIndex(value) {
648
748
  const index = valueToIndex(value);
@@ -650,41 +750,42 @@ class TChnAliasList {
650
750
  }
651
751
 
652
752
  /**
753
+ * Searches an index of an element by its ID
653
754
  * @param {string} value - an element ID
654
755
  * @returns {number}
655
- * @description Searches an index of an element by its ID
756
+ * @see valueToIDString
656
757
  */
657
758
  searchIndexByID(value) {
658
759
  const id = valueToIDString(value);
659
760
  let index = -1;
660
- if (id !== null && this.hasItems) {
761
+ if (id !== null && this.isNotEmpty()) {
661
762
  index = this.#_items.findIndex((item) => id === item.id);
662
763
  };
663
764
  return index;
664
765
  }
665
766
 
666
767
  /**
768
+ * Returns an alias element by its index
667
769
  * @param {number} value - an element index
668
770
  * @returns {?TChnAliasItem}
669
- * @description Returns an alias element by its index
670
771
  */
671
772
  getItem(value) {
672
773
  return this.chkIndex(value) ? this.#_items[Number(value)] : null;
673
774
  }
674
775
 
675
776
  /**
777
+ * Returns an alias element by its ID
676
778
  * @param {string} value - an element ID
677
779
  * @returns {?TChnAliasItem}
678
- * @description Returns an alias element by its ID
679
780
  */
680
781
  getItemByID(value) {
681
782
  return this.getItem(this.searchIndexByID(value));
682
783
  }
683
784
 
684
785
  /**
786
+ * Adds an alias element
685
787
  * @param {object} obj - an element to be added
686
788
  * @returns {number}
687
- * @description Adds an alias element
688
789
  */
689
790
  addItem(obj) {
690
791
  const item = TChnAliasItem.create(obj);
@@ -703,9 +804,9 @@ class TChnAliasList {
703
804
  }
704
805
 
705
806
  /**
807
+ * Tries to delete an element addressed by a given index
706
808
  * @param {number} value - an element index
707
809
  * @returns {boolean}
708
- * @description Tries to delete an element addressed by a given index
709
810
  */
710
811
  delItem(value) {
711
812
  let isSucceed = this.chkIndex(value)
@@ -714,10 +815,10 @@ class TChnAliasList {
714
815
  }
715
816
 
716
817
  /**
818
+ * Loads a list of a new alias elements
717
819
  * @param {array} list - a list of an elements
718
820
  * @param {boolean} [opt=true] - defines whether to clear the list before load a new one
719
821
  * @returns {number}
720
- * @description Loads a list of a new alias elements
721
822
  */
722
823
  loadItems(list, opt) {
723
824
  const useClear = typeof opt === 'boolean' ? opt : true;
@@ -730,22 +831,31 @@ class TChnAliasList {
730
831
  };
731
832
 
732
833
  /**
834
+ * Removes all of the instance members
733
835
  * @returns {void}
734
- * @description removes all of the instance members
735
836
  */
736
837
  clear() {
737
838
  this.#_items.length = 0;
738
839
  }
739
840
 
740
841
  /**
842
+ * Calls given function for each alias element
741
843
  * @param {forEachProcEx} cb - a callback function
742
844
  * @returns {void}
743
- * @description Calls given function for each alias element
744
845
  */
745
846
  forEach(cb) {
746
847
  if (typeof cb === 'function') this.#_items.forEach(cb);
747
848
  }
748
849
 
850
+ /**
851
+ * Returns an array of a name records picked up by a given function
852
+ * @param {cbArrECheck} cb - a callback function
853
+ * @returns {TChnAliasItem[]}
854
+ */
855
+ filter(cb) {
856
+ return typeof cb === 'function' ? this.#_items.filter(cb) : [];
857
+ }
858
+
749
859
  };
750
860
 
751
861
  // === 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.6",
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
- "@ygracs/bsfoc-lib-js": "^0.2.2"
28
+ "@cntwg/file-helper": "^0.0.1",
29
+ "@ygracs/bsfoc-lib-js": "^0.3.0"
30
30
  },
31
31
  "devDependencies": {
32
- "jest": "^29.7.0",
33
- "jsdoc-to-markdown": "^9.1.1",
32
+ "jest": "^30.0.5",
33
+ "jsdoc-to-markdown": "^9.1.2",
34
34
  "minimist": "^1.2.8"
35
35
  }
36
36
  }
@@ -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;