@ygracs/xepg-lib-js 0.0.26-b → 0.0.30

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,587 @@
1
+ // [v0.3.152-20260612]
2
+
3
+ // === module init block ===
4
+
5
+ const {
6
+ valueToIndex,
7
+ readAsNumber,
8
+ isArray, isPlainObject,
9
+ } = require('@ygracs/bsfoc-lib-js');
10
+
11
+ const {
12
+ tryDTValue,
13
+ } = require('@ygracs/dtf-lib-js');
14
+
15
+ const {
16
+ convStringToDTValue, convDTValueToString,
17
+ _getDTSTypeValueForXObj, _getTZSrcValueFromXObj,
18
+ } = require('./dts-helper.js');
19
+
20
+ const xObj = require('@ygracs/xobj-lib-js');
21
+ const {
22
+ insertXObjElement,
23
+ writeXObjParam,
24
+ //TXmlContentParseOptions,
25
+ //DEF_XML_PARSE_OPTIONS: XML_DEF_PARSE_OPTIONS,
26
+ } = xObj;
27
+ // keep the next line for a type checking work properly :(
28
+ const { TXmlContentParseOptions } = require('@ygracs/xobj-lib-js/lib/xObj-defs');
29
+ // keep the next line for a type checking work properly :(
30
+ const { INode } = require('@ygracs/xobj-lib-js/lib/xObj-lib');
31
+
32
+ const {
33
+ //readAsTagName,
34
+ //readAsAttrName,
35
+ //valueToElementID,
36
+ serializeXObjToXMLString, deserializeXObjFromXMLString,
37
+ _getXmlChildElementParam,
38
+ } = require('./xml-base');
39
+
40
+ const {
41
+ XEPG_DEF_TNAMES,
42
+ defParseOptionsSettings,
43
+ } = require('./xepg-defs');
44
+
45
+ // === module inner block ===
46
+
47
+ const {
48
+ XEPG_SCHED_ETAG_NAME,
49
+ XEPG_SCHEDTIME_ETAG_NAME,
50
+ XEPG_SCHEDTITL_ETAG_NAME,
51
+ XEPG_SCHEDCAT_ETAG_NAME,
52
+ XEPG_SCHEDPMARK_ETAG_NAME,
53
+ XEPG_SCHEDDESC_ETAG_NAME,
54
+ } = XEPG_DEF_TNAMES;
55
+
56
+ /**
57
+ * An XEPG-element options set (base)
58
+ * @typedef {Object} OPT_epgelsett_bs
59
+ * @property {TXmlContentParseOptions} [parseOptions] - parser options
60
+ */
61
+
62
+ /**
63
+ * An XEPG-element options set (base)
64
+ * @typedef {Object} XEpgElementConfigBase
65
+ * @property {TXmlContentParseOptions} parseOptions - parser options
66
+ */
67
+
68
+ /**
69
+ * Evaluates an XML-node base settings
70
+ * @function __evalXMLBaseNodeSettings
71
+ * @param {any} value - element settings to evaluate
72
+ * @returns {XEpgElementConfigBase}
73
+ * @inner
74
+ */
75
+ function __evalXMLBaseNodeSettings(value) {
76
+ /** @type {OPT_epgelsett_bs} */
77
+ let settings = isPlainObject(value) ? value : {};
78
+ if (settings instanceof Map) settings = Object.fromEntries(settings);
79
+ let {
80
+ parseOptions,
81
+ } = settings;
82
+ if (!(parseOptions instanceof TXmlContentParseOptions)) {
83
+ settings.parseOptions = new TXmlContentParseOptions(parseOptions);
84
+ };
85
+ // @ts-expect-error
86
+ return settings;
87
+ };
88
+
89
+ // === module main block ===
90
+
91
+ /**
92
+ * A schedule info descriptor
93
+ * @typedef {Object} IXEpgShedInfoDesc
94
+ * @property {number} time - a schedule start time
95
+ * @property {string} title - schedule title
96
+ * @property {string} [cat] - schedule category
97
+ * @property {number} [pcmark] - schedule rating
98
+ * @property {string} [descr] - schedule description
99
+ */
100
+ /**
101
+ * A virtual constant meant for support jsdoc notation:
102
+ * @type {IXEpgShedInfoDesc}
103
+ */
104
+ module.exports.IXEpgShedInfoDesc = { time: 0, title: '' };
105
+
106
+ /**
107
+ * An extra options set
108
+ * @typedef {Object} OPT_extra
109
+ * @property {object} [dts_src] - 'DTS'-settings
110
+ * @property {object} [tz_src] - 'TZ'-settings
111
+ */
112
+
113
+ /**
114
+ * An XEPG-element options set (extend)
115
+ * @typedef {Object} OPT_epgelsett_ex
116
+ * @property {TXmlContentParseOptions} [parseOptions] - parser options
117
+ * @property {object} [dts_src]
118
+ * @property {object} [tz_src]
119
+ */
120
+
121
+ /**
122
+ * @since 0.0.22
123
+ * @classdesc This class implements an interface of a EPG-schedule element
124
+ */
125
+ class TXEpgScheduleItem {
126
+ /** @type {INode} */
127
+ #_host;
128
+ /** @type {OPT_epgelsett_ex} */
129
+ #_options;
130
+ /** @type {TXmlContentParseOptions} */
131
+ #_parseOptions;
132
+ /** @type {?object} */
133
+ #_dts_src = null;
134
+ /** @type {?object} */
135
+ #_tz_src = null;
136
+
137
+ /**
138
+ * Creates an instance of the EPG-schedule element
139
+ * @param {INode} obj - host object
140
+ * @param {OPT_epgelsett_ex} [opt] - options
141
+ * @param {OPT_extra} [extra] - \[since `v0.0.24`] extra options
142
+ */
143
+ constructor(obj, opt, extra) {
144
+ // load content
145
+ const _host = isPlainObject(obj) ? obj : {};
146
+ // load options
147
+ /** @type {OPT_epgelsett_ex} */
148
+ const _options = isPlainObject(opt) ? opt : {};
149
+ let {
150
+ parseOptions,
151
+ } = _options;
152
+ if (!(parseOptions instanceof TXmlContentParseOptions)) {
153
+ parseOptions = new TXmlContentParseOptions(parseOptions);
154
+ _options.parseOptions = parseOptions;
155
+ };
156
+ let {
157
+ dts_src = _options.dts_src,
158
+ tz_src = _options.tz_src,
159
+ } = isPlainObject(extra) ? extra : {};
160
+ if (!isPlainObject(dts_src)) dts_src = null;
161
+ if (!isPlainObject(tz_src)) tz_src = null;
162
+ // save content source
163
+ this.#_host = _host;
164
+ // save options
165
+ this.#_options = _options;
166
+ // save parser options
167
+ this.#_parseOptions = parseOptions;
168
+ // save refs to a time source priveders
169
+ this.#_dts_src = dts_src;
170
+ this.#_tz_src = tz_src;
171
+ }
172
+
173
+ /**
174
+ * Contains a dtstype ID
175
+ * @type {number}
176
+ * @readonly
177
+ */
178
+ get dtstype() {
179
+ return _getDTSTypeValueForXObj(this.#_dts_src);
180
+ }
181
+
182
+ /**
183
+ * Contains a TZ-offset
184
+ * @type {number}
185
+ * @readonly
186
+ */
187
+ get curDocTZ() {
188
+ return _getTZSrcValueFromXObj(this.#_tz_src);
189
+ }
190
+
191
+ /**
192
+ * Contains a schedule start time as a timestamp
193
+ * @type {number}
194
+ * @readonly
195
+ */
196
+ get time() {
197
+ return convStringToDTValue(
198
+ _getXmlChildElementParam(this.#_host, XEPG_SCHEDTIME_ETAG_NAME).trim(),
199
+ this.dtstype, this.curDocTZ
200
+ );
201
+ }
202
+
203
+ /**
204
+ * Contains a schedule title
205
+ * @type {string}
206
+ * @readonly
207
+ */
208
+ get title() {
209
+ return _getXmlChildElementParam(this.#_host, XEPG_SCHEDTITL_ETAG_NAME).trim();
210
+ }
211
+
212
+ /**
213
+ * Contains a schedule category
214
+ * @type {string}
215
+ * @readonly
216
+ */
217
+ get category() {
218
+ return _getXmlChildElementParam(this.#_host, XEPG_SCHEDCAT_ETAG_NAME).trim();
219
+ }
220
+
221
+ /**
222
+ * Contains a schedule rating value (aka pcmark)
223
+ * @type {number}
224
+ * @readonly
225
+ */
226
+ get rating() {
227
+ return readAsNumber(
228
+ _getXmlChildElementParam(this.#_host, XEPG_SCHEDPMARK_ETAG_NAME), 0
229
+ );
230
+ }
231
+
232
+ /**
233
+ * Contains a schedule description
234
+ * @type {string}
235
+ * @readonly
236
+ */
237
+ get descr() {
238
+ return _getXmlChildElementParam(this.#_host, XEPG_SCHEDDESC_ETAG_NAME);
239
+ }
240
+
241
+ /**
242
+ * Returns a schedule info item descriptor
243
+ * @returns {IXEpgShedInfoDesc}
244
+ */
245
+ getInfo() {
246
+ const {
247
+ time,
248
+ title,
249
+ category: cat,
250
+ rating: pcmark,
251
+ descr,
252
+ } = this;
253
+ return {
254
+ time,
255
+ title,
256
+ cat,
257
+ pcmark,
258
+ descr,
259
+ };
260
+ }
261
+
262
+ /**
263
+ * Sets a schedule start time.
264
+ * @param {number|string|Date} value - some time value
265
+ * @returns {boolean}
266
+ */
267
+ setTime(value) {
268
+ let { isSucceed, value: dtValue } = tryDTValue(value);
269
+ let result = false;
270
+ if (isSucceed && dtValue != null) {
271
+ const text = convDTValueToString(dtValue, this.dtstype, this.curDocTZ);
272
+ if (text !== '') {
273
+ const opt = { force: true };
274
+ const item = insertXObjElement(this.#_host, XEPG_SCHEDTIME_ETAG_NAME, opt);
275
+ if (item) {
276
+ result = writeXObjParam(item, text, defParseOptionsSettings.textKey);
277
+ };
278
+ };
279
+ };
280
+ return result;
281
+ }
282
+
283
+ /**
284
+ * Sets a schedule title.
285
+ * @param {string} value - some title
286
+ * @returns {boolean}
287
+ */
288
+ setTitle(value) {
289
+ let result = false;
290
+ if (typeof value === 'string') {
291
+ const opt = { force: true };
292
+ const item = insertXObjElement(this.#_host, XEPG_SCHEDTITL_ETAG_NAME, opt);
293
+ if (item) {
294
+ result = writeXObjParam(item, value.trim(), defParseOptionsSettings.textKey);
295
+ };
296
+ };
297
+ return result;
298
+ }
299
+
300
+ /**
301
+ * Sets a schedule title.
302
+ * @param {string} value - some title
303
+ * @returns {boolean}
304
+ */
305
+ setCategory(value) {
306
+ let result = false;
307
+ if (typeof value === 'string') {
308
+ const opt = { force: true };
309
+ const item = insertXObjElement(this.#_host, XEPG_SCHEDCAT_ETAG_NAME, opt);
310
+ if (item) {
311
+ result = writeXObjParam(item, value.trim(), defParseOptionsSettings.textKey);
312
+ };
313
+ };
314
+ return result;
315
+ }
316
+
317
+ /**
318
+ * Sets a schedule rating value.
319
+ * @param {number|string} value - some value
320
+ * @returns {boolean}
321
+ */
322
+ setRating(value) {
323
+ const _value = valueToIndex(value);
324
+ let result = false;
325
+ if (_value !== -1) {
326
+ const opt = { force: true };
327
+ const item = insertXObjElement(this.#_host, XEPG_SCHEDPMARK_ETAG_NAME, opt);
328
+ if (item) {
329
+ result = writeXObjParam(item, _value, defParseOptionsSettings.textKey);
330
+ };
331
+ };
332
+ return result;
333
+ }
334
+
335
+ /**
336
+ * Sets a schedule description.
337
+ * @param {string} value - some text
338
+ * @returns {boolean}
339
+ */
340
+ setDescr(value) {
341
+ let result = false;
342
+ if (typeof value === 'string') {
343
+ const opt = { force: true };
344
+ const item = insertXObjElement(this.#_host, XEPG_SCHEDDESC_ETAG_NAME, opt);
345
+ if (item) {
346
+ result = writeXObjParam(item, value.trim(), defParseOptionsSettings.textKey);
347
+ };
348
+ };
349
+ return result;
350
+ }
351
+
352
+ /**
353
+ * Returns an instance content as a string in an XML-format.
354
+ * @returns {string}
355
+ */
356
+ saveToXMLString() {
357
+ const options = this.#_parseOptions.js2xml;
358
+ options.ignoreDocType = true;
359
+ options.ignoreDeclaration = true;
360
+ return serializeXObjToXMLString(
361
+ this.#_host,
362
+ options,
363
+ XEPG_SCHED_ETAG_NAME,
364
+ );
365
+ }
366
+
367
+ /**
368
+ * Loads an element content from a string.
369
+ * @since v0.0.24
370
+ * @param {string} xmlString - some content
371
+ * @returns {boolean}
372
+ * @throws {Error}
373
+ * @experimental
374
+ */
375
+ loadFromXMLString(xmlString) {
376
+ const options = this.#_parseOptions.xml2js;
377
+ options.ignoreDocType = true;
378
+ options.ignoreDeclaration = true;
379
+ return deserializeXObjFromXMLString(
380
+ this.#_host,
381
+ options,
382
+ xmlString,
383
+ XEPG_SCHED_ETAG_NAME,
384
+ );
385
+ }
386
+
387
+ };
388
+ module.exports.TXEpgScheduleItem = TXEpgScheduleItem;
389
+
390
+ /**
391
+ * @classdesc This class implements an interface of a EPG-schedules list
392
+ */
393
+ class TXEpgSchedulesList {
394
+ /** @type {INode[]} */
395
+ #_content;
396
+ /** @type {OPT_epgelsett_ex} */
397
+ #_options;
398
+ /** @type {?object} */
399
+ #_dts_src = null;
400
+ /** @type {?object} */
401
+ #_tz_src = null;
402
+
403
+ /**
404
+ * Creates an instance of the schedules list
405
+ * @param {INode[]} obj - list of an elements
406
+ * @param {OPT_epgelsett_ex} [opt] - options
407
+ * @param {OPT_extra} [extra] - \[since `v0.0.24`] extra options
408
+ */
409
+ constructor(obj, opt, extra) {
410
+ const _content = isArray(obj) ? obj : [];
411
+ /** @type {OPT_epgelsett_ex} */
412
+ const _options = isPlainObject(opt) ? opt : {};
413
+ let {
414
+ dts_src = _options.dts_src,
415
+ tz_src = _options.tz_src,
416
+ } = isPlainObject(extra) ? extra : {};
417
+ if (!isPlainObject(dts_src)) dts_src = null;
418
+ if (!isPlainObject(tz_src)) tz_src = null;
419
+ // save content source
420
+ this.#_content = _content;
421
+ // save options
422
+ this.#_options = _options;
423
+ // save refs to a time source priveders
424
+ this.#_dts_src = dts_src;
425
+ this.#_tz_src = tz_src;
426
+ }
427
+
428
+ [Symbol.iterator]() {
429
+ let index = 0;
430
+ return {
431
+ next: () => {
432
+ if (index < this.count) {
433
+ return { done: false, value: this.schedule(index++) };
434
+ } else {
435
+ return { done: true, value: undefined };
436
+ };
437
+ },
438
+ return() {
439
+ return { done: true, value: undefined };
440
+ },
441
+ };
442
+ }
443
+
444
+ /**
445
+ * Returns a raw schedule element
446
+ * @param {number|string} value - element index
447
+ * @returns {any}
448
+ */
449
+ #_getItem(value) {
450
+ if (this.checkIndex(value)) {
451
+ return this.#_content[Number(value)];
452
+ };
453
+ };
454
+
455
+ /**
456
+ * Contains a quantity of a schedules
457
+ * @type {number}
458
+ * @readonly
459
+ * @deprecated
460
+ * @todo \[since `v0.0.24`] deprecated. Use `TXEpgSchedulesList.count`
461
+ */
462
+ get schedQty() {
463
+ return this.#_content.length;
464
+ }
465
+
466
+ /**
467
+ * Contains a quantity of a schedules
468
+ * @since v0.0.24
469
+ * @type {number}
470
+ * @readonly
471
+ */
472
+ get count() {
473
+ return this.#_content.length;
474
+ }
475
+
476
+ /**
477
+ * Contains a dtstype ID
478
+ * @type {number}
479
+ * @readonly
480
+ */
481
+ get dtstype() {
482
+ return _getDTSTypeValueForXObj(this.#_dts_src);
483
+ }
484
+
485
+ /**
486
+ * Contains a TZ-offset
487
+ * @type {number}
488
+ * @readonly
489
+ */
490
+ get curDocTZ() {
491
+ return _getTZSrcValueFromXObj(this.#_tz_src);
492
+ }
493
+
494
+ /**
495
+ * Indicates whether an instance has none member
496
+ * @since v0.0.24
497
+ * @returns {boolean}
498
+ */
499
+ isEmpty() {
500
+ return this.count === 0;
501
+ }
502
+
503
+ /**
504
+ * Indicates whether an instance has any member
505
+ * @since v0.0.24
506
+ * @returns {boolean}
507
+ */
508
+ isNotEmpty() {
509
+ return this.count > 0;
510
+ }
511
+
512
+ /**
513
+ * Checks whether a given value is a valid index and it fits the index range
514
+ * of an instance
515
+ * @since 0.0.20
516
+ * @param {number|string} value - index of some element
517
+ * @returns {boolean}
518
+ * @deprecated
519
+ * @todo \[since v0.0.30] deprecated. Use {@link checkIndex} instead
520
+ */
521
+ chkIndex(value) {
522
+ return this.checkIndex(value);
523
+ }
524
+
525
+ /**
526
+ * Checks whether a given value is a valid index and it fits the index range
527
+ * of an instance
528
+ * @since 0.0.30
529
+ * @param {number|string} value - index of some element
530
+ * @returns {boolean}
531
+ */
532
+ checkIndex(value) {
533
+ const index = valueToIndex(value);
534
+ return index !== -1 && index < this.count;
535
+ }
536
+
537
+ /**
538
+ * Returns a schedule element addressed by a given index
539
+ * @since 0.0.22
540
+ * @param {number|string} index - index of some element
541
+ * @returns {?TXEpgScheduleItem}
542
+ */
543
+ schedule(index) {
544
+ const curObj = this.#_getItem(index);
545
+ let item = null;
546
+ if (isPlainObject(curObj)) {
547
+ item = new TXEpgScheduleItem(curObj, {
548
+ dts_src: this.#_dts_src,
549
+ tz_src: this.#_tz_src,
550
+ });
551
+ };
552
+ return item;
553
+ }
554
+
555
+ /**
556
+ * Returns a schedule info item for instance element addressed
557
+ * by a given index
558
+ * @param {number|string} index - index of some element
559
+ * @returns {?IXEpgShedInfoDesc}
560
+ */
561
+ getScheduleInfo(index) {
562
+ const obj = this.schedule(index);
563
+ return obj ? obj.getInfo() : null;
564
+ }
565
+
566
+ /**
567
+ * Returns a schedule info for instance members
568
+ * @returns {IXEpgShedInfoDesc[]}
569
+ */
570
+ getAllSchedulesInfo() {
571
+ let result = [];
572
+ for (let i = 0; i < this.count; i++) {
573
+ let item = this.getScheduleInfo(i);
574
+ if (isPlainObject(item)) result.push(item);
575
+ };
576
+ return result;
577
+ }
578
+
579
+ /**
580
+ * @protected
581
+ */
582
+ asJSON() {
583
+ return JSON.stringify(this.#_content, null, 2);
584
+ }
585
+
586
+ };
587
+ module.exports.TXEpgSchedulesList = TXEpgSchedulesList;