@ygracs/chn-alias-list 0.0.7 → 0.0.9

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.
@@ -1,165 +1,41 @@
1
- // [v0.1.050-20260130]
1
+ // [v0.1.058-20260628]
2
2
 
3
3
  // === module init block ===
4
4
 
5
5
  const {
6
6
  isArray, isPlainObject,
7
- valueToArray, valueToIndex, valueToIDString,
7
+ valueToIndex, valueToIDString,
8
8
  readAsString,
9
9
  } = require('@ygracs/bsfoc-lib-js');
10
10
 
11
- // === module inner block ===
12
-
13
- /***
14
- * (* helper function definitions *)
15
- */
11
+ const {
12
+ TChnNameRecord, TChnNamesList,
13
+ // * import types definitions *
14
+ ILangTextPair,
15
+ ILoadListItemsOptions,
16
+ } = require('./chn-names');
16
17
 
17
- /**
18
- * Converts a <Lang, Text> key-pair to a string
19
- * @function convertLangTextValueToString
20
- * @param {array} data - contains a data to be printed
21
- * @param {object} [opt]
22
- * @returns {string}
23
- * @inner
24
- */
25
- function convertLangTextValueToString(data, opt) {
26
- let name = [ '', '' ];
27
- let result = '';
28
- if (isArray(data)) name = data;
29
- let [ lang, text ] = name;
30
- if (lang === '') {
31
- result = text;
32
- } else if (text !== '') {
33
- result = `(${lang}): ${text}`;
34
- };
35
- return result;
36
- };
18
+ // === module inner block ===
37
19
 
38
20
  // === module main block ===
39
21
 
40
- /***
41
- * (* constant definitions *)
42
- */
43
-
44
22
  /**
45
23
  * Defines an object for storing a channel data
46
24
  * @typedef {Object} IChannelRecord
47
25
  * @property {string} id - channel ID
48
26
  * @property {string} alias - target ID
49
- * @property {any[]} name - list of a channel names
27
+ * @property {ILangTextPair[]} name - list of a channel names
50
28
  * @property {string} status - channel status
51
29
  */
52
-
53
- /***
54
- * (* function definitions *)
55
- */
56
-
57
- /***
58
- * (* class definitions *)
59
- */
60
-
61
30
  /**
62
- * @classdesc This class implements an interface of the name record
31
+ * A virtual constant meant for support jsdoc notation:
32
+ * @type {IChannelRecord}
63
33
  */
64
- class TChnNameRecord {
65
- /** @type {string} */
66
- #_lang;
67
- /** @type {string} */
68
- #_value;
69
-
70
- /**
71
- * Creates an instance of the name record
72
- */
73
- constructor() {
74
- this.reset();
75
- }
76
-
77
- /**
78
- * Contains a language
79
- * @type {string}
80
- * @readonly
81
- */
82
- get lang() {
83
- return this.#_lang;
84
- }
85
-
86
- /**
87
- * Contains a value of the record
88
- * @type {string}
89
- * @readonly
90
- */
91
- get text() {
92
- return this.#_value;
93
- }
94
-
95
- /**
96
- * Contains a value of the record
97
- * @type {string[]}
98
- */
99
- get value() {
100
- return [ this.#_lang, this.#_value ];
101
- }
102
-
103
- set value(data) {
104
- this.setValue(data);
105
- }
106
-
107
- /**
108
- * Sets the record value
109
- * @param {any} data - a value of the record
110
- * @returns {boolean}
111
- */
112
- setValue(data) {
113
- let _data = data;
114
- let isSucceed = false;
115
- if (_data !== undefined) {
116
- if (isPlainObject(_data)) {
117
- let { lang, text } = _data;
118
- _data = [ lang, text ];
119
- } else {
120
- _data = valueToArray(_data);
121
- };
122
- switch (_data.length) {
123
- case 0: {
124
- break;
125
- }
126
- case 1: {
127
- _data = [ '', _data[0] ];
128
- }
129
- default: {
130
- let [ lang, value = null ] = _data;
131
- if (value !== null && typeof value !== 'boolean') {
132
- value = readAsString(value, { numberToString: true });
133
- lang = value !== '' ? valueToIDString(lang) : null;
134
- if (lang === null) lang = '';
135
- this.#_lang = lang;
136
- this.#_value = value;
137
- isSucceed = true;
138
- };
139
- }
140
- };
141
- };
142
- return isSucceed;
143
- }
144
-
145
- /**
146
- * Returns value as a formated string
147
- * @param {any} opt - <reserved>
148
- * @returns {string}
149
- */
150
- toFormatString(opt) {
151
- return convertLangTextValueToString(this.value, opt);
152
- }
153
-
154
- /**
155
- * Clears the record
156
- * @returns {void}
157
- */
158
- reset() {
159
- this.#_lang = '';
160
- this.#_value = '';
161
- }
162
-
34
+ module.exports.IChannelRecord = {
35
+ id: '',
36
+ alias: '',
37
+ name: [],
38
+ status: '',
163
39
  };
164
40
 
165
41
  /**
@@ -180,218 +56,6 @@ class TChnNameRecord {
180
56
  * @returns {any}
181
57
  */
182
58
 
183
- /**
184
- * @classdesc This class implements an interface of the name records list
185
- */
186
- class TChnNamesList {
187
- /** @type {TChnNameRecord[]} */
188
- #_items;
189
-
190
- /**
191
- * Creates an instance of the name records list
192
- */
193
- constructor() {
194
- this.#_items = [];
195
- }
196
-
197
- [Symbol.iterator]() {
198
- let index = 0;
199
- return {
200
- next: () => {
201
- if (index < this.count) {
202
- return { done: false, value: this.getItem(index++) };
203
- } else {
204
- return { done: true };
205
- };
206
- },
207
- return() {
208
- return { done: true };
209
- },
210
- };
211
- }
212
-
213
- /**
214
- * Contains a quantity of a name records
215
- * @type {number}
216
- * @readonly
217
- */
218
- get count() {
219
- return this.#_items.length;
220
- }
221
-
222
- /**
223
- * Contains a list of a name records
224
- * @type {TChnNameRecord[]}
225
- * @readonly
226
- */
227
- get value() {
228
- const result = [];
229
- this.#_items.forEach((item, i) => {
230
- result.push(item.value);
231
- });
232
- return result;
233
- }
234
-
235
- /**
236
- * Returns a flag whether a list is empty or not
237
- * @returns {boolean}
238
- */
239
- isEmpty() {
240
- return this.count === 0;
241
- }
242
-
243
- /**
244
- * Returns a flag whether a list has any members
245
- * @returns {boolean}
246
- */
247
- isNotEmpty() {
248
- return this.count > 0;
249
- }
250
-
251
- /**
252
- * Checks whether a given value is an index and fits an index range
253
- * within the instance
254
- * @param {any} value - a value to be verified
255
- * @returns {boolean}
256
- */
257
- chkIndex(value) {
258
- const index = valueToIndex(value);
259
- return index !== -1 && index < this.count;
260
- }
261
-
262
- /**
263
- * Searches an index of a given name
264
- * @param {string} value - a name
265
- * @returns {number}
266
- */
267
- getIndex(value) {
268
- const opt = { numberToString: true };
269
- const name = readAsString(value, opt);
270
- let index = -1;
271
- if (name !== '') {
272
- index = this.#_items.findIndex((item) => name === item.text);
273
- };
274
- return index;
275
- }
276
-
277
- /**
278
- * @deprecated
279
- * @see TChnNamesList.getItem
280
- * @todo [from v0.0.5] make obsolete
281
- */
282
- getName(value) {
283
- return this.getItem(value);
284
- }
285
-
286
- /**
287
- * Returns a name record
288
- * @since 0.0.5
289
- * @param {number} value - an element index
290
- * @returns {?TChnNameRecord}
291
- */
292
- getItem(value) {
293
- return this.chkIndex(value) ? this.#_items[Number(value)] : null;
294
- }
295
-
296
- /**
297
- * @deprecated
298
- * @see TChnNamesList.addItem
299
- * @todo [from v0.0.5] make obsolete
300
- */
301
- addName(data) {
302
- return this.addItem(data);
303
- }
304
-
305
- /**
306
- * Adds a new name record to a list members
307
- * @since 0.0.5
308
- * @param {any} data - a value of a name record
309
- * @returns {boolean}
310
- */
311
- addItem(data) {
312
- const _data = data instanceof TChnNameRecord ? data.value : data;
313
- const item = new TChnNameRecord();
314
- const isSucceed = item.setValue(_data) && item.text !== '';
315
- if (isSucceed) this.#_items.push(item);
316
- return isSucceed;
317
- }
318
-
319
- /**
320
- * @deprecated
321
- * @see TChnNamesList.delItem
322
- * @todo [from v0.0.5] make obsolete
323
- */
324
- delName(value) {
325
- return this.delItem(value);
326
- }
327
-
328
- /**
329
- * Tries to delete a name record addressed by a given index
330
- * @since 0.0.5
331
- * @param {number} value - an element index
332
- * @returns {boolean}
333
- */
334
- delItem(value) {
335
- let isSucceed = this.chkIndex(value)
336
- if (isSucceed) this.#_items.splice(Number(value), 1);
337
- return isSucceed;
338
- }
339
-
340
- /**
341
- * @deprecated
342
- * @see TChnNamesList.loadItems
343
- * @todo [from v0.0.5] make obsolete
344
- */
345
- loadNames(...args) {
346
- return this.loadItems(...args);
347
- }
348
-
349
- /**
350
- * Loads a new name records
351
- * @since 0.0.5
352
- * @param {any} list - an element or a list of an elements
353
- * @param {boolean} [opt=true] - defines whether to clear the list before load a new one
354
- * @returns {number}
355
- */
356
- loadItems(list, opt) {
357
- const useClear = typeof opt === 'boolean' ? opt : true;
358
- const items = valueToArray(list);
359
- let count = 0;
360
- if (items.length) {
361
- if (useClear) this.clear();
362
- items.forEach((item) => { if (this.addItem(item)) count++; });
363
- };
364
- return count;
365
- };
366
-
367
- /**
368
- * Removes all of the instance members
369
- * @returns {void}
370
- */
371
- clear() {
372
- this.#_items.length = 0;
373
- }
374
-
375
- /**
376
- * Calls given function for each name record
377
- * @param {forEachProcEx} cb - a callback function
378
- * @returns {void}
379
- */
380
- forEach(cb) {
381
- if (typeof cb === 'function') this.#_items.forEach(cb);
382
- }
383
-
384
- /**
385
- * Returns an array of a name records picked up by a given function
386
- * @param {cbArrECheck} cb - a callback function
387
- * @returns {TChnNameRecord[]}
388
- */
389
- filter(cb) {
390
- return typeof cb === 'function' ? this.#_items.filter(cb) : [];
391
- }
392
-
393
- };
394
-
395
59
  /**
396
60
  * @classdesc This class implements an interface of the channel item
397
61
  */
@@ -519,9 +183,15 @@ class TChnAliasItem {
519
183
 
520
184
  /**
521
185
  * Loads a list of a new name records
522
- * @param {array} list - a list of an elements
186
+ * @callback loadItemsFn
187
+ * @param {any} list - a list of an elements
523
188
  * @param {boolean} [opt=true] - defines whether to clear the list before load a new one
524
189
  * @returns {number}
190
+ * @see TChnNamesList.loadItems
191
+ */
192
+ /**
193
+ * Loads a list of a new name records
194
+ * @type {loadItemsFn}
525
195
  * @deprecated
526
196
  * @see TChnNamesList.loadItems
527
197
  * @todo [from v0.0.7] deprecated. Use `TChnNamesList.loadItems` instead.
@@ -624,7 +294,7 @@ class TChnAliasItem {
624
294
  let { alias, name, status } = obj;
625
295
  if (obj instanceof TChnAliasItem) name = obj.names.value;
626
296
  item.setAlias(alias);
627
- item.loadNames(name);
297
+ item.names.loadItems(name);
628
298
  if (typeof isDisabled === 'boolean') {
629
299
  // // TODO: process "status"
630
300
  if (isDisabled) item.disable();
@@ -638,6 +308,7 @@ class TChnAliasItem {
638
308
  }
639
309
 
640
310
  };
311
+ module.exports.TChnAliasItem = TChnAliasItem;
641
312
 
642
313
  /**
643
314
  * @classdesc This class implements an interface of the channel items list
@@ -699,12 +370,38 @@ class TChnAliasList {
699
370
  * within the instance
700
371
  * @param {any} value - a value to be verified
701
372
  * @returns {boolean}
373
+ * @deprecated
374
+ * @todo \[since v0.0.9] deprecated. Use {@link checkIndex} instead.
702
375
  */
703
376
  chkIndex(value) {
704
377
  const index = valueToIndex(value);
705
378
  return index !== -1 && index < this.count;
706
379
  }
707
380
 
381
+ /**
382
+ * Checks whether a given value is an index and fits an index range
383
+ * within the instance
384
+ * @since 0.0.9
385
+ * @param {any} value - a value to be verified
386
+ * @returns {boolean}
387
+ */
388
+ checkIndex(value) {
389
+ const index = valueToIndex(value);
390
+ return index !== -1 && index < this.count;
391
+ }
392
+
393
+ /**
394
+ * Returns an index in case a given value is a valid index value and not exceeds
395
+ * an index range within the instance. If failed a `-1` returned
396
+ * @since 0.0.9
397
+ * @param {any} value - value to evaluate
398
+ * @returns {number}
399
+ */
400
+ tryIndex(value) {
401
+ const index = valueToIndex(value);
402
+ return index !== -1 && index < this.count ? index : -1;
403
+ }
404
+
708
405
  /**
709
406
  * Searches an index of an element by its ID
710
407
  * @param {string} value - an element ID
@@ -726,7 +423,8 @@ class TChnAliasList {
726
423
  * @returns {?TChnAliasItem}
727
424
  */
728
425
  getItem(value) {
729
- return this.chkIndex(value) ? this.#_items[Number(value)] : null;
426
+ const i = this.tryIndex(value);
427
+ return i === -1 ? null : this.#_items[i];
730
428
  }
731
429
 
732
430
  /**
@@ -765,19 +463,27 @@ class TChnAliasList {
765
463
  * @returns {boolean}
766
464
  */
767
465
  delItem(value) {
768
- let isSucceed = this.chkIndex(value)
769
- if (isSucceed) this.#_items.splice(Number(value), 1);
466
+ const i = this.tryIndex(value);
467
+ let isSucceed = i !== -1;
468
+ if (isSucceed) this.#_items.splice(i, 1);
770
469
  return isSucceed;
771
470
  }
772
471
 
472
+ /**
473
+ * Defines whether to clear the list before load
474
+ * @typedef {boolean} loadListItemsOption
475
+ */
773
476
  /**
774
477
  * Loads a list of a new alias elements
775
478
  * @param {IChannelRecord[]} list - a list of an elements
776
- * @param {boolean} [opt=true] - defines whether to clear the list before load a new one
479
+ * @param {loadListItemsOption|ILoadListItemsOptions} [opt] - load options
777
480
  * @returns {number}
778
481
  */
779
482
  loadItems(list, opt) {
780
- const useClear = typeof opt === 'boolean' ? opt : true;
483
+ let {
484
+ useClear = true,
485
+ } = isPlainObject(opt) ? opt : { useClear: opt };
486
+ if (typeof useClear !== 'boolean') useClear = true;
781
487
  let count = 0;
782
488
  if (isArray(list)) {
783
489
  if (list.length > 0 && useClear) this.clear();
@@ -813,10 +519,4 @@ class TChnAliasList {
813
519
  }
814
520
 
815
521
  };
816
-
817
- // === module exports block ===
818
-
819
- module.exports.TChnNameRecord = TChnNameRecord;
820
- module.exports.TChnNamesList = TChnNamesList;
821
- module.exports.TChnAliasItem = TChnAliasItem;
822
522
  module.exports.TChnAliasList = TChnAliasList;
@@ -0,0 +1,146 @@
1
+ /**
2
+ * An array of two elements that represents a `<lang>`-`<text>` pair
3
+ */
4
+ export type ILangTextPair = string[];
5
+ export const ILangTextPair: ILangTextPair;
6
+ /**
7
+ * A settings for the list load ops
8
+ */
9
+ export type ILoadListItemsOptions = {
10
+ /**
11
+ * - indicates whether to clear the list
12
+ * before load
13
+ */
14
+ useClear?: boolean;
15
+ };
16
+ export const ILoadListItemsOptions: ILoadListItemsOptions;
17
+ /**
18
+ * A user defined procedure to process an array elements
19
+ */
20
+ export type forEachProcEx = (item: any, index?: number, arr?: any[]) => void;
21
+ /**
22
+ * A user defined procedure to process an array elements
23
+ */
24
+ export type cbArrECheck = (item: any, index?: number, arr?: any[]) => any;
25
+
26
+ /**
27
+ * @classdesc This class implements an interface of the name record
28
+ */
29
+ export class TChnNameRecord {
30
+ /**
31
+ * Contains a language
32
+ */
33
+ get lang(): string;
34
+ /**
35
+ * Contains a value of the record
36
+ */
37
+ get text(): string;
38
+ /**
39
+ * Contains a value of the record
40
+ */
41
+ get value(): ILangTextPair;
42
+ set value(data: ILangTextPair);
43
+ /**
44
+ * Sets the record value
45
+ */
46
+ setValue(data: any): boolean;
47
+ /**
48
+ * Returns value as a formated string
49
+ */
50
+ toFormatString(opt: any): string;
51
+ /**
52
+ * Clears the record
53
+ */
54
+ reset(): void;
55
+ #private;
56
+ }
57
+
58
+ /**
59
+ * @classdesc This class implements an interface of the name records list
60
+ */
61
+ export class TChnNamesList {
62
+ /**
63
+ * Contains a quantity of a name records
64
+ */
65
+ get count(): number;
66
+ /**
67
+ * Contains a list of a name entries
68
+ */
69
+ get value(): ILangTextPair[];
70
+ /**
71
+ * Returns a flag whether a list is empty or not
72
+ */
73
+ isEmpty(): boolean;
74
+ /**
75
+ * Returns a flag whether a list has any members
76
+ */
77
+ isNotEmpty(): boolean;
78
+ /**
79
+ * Checks whether a given value is an index and fits an index range
80
+ * within the instance
81
+ * @deprecated
82
+ * @todo \[since v0.0.9] deprecated. Use {@link checkIndex} instead.
83
+ */
84
+ chkIndex(value: any): boolean;
85
+ /**
86
+ * Checks whether a given value is an index and fits an index range
87
+ * within the instance
88
+ * @since 0.0.9
89
+ */
90
+ checkIndex(value: any): boolean;
91
+ /**
92
+ * Returns an index in case a given value is a valid index value and not exceeds
93
+ * an index range within the instance. If failed a `-1` returned
94
+ * @since 0.0.9
95
+ */
96
+ tryIndex(value: any): number;
97
+ /**
98
+ * Searches an index of a given name
99
+ */
100
+ getIndex(value: string): number;
101
+ /**
102
+ * Returns a name record
103
+ * @since 0.0.5
104
+ */
105
+ getItem(value: number): TChnNameRecord | null;
106
+ /**
107
+ * Adds a new name record to a list members
108
+ * @since 0.0.5
109
+ */
110
+ addItem(data: any): boolean;
111
+ /**
112
+ * Tries to delete a name record addressed by a given index
113
+ * @since 0.0.5
114
+ */
115
+ delItem(value: number): boolean;
116
+ /**
117
+ * Loads a new name records
118
+ * @since 0.0.5
119
+ */
120
+ loadItems(list: any, opt?: boolean | ILoadListItemsOptions): number;
121
+ /**
122
+ * Removes all of the instance members
123
+ */
124
+ clear(): void;
125
+ /**
126
+ * Calls given function for each name record
127
+ */
128
+ forEach(cb: forEachProcEx): void;
129
+ /**
130
+ * Returns an array of a name records picked up by a given function
131
+ */
132
+ filter(cb: cbArrECheck): TChnNameRecord[];
133
+ [Symbol.iterator](): {
134
+ next: () => {
135
+ done: boolean;
136
+ value: TChnNameRecord | null;
137
+ } | {
138
+ done: boolean;
139
+ value?: undefined;
140
+ };
141
+ return(): {
142
+ done: boolean;
143
+ };
144
+ };
145
+ #private;
146
+ }