-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
1208 lines (1151 loc) · 45.8 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**!
* OpString
*
* @version 0.4.0
* @license MIT
* @copyright meezwhite
*/
export default class OpString {
version = '0.4.0';
#sequence = '';
#sequenceData = [];
#operations = {};
#values = {};
#labels = {};
#maxSequenceLength;
#ignoreWarnings = false;
#strictMode = false;
#nextOperationId = 1;
#symbolTypeInvalid = 0;
#symbolTypeString = 1;
#symbolTypeInteger = 2;
#minCharCode = 0;
#maxCharCode = 65535;
#validConfigKeys = [
'sequence',
'operations',
'values',
'labels',
'maxSequenceLength',
'ignoreWarnings',
'strictMode',
];
/**
* Creates an instance of OpString.
*
* @constructor
*
* @param {Object} [config] - Config object to configure OpString features.
* @param {string} [config.sequence] - The character sequence to be executed when `execute` is
* called without providing a `sequence` parameter. (default: '')
* @param {Object} [config.operations] - Object containing the operation mappings to be
* registered. (default: {})
* @param {Object} [config.values] - Object containing the value mappings to be
* registered. (default: {})
* @param {Object} [config.labels] - Object containing the label mappings to be
* registered. (default: {})
* @param {number} [config.maxSequenceLength] - Specifies a maximum allowed sequence length.
* If defined, it must be a positive safe integer. (default: undefined)
* @param {boolean} [config.ignoreWarnings] - Specifies whether warnings should be ignored.
* (default: false)
* @param {boolean} [config.strictMode] - Specifies the behavior of the OpString with regard to
* errors. If set to `true`, errors will be logged; otherwise, warnings will be logged.
* Furthermore, if set to `true` the `maxSequenceLength` must strictly be adhered to,
* otherwise, the respective character sequence will not be set/executed. (default: false)
*/
constructor(config) {
try {
this.#validateArguments('constructor', arguments);
if (config !== undefined) {
if (typeof config.maxSequenceLength !== 'undefined') {
this.#maxSequenceLength = config.maxSequenceLength;
}
if (typeof config.ignoreWarnings === 'boolean') {
this.#ignoreWarnings = config.ignoreWarnings;
}
if (typeof config.strictMode === 'boolean') {
this.#strictMode = config.strictMode;
}
if (typeof config.operations !== 'undefined') {
this.#registerOperationsInternal(config.operations);
}
if (typeof config.values !== 'undefined') {
this.#registerValuesInternal(config.values);
}
if (typeof config.labels !== 'undefined') {
this.#registerLabelsInternal(config.labels);
}
if (typeof config.sequence !== 'undefined') {
this.setSequence(config.sequence);
}
}
} catch (error) {
this.#logError(error);
}
}
/**
* Appends an operation to the sequence and returns the id of the appended operation.
*
* @method append
*
* @param {string|number} operation - The character or character code of the operation to be
* appended.
* @param {Array<string|number>} [values] - An array with the characters or character codes
* of the values corresponding to the operation to be appended.
* @returns {number|boolean} - The id of the appended operation or `false` if the operation
* wasn't appended.
*/
append(operation, values) {
const operationId = this.#nextOperationId;
try {
this.#validateArguments('append', arguments);
this.#sequenceData.push({
id: operationId,
operation: this.#computeCharCode(operation),
values: this.#computeCharCodes(values),
});
this.#nextOperationId++;
this.#computeSequence();
return operationId;
} catch (error) {
this.#logError(error);
}
return false;
}
/**
* Inserts an operation to the sequence at the specified index and returns the id of the
* inserted operation.
*
* @method insert
*
* @param {number} index - The index at which the operation should be added.
* @param {string|number} operation - The character or character code of the operation to be
* inserted.
* @param {Array<string|number>} [values] - An array with the characters or character codes of
* the values corresponding to the operation to be inserted.
* @returns {number|boolean} - The id of the inserted operation or `false` if the operation
* wasn't inserted.
*/
insert(index, operation, values) {
const operationId = this.#nextOperationId;
try {
this.#validateArguments('add', arguments);
this.#sequenceData.splice(index, 0, {
id: operationId,
operation: this.#computeCharCode(operation),
values: this.#computeCharCodes(values),
});
this.#nextOperationId++;
this.#computeSequence();
return operationId;
} catch (error) {
this.#logError(error);
}
return false;
}
/**
* Prepends an operation to the sequence and returns its id.
*
* @method prepend
*
* @param {string|number} operation - The character or character code of the operation to be
* prepended.
* @param {Array<string|number>} [values] - An array with the characters or character codes of
* the values corresponding to the operation to be prepended.
* @returns {number|boolean} - The id of the prepended operation or `false` if the operation
* wasn't prepended.
*/
prepend(operation, values) {
const operationId = this.#nextOperationId;
try {
this.#validateArguments('prepend', arguments);
this.#sequenceData.unshift({
id: operationId,
operation: this.#computeCharCode(operation),
values: this.#computeCharCodes(values),
});
this.#nextOperationId++;
this.#computeSequence();
return operationId;
} catch (error) {
this.#logError(error);
}
return false;
}
/**
* Removes the operation with the specified id from the sequence.
*
* @method remove
*
* @param {number} id - The id of the operation that should be removed.
*
* @throws {ReferenceError} - If there is no operation with the specified id.
*
* @returns {boolean} - If the respective operation was removed `true`, otherwise `false`.
*/
remove(id) {
try {
this.#validateArguments('remove', arguments);
const index = this.#sequenceData.findIndex(operation => operation.id === id);
if (index !== -1) {
this.#sequenceData.splice(index, 1);
this.#computeSequence();
} else {
throw new ReferenceError(`Cannot remove operation with id ${id}, since not found.`);
}
} catch (error) {
this.#logError(error);
return false;
}
return true;
}
/**
* Returns the index of the operation with the provided id in the sequence.
*
* @param {number} id - The id of the operation in the sequence for which the index should be returned.
*
* @throws {ReferenceError} - If there is no operation with the specified id.
*
* @returns {number|undefined} - Index of the operation in the sequence, or `undefined` if not found.
*/
index(id) {
try {
this.#validateArguments('index', arguments);
const index = this.#sequenceData.findIndex(operation => operation.id === id);
if (index !== -1) {
return index;
} else {
throw new ReferenceError(`Cannot find index of operation with id ${id}, since not found.`);
}
} catch (error) {
this.#logError(error);
}
return undefined;
}
/**
* Computes the character code of the provided value.
*
* @private
* @method computeCharCode
*
* @param {*} value - The value for which the character code should be computed.
* @returns {*} - If the provided value is a string, the character code of the first position
* of the string is computed; otherwise, the provided value is returned back.
*/
#computeCharCode(value) {
if (this.#getSymbolType(value) === this.#symbolTypeString) {
return value.charCodeAt(0);
}
return value;
}
/**
* Computes an array of character codes given the provided values.
*
* @private
* @method computeCharCodes
*
* @param {Array<*>} values - The array of values for which character codes should be computed.
* @returns {Array<number|null>} - An array of character codes. If a character code cannot be
* computed, `null` will be used instead.
*/
#computeCharCodes(values) {
if (values !== undefined) {
return values.map((value) => {
const symbolType = this.#getSymbolType(value);
if (symbolType === this.#symbolTypeString) {
return value.charCodeAt(0);
} else if (symbolType === this.#symbolTypeInteger) {
return value;
}
return null;
});
}
return [];
}
/**
* Computes the character sequence from the sequence data array.
*/
#computeSequence() {
let sequence = '';
for (let i = 0; i < this.#sequenceData.length; i++) {
sequence += String.fromCharCode(this.#sequenceData[i].operation);
for (let j = 0; j < this.#sequenceData[i].values.length; j++) {
sequence += String.fromCharCode(this.#sequenceData[i].values[j]);
}
}
this.#sequence = sequence;
}
/**
* Sets the character sequence.
*
* @method setSequence
*
* @param {string} sequence - The character sequence that should be set.
*/
setSequence(sequence) {
let caughtError = false;
try {
this.#validateArguments('setSequence', arguments);
} catch (error) {
caughtError = true;
this.#logError(error);
} finally {
if (! caughtError || (caughtError && ! this.#strictMode)) {
this.#sequence = sequence;
this.#sequenceData = [];
for (let i = 0; i < sequence.length; i++) {
const operationCharCode = sequence.charCodeAt(i);
const operation = this.#operations[operationCharCode];
if (operation) {
const args = [];
for (let j = i+1; j < sequence.length; j++) {
const valueCharCode = sequence.charCodeAt(j);
const value = this.#values[valueCharCode];
if (value) {
args.push(valueCharCode);
} else {
if (this.#operations[valueCharCode]) {
break;
} else {
/**
* NOTE: Register unknown value symbols with a value of `null`.
* Enables handling of unknown value symbols appropriately.
*/
this.registerValue(valueCharCode, null);
args.push(valueCharCode);
}
}
}
this.#sequenceData.push({
id: this.#nextOperationId++,
operation: operationCharCode,
values: args,
});
}
}
}
}
}
/**
* Returns the character sequence.
*
* @method getSequence
*
* @returns {string} - The character sequence
*/
getSequence() {
return this.#sequence;
}
/**
* Returns the sequence data array.
*
* @method getSequenceData
*
* @return {Array<Object>} - The sequence data array
*/
getSequenceData() {
return this.#sequenceData;
}
/**
* Registers an operation mapping.
*
* @method registerOperation
*
* @param {string|number} symbol - The character or character code to be mapped to a function.
* @param {function} callback - The function to which the symbol should be mapped to.
*/
registerOperation(symbol, callback) {
try {
this.#validateArguments('registerOperation', arguments);
const symbolType = this.#getSymbolType(symbol);
if (symbolType === this.#symbolTypeString) {
this.#operations[symbol.charCodeAt(0)] = callback;
} else if (symbolType === this.#symbolTypeInteger) {
this.#operations[symbol] = callback;
}
} catch (error) {
this.#logError(error);
}
}
/**
* Registers additional operation mappings provided by the `operations` object.
*
* @method registerOperations
*
* @param {Object} operations - Object containing additional operation mappings to be
* registered.
*/
registerOperations(operations) {
try {
this.#validateArguments('registerOperations', arguments);
this.#registerOperationsInternal(operations);
} catch (error) {
this.#logError(error);
}
}
/**
* Registers additional operation mappings provided by the `operations` object without
* re-validating the `operations` object.
*
* @private
* @method registerOperations
*
* @param {Object} operations - Object containing the operation mappings to be registered.
*/
#registerOperationsInternal(operations) {
for (const [symbol, callback] of Object.entries(operations)) {
this.registerOperation(symbol, callback);
}
}
/**
* Registers the operation mappings provided by the `operations` object. Previously registered
* operation mappings will be deleted.
*
* @method setOperations
*
* @param {Object} operations - Object containing new operation mappings to be registered.
*/
setOperations(operations) {
try {
this.#validateArguments('setOperations', arguments);
this.#operations = {};
this.#registerOperationsInternal(operations);
} catch (error) {
this.#logError(error);
}
}
/**
* Returns the registered operations.
*
* @method getOperations
*
* @returns {Object} - The registered operations
*/
getOperations() {
return this.#operations;
}
/**
* Registers a value mapping.
*
* @method registerValue
*
* @param {string|number} symbol - The character or character code to be mapped to a value.
* @param {*} value - The value to which the symbol should be mapped to.
*/
registerValue(symbol, value) {
try {
this.#validateArguments('registerValue', arguments);
const symbolType = this.#getSymbolType(symbol);
if (symbolType === this.#symbolTypeString) {
this.#values[symbol.charCodeAt(0)] = value;
} else if (symbolType === this.#symbolTypeInteger) {
this.#values[symbol] = value;
}
} catch (error) {
this.#logError(error);
}
}
/**
* Registers additional value mappings provided by the `values` object.
*
* @method registerValues
*
* @param {Object} values - Object containing additional value mappings to be registered.
*/
registerValues(values) {
try {
this.#validateArguments('registerValues', arguments);
this.#registerValuesInternal(values);
} catch (error) {
this.#logError(error);
}
}
/**
* Registers additional value mappings provided by the `values` object without re-validating
* the `values` object.
*
* @private
* @method registerValues
*
* @param {Object} values - Object containing the value mappings to be registered.
*/
#registerValuesInternal(values) {
for (const [symbol, callback] of Object.entries(values)) {
this.registerValue(symbol, callback);
}
}
/**
* Registers the value mappings provided by the `values` object. Previously registered
* value mappings will be deleted.
*
* @method setValues
*
* @param {Object} values - Object containing new values mappings to be registered.
*/
setValues(values) {
try {
this.#validateArguments('setValues', arguments);
this.#values = {};
this.#registerValuesInternal(values);
} catch (error) {
this.#logError(error);
}
}
/**
* Returns the registered values.
*
* @method getValues
*
* @returns {Object} - The registered values
*/
getValues() {
return this.#values;
}
/**
* Returns the corresponding character for the provided value, if the value is registered.
*
* @param {*} value - The value for which a coresponding character should be returned.
* @returns {string|undefined} - If the value is registered, the corresponding character
* is returned; otherwise `undefined`.
*/
getCharForValue(value) {
try {
this.#validateArguments('getCharForValue', arguments);
const charCode = this.getCharCodeForValue(value);
if (charCode !== undefined) {
return String.fromCharCode(charCode);
}
} catch (error) {
this.#logError(error);
}
return undefined;
}
/**
* Returns the corresponding character code for the provided value, if the value is registered.
*
* @param {*} value - The value for which a coresponding character code should be returned.
* @returns {number|undefined} - If the value is registered, the corresponding character code
* is returned; otherwise `undefined`.
*/
getCharCodeForValue(value) {
try {
this.#validateArguments('getCharCodeForValue', arguments);
const keys = Object.keys(this.#values);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
if (this.#values[key] === value) {
return key;
}
}
} catch (error) {
this.#logError(error);
}
return undefined;
}
/**
* Registers a label mapping to represent the provided symbol.
*
* @method registerLabel
*
* @param {string} label - The label to represent the provided symbol.
* @param {string|number} symbol - The character or character code to be mapped to the label.
*/
registerLabel(label, symbol) {
try {
this.#validateArguments('registerLabel', arguments);
const symbolType = this.#getSymbolType(symbol);
if (symbolType === this.#symbolTypeString) {
this.#labels[label] = symbol.charCodeAt(0);
} else if (symbolType === this.#symbolTypeInteger) {
this.#labels[label] = symbol;
}
} catch (error) {
this.#logError(error);
}
}
/**
* Registers additional label mappings provided by the `labels` object.
*
* @method registerLabels
*
* @param {Object} labels - Object containing additional label mappings to be registered.
*/
registerLabels(labels) {
try {
this.#validateArguments('registerLabels', arguments);
this.#registerLabelsInternal(labels);
} catch (error) {
this.#logError(error);
}
}
/**
* Registers additional label mappings provided by the `labels` object without re-validating
* the `labels` object.
*
* @private
* @method registerLabels
*
* @param {Object} labels - Object containing the label mappings to be registered.
*/
#registerLabelsInternal(labels) {
for (const [label, symbol] of Object.entries(labels)) {
this.registerLabel(label, symbol);
}
}
/**
* Registers the label mappings provided by the `labels` object. Previously registered
* label mappings will be deleted.
*
* @method setLabels
*
* @param {Object} labels - Object containing new labels mappings to be registered.
*/
setLabels(labels) {
try {
this.#validateArguments('setLabels', arguments);
this.#labels = {};
this.#registerLabelsInternal(labels);
} catch (error) {
this.#logError(error);
}
}
/**
* Returns the registered labels.
*
* @method getLabels
*
* @returns {Object} - The registered labels
*/
getLabels() {
return this.#labels;
}
/**
* Returns the corresponding character for the provided label, if the label is registered.
*
* @param {string} label - The label for which a coresponding character should be returned.
* @returns {string|undefined} - If the label is registered, the corresponding character
* is returned; otherwise `undefined`.
*/
getCharForLabel(label) {
try {
this.#validateArguments('getCharForLabel', arguments);
const charCode = this.getCharCodeForLabel(label);
if (charCode !== undefined) {
return String.fromCharCode(charCode);
}
} catch (error) {
this.#logError(error);
}
return undefined;
}
/**
* Returns the corresponding character code for the provided label, if the label is registered.
*
* @param {string} label - The label for which a coresponding character code should be returned.
* @returns {number|undefined} - If the label is registered, the corresponding character code
* is returned; otherwise `undefined`.
*/
getCharCodeForLabel(label) {
try {
this.#validateArguments('getCharCodeForLabel', arguments);
if (this.#labels[label] !== undefined) {
return this.#labels[label];
}
} catch (error) {
this.#logError(error);
}
return undefined;
}
/**
* Sets the maximum allowed sequence limit.
*
* @method setMaxSequenceLength
*
* @param {number} maxSequenceLength - The maximum allowed sequence length
*/
setMaxSequenceLength(maxSequenceLength) {
try {
this.#validateArguments('setMaxSequenceLength', arguments);
this.#maxSequenceLength = maxSequenceLength;
} catch (error) {
this.#logError(error);
}
}
/**
* Returns the `maxSequenceLength` value. If the `maxSequenceLength` has not been
* configured, `undefined` is returned.
*
* @method getMaxSequenceLength
*
* @returns {number|undefined} - The configured `maxSequenceLength` value, or `undefined` if
* not configured.
*/
getMaxSequenceLength() {
return this.#maxSequenceLength;
}
/**
* Attempts to execute the character sequence of the current instance or a provided
* character sequence specified by the `sequence` parameter.
*
* @method execute
*
* @param {string} [sequence] - The character sequence to be executed instead of
* the character sequence of the current instance.
*/
execute(sequence) {
const sequence_isUndefined = sequence === undefined;
let caughtError = false;
try {
if (sequence_isUndefined) {
this.#validateArguments('executeMain', [this.#sequence]);
} else {
this.#validateArguments('executeProvided', arguments);
}
} catch (error) {
caughtError = true;
this.#logError(error);
} finally {
if (! caughtError || (caughtError && ! this.#strictMode)) {
if (sequence_isUndefined) {
this.#executeSequenceFromData();
} else {
this.#executeSequence(sequence);
}
}
}
}
/**
* Attempts to execute the provided character sequence.
*
* @private
* @method executeSequence
*
* @param {string} sequence - The character sequence to be executed.
*/
#executeSequence(sequence) {
if (typeof sequence !== 'string') {
sequence = '';
}
for (let i = 0; i < sequence.length; i++) {
const operation = this.#operations[sequence.charCodeAt(i)];
if (operation) {
const args = [];
for (let j = i+1; j < sequence.length; j++) {
const valueCharCode = sequence.charCodeAt(j);
const value = this.#values[valueCharCode];
if (value !== undefined) {
args.push(value);
} else {
if (this.#operations[valueCharCode]) {
break;
} else {
args.push(undefined);
}
}
}
operation(...args);
}
}
}
/**
* Attempts to execute the provided character sequence from the sequence data array.
*
* @private
* @method executeSequenceFromData
*/
#executeSequenceFromData() {
for (let i = 0; i < this.#sequenceData.length; i++) {
const operationCharCode = this.#sequenceData[i].operation;
const operation = this.#operations[operationCharCode];
if (operation) {
const args = [];
for (let j = 0; j < this.#sequenceData[i].values.length; j++) {
const valueCharCode = this.#sequenceData[i].values[j];
const value = this.#values[valueCharCode];
if (value !== undefined) {
args.push(value);
} else {
if (this.#operations[valueCharCode]) {
break;
} else {
args.push(undefined);
}
}
}
operation(...args);
}
}
}
/**
* Checks the type of a symbol and returns the corresponding symbol type.
*
* Returns:
* - `0` if the symbol has an invalid type
* - `1` if the symbol is an integer
* - `2` if the symbol is a string
*
* @private
* @method getSymbolType
*
* @param {*} value - The value for which the symbol type should be determined.
* @returns {number}
*/
#getSymbolType(value) {
const value_isInteger = /^\d+$/.test(value);
if (value_isInteger) {
return this.#symbolTypeInteger;
} else if (! value_isInteger && typeof value === 'string') {
return this.#symbolTypeString;
}
return this.#symbolTypeInvalid;
}
/**
* Checks whether the value is a valid store object with key-value pairs.
*
* @private
* @method isValidStoreObject
*
* @param {*} value - The value to be checked.
* @param {Array<string>} [validKeys] - The keys that the object may have. (default: [])
* @returns {boolean}
*/
#isValidStoreObject(value, validKeys = []) {
let hasValidKeys = true;
if (validKeys.length !== 0) {
hasValidKeys = Object.keys(value).every(key => validKeys.includes(key));
}
return (
typeof value === 'object'
&& value !== null
&& ! Array.isArray(value)
&& Object.keys(value).length > 0
&& hasValidKeys
&& typeof value.constructor !== 'undefined'
&& value.constructor.prototype.hasOwnProperty('isPrototypeOf') // Object.prototype
);
}
/**
* Checks whether the value is within the allowed character code range.
*
* @private
* @method isCharCodeWithinRange
*
* @param {number} value - The value to be checked.
* @returns {boolean}
*/
#isCharCodeWithinRange(value) {
return value >= this.#minCharCode && value <= this.#maxCharCode;
}
/**
* Check whether the value is a positive save integer.
*
* @private
* @method isPositiveSafeInteger
*
* @param {*} value - The value to be checked.
* @returns {boolean}
*/
#isPositiveSafeInteger(value) {
return (
typeof value === 'number'
&& Number.isSafeInteger(value)
&& value > 0
);
}
/**
* Checks whether the provided character sequence is within the configured `maxSequenceLength`
* limit. If `maxSequenceLength` has not been configured, return `true`.
*
* @private
* @method isSequenceLengthWithinLimit
*
* @param {string} sequence - The character sequence to be checked.
* @returns {boolean}
*/
#isSequenceLengthWithinLimit(sequence) {
if (
this.#maxSequenceLength !== undefined
&& sequence.length > this.#maxSequenceLength
) {
return false;
}
return true;
}
/**
* Logs the provided error based on the current `strictMode` configuration.
*
* @param {TypeError|SyntaxError|RangeError|ReferenceError} error - The error to be logged.
*/
#logError(error) {
error = `[${this.constructor.name}] ${error.name}: ${error.message}`;
if (this.#strictMode) {
console.error(error);
} else {
if (! this.#ignoreWarnings) {
console.warn(error);
}
}
// console.trace();
}
/**
* Checks the validity of the arguments of the respective method and throws errors if necessary.
*
* @private
* @method validateArguments
*
* @param {string} method - The method for which the arguments should be checked.
* @param {Array<*>} args - The user provided arguments to the respective method to be checked.
*
* @throws {TypeError} - If the arguments are of an invalid type:
* - `constructor`: If the `config` parameter is empty, not a plain object or doesn't have valid keys, or if the config object properties are of an invalid type.
* - `insert`: If the `index` parameter is not a non-negative integer.
* - `append`, `insert` and `prepend`: If the `values` parameter is not an array or an empty array.
* - `remove`, `index`: If the `id` parameter is not a positive safe integer.
* - `append`, `insert`, `prepend`, `registerOperation`, `registerValue` and `registerLabel`: If the `symbol` parameter is not a string or an integer.
* - `registerOperation`: If the `callback` parameter is not a function.
* - `setOperations`and `registerOperations`: If the `operations` parameter is empty or not a plain object.
* - `registerValue`: If the `value` parameter is `undefined`.
* - `setValues` and `registerValues`: If the `values` parameter is empty or not a plain object.
* - `registerLabel`: If the `label` parameter is not a string.
* - `setLabels` and `registerLabels`: If the `labels` parameter is empty or not a plain object.
* - `getCharForValue` and `getCharCodeForValue`: If the `value` parameter is `undefined`.
* - `getCharForLabel` and `getCharCodeForLabel`: If the `label` parameter is `undefined`.
* - `setMaxSequenceLength`: If the `maxSequenceLength` parameter is not a positive safe integer.
* - `execute`: If the character sequence of the current instance or the `sequence` parameter is not a string.
*
* @throws {SyntaxError} - If the arguments have syntax errors:
* - `append`, `insert` and `prepend`: If the `values` parameter contains invalid symbols.
* - `append`, `insert`, `prepend`, `registerOperation`, `registerValue` and `registerLabel`: If the `symbol` parameter is a string but not a single character.
* - `execute`: If the sequence to be executed is empty.
*
* @throws {RangeError} - If the arguments are out of valid range:
* - `append`, `insert`, `prepend`, `registerOperation`, `registerValue` and `registerLabel`: If the `symbol` parameter is an integer but out of range.
* - `execute`: If the character sequence of the current instance or the `sequence` parameter exceeds the configured `maxSequenceLength`.
*/
#validateArguments(method, args) {
let introMsg;
switch (method) {
case 'constructor':
if (args[0] !== undefined) {
if (! this.#isValidStoreObject(args[0], this.#validConfigKeys)) {
const validConfigKeysStr = this.#validConfigKeys.slice(0, -1).map(key => `'${key}'`).join(', ');
const lastValidConfigKey = this.#validConfigKeys[this.#validConfigKeys.length-1];
throw new TypeError(`The 'config' parameter, if defined, must be a non-empty plain object with valid 'config' properties; these are ${validConfigKeysStr} and '${lastValidConfigKey}'.`);
} else {
if (
typeof args[0].sequence !== 'undefined'
&& typeof args[0].sequence !== 'string'
) {
throw new TypeError(`The 'config.sequence' property, if defined, must be a string.`);
}
if (
typeof args[0].operations !== 'undefined'
&& ! this.#isValidStoreObject(args[0].operations)
) {
throw new TypeError(`The 'config.operations' property, if defined, must be a non-empty plain object.`);
}
if (
typeof args[0].values !== 'undefined'
&& ! this.#isValidStoreObject(args[0].values)
) {
throw new TypeError(`The 'config.values' property, if defined, must be a non-empty plain object`);
}