@ygracs/chn-alias-list 0.0.8 → 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.
@@ -13,12 +13,13 @@ export type IChannelRecord = {
13
13
  /**
14
14
  * - list of a channel names
15
15
  */
16
- name: any[];
16
+ name: ILangTextPair[];
17
17
  /**
18
18
  * - channel status
19
19
  */
20
20
  status: string;
21
21
  };
22
+ export const IChannelRecord: IChannelRecord;
22
23
  /**
23
24
  * A user defined procedure to process an array elements
24
25
  */
@@ -27,115 +28,9 @@ export type forEachProcEx = (item: any, index?: number, arr?: any[]) => void;
27
28
  * A user defined procedure to process an array elements
28
29
  */
29
30
  export type cbArrECheck = (item: any, index?: number, arr?: any[]) => any;
31
+ import { ILangTextPair } from "./chn-names";
30
32
 
31
- /**
32
- * @classdesc This class implements an interface of the name record
33
- */
34
- export class TChnNameRecord {
35
- /**
36
- * Contains a language
37
- */
38
- get lang(): string;
39
- /**
40
- * Contains a value of the record
41
- */
42
- get text(): string;
43
- /**
44
- * Contains a value of the record
45
- */
46
- get value(): string[];
47
- set value(data: string[]);
48
- /**
49
- * Sets the record value
50
- */
51
- setValue(data: any): boolean;
52
- /**
53
- * Returns value as a formated string
54
- */
55
- toFormatString(opt: any): string;
56
- /**
57
- * Clears the record
58
- */
59
- reset(): void;
60
- #private;
61
- }
62
-
63
- /**
64
- * @classdesc This class implements an interface of the name records list
65
- */
66
- export class TChnNamesList {
67
- /**
68
- * Contains a quantity of a name records
69
- */
70
- get count(): number;
71
- /**
72
- * Contains a list of a name records
73
- */
74
- get value(): TChnNameRecord[];
75
- /**
76
- * Returns a flag whether a list is empty or not
77
- */
78
- isEmpty(): boolean;
79
- /**
80
- * Returns a flag whether a list has any members
81
- */
82
- isNotEmpty(): boolean;
83
- /**
84
- * Checks whether a given value is an index and fits an index range
85
- * within the instance
86
- */
87
- chkIndex(value: any): boolean;
88
- /**
89
- * Searches an index of a given name
90
- */
91
- getIndex(value: string): number;
92
- /**
93
- * Returns a name record
94
- * @since 0.0.5
95
- */
96
- getItem(value: number): TChnNameRecord | null;
97
- /**
98
- * Adds a new name record to a list members
99
- * @since 0.0.5
100
- */
101
- addItem(data: any): boolean;
102
- /**
103
- * Tries to delete a name record addressed by a given index
104
- * @since 0.0.5
105
- */
106
- delItem(value: number): boolean;
107
- /**
108
- * Loads a new name records
109
- * @since 0.0.5
110
- */
111
- loadItems(list: any, opt?: boolean): number;
112
- /**
113
- * Removes all of the instance members
114
- */
115
- clear(): void;
116
- /**
117
- * Calls given function for each name record
118
- */
119
- forEach(cb: forEachProcEx): void;
120
- /**
121
- * Returns an array of a name records picked up by a given function
122
- */
123
- filter(cb: cbArrECheck): TChnNameRecord[];
124
- [Symbol.iterator](): {
125
- next: () => {
126
- done: boolean;
127
- value: TChnNameRecord | null;
128
- } | {
129
- done: boolean;
130
- value?: undefined;
131
- };
132
- return(): {
133
- done: boolean;
134
- };
135
- };
136
- #private;
137
- }
138
-
33
+ type loadItemsParams = Parameters<TChnNamesList["loadItems"]>;
139
34
  /**
140
35
  * @classdesc This class implements an interface of the channel item
141
36
  */
@@ -192,7 +87,7 @@ export class TChnAliasItem {
192
87
  * @see TChnNamesList.loadItems
193
88
  * @todo [from v0.0.7] deprecated. Use `TChnNamesList.loadItems` instead.
194
89
  */
195
- loadNames(...args: [list: any, opt?: boolean]): number;
90
+ loadNames(...args: loadItemsParams): number;
196
91
  /**
197
92
  * Sets item status to enabled
198
93
  */
@@ -239,8 +134,22 @@ export class TChnAliasList {
239
134
  /**
240
135
  * Checks whether a given value is an index and fits an index range
241
136
  * within the instance
137
+ * @deprecated
138
+ * @todo \[since v0.0.9] deprecated. Use {@link checkIndex} instead.
242
139
  */
243
140
  chkIndex(value: any): boolean;
141
+ /**
142
+ * Checks whether a given value is an index and fits an index range
143
+ * within the instance
144
+ * @since 0.0.9
145
+ */
146
+ checkIndex(value: any): boolean;
147
+ /**
148
+ * Returns an index in case a given value is a valid index value and not exceeds
149
+ * an index range within the instance. If failed a `-1` returned
150
+ * @since 0.0.9
151
+ */
152
+ tryIndex(value: any): number;
244
153
  /**
245
154
  * Searches an index of an element by its ID
246
155
  * @see valueToIDString
@@ -265,7 +174,7 @@ export class TChnAliasList {
265
174
  /**
266
175
  * Loads a list of a new alias elements
267
176
  */
268
- loadItems(list: IChannelRecord[], opt?: boolean): number;
177
+ loadItems(list: IChannelRecord[], opt?: boolean | ILoadListItemsOptions): number;
269
178
  /**
270
179
  * Removes all of the instance members
271
180
  */
@@ -292,3 +201,6 @@ export class TChnAliasList {
292
201
  };
293
202
  #private;
294
203
  }
204
+ import { TChnNamesList } from "./chn-names";
205
+ import { TChnNameRecord } from "./chn-names";
206
+ import { ILoadListItemsOptions } from "./chn-names";
@@ -1,35 +1,21 @@
1
- // [v0.1.053-20260308]
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 ===
11
+ const {
12
+ TChnNameRecord, TChnNamesList,
13
+ // * import types definitions *
14
+ ILangTextPair,
15
+ ILoadListItemsOptions,
16
+ } = require('./chn-names');
12
17
 
13
- /**
14
- * Converts a <Lang, Text> key-pair to a string
15
- * @function convertLangTextValueToString
16
- * @param {array} data - contains a data to be printed
17
- * @param {object} [opt]
18
- * @returns {string}
19
- * @inner
20
- */
21
- function convertLangTextValueToString(data, opt) {
22
- let name = [ '', '' ];
23
- let result = '';
24
- if (isArray(data)) name = data;
25
- let [ lang, text ] = name;
26
- if (lang === '') {
27
- result = text;
28
- } else if (text !== '') {
29
- result = `(${lang}): ${text}`;
30
- };
31
- return result;
32
- };
18
+ // === module inner block ===
33
19
 
34
20
  // === module main block ===
35
21
 
@@ -38,114 +24,19 @@ function convertLangTextValueToString(data, opt) {
38
24
  * @typedef {Object} IChannelRecord
39
25
  * @property {string} id - channel ID
40
26
  * @property {string} alias - target ID
41
- * @property {any[]} name - list of a channel names
27
+ * @property {ILangTextPair[]} name - list of a channel names
42
28
  * @property {string} status - channel status
43
29
  */
44
-
45
30
  /**
46
- * @classdesc This class implements an interface of the name record
31
+ * A virtual constant meant for support jsdoc notation:
32
+ * @type {IChannelRecord}
47
33
  */
48
- class TChnNameRecord {
49
- /** @type {string} */
50
- #_lang;
51
- /** @type {string} */
52
- #_value;
53
-
54
- /**
55
- * Creates an instance of the name record
56
- */
57
- constructor() {
58
- this.reset();
59
- }
60
-
61
- /**
62
- * Contains a language
63
- * @type {string}
64
- * @readonly
65
- */
66
- get lang() {
67
- return this.#_lang;
68
- }
69
-
70
- /**
71
- * Contains a value of the record
72
- * @type {string}
73
- * @readonly
74
- */
75
- get text() {
76
- return this.#_value;
77
- }
78
-
79
- /**
80
- * Contains a value of the record
81
- * @type {string[]}
82
- */
83
- get value() {
84
- return [ this.#_lang, this.#_value ];
85
- }
86
-
87
- set value(data) {
88
- this.setValue(data);
89
- }
90
-
91
- /**
92
- * Sets the record value
93
- * @param {any} data - a value of the record
94
- * @returns {boolean}
95
- */
96
- setValue(data) {
97
- let _data = data;
98
- let isSucceed = false;
99
- if (_data !== undefined) {
100
- if (isPlainObject(_data)) {
101
- let { lang, text } = _data;
102
- _data = [ lang, text ];
103
- } else {
104
- _data = valueToArray(_data);
105
- };
106
- switch (_data.length) {
107
- case 0: {
108
- break;
109
- }
110
- case 1: {
111
- _data = [ '', _data[0] ];
112
- }
113
- default: {
114
- let [ lang, value = null ] = _data;
115
- if (value !== null && typeof value !== 'boolean') {
116
- value = readAsString(value, { numberToString: true });
117
- lang = value !== '' ? valueToIDString(lang) : null;
118
- if (lang === null) lang = '';
119
- this.#_lang = lang;
120
- this.#_value = value;
121
- isSucceed = true;
122
- };
123
- }
124
- };
125
- };
126
- return isSucceed;
127
- }
128
-
129
- /**
130
- * Returns value as a formated string
131
- * @param {any} opt - <reserved>
132
- * @returns {string}
133
- */
134
- toFormatString(opt) {
135
- return convertLangTextValueToString(this.value, opt);
136
- }
137
-
138
- /**
139
- * Clears the record
140
- * @returns {void}
141
- */
142
- reset() {
143
- this.#_lang = '';
144
- this.#_value = '';
145
- }
146
-
34
+ module.exports.IChannelRecord = {
35
+ id: '',
36
+ alias: '',
37
+ name: [],
38
+ status: '',
147
39
  };
148
- module.exports.TChnNameRecord = TChnNameRecord;
149
40
 
150
41
  /**
151
42
  * A user defined procedure to process an array elements
@@ -165,183 +56,6 @@ module.exports.TChnNameRecord = TChnNameRecord;
165
56
  * @returns {any}
166
57
  */
167
58
 
168
- /**
169
- * @classdesc This class implements an interface of the name records list
170
- */
171
- class TChnNamesList {
172
- /** @type {TChnNameRecord[]} */
173
- #_items;
174
-
175
- /**
176
- * Creates an instance of the name records list
177
- */
178
- constructor() {
179
- this.#_items = [];
180
- }
181
-
182
- [Symbol.iterator]() {
183
- let index = 0;
184
- return {
185
- next: () => {
186
- if (index < this.count) {
187
- return { done: false, value: this.getItem(index++) };
188
- } else {
189
- return { done: true };
190
- };
191
- },
192
- return() {
193
- return { done: true };
194
- },
195
- };
196
- }
197
-
198
- /**
199
- * Contains a quantity of a name records
200
- * @type {number}
201
- * @readonly
202
- */
203
- get count() {
204
- return this.#_items.length;
205
- }
206
-
207
- /**
208
- * Contains a list of a name records
209
- * @type {TChnNameRecord[]}
210
- * @readonly
211
- */
212
- get value() {
213
- const result = [];
214
- this.#_items.forEach((item, i) => {
215
- result.push(item.value);
216
- });
217
- return result;
218
- }
219
-
220
- /**
221
- * Returns a flag whether a list is empty or not
222
- * @returns {boolean}
223
- */
224
- isEmpty() {
225
- return this.count === 0;
226
- }
227
-
228
- /**
229
- * Returns a flag whether a list has any members
230
- * @returns {boolean}
231
- */
232
- isNotEmpty() {
233
- return this.count > 0;
234
- }
235
-
236
- /**
237
- * Checks whether a given value is an index and fits an index range
238
- * within the instance
239
- * @param {any} value - a value to be verified
240
- * @returns {boolean}
241
- */
242
- chkIndex(value) {
243
- const index = valueToIndex(value);
244
- return index !== -1 && index < this.count;
245
- }
246
-
247
- /**
248
- * Searches an index of a given name
249
- * @param {string} value - a name
250
- * @returns {number}
251
- */
252
- getIndex(value) {
253
- const opt = { numberToString: true };
254
- const name = readAsString(value, opt);
255
- let index = -1;
256
- if (name !== '') {
257
- index = this.#_items.findIndex((item) => name === item.text);
258
- };
259
- return index;
260
- }
261
-
262
- /**
263
- * Returns a name record
264
- * @since 0.0.5
265
- * @param {number} value - an element index
266
- * @returns {?TChnNameRecord}
267
- */
268
- getItem(value) {
269
- return this.chkIndex(value) ? this.#_items[Number(value)] : null;
270
- }
271
-
272
- /**
273
- * Adds a new name record to a list members
274
- * @since 0.0.5
275
- * @param {any} data - a value of a name record
276
- * @returns {boolean}
277
- */
278
- addItem(data) {
279
- const _data = data instanceof TChnNameRecord ? data.value : data;
280
- const item = new TChnNameRecord();
281
- const isSucceed = item.setValue(_data) && item.text !== '';
282
- if (isSucceed) this.#_items.push(item);
283
- return isSucceed;
284
- }
285
-
286
- /**
287
- * Tries to delete a name record addressed by a given index
288
- * @since 0.0.5
289
- * @param {number} value - an element index
290
- * @returns {boolean}
291
- */
292
- delItem(value) {
293
- let isSucceed = this.chkIndex(value)
294
- if (isSucceed) this.#_items.splice(Number(value), 1);
295
- return isSucceed;
296
- }
297
-
298
- /**
299
- * Loads a new name records
300
- * @since 0.0.5
301
- * @param {any} list - an element or a list of an elements
302
- * @param {boolean} [opt=true] - defines whether to clear the list before load a new one
303
- * @returns {number}
304
- */
305
- loadItems(list, opt) {
306
- const useClear = typeof opt === 'boolean' ? opt : true;
307
- const items = valueToArray(list);
308
- let count = 0;
309
- if (items.length) {
310
- if (useClear) this.clear();
311
- items.forEach((item) => { if (this.addItem(item)) count++; });
312
- };
313
- return count;
314
- };
315
-
316
- /**
317
- * Removes all of the instance members
318
- * @returns {void}
319
- */
320
- clear() {
321
- this.#_items.length = 0;
322
- }
323
-
324
- /**
325
- * Calls given function for each name record
326
- * @param {forEachProcEx} cb - a callback function
327
- * @returns {void}
328
- */
329
- forEach(cb) {
330
- if (typeof cb === 'function') this.#_items.forEach(cb);
331
- }
332
-
333
- /**
334
- * Returns an array of a name records picked up by a given function
335
- * @param {cbArrECheck} cb - a callback function
336
- * @returns {TChnNameRecord[]}
337
- */
338
- filter(cb) {
339
- return typeof cb === 'function' ? this.#_items.filter(cb) : [];
340
- }
341
-
342
- };
343
- module.exports.TChnNamesList = TChnNamesList;
344
-
345
59
  /**
346
60
  * @classdesc This class implements an interface of the channel item
347
61
  */
@@ -469,9 +183,15 @@ class TChnAliasItem {
469
183
 
470
184
  /**
471
185
  * Loads a list of a new name records
472
- * @param {array} list - a list of an elements
186
+ * @callback loadItemsFn
187
+ * @param {any} list - a list of an elements
473
188
  * @param {boolean} [opt=true] - defines whether to clear the list before load a new one
474
189
  * @returns {number}
190
+ * @see TChnNamesList.loadItems
191
+ */
192
+ /**
193
+ * Loads a list of a new name records
194
+ * @type {loadItemsFn}
475
195
  * @deprecated
476
196
  * @see TChnNamesList.loadItems
477
197
  * @todo [from v0.0.7] deprecated. Use `TChnNamesList.loadItems` instead.
@@ -650,12 +370,38 @@ class TChnAliasList {
650
370
  * within the instance
651
371
  * @param {any} value - a value to be verified
652
372
  * @returns {boolean}
373
+ * @deprecated
374
+ * @todo \[since v0.0.9] deprecated. Use {@link checkIndex} instead.
653
375
  */
654
376
  chkIndex(value) {
655
377
  const index = valueToIndex(value);
656
378
  return index !== -1 && index < this.count;
657
379
  }
658
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
+
659
405
  /**
660
406
  * Searches an index of an element by its ID
661
407
  * @param {string} value - an element ID
@@ -677,7 +423,8 @@ class TChnAliasList {
677
423
  * @returns {?TChnAliasItem}
678
424
  */
679
425
  getItem(value) {
680
- return this.chkIndex(value) ? this.#_items[Number(value)] : null;
426
+ const i = this.tryIndex(value);
427
+ return i === -1 ? null : this.#_items[i];
681
428
  }
682
429
 
683
430
  /**
@@ -716,19 +463,27 @@ class TChnAliasList {
716
463
  * @returns {boolean}
717
464
  */
718
465
  delItem(value) {
719
- let isSucceed = this.chkIndex(value)
720
- 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);
721
469
  return isSucceed;
722
470
  }
723
471
 
472
+ /**
473
+ * Defines whether to clear the list before load
474
+ * @typedef {boolean} loadListItemsOption
475
+ */
724
476
  /**
725
477
  * Loads a list of a new alias elements
726
478
  * @param {IChannelRecord[]} list - a list of an elements
727
- * @param {boolean} [opt=true] - defines whether to clear the list before load a new one
479
+ * @param {loadListItemsOption|ILoadListItemsOptions} [opt] - load options
728
480
  * @returns {number}
729
481
  */
730
482
  loadItems(list, opt) {
731
- 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;
732
487
  let count = 0;
733
488
  if (isArray(list)) {
734
489
  if (list.length > 0 && useClear) this.clear();