@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.
@@ -0,0 +1,392 @@
1
+ // [v0.1.059-20260628]
2
+
3
+ // === module init block ===
4
+
5
+ const {
6
+ isArray, isPlainObject,
7
+ valueToArray, valueToIndex, valueToIDString,
8
+ readAsString,
9
+ } = require('@ygracs/bsfoc-lib-js');
10
+
11
+ // === module inner block ===
12
+
13
+ /**
14
+ * An array of two elements that represents a `<lang>`-`<text>` pair
15
+ * @typedef {string[]} ILangTextPair
16
+ */
17
+ /**
18
+ * A virtual constant meant for support jsdoc notation:
19
+ * @type {ILangTextPair}
20
+ */
21
+ module.exports.ILangTextPair = [];
22
+
23
+ /**
24
+ * Converts a <Lang, Text> key-pair to a string
25
+ * @function convertLangTextValueToString
26
+ * @param {ILangTextPair} data - contains a data to be printed
27
+ * @param {object} [opt]
28
+ * @returns {string}
29
+ * @inner
30
+ */
31
+ function convertLangTextValueToString(data, opt) {
32
+ let name = [ '', '' ];
33
+ let result = '';
34
+ if (isArray(data)) name = data;
35
+ let [ lang, text ] = name;
36
+ if (lang === '') {
37
+ result = text;
38
+ } else if (text !== '') {
39
+ result = `(${lang}): ${text}`;
40
+ };
41
+ return result;
42
+ };
43
+
44
+ // === module main block ===
45
+
46
+ /**
47
+ * @classdesc This class implements an interface of the name record
48
+ */
49
+ class TChnNameRecord {
50
+ /** @type {string} */
51
+ #_lang;
52
+ /** @type {string} */
53
+ #_value;
54
+
55
+ /**
56
+ * Creates an instance of the name record
57
+ */
58
+ constructor() {
59
+ this.reset();
60
+ }
61
+
62
+ /**
63
+ * Contains a language
64
+ * @type {string}
65
+ * @readonly
66
+ */
67
+ get lang() {
68
+ return this.#_lang;
69
+ }
70
+
71
+ /**
72
+ * Contains a value of the record
73
+ * @type {string}
74
+ * @readonly
75
+ */
76
+ get text() {
77
+ return this.#_value;
78
+ }
79
+
80
+ /**
81
+ * Contains a value of the record
82
+ * @type {ILangTextPair}
83
+ */
84
+ get value() {
85
+ return [ this.#_lang, this.#_value ];
86
+ }
87
+
88
+ set value(data) {
89
+ this.setValue(data);
90
+ }
91
+
92
+ /**
93
+ * Sets the record value
94
+ * @param {any} data - a value of the record
95
+ * @returns {boolean}
96
+ */
97
+ setValue(data) {
98
+ let _data = data;
99
+ let isSucceed = false;
100
+ if (_data !== undefined) {
101
+ if (isPlainObject(_data)) {
102
+ let { lang, text } = _data;
103
+ _data = [ lang, text ];
104
+ } else {
105
+ _data = valueToArray(_data);
106
+ };
107
+ switch (_data.length) {
108
+ case 0: {
109
+ break;
110
+ }
111
+ case 1: {
112
+ _data = [ '', _data[0] ];
113
+ }
114
+ default: {
115
+ let [ lang, value = null ] = _data;
116
+ if (value !== null && typeof value !== 'boolean') {
117
+ value = readAsString(value, { numberToString: true });
118
+ lang = value !== '' ? valueToIDString(lang) : null;
119
+ if (lang === null) lang = '';
120
+ this.#_lang = lang;
121
+ this.#_value = value;
122
+ isSucceed = true;
123
+ };
124
+ }
125
+ };
126
+ };
127
+ return isSucceed;
128
+ }
129
+
130
+ /**
131
+ * Returns value as a formated string
132
+ * @param {any} opt - <reserved>
133
+ * @returns {string}
134
+ */
135
+ toFormatString(opt) {
136
+ return convertLangTextValueToString(this.value, opt);
137
+ }
138
+
139
+ /**
140
+ * Clears the record
141
+ * @returns {void}
142
+ */
143
+ reset() {
144
+ this.#_lang = '';
145
+ this.#_value = '';
146
+ }
147
+
148
+ };
149
+ module.exports.TChnNameRecord = TChnNameRecord;
150
+
151
+ /**
152
+ * A settings for the list load ops
153
+ * @typedef {Object} ILoadListItemsOptions
154
+ * @property {boolean} [useClear=true] - indicates whether to clear the list
155
+ * before load
156
+ */
157
+ /**
158
+ * A virtual constant meant for support jsdoc notation:
159
+ * @type {ILoadListItemsOptions}
160
+ */
161
+ module.exports.ILoadListItemsOptions = {};
162
+
163
+ /**
164
+ * A user defined procedure to process an array elements
165
+ * @callback forEachProcEx
166
+ * @param {any} item - some element
167
+ * @param {number} [index] - element index
168
+ * @param {any[]} [arr]
169
+ * @returns {void}
170
+ */
171
+
172
+ /**
173
+ * A user defined procedure to process an array elements
174
+ * @callback cbArrECheck
175
+ * @param {any} item - some element
176
+ * @param {number} [index] - element index
177
+ * @param {any[]} [arr] - array a callback was called upon
178
+ * @returns {any}
179
+ */
180
+
181
+ /**
182
+ * @classdesc This class implements an interface of the name records list
183
+ */
184
+ class TChnNamesList {
185
+ /** @type {TChnNameRecord[]} */
186
+ #_items;
187
+
188
+ /**
189
+ * Creates an instance of the name records list
190
+ */
191
+ constructor() {
192
+ this.#_items = [];
193
+ }
194
+
195
+ [Symbol.iterator]() {
196
+ let index = 0;
197
+ return {
198
+ next: () => {
199
+ if (index < this.count) {
200
+ return { done: false, value: this.getItem(index++) };
201
+ } else {
202
+ return { done: true };
203
+ };
204
+ },
205
+ return() {
206
+ return { done: true };
207
+ },
208
+ };
209
+ }
210
+
211
+ /**
212
+ * Contains a quantity of a name records
213
+ * @type {number}
214
+ * @readonly
215
+ */
216
+ get count() {
217
+ return this.#_items.length;
218
+ }
219
+
220
+ /**
221
+ * Contains a list of a name entries
222
+ * @type {ILangTextPair[]}
223
+ * @readonly
224
+ */
225
+ get value() {
226
+ /** @type {ILangTextPair[]} */
227
+ const result = [];
228
+ this.#_items.forEach((item, i) => {
229
+ result.push(item.value);
230
+ });
231
+ return result;
232
+ }
233
+
234
+ /**
235
+ * Returns a flag whether a list is empty or not
236
+ * @returns {boolean}
237
+ */
238
+ isEmpty() {
239
+ return this.count === 0;
240
+ }
241
+
242
+ /**
243
+ * Returns a flag whether a list has any members
244
+ * @returns {boolean}
245
+ */
246
+ isNotEmpty() {
247
+ return this.count > 0;
248
+ }
249
+
250
+ /**
251
+ * Checks whether a given value is an index and fits an index range
252
+ * within the instance
253
+ * @param {any} value - a value to be verified
254
+ * @returns {boolean}
255
+ * @deprecated
256
+ * @todo \[since v0.0.9] deprecated. Use {@link checkIndex} instead.
257
+ */
258
+ chkIndex(value) {
259
+ const index = valueToIndex(value);
260
+ return index !== -1 && index < this.count;
261
+ }
262
+
263
+ /**
264
+ * Checks whether a given value is an index and fits an index range
265
+ * within the instance
266
+ * @since 0.0.9
267
+ * @param {any} value - a value to be verified
268
+ * @returns {boolean}
269
+ */
270
+ checkIndex(value) {
271
+ const index = valueToIndex(value);
272
+ return index !== -1 && index < this.count;
273
+ }
274
+
275
+ /**
276
+ * Returns an index in case a given value is a valid index value and not exceeds
277
+ * an index range within the instance. If failed a `-1` returned
278
+ * @param {any} value - value to evaluate
279
+ * @returns {number}
280
+ * @since 0.0.9
281
+ */
282
+ tryIndex(value) {
283
+ const index = valueToIndex(value);
284
+ return index !== -1 && index < this.count ? index : -1;
285
+ }
286
+
287
+ /**
288
+ * Searches an index of a given name
289
+ * @param {string} value - a name
290
+ * @returns {number}
291
+ */
292
+ getIndex(value) {
293
+ const opt = { numberToString: true };
294
+ const name = readAsString(value, opt);
295
+ let index = -1;
296
+ if (name !== '') {
297
+ index = this.#_items.findIndex((item) => name === item.text);
298
+ };
299
+ return index;
300
+ }
301
+
302
+ /**
303
+ * Returns a name record
304
+ * @since 0.0.5
305
+ * @param {number} value - an element index
306
+ * @returns {?TChnNameRecord}
307
+ */
308
+ getItem(value) {
309
+ const i = this.tryIndex(value);
310
+ return i === -1 ? null: this.#_items[i];
311
+ }
312
+
313
+ /**
314
+ * Adds a new name record to a list members
315
+ * @since 0.0.5
316
+ * @param {any} data - a value of a name record
317
+ * @returns {boolean}
318
+ */
319
+ addItem(data) {
320
+ const _data = data instanceof TChnNameRecord ? data.value : data;
321
+ const item = new TChnNameRecord();
322
+ const isSucceed = item.setValue(_data) && item.text !== '';
323
+ if (isSucceed) this.#_items.push(item);
324
+ return isSucceed;
325
+ }
326
+
327
+ /**
328
+ * Tries to delete a name record addressed by a given index
329
+ * @since 0.0.5
330
+ * @param {number} value - an element index
331
+ * @returns {boolean}
332
+ */
333
+ delItem(value) {
334
+ const i = this.tryIndex(value);
335
+ let isSucceed = i !== -1;
336
+ if (isSucceed) this.#_items.splice(i, 1);
337
+ return isSucceed;
338
+ }
339
+
340
+ /**
341
+ * Defines whether to clear the list before load
342
+ * @typedef {boolean} loadListItemsOption
343
+ */
344
+ /**
345
+ * Loads a new name records
346
+ * @since 0.0.5
347
+ * @param {any} list - an element or a list of an elements
348
+ * @param {loadListItemsOption|ILoadListItemsOptions} [opt] - load options
349
+ * @returns {number}
350
+ */
351
+ loadItems(list, opt) {
352
+ let {
353
+ useClear = true,
354
+ } = isPlainObject(opt) ? opt : { useClear: opt };
355
+ if (typeof useClear !== 'boolean') useClear = true;
356
+ const items = valueToArray(list);
357
+ let count = 0;
358
+ if (items.length) {
359
+ if (useClear) this.clear();
360
+ items.forEach((item) => { if (this.addItem(item)) count++; });
361
+ };
362
+ return count;
363
+ };
364
+
365
+ /**
366
+ * Removes all of the instance members
367
+ * @returns {void}
368
+ */
369
+ clear() {
370
+ this.#_items.length = 0;
371
+ }
372
+
373
+ /**
374
+ * Calls given function for each name record
375
+ * @param {forEachProcEx} cb - a callback function
376
+ * @returns {void}
377
+ */
378
+ forEach(cb) {
379
+ if (typeof cb === 'function') this.#_items.forEach(cb);
380
+ }
381
+
382
+ /**
383
+ * Returns an array of a name records picked up by a given function
384
+ * @param {cbArrECheck} cb - a callback function
385
+ * @returns {TChnNameRecord[]}
386
+ */
387
+ filter(cb) {
388
+ return typeof cb === 'function' ? this.#_items.filter(cb) : [];
389
+ }
390
+
391
+ };
392
+ module.exports.TChnNamesList = TChnNamesList;
@@ -1,4 +1,4 @@
1
- // [v0.1.049-20260130]
1
+ // [v0.1.050-20260308]
2
2
 
3
3
  // === module init block ===
4
4
 
@@ -21,14 +21,6 @@ const {
21
21
 
22
22
  // === module main block ===
23
23
 
24
- /***
25
- * (* constant definitions *)
26
- */
27
-
28
- /***
29
- * (* function definitions *)
30
- */
31
-
32
24
  /**
33
25
  * A result of `loadAliasFromFile...`
34
26
  * @typedef {Object} RVAL_loadaliasff
@@ -58,6 +50,7 @@ function loadAliasFromFileSync(src, opt) {
58
50
  };
59
51
  return { descr, result };
60
52
  };
53
+ module.exports.loadAliasFromFileSync = loadAliasFromFileSync;
61
54
 
62
55
  /**
63
56
  * Saves an alias to a file
@@ -91,12 +84,4 @@ function saveAliasToFileSync(src, content, opt) {
91
84
  };
92
85
  return data;
93
86
  };
94
-
95
- /***
96
- * (* class definitions *)
97
- */
98
-
99
- // === module exports block ===
100
-
101
- module.exports.loadAliasFromFileSync = loadAliasFromFileSync;
102
87
  module.exports.saveAliasToFileSync = saveAliasToFileSync;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ygracs/chn-alias-list",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
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,6 +12,7 @@
12
12
  "types": "./index.d.ts",
13
13
  "files": [
14
14
  "doc/*.md",
15
+ "lib/chn-names.js",
15
16
  "lib/chn-alias-list.js",
16
17
  "lib/file-helper-ext.js",
17
18
  "lib/*.d.ts",
@@ -30,12 +31,12 @@
30
31
  "#test-dir/*": "./__test__/*"
31
32
  },
32
33
  "dependencies": {
33
- "@cntwg/file-helper": "^0.0.3",
34
- "@ygracs/bsfoc-lib-js": "~0.3.3"
34
+ "@cntwg/file-helper": "~0.0.4",
35
+ "@ygracs/bsfoc-lib-js": "~0.4.0"
35
36
  },
36
37
  "devDependencies": {
37
38
  "@ygracs/test-helper": "~0.0.2-b",
38
- "jest": "^30.2.0",
39
+ "jest": "^30.4.2",
39
40
  "jsdoc-to-markdown": "^9.1.3",
40
41
  "minimist": "^1.2.8",
41
42
  "typescript": "~5.9.3"