@aidc-toolkit/utility 0.9.3 → 0.9.5

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.
package/dist/index.js ADDED
@@ -0,0 +1,1674 @@
1
+ // src/iterator_proxy.ts
2
+ var IteratorProxyBase = class _IteratorProxyBase {
3
+ /**
4
+ * Convert an iteration source to an iterable.
5
+ *
6
+ * @param iterationSource
7
+ * Iteration source.
8
+ *
9
+ * @returns
10
+ * Iteration source if it is already an iterable, otherwise iteration source wrapped in an iterable.
11
+ */
12
+ static toIterable(iterationSource) {
13
+ return Symbol.iterator in iterationSource ? iterationSource : {
14
+ [Symbol.iterator]() {
15
+ return iterationSource;
16
+ }
17
+ };
18
+ }
19
+ /**
20
+ * Initial iterable.
21
+ */
22
+ _initialIterable;
23
+ /**
24
+ * Initial iterator.
25
+ */
26
+ _initialIterator;
27
+ /**
28
+ * Constructor.
29
+ *
30
+ * @param initialIterationSource
31
+ * Initial iteration source.
32
+ */
33
+ constructor(initialIterationSource) {
34
+ this._initialIterable = _IteratorProxyBase.toIterable(initialIterationSource);
35
+ }
36
+ /**
37
+ * Get the initial iterable.
38
+ */
39
+ get initialIterable() {
40
+ return this._initialIterable;
41
+ }
42
+ /**
43
+ * Get the initial iterator.
44
+ */
45
+ get initialIterator() {
46
+ if (this._initialIterator === void 0) {
47
+ this._initialIterator = this.initialIterable[Symbol.iterator]();
48
+ }
49
+ return this._initialIterator;
50
+ }
51
+ /**
52
+ * @inheritDoc
53
+ */
54
+ get [Symbol.toStringTag]() {
55
+ return "IteratorProxy";
56
+ }
57
+ /**
58
+ * @inheritDoc
59
+ */
60
+ [Symbol.dispose]() {
61
+ }
62
+ /**
63
+ * @inheritDoc
64
+ */
65
+ [Symbol.iterator]() {
66
+ return this;
67
+ }
68
+ /**
69
+ * Get the next result from the initial iterator.
70
+ *
71
+ * @param value
72
+ * Tuple value to be passed to Iterator.next().
73
+ *
74
+ * @returns
75
+ * Next result from the initial iterator.
76
+ */
77
+ initialNext(...value) {
78
+ return this.initialIterator.next(...value);
79
+ }
80
+ /**
81
+ * @inheritDoc
82
+ */
83
+ map(callback) {
84
+ return new IteratorMapProxy(this, callback);
85
+ }
86
+ /**
87
+ * @inheritDoc
88
+ */
89
+ flatMap(callback) {
90
+ return new IteratorFlatMapProxy(this, callback);
91
+ }
92
+ /**
93
+ * @inheritDoc
94
+ */
95
+ filter(predicate) {
96
+ return new IteratorFilterProxy(this, predicate, true);
97
+ }
98
+ /**
99
+ * @inheritDoc
100
+ */
101
+ take(limit) {
102
+ return new IteratorTakeProxy(this, limit);
103
+ }
104
+ /**
105
+ * @inheritDoc
106
+ */
107
+ drop(count) {
108
+ return new IteratorDropProxy(this, count);
109
+ }
110
+ /**
111
+ * @inheritDoc
112
+ */
113
+ reduce(callback, initialValue) {
114
+ let index = 0;
115
+ let result = initialValue;
116
+ for (const value of this) {
117
+ if (index === 0 && arguments.length === 1) {
118
+ result = value;
119
+ } else {
120
+ result = callback(result, value, index);
121
+ }
122
+ index++;
123
+ }
124
+ if (index === 0 && arguments.length === 1) {
125
+ throw new Error("reduce() of empty iterator with no initial value");
126
+ }
127
+ return result;
128
+ }
129
+ /**
130
+ * @inheritDoc
131
+ */
132
+ toArray() {
133
+ return Array.from(this);
134
+ }
135
+ /**
136
+ * @inheritDoc
137
+ */
138
+ forEach(callback) {
139
+ let index = 0;
140
+ for (const element of this) {
141
+ callback(element, index++);
142
+ }
143
+ }
144
+ /**
145
+ * @inheritDoc
146
+ */
147
+ some(predicate) {
148
+ return new IteratorFilterProxy(this, predicate, true).next().done !== true;
149
+ }
150
+ /**
151
+ * @inheritDoc
152
+ */
153
+ every(predicate) {
154
+ return new IteratorFilterProxy(this, predicate, false).next().done === true;
155
+ }
156
+ /**
157
+ * @inheritDoc
158
+ */
159
+ find(predicate) {
160
+ return new IteratorFilterProxy(this, predicate, true).next().value;
161
+ }
162
+ };
163
+ var IteratorProxyObject = class extends IteratorProxyBase {
164
+ /**
165
+ * @inheritDoc
166
+ */
167
+ next(...value) {
168
+ return this.initialNext(...value);
169
+ }
170
+ };
171
+ var IteratorMapProxyBase = class extends IteratorProxyBase {
172
+ /**
173
+ * Callback.
174
+ */
175
+ _callback;
176
+ /**
177
+ * Index into initial iteration source.
178
+ */
179
+ _index;
180
+ /**
181
+ * Constructor.
182
+ *
183
+ * @param initialIterationSource
184
+ * Initial iteration source.
185
+ *
186
+ * @param callback
187
+ * Callback.
188
+ */
189
+ constructor(initialIterationSource, callback) {
190
+ super(initialIterationSource);
191
+ this._callback = callback;
192
+ this._index = 0;
193
+ }
194
+ /**
195
+ * Get the next result from the intermediate iterator.
196
+ *
197
+ * @param value
198
+ * Tuple value to be passed to Iterator.next().
199
+ *
200
+ * @returns
201
+ * Next result from the intermediate iterator.
202
+ */
203
+ intermediateNext(...value) {
204
+ const initialResult = this.initialNext(...value);
205
+ return initialResult.done !== true ? {
206
+ value: this._callback(initialResult.value, this._index++)
207
+ } : {
208
+ done: true,
209
+ value: void 0
210
+ };
211
+ }
212
+ };
213
+ var IteratorMapProxy = class extends IteratorMapProxyBase {
214
+ /**
215
+ * @inheritDoc
216
+ */
217
+ next(...value) {
218
+ return this.intermediateNext(...value);
219
+ }
220
+ };
221
+ var IteratorFlatMapProxy = class extends IteratorMapProxyBase {
222
+ _intermediateIterator;
223
+ /**
224
+ * @inheritDoc
225
+ */
226
+ next(...value) {
227
+ let finalResult = void 0;
228
+ do {
229
+ if (this._intermediateIterator === void 0) {
230
+ const intermediateResult = this.intermediateNext(...value);
231
+ if (intermediateResult.done === true) {
232
+ finalResult = intermediateResult;
233
+ } else {
234
+ this._intermediateIterator = IteratorProxyBase.toIterable(intermediateResult.value)[Symbol.iterator]();
235
+ }
236
+ } else {
237
+ const pendingFinalResult = this._intermediateIterator.next();
238
+ if (pendingFinalResult.done === true) {
239
+ this._intermediateIterator = void 0;
240
+ } else {
241
+ finalResult = pendingFinalResult;
242
+ }
243
+ }
244
+ } while (finalResult === void 0);
245
+ return finalResult;
246
+ }
247
+ };
248
+ var IteratorFilterProxy = class extends IteratorProxyBase {
249
+ /**
250
+ * Predicate.
251
+ */
252
+ _predicate;
253
+ /**
254
+ * Expected truthy result of the predicate.
255
+ */
256
+ _expectedTruthy;
257
+ /**
258
+ * Index into iteration source.
259
+ */
260
+ _index;
261
+ /**
262
+ * Constructor.
263
+ *
264
+ * @param iterationSource
265
+ * Iteration source.
266
+ *
267
+ * @param predicate
268
+ * Predicate.
269
+ *
270
+ * @param expectedTruthy
271
+ * Expected truthy result of the predicate.
272
+ */
273
+ constructor(iterationSource, predicate, expectedTruthy) {
274
+ super(iterationSource);
275
+ this._predicate = predicate;
276
+ this._expectedTruthy = expectedTruthy;
277
+ this._index = 0;
278
+ }
279
+ /**
280
+ * @inheritDoc
281
+ */
282
+ next(...value) {
283
+ let result;
284
+ const expectedTruthy = this._expectedTruthy;
285
+ do {
286
+ result = this.initialNext(...value);
287
+ } while (result.done !== true && Boolean(this._predicate(result.value, this._index++)) !== expectedTruthy);
288
+ return result;
289
+ }
290
+ };
291
+ var IteratorCountProxyBase = class extends IteratorProxyObject {
292
+ /**
293
+ * Count.
294
+ */
295
+ _count;
296
+ /**
297
+ * Constructor.
298
+ *
299
+ * @param initialIterationSource
300
+ * Initial iteration source.
301
+ *
302
+ * @param count
303
+ * Count.
304
+ */
305
+ constructor(initialIterationSource, count) {
306
+ super(initialIterationSource);
307
+ if (!Number.isInteger(count) || count < 0) {
308
+ throw new RangeError("Count must be a positive integer");
309
+ }
310
+ this._count = count;
311
+ }
312
+ /**
313
+ * Determine if iterator is exhausted (by count or by iterator itself).
314
+ */
315
+ get exhausted() {
316
+ return this._count <= 0;
317
+ }
318
+ /**
319
+ * @inheritDoc
320
+ */
321
+ next(...value) {
322
+ const result = super.next(...value);
323
+ if (result.done !== true) {
324
+ this._count--;
325
+ } else {
326
+ this._count = 0;
327
+ }
328
+ return result;
329
+ }
330
+ };
331
+ var IteratorTakeProxy = class extends IteratorCountProxyBase {
332
+ /**
333
+ * @inheritDoc
334
+ */
335
+ next(...value) {
336
+ return !this.exhausted ? super.next(...value) : {
337
+ done: true,
338
+ value: void 0
339
+ };
340
+ }
341
+ };
342
+ var IteratorDropProxy = class extends IteratorCountProxyBase {
343
+ /**
344
+ * @inheritDoc
345
+ */
346
+ next(...value) {
347
+ while (!this.exhausted) {
348
+ super.next(...value);
349
+ }
350
+ return super.next(...value);
351
+ }
352
+ };
353
+ function iteratorProxy() {
354
+ let supported;
355
+ try {
356
+ supported = process.env["NODE_ENV"] !== "test";
357
+ } catch (_e) {
358
+ supported = true;
359
+ }
360
+ if (supported) {
361
+ try {
362
+ Iterator.from([]);
363
+ } catch (_e) {
364
+ supported = false;
365
+ }
366
+ }
367
+ return supported ? Iterator : {
368
+ /**
369
+ * @inheritDoc
370
+ */
371
+ from(value) {
372
+ return value instanceof IteratorProxyBase ? value : new IteratorProxyObject(value);
373
+ }
374
+ };
375
+ }
376
+ var IteratorProxy = iteratorProxy();
377
+
378
+ // src/sequencer.ts
379
+ var Sequencer = class {
380
+ /**
381
+ * Start value (inclusive).
382
+ */
383
+ _startValue;
384
+ /**
385
+ * End value (exclusive).
386
+ */
387
+ _endValue;
388
+ /**
389
+ * Count of values.
390
+ */
391
+ _count;
392
+ /**
393
+ * Delta to the next value; equal to the sign of the count.
394
+ */
395
+ _nextDelta;
396
+ /**
397
+ * Minimum value (inclusive).
398
+ */
399
+ _minValue;
400
+ /**
401
+ * Maximum value (inclusive).
402
+ */
403
+ _maxValue;
404
+ /**
405
+ * Next value.
406
+ */
407
+ _nextValue;
408
+ /**
409
+ * Constructor.
410
+ *
411
+ * @param startValue
412
+ * Start value.
413
+ *
414
+ * @param count
415
+ * Count of values. If count is zero or positive, iteration ascends from start value, otherwise it descends from
416
+ * start value.
417
+ */
418
+ constructor(startValue, count) {
419
+ this._startValue = BigInt(startValue);
420
+ this._endValue = this._startValue + BigInt(count);
421
+ this._count = count;
422
+ const ascending = count >= 0;
423
+ if (ascending) {
424
+ this._nextDelta = 1n;
425
+ this._minValue = this._startValue;
426
+ this._maxValue = this._endValue - 1n;
427
+ } else {
428
+ this._nextDelta = -1n;
429
+ this._minValue = this._endValue + 1n;
430
+ this._maxValue = this._startValue;
431
+ }
432
+ this._nextValue = this._startValue;
433
+ }
434
+ /**
435
+ * Get the start value (inclusive).
436
+ */
437
+ get startValue() {
438
+ return this._startValue;
439
+ }
440
+ /**
441
+ * Get the end value (exclusive).
442
+ */
443
+ get endValue() {
444
+ return this._endValue;
445
+ }
446
+ /**
447
+ * Get the count of values.
448
+ */
449
+ get count() {
450
+ return this._count;
451
+ }
452
+ /**
453
+ * Get the minimum value (inclusive).
454
+ */
455
+ get minValue() {
456
+ return this._minValue;
457
+ }
458
+ /**
459
+ * Get the maximum value (inclusive).
460
+ */
461
+ get maxValue() {
462
+ return this._maxValue;
463
+ }
464
+ /**
465
+ * Iterable implementation.
466
+ *
467
+ * @returns
468
+ * this
469
+ */
470
+ [Symbol.iterator]() {
471
+ return this;
472
+ }
473
+ /**
474
+ * Iterator implementation.
475
+ *
476
+ * @returns
477
+ * Iterator result. If iterator is exhausted, the value is absolute value of the count.
478
+ */
479
+ next() {
480
+ const done = this._nextValue === this._endValue;
481
+ let result;
482
+ if (!done) {
483
+ result = {
484
+ value: this._nextValue
485
+ };
486
+ this._nextValue += this._nextDelta;
487
+ } else {
488
+ result = {
489
+ done: true,
490
+ value: Math.abs(this._count)
491
+ };
492
+ }
493
+ return result;
494
+ }
495
+ /**
496
+ * Reset the iterator.
497
+ */
498
+ reset() {
499
+ this._nextValue = this._startValue;
500
+ }
501
+ };
502
+
503
+ // src/locale/i18n.ts
504
+ import { i18nAddResourceBundle, i18nAssertValidResources, i18next } from "@aidc-toolkit/core";
505
+
506
+ // src/locale/en/locale_strings.ts
507
+ var localeStrings = {
508
+ Transformer: {
509
+ domainMustBeGreaterThanZero: "Domain {{domain}} must be greater than 0",
510
+ tweakMustBeGreaterThanOrEqualToZero: "Tweak {{tweak}} must be greater than or equal to 0",
511
+ valueMustBeGreaterThanOrEqualToZero: "Value {{value}} must be greater than or equal to 0",
512
+ valueMustBeLessThan: "Value {{value}} must be less than {{domain}}",
513
+ minValueMustBeGreaterThanOrEqualToZero: "Minimum value {{minValue}} must be greater than or equal to 0",
514
+ maxValueMustBeLessThan: "Maximum value {{maxValue}} must be less than {{domain}}"
515
+ },
516
+ RegExpValidator: {
517
+ stringDoesNotMatchPattern: "String {{s}} does not match pattern"
518
+ },
519
+ CharacterSetValidator: {
520
+ firstZeroFirstCharacter: "Character set must support zero as first character",
521
+ allNumericAllNumericCharacters: "Character set must support all numeric characters in sequence",
522
+ stringMustNotBeAllNumeric: "String must not be all numeric",
523
+ lengthMustBeGreaterThanOrEqualTo: "Length {{length}} must be greater than or equal to {{minimumLength}}",
524
+ lengthMustBeLessThanOrEqualTo: "Length {{length}} must be less than or equal to {{maximumLength}}",
525
+ lengthMustBeEqualTo: "Length {{length}} must be equal to {{exactLength}}",
526
+ lengthOfComponentMustBeGreaterThanOrEqualTo: "Length {{length}} of {{component}} must be greater than or equal to {{minimumLength}}",
527
+ lengthOfComponentMustBeLessThanOrEqualTo: "Length {{length}} of {{component}} must be less than or equal to {{maximumLength}}",
528
+ lengthOfComponentMustBeEqualTo: "Length {{length}} of {{component}} must be equal to {{exactLength}}",
529
+ invalidCharacterAtPosition: "Invalid character '{{c}}' at position {{position}}",
530
+ invalidCharacterAtPositionOfComponent: "Invalid character '{{c}}' at position {{position}} of {{component}}",
531
+ exclusionNotSupported: "Exclusion value of {{exclusion}} is not supported",
532
+ invalidTweakWithAllNumericExclusion: "Tweak must not be used with all-numeric exclusion",
533
+ endSequenceValueMustBeLessThanOrEqualTo: "End sequence value (start sequence value + count - 1) must be less than {{domain}}"
534
+ },
535
+ RecordValidator: {
536
+ typeNameKeyNotFound: '{{typeName}} "{{key}}" not found'
537
+ }
538
+ };
539
+
540
+ // src/locale/fr/locale_strings.ts
541
+ var localeStrings2 = {
542
+ Transformer: {
543
+ domainMustBeGreaterThanZero: "Le domaine {{domain}} doit \xEAtre sup\xE9rieur \xE0 0",
544
+ tweakMustBeGreaterThanOrEqualToZero: "Le r\xE9glage {{tweak}} doit \xEAtre sup\xE9rieur ou \xE9gal \xE0 0",
545
+ valueMustBeGreaterThanOrEqualToZero: "La valeur {{value}} doit \xEAtre sup\xE9rieure ou \xE9gale \xE0 0",
546
+ valueMustBeLessThan: "La valeur {{value}} doit \xEAtre inf\xE9rieure \xE0 {{domain}}",
547
+ minValueMustBeGreaterThanOrEqualToZero: "La valeur minimale {{minValue}} doit \xEAtre sup\xE9rieure ou \xE9gale \xE0 0",
548
+ maxValueMustBeLessThan: "La valeur maximale {{maxValue}} doit \xEAtre inf\xE9rieure \xE0 {{domain}}"
549
+ },
550
+ RegExpValidator: {
551
+ stringDoesNotMatchPattern: "La cha\xEEne {{s}} ne correspond pas au mod\xE8le"
552
+ },
553
+ CharacterSetValidator: {
554
+ firstZeroFirstCharacter: "Le jeu de caract\xE8res doit prendre en charge z\xE9ro comme premier caract\xE8re",
555
+ allNumericAllNumericCharacters: "Le jeu de caract\xE8res doit prendre en charge tous les caract\xE8res num\xE9riques en s\xE9quence",
556
+ stringMustNotBeAllNumeric: "La cha\xEEne ne doit pas \xEAtre enti\xE8rement num\xE9rique",
557
+ lengthMustBeGreaterThanOrEqualTo: "La longueur {{length}} doit \xEAtre sup\xE9rieure ou \xE9gale \xE0 {{minimumLength}}",
558
+ lengthMustBeLessThanOrEqualTo: "La longueur {{length}} doit \xEAtre inf\xE9rieure ou \xE9gale \xE0 {{maximumLength}}",
559
+ lengthMustBeEqualTo: "La longueur {{length}} doit \xEAtre \xE9gale \xE0 {{exactLength}}",
560
+ lengthOfComponentMustBeGreaterThanOrEqualTo: "La longueur {{length}} de {{component}} doit \xEAtre sup\xE9rieure ou \xE9gale \xE0 {{minimumLength}}",
561
+ lengthOfComponentMustBeLessThanOrEqualTo: "La longueur {{length}} de {{component}} doit \xEAtre inf\xE9rieure ou \xE9gale \xE0 {{maximumLength}}",
562
+ lengthOfComponentMustBeEqualTo: "La longueur {{length}} de {{component}} doit \xEAtre \xE9gale \xE0 {{exactLength}}",
563
+ invalidCharacterAtPosition: "Caract\xE8re non valide '{{c}}' \xE0 la position {{position}}",
564
+ invalidCharacterAtPositionOfComponent: "Caract\xE8re non valide '{{c}}' \xE0 la position {{position}} de {{component}}",
565
+ exclusionNotSupported: "La valeur d'exclusion de {{exclusion}} n'est pas prise en charge",
566
+ invalidTweakWithAllNumericExclusion: "Le r\xE9glage ne doit pas \xEAtre utilis\xE9 avec une exclusion enti\xE8rement num\xE9rique",
567
+ endSequenceValueMustBeLessThanOrEqualTo: "La valeur de la s\xE9quence de fin (valeur de la s\xE9quence de d\xE9but + nombre - 1) doit \xEAtre inf\xE9rieure \xE0 {{domaine}}"
568
+ },
569
+ RecordValidator: {
570
+ typeNameKeyNotFound: '{{typeName}} "{{key}}" introuvable'
571
+ }
572
+ };
573
+
574
+ // src/locale/i18n.ts
575
+ var utilityNS = "aidct_utility";
576
+ i18nAssertValidResources(localeStrings, "fr", localeStrings2);
577
+ i18nAddResourceBundle("en", utilityNS, localeStrings);
578
+ i18nAddResourceBundle("fr", utilityNS, localeStrings2);
579
+ var i18n_default = i18next;
580
+
581
+ // src/transformer.ts
582
+ var Transformer = class _Transformer {
583
+ /**
584
+ * Transformers cache, mapping a domain to another map, which maps an optional tweak to a transformer.
585
+ */
586
+ static TRANSFORMER_MAPS_MAP = /* @__PURE__ */ new Map();
587
+ /**
588
+ * Domain.
589
+ */
590
+ _domain;
591
+ /**
592
+ * Constructor.
593
+ *
594
+ * @param domain
595
+ * Domain.
596
+ */
597
+ constructor(domain) {
598
+ this._domain = BigInt(domain);
599
+ if (this._domain <= 0n) {
600
+ throw new RangeError(i18n_default.t("Transformer.domainMustBeGreaterThanZero", {
601
+ ns: utilityNS,
602
+ domain
603
+ }));
604
+ }
605
+ }
606
+ /**
607
+ * Get a transformer, constructing it if necessary. The type returned is {@link IdentityTransformer} if tweak is
608
+ * undefined, {@link EncryptionTransformer} if tweak is defined. Note that although an {@link EncryptionTransformer}
609
+ * with a zero tweak operates as an {@link IdentityTransformer}, {@link EncryptionTransformer} is still the type
610
+ * returned if a zero tweak is explicitly specified.
611
+ *
612
+ * @param domain
613
+ * Domain.
614
+ *
615
+ * @param tweak
616
+ * Tweak.
617
+ *
618
+ * @returns
619
+ * {@link IdentityTransformer} if tweak is undefined, {@link EncryptionTransformer} if tweak is defined.
620
+ */
621
+ static get(domain, tweak) {
622
+ const domainN = BigInt(domain);
623
+ let transformersMap = _Transformer.TRANSFORMER_MAPS_MAP.get(domainN);
624
+ if (transformersMap === void 0) {
625
+ transformersMap = /* @__PURE__ */ new Map();
626
+ _Transformer.TRANSFORMER_MAPS_MAP.set(domainN, transformersMap);
627
+ }
628
+ const tweakN = tweak === void 0 ? void 0 : BigInt(tweak);
629
+ let transformer = transformersMap.get(tweakN);
630
+ if (transformer === void 0) {
631
+ transformer = tweakN === void 0 ? new IdentityTransformer(domainN) : new EncryptionTransformer(domainN, tweakN);
632
+ transformersMap.set(tweakN, transformer);
633
+ }
634
+ return transformer;
635
+ }
636
+ /**
637
+ * Get the domain.
638
+ */
639
+ get domain() {
640
+ return this._domain;
641
+ }
642
+ /**
643
+ * Validate that a value is within the domain.
644
+ *
645
+ * @param value
646
+ * Value.
647
+ */
648
+ validate(value) {
649
+ if (value < 0n) {
650
+ throw new RangeError(i18n_default.t("Transformer.valueMustBeGreaterThanOrEqualToZero", {
651
+ ns: utilityNS,
652
+ value
653
+ }));
654
+ }
655
+ if (value >= this.domain) {
656
+ throw new RangeError(i18n_default.t("Transformer.valueMustBeLessThan", {
657
+ ns: utilityNS,
658
+ value,
659
+ domain: this.domain
660
+ }));
661
+ }
662
+ }
663
+ /**
664
+ * Transform a value or values forward.
665
+ *
666
+ * @template T
667
+ * Type returned by transformation callback.
668
+ *
669
+ * @param valueOrValues
670
+ * Value(s).
671
+ *
672
+ * @param transformationCallback
673
+ * Called after value(s) is/are transformed to convert it/them to its/their final value(s).
674
+ *
675
+ * @returns
676
+ * Value(s) transformed into object(s).
677
+ */
678
+ forward(valueOrValues, transformationCallback) {
679
+ let result;
680
+ if (typeof valueOrValues !== "object") {
681
+ const valueN = BigInt(valueOrValues);
682
+ this.validate(valueN);
683
+ const transformedValue = this.doForward(valueN);
684
+ result = transformationCallback === void 0 ? transformedValue : transformationCallback(transformedValue, 0);
685
+ } else if (valueOrValues instanceof Sequencer) {
686
+ if (valueOrValues.minValue < 0n) {
687
+ throw new RangeError(i18n_default.t("Transformer.minValueMustBeGreaterThanOrEqualToZero", {
688
+ ns: utilityNS,
689
+ minValue: valueOrValues.minValue
690
+ }));
691
+ }
692
+ if (valueOrValues.maxValue >= this.domain) {
693
+ throw new RangeError(i18n_default.t("Transformer.maxValueMustBeLessThan", {
694
+ ns: utilityNS,
695
+ maxValue: valueOrValues.maxValue,
696
+ domain: this.domain
697
+ }));
698
+ }
699
+ result = transformationCallback === void 0 ? IteratorProxy.from(valueOrValues).map((value) => this.doForward(value)) : IteratorProxy.from(valueOrValues).map((value, index) => transformationCallback(this.doForward(value), index));
700
+ } else {
701
+ result = transformationCallback === void 0 ? IteratorProxy.from(valueOrValues).map((value) => {
702
+ const valueN = BigInt(value);
703
+ this.validate(valueN);
704
+ return this.doForward(valueN);
705
+ }) : IteratorProxy.from(valueOrValues).map((value, index) => {
706
+ const valueN = BigInt(value);
707
+ this.validate(valueN);
708
+ return transformationCallback(this.doForward(valueN), index);
709
+ });
710
+ }
711
+ return result;
712
+ }
713
+ /**
714
+ * Transform a value in reverse.
715
+ *
716
+ * @param transformedValue
717
+ * Transformed value.
718
+ *
719
+ * @returns
720
+ * Value.
721
+ */
722
+ reverse(transformedValue) {
723
+ const transformedValueN = BigInt(transformedValue);
724
+ this.validate(transformedValueN);
725
+ return this.doReverse(transformedValueN);
726
+ }
727
+ };
728
+ var IdentityTransformer = class extends Transformer {
729
+ /**
730
+ * @inheritDoc
731
+ */
732
+ doForward(value) {
733
+ return value;
734
+ }
735
+ /**
736
+ * @inheritDoc
737
+ */
738
+ doReverse(transformedValue) {
739
+ return transformedValue;
740
+ }
741
+ };
742
+ var EncryptionTransformer = class _EncryptionTransformer extends Transformer {
743
+ /**
744
+ * Individual bits, pre-calculated for performance.
745
+ */
746
+ static BITS = new Uint8Array([
747
+ 1,
748
+ 2,
749
+ 4,
750
+ 8,
751
+ 16,
752
+ 32,
753
+ 64,
754
+ 128
755
+ ]);
756
+ /**
757
+ * Inverse individual bits, pre-calculated for performance.
758
+ */
759
+ static INVERSE_BITS = new Uint8Array([
760
+ 254,
761
+ 253,
762
+ 251,
763
+ 247,
764
+ 239,
765
+ 223,
766
+ 191,
767
+ 127
768
+ ]);
769
+ /**
770
+ * Number of bytes covered by the domain.
771
+ */
772
+ _domainBytes;
773
+ /**
774
+ * Tweak.
775
+ */
776
+ _tweak;
777
+ /**
778
+ * Xor bytes array generated from the domain and tweak.
779
+ */
780
+ _xorBytes;
781
+ /**
782
+ * Bits array generated from the domain and tweak.
783
+ */
784
+ _bits;
785
+ /**
786
+ * Inverse bits array generated from the domain and tweak.
787
+ */
788
+ _inverseBits;
789
+ /**
790
+ * Number of rounds (length of arrays) generated from the domain and tweak.
791
+ */
792
+ _rounds;
793
+ /**
794
+ * Constructor.
795
+ *
796
+ * @param domain
797
+ * Domain.
798
+ *
799
+ * @param tweak
800
+ * Tweak.
801
+ */
802
+ constructor(domain, tweak) {
803
+ super(domain);
804
+ if (tweak < 0n) {
805
+ throw new RangeError(i18n_default.t("Transformer.tweakMustBeGreaterThanOrEqualToZero", {
806
+ ns: utilityNS,
807
+ tweak
808
+ }));
809
+ }
810
+ let domainBytes = 0;
811
+ for (let reducedDomainMinusOne = this.domain - 1n; reducedDomainMinusOne !== 0n; reducedDomainMinusOne = reducedDomainMinusOne >> 8n) {
812
+ domainBytes++;
813
+ }
814
+ this._domainBytes = domainBytes;
815
+ this._tweak = BigInt(tweak);
816
+ const xorBytes = new Array();
817
+ const bits = new Array();
818
+ const inverseBits = new Array();
819
+ for (let reducedKey = this.domain * this.tweak * 603868999n; reducedKey !== 0n; reducedKey = reducedKey >> 8n) {
820
+ const keyByte = Number(reducedKey & 0xFFn);
821
+ xorBytes.unshift(keyByte);
822
+ const bitNumber = keyByte & 7;
823
+ bits.push(_EncryptionTransformer.BITS[bitNumber]);
824
+ inverseBits.push(_EncryptionTransformer.INVERSE_BITS[bitNumber]);
825
+ }
826
+ if (domainBytes === 1) {
827
+ const domainMask = _EncryptionTransformer.BITS.filter((bit) => bit < domain).reduce((accumulator, bit) => accumulator | bit, 0);
828
+ this._xorBytes = new Uint8Array([xorBytes.reduce((accumulator, xorByte) => accumulator ^ xorByte, 0) & domainMask]);
829
+ this._bits = new Uint8Array([_EncryptionTransformer.BITS[0]]);
830
+ this._inverseBits = new Uint8Array([_EncryptionTransformer.INVERSE_BITS[0]]);
831
+ this._rounds = 1;
832
+ } else {
833
+ this._xorBytes = new Uint8Array(xorBytes);
834
+ this._bits = new Uint8Array(bits);
835
+ this._inverseBits = new Uint8Array(inverseBits);
836
+ this._rounds = xorBytes.length;
837
+ }
838
+ }
839
+ /**
840
+ * Get the tweak.
841
+ */
842
+ get tweak() {
843
+ return this._tweak;
844
+ }
845
+ /**
846
+ * Convert a value to a byte array big enough to handle the entire domain.
847
+ *
848
+ * @param value
849
+ * Value.
850
+ *
851
+ * @returns
852
+ * Big-endian byte array equivalent to the value.
853
+ */
854
+ valueToBytes(value) {
855
+ const bytes = new Uint8Array(this._domainBytes);
856
+ let reducedValue = value;
857
+ for (let index = this._domainBytes - 1; index >= 0; index--) {
858
+ bytes[index] = Number(reducedValue & 0xFFn);
859
+ reducedValue = reducedValue >> 8n;
860
+ }
861
+ return bytes;
862
+ }
863
+ /**
864
+ * Convert a byte array to a value.
865
+ *
866
+ * @param bytes
867
+ * Big-endian byte array equivalent to the value.
868
+ *
869
+ * @returns
870
+ * Value.
871
+ */
872
+ static bytesToValue(bytes) {
873
+ return bytes.reduce((accumulator, byte) => accumulator << 8n | BigInt(byte), 0n);
874
+ }
875
+ /**
876
+ * Shuffle a byte array.
877
+ *
878
+ * The input array to the forward operation (output from the reverse operation) is `bytes` and the output array from
879
+ * the forward operation (input to the reverse operation) is `bytes'`.
880
+ *
881
+ * The shuffle operation starts by testing the bit at `bits[round]` for each `byte` in `bytes`. The indexes for all
882
+ * bytes with that bit set are put into one array (`shuffleIndexes1`) and the rest are put into another
883
+ * (`shuffleIndexes0`). The two arrays are concatenated and used to shuffle the input array, using their values
884
+ * (`shuffleIndex`) and the indexes of those values (`index`) in the concatenated array.
885
+ *
886
+ * Forward shuffling moves the entry at `shuffleIndex` to the `index` position.
887
+ *
888
+ * Reverse shuffling moves the entry at `index` to the `shuffleIndex` position.
889
+ *
890
+ * As each byte is moved, the bit at `bits[round]` is preserved in its original position. This ensures that the
891
+ * process is reversible.
892
+ *
893
+ * @param bytes
894
+ * Byte array.
895
+ *
896
+ * @param round
897
+ * Round number.
898
+ *
899
+ * @param forward
900
+ * True if operating forward (encrypting), false if operating in reverse (decrypting).
901
+ *
902
+ * @returns
903
+ * Shuffled byte array.
904
+ */
905
+ shuffle(bytes, round, forward) {
906
+ const bytesLength = bytes.length;
907
+ const determinants = new Uint8Array(bytesLength);
908
+ const shuffleIndexes1 = new Array();
909
+ const shuffleIndexes0 = new Array();
910
+ const bit = this._bits[round];
911
+ bytes.forEach((byte, index) => {
912
+ const determinant = byte & bit;
913
+ determinants[index] = determinant;
914
+ (determinant !== 0 ? shuffleIndexes1 : shuffleIndexes0).push(index);
915
+ });
916
+ const inverseBit = this._inverseBits[round];
917
+ const shuffleBytes = new Uint8Array(bytesLength);
918
+ [...shuffleIndexes1, ...shuffleIndexes0].forEach((shuffleIndex, index) => {
919
+ if (forward) {
920
+ shuffleBytes[index] = bytes[shuffleIndex] & inverseBit | determinants[index];
921
+ } else {
922
+ shuffleBytes[shuffleIndex] = bytes[index] & inverseBit | determinants[shuffleIndex];
923
+ }
924
+ });
925
+ return shuffleBytes;
926
+ }
927
+ /**
928
+ * Xor a byte array.
929
+ *
930
+ * The input array to the forward operation (output from the reverse operation) is `bytes` and the output array from
931
+ * the forward operation (input to the reverse operation) is `bytes'`.
932
+ *
933
+ * Forward:
934
+ * - `bytes'[0] = bytes[0] ^ xorBytes[round]`
935
+ * - `bytes'[1] = bytes[1] ^ bytes'[0]`
936
+ * - `bytes'[2] = bytes[2] ^ bytes'[1]`
937
+ * - `...`
938
+ * - `bytes'[domainBytes - 1] = bytes[domainBytes - 1] ^ bytes'[domainBytes - 2]`
939
+ *
940
+ * Reverse:
941
+ * - `bytes[0] = bytes'[0] ^ xorBytes[round]`
942
+ * - `bytes[1] = bytes'[1] ^ bytes'[0]`
943
+ * - `bytes[2] = bytes'[2] ^ bytes'[1]`
944
+ * - `...`
945
+ * - `bytes[domainBytes - 1] = bytes'[domainBytes - 1] ^ bytes'[domainBytes - 2]`
946
+ *
947
+ * @param bytes
948
+ * Byte array.
949
+ *
950
+ * @param round
951
+ * Round number.
952
+ *
953
+ * @param forward
954
+ * True if operating forward (encrypting), false if operating in reverse (decrypting).
955
+ *
956
+ * @returns
957
+ * Xored byte array.
958
+ */
959
+ xor(bytes, round, forward) {
960
+ let cumulativeXorByte = this._xorBytes[round];
961
+ return bytes.map((byte) => {
962
+ const xorByte = byte ^ cumulativeXorByte;
963
+ cumulativeXorByte = forward ? xorByte : byte;
964
+ return xorByte;
965
+ });
966
+ }
967
+ /**
968
+ * @inheritDoc
969
+ */
970
+ doForward(value) {
971
+ let bytes = this.valueToBytes(value);
972
+ let transformedValue;
973
+ do {
974
+ for (let round = 0; round < this._rounds; round++) {
975
+ bytes = this.xor(this.shuffle(bytes, round, true), round, true);
976
+ }
977
+ transformedValue = _EncryptionTransformer.bytesToValue(bytes);
978
+ } while (transformedValue >= this.domain);
979
+ return transformedValue;
980
+ }
981
+ /**
982
+ * @inheritDoc
983
+ */
984
+ doReverse(transformedValue) {
985
+ let bytes = this.valueToBytes(transformedValue);
986
+ let value;
987
+ do {
988
+ for (let round = this._rounds - 1; round >= 0; round--) {
989
+ bytes = this.shuffle(this.xor(bytes, round, false), round, false);
990
+ }
991
+ value = _EncryptionTransformer.bytesToValue(bytes);
992
+ } while (value >= this.domain);
993
+ return value;
994
+ }
995
+ };
996
+
997
+ // src/reg_exp.ts
998
+ var RegExpValidator = class {
999
+ /**
1000
+ * Regular expression.
1001
+ */
1002
+ _regExp;
1003
+ /**
1004
+ * Constructor.
1005
+ *
1006
+ * @param regExp
1007
+ * Regular expression. See {@link RegExpValidator | class documentation} for notes.
1008
+ */
1009
+ constructor(regExp) {
1010
+ this._regExp = regExp;
1011
+ }
1012
+ /**
1013
+ * Get the regular expression.
1014
+ */
1015
+ get regExp() {
1016
+ return this._regExp;
1017
+ }
1018
+ /**
1019
+ * Create an error message for a string. The generic error message is sufficient for many use cases but a more
1020
+ * domain-specific error message, possibly including the pattern itself, is often required.
1021
+ *
1022
+ * @param s
1023
+ * String.
1024
+ *
1025
+ * @returns
1026
+ * Error message.
1027
+ */
1028
+ createErrorMessage(s) {
1029
+ return i18n_default.t("RegExpValidator.stringDoesNotMatchPattern", {
1030
+ ns: utilityNS,
1031
+ s
1032
+ });
1033
+ }
1034
+ /**
1035
+ * @inheritDoc
1036
+ */
1037
+ validate(s) {
1038
+ if (!this._regExp.test(s)) {
1039
+ throw new RangeError(this.createErrorMessage(s));
1040
+ }
1041
+ }
1042
+ };
1043
+
1044
+ // src/record.ts
1045
+ var RecordValidator = class {
1046
+ /**
1047
+ * Type name for error message.
1048
+ */
1049
+ _typeName;
1050
+ /**
1051
+ * Record in which to look up keys.
1052
+ */
1053
+ _record;
1054
+ /**
1055
+ * Constructor.
1056
+ *
1057
+ * @param typeName
1058
+ * Type name for error message.
1059
+ *
1060
+ * @param record
1061
+ * Record in which to look up keys.
1062
+ */
1063
+ constructor(typeName, record) {
1064
+ this._typeName = typeName;
1065
+ this._record = record;
1066
+ }
1067
+ /**
1068
+ * Get the type name.
1069
+ */
1070
+ get typeName() {
1071
+ return this._typeName;
1072
+ }
1073
+ /**
1074
+ * Get the record.
1075
+ */
1076
+ get record() {
1077
+ return this._record;
1078
+ }
1079
+ /**
1080
+ * Validate a key by looking it up in the record.
1081
+ *
1082
+ * @param key
1083
+ * Record key.
1084
+ */
1085
+ validate(key) {
1086
+ if (this.record[key] === void 0) {
1087
+ throw new RangeError(i18n_default.t("RecordValidator.typeNameKeyNotFound", {
1088
+ ns: utilityNS,
1089
+ typeName: this.typeName,
1090
+ key
1091
+ }));
1092
+ }
1093
+ }
1094
+ };
1095
+
1096
+ // src/character_set.ts
1097
+ var Exclusion = /* @__PURE__ */ ((Exclusion2) => {
1098
+ Exclusion2[Exclusion2["None"] = 0] = "None";
1099
+ Exclusion2[Exclusion2["FirstZero"] = 1] = "FirstZero";
1100
+ Exclusion2[Exclusion2["AllNumeric"] = 2] = "AllNumeric";
1101
+ return Exclusion2;
1102
+ })(Exclusion || {});
1103
+ var CharacterSetValidator = class _CharacterSetValidator {
1104
+ static NOT_ALL_NUMERIC_VALIDATOR = new class extends RegExpValidator {
1105
+ /**
1106
+ * Create an error message for an all-numeric string.
1107
+ *
1108
+ * @param _s
1109
+ * String.
1110
+ *
1111
+ * @returns
1112
+ * Error message.
1113
+ */
1114
+ createErrorMessage(_s) {
1115
+ return i18n_default.t("CharacterSetValidator.stringMustNotBeAllNumeric", {
1116
+ ns: utilityNS
1117
+ });
1118
+ }
1119
+ }(/\D/);
1120
+ /**
1121
+ * Character set.
1122
+ */
1123
+ _characterSet;
1124
+ /**
1125
+ * Character set map, mapping each character in the character set to its index such that
1126
+ * `_characterSetMap.get(_characterSet[index]) === index`.
1127
+ */
1128
+ _characterSetMap;
1129
+ /**
1130
+ * Exclusions supported by the character set.
1131
+ */
1132
+ _exclusionSupport;
1133
+ /**
1134
+ * Constructor.
1135
+ *
1136
+ * @param characterSet
1137
+ * Character set. Each element is a single-character string, unique within the array, that defines the character
1138
+ * set.
1139
+ *
1140
+ * @param exclusionSupport
1141
+ * Exclusions supported by the character set. All character sets implicitly support {@link Exclusion.None}.
1142
+ */
1143
+ constructor(characterSet, ...exclusionSupport) {
1144
+ this._characterSet = characterSet;
1145
+ const characterSetMap = /* @__PURE__ */ new Map();
1146
+ characterSet.forEach((c, index) => {
1147
+ characterSetMap.set(c, index);
1148
+ });
1149
+ this._characterSetMap = characterSetMap;
1150
+ this._exclusionSupport = exclusionSupport;
1151
+ }
1152
+ /**
1153
+ * Get the character set.
1154
+ */
1155
+ get characterSet() {
1156
+ return this._characterSet;
1157
+ }
1158
+ /**
1159
+ * Get the character set size.
1160
+ */
1161
+ get characterSetSize() {
1162
+ return this._characterSet.length;
1163
+ }
1164
+ /**
1165
+ * Get the exclusions supported by the character set.
1166
+ */
1167
+ get exclusionSupport() {
1168
+ return this._exclusionSupport;
1169
+ }
1170
+ /**
1171
+ * Get the character at an index.
1172
+ *
1173
+ * @param index
1174
+ * Index into the character set.
1175
+ *
1176
+ * @returns
1177
+ * Character at the index.
1178
+ */
1179
+ character(index) {
1180
+ return this._characterSet[index];
1181
+ }
1182
+ /**
1183
+ * Get the index for a character.
1184
+ *
1185
+ * @param c
1186
+ * Character.
1187
+ *
1188
+ * @returns
1189
+ * Index for the character or undefined if the character is not in the character set.
1190
+ */
1191
+ characterIndex(c) {
1192
+ return this._characterSetMap.get(c);
1193
+ }
1194
+ /**
1195
+ * Get the indexes for all characters in a string.
1196
+ *
1197
+ * @param s
1198
+ * String.
1199
+ *
1200
+ * @returns
1201
+ * Array of indexes for each character or undefined if the character is not in the character set.
1202
+ */
1203
+ characterIndexes(s) {
1204
+ return [...s].map((c) => this._characterSetMap.get(c));
1205
+ }
1206
+ /**
1207
+ * Convert a component definition to a string or undefined. Checks the type of the component and makes the callback
1208
+ * if required.
1209
+ *
1210
+ * @param component
1211
+ * Component definition as a string, callback, or undefined.
1212
+ *
1213
+ * @returns
1214
+ * Component as a string or undefined.
1215
+ */
1216
+ static componentToString(component) {
1217
+ return typeof component === "function" ? component() : component;
1218
+ }
1219
+ /**
1220
+ * Validate that an exclusion is supported. If not, an error is thrown.
1221
+ *
1222
+ * @param exclusion
1223
+ * Exclusion.
1224
+ */
1225
+ validateExclusion(exclusion) {
1226
+ if (exclusion !== 0 /* None */ && !this._exclusionSupport.includes(exclusion)) {
1227
+ throw new RangeError(i18n_default.t("CharacterSetValidator.exclusionNotSupported", {
1228
+ ns: utilityNS,
1229
+ exclusion
1230
+ }));
1231
+ }
1232
+ }
1233
+ /**
1234
+ * Validate a string. If the string violates the character set or any of the character set validation parameters, an
1235
+ * error is thrown.
1236
+ *
1237
+ * @param s
1238
+ * String.
1239
+ *
1240
+ * @param validation
1241
+ * Character set validation parameters.
1242
+ */
1243
+ validate(s, validation) {
1244
+ const length = s.length;
1245
+ const minimumLength = validation?.minimumLength;
1246
+ const maximumLength = validation?.maximumLength;
1247
+ if (minimumLength !== void 0 && length < minimumLength) {
1248
+ let errorMessage;
1249
+ if (maximumLength !== void 0 && maximumLength === minimumLength) {
1250
+ errorMessage = i18n_default.t(validation?.component === void 0 ? "CharacterSetValidator.lengthMustBeEqualTo" : "CharacterSetValidator.lengthOfComponentMustBeEqualTo", {
1251
+ ns: utilityNS,
1252
+ component: _CharacterSetValidator.componentToString(validation?.component),
1253
+ length,
1254
+ exactLength: minimumLength
1255
+ });
1256
+ } else {
1257
+ errorMessage = i18n_default.t(validation?.component === void 0 ? "CharacterSetValidator.lengthMustBeGreaterThanOrEqualTo" : "CharacterSetValidator.lengthOfComponentMustBeGreaterThanOrEqualTo", {
1258
+ ns: utilityNS,
1259
+ component: _CharacterSetValidator.componentToString(validation?.component),
1260
+ length,
1261
+ minimumLength
1262
+ });
1263
+ }
1264
+ throw new RangeError(errorMessage);
1265
+ }
1266
+ if (maximumLength !== void 0 && length > maximumLength) {
1267
+ throw new RangeError(i18n_default.t(validation?.component === void 0 ? "CharacterSetValidator.lengthMustBeLessThanOrEqualTo" : "CharacterSetValidator.lengthOfComponentMustBeLessThanOrEqualTo", {
1268
+ ns: utilityNS,
1269
+ component: _CharacterSetValidator.componentToString(validation?.component),
1270
+ length,
1271
+ maximumLength
1272
+ }));
1273
+ }
1274
+ const index = this.characterIndexes(s).findIndex((characterIndex) => characterIndex === void 0);
1275
+ if (index !== -1) {
1276
+ throw new RangeError(i18n_default.t(validation?.component === void 0 ? "CharacterSetValidator.invalidCharacterAtPosition" : "CharacterSetValidator.invalidCharacterAtPositionOfComponent", {
1277
+ ns: utilityNS,
1278
+ component: _CharacterSetValidator.componentToString(validation?.component),
1279
+ c: s.charAt(index),
1280
+ position: index + (validation?.positionOffset ?? 0) + 1
1281
+ }));
1282
+ }
1283
+ if (validation?.exclusion !== void 0) {
1284
+ this.validateExclusion(validation.exclusion);
1285
+ switch (validation.exclusion) {
1286
+ case 0 /* None */:
1287
+ break;
1288
+ case 1 /* FirstZero */:
1289
+ if (s.startsWith("0")) {
1290
+ throw new RangeError(i18n_default.t(validation.component === void 0 ? "CharacterSetValidator.invalidCharacterAtPosition" : "CharacterSetValidator.invalidCharacterAtPositionOfComponent", {
1291
+ ns: utilityNS,
1292
+ component: _CharacterSetValidator.componentToString(validation.component),
1293
+ c: "0",
1294
+ position: (validation.positionOffset ?? 0) + 1
1295
+ }));
1296
+ }
1297
+ break;
1298
+ case 2 /* AllNumeric */:
1299
+ _CharacterSetValidator.NOT_ALL_NUMERIC_VALIDATOR.validate(s);
1300
+ break;
1301
+ }
1302
+ }
1303
+ }
1304
+ };
1305
+ var CharacterSetCreator = class _CharacterSetCreator extends CharacterSetValidator {
1306
+ /**
1307
+ * Maximum string length supported.
1308
+ */
1309
+ static MAXIMUM_STRING_LENGTH = 40;
1310
+ /**
1311
+ * Powers of 10 from 1 (`10**0`) to `10**MAXIMUM_STRING_LENGTH`.
1312
+ */
1313
+ static _powersOf10 = _CharacterSetCreator.createPowersOf(10);
1314
+ /**
1315
+ * Create powers of a given base from 1 (`base**0`) to `base**MAXIMUM_STRING_LENGTH`.
1316
+ *
1317
+ * @param base
1318
+ * Number base.
1319
+ *
1320
+ * @returns
1321
+ * Array of powers of base.
1322
+ */
1323
+ static createPowersOf(base) {
1324
+ const powersOf = new Array(this.MAXIMUM_STRING_LENGTH + 1);
1325
+ const baseN = BigInt(base);
1326
+ for (let index = 0, powerOf = 1n; index <= this.MAXIMUM_STRING_LENGTH; index++, powerOf *= baseN) {
1327
+ powersOf[index] = powerOf;
1328
+ }
1329
+ return powersOf;
1330
+ }
1331
+ /**
1332
+ * Get a power of 10.
1333
+ *
1334
+ * @param power
1335
+ * Power.
1336
+ *
1337
+ * @returns
1338
+ * `10**power`.
1339
+ */
1340
+ static powerOf10(power) {
1341
+ return this._powersOf10[power];
1342
+ }
1343
+ /**
1344
+ * Character set size as big integer, cached for performance purposes.
1345
+ */
1346
+ _characterSetSizeN;
1347
+ /**
1348
+ * Character set size minus 1 as big integer, cached for performance purposes.
1349
+ */
1350
+ _characterSetSizeMinusOneN;
1351
+ /**
1352
+ * Domains for every length for every supported {@link Exclusion}.
1353
+ */
1354
+ _exclusionDomains;
1355
+ /**
1356
+ * Values that would generate all zeros in the created string.
1357
+ */
1358
+ _allZerosValues;
1359
+ /**
1360
+ * Constructor.
1361
+ *
1362
+ * @param characterSet
1363
+ * Character set. Each element is a single-character string, unique within the array, that defines the character
1364
+ * set.
1365
+ *
1366
+ * @param exclusionSupport
1367
+ * Exclusions supported by the character set. All character sets implicitly support {@link Exclusion.None}.
1368
+ */
1369
+ constructor(characterSet, ...exclusionSupport) {
1370
+ super(characterSet, ...exclusionSupport);
1371
+ this._characterSetSizeN = BigInt(this.characterSetSize);
1372
+ this._characterSetSizeMinusOneN = BigInt(this.characterSetSize - 1);
1373
+ const exclusionDomains = [];
1374
+ const exclusionNoneDomains = _CharacterSetCreator.createPowersOf(this.characterSetSize);
1375
+ exclusionDomains[0 /* None */] = exclusionNoneDomains;
1376
+ if (exclusionSupport.includes(1 /* FirstZero */)) {
1377
+ if (characterSet[0] !== "0") {
1378
+ throw new RangeError(i18n_default.t("CharacterSetValidator.firstZeroFirstCharacter", {
1379
+ ns: utilityNS
1380
+ }));
1381
+ }
1382
+ const exclusionFirstZeroDomains = new Array(_CharacterSetCreator.MAXIMUM_STRING_LENGTH + 1);
1383
+ exclusionFirstZeroDomains[0] = 0n;
1384
+ for (let index = 1; index <= _CharacterSetCreator.MAXIMUM_STRING_LENGTH; index++) {
1385
+ exclusionFirstZeroDomains[index] = this._characterSetSizeMinusOneN * exclusionNoneDomains[index - 1];
1386
+ }
1387
+ exclusionDomains[1 /* FirstZero */] = exclusionFirstZeroDomains;
1388
+ }
1389
+ if (exclusionSupport.includes(2 /* AllNumeric */)) {
1390
+ const exclusionAllNumericDomains = new Array(_CharacterSetCreator.MAXIMUM_STRING_LENGTH + 1);
1391
+ const numberIndexes = this.characterIndexes("0123456789");
1392
+ let expectedNumberIndex = numberIndexes[0];
1393
+ for (const numberIndex of numberIndexes) {
1394
+ if (numberIndex === void 0 || numberIndex !== expectedNumberIndex) {
1395
+ throw new RangeError(i18n_default.t("CharacterSetValidator.allNumericAllNumericCharacters", {
1396
+ ns: utilityNS
1397
+ }));
1398
+ }
1399
+ expectedNumberIndex = numberIndex + 1;
1400
+ }
1401
+ const zeroIndex = BigInt(numberIndexes[0]);
1402
+ const allZerosValues = new Array(_CharacterSetCreator.MAXIMUM_STRING_LENGTH + 1);
1403
+ let allZerosValue = 0n;
1404
+ for (let index = 0; index <= _CharacterSetCreator.MAXIMUM_STRING_LENGTH; index++) {
1405
+ exclusionAllNumericDomains[index] = exclusionNoneDomains[index] - _CharacterSetCreator.powerOf10(index);
1406
+ allZerosValues[index] = allZerosValue;
1407
+ allZerosValue = allZerosValue * this._characterSetSizeN + zeroIndex;
1408
+ }
1409
+ this._allZerosValues = allZerosValues;
1410
+ exclusionDomains[2 /* AllNumeric */] = exclusionAllNumericDomains;
1411
+ } else {
1412
+ this._allZerosValues = [];
1413
+ }
1414
+ this._exclusionDomains = exclusionDomains;
1415
+ }
1416
+ /**
1417
+ * Get a power of character set size.
1418
+ *
1419
+ * @param power
1420
+ * Power.
1421
+ *
1422
+ * @returns
1423
+ * `characterSetSize**power`.
1424
+ */
1425
+ powerOfSize(power) {
1426
+ return this._exclusionDomains[0 /* None */][power];
1427
+ }
1428
+ /**
1429
+ * Determine the shift required to skip all all-numeric strings up to the value.
1430
+ *
1431
+ * @param shiftForward
1432
+ * True to shift forward (value to string), false to shift backward (string to value).
1433
+ *
1434
+ * @param length
1435
+ * Length of string for which to get the all-numeric shift.
1436
+ *
1437
+ * @param value
1438
+ * Value for which to get the all-numeric shift.
1439
+ *
1440
+ * @returns
1441
+ * Shift required to skip all all-numeric strings.
1442
+ */
1443
+ allNumericShift(shiftForward, length, value) {
1444
+ let shift;
1445
+ if (length === 0) {
1446
+ if (!shiftForward && value < 10n) {
1447
+ throw new RangeError(i18n_default.t("CharacterSetValidator.stringMustNotBeAllNumeric", {
1448
+ ns: utilityNS
1449
+ }));
1450
+ }
1451
+ shift = 10n;
1452
+ } else {
1453
+ const powerOfSize = this.powerOfSize(length);
1454
+ const powerOf10 = _CharacterSetCreator.powerOf10(length);
1455
+ const gap = shiftForward ? powerOfSize - powerOf10 : powerOfSize;
1456
+ const gaps = value / gap;
1457
+ if (gaps >= 10n) {
1458
+ shift = _CharacterSetCreator.powerOf10(length + 1);
1459
+ } else {
1460
+ shift = gaps * powerOf10 + this.allNumericShift(shiftForward, length - 1, value - gaps * gap);
1461
+ }
1462
+ }
1463
+ return shift;
1464
+ }
1465
+ /**
1466
+ * Validate that a length is less than or equal to {@link MAXIMUM_STRING_LENGTH}. If not, an error is thrown.
1467
+ *
1468
+ * @param length
1469
+ * Length.
1470
+ */
1471
+ validateLength(length) {
1472
+ if (length < 0) {
1473
+ throw new RangeError(i18n_default.t("CharacterSetValidator.lengthMustBeGreaterThanOrEqualTo", {
1474
+ ns: utilityNS,
1475
+ length,
1476
+ minimumLength: 0
1477
+ }));
1478
+ }
1479
+ if (length > _CharacterSetCreator.MAXIMUM_STRING_LENGTH) {
1480
+ throw new RangeError(i18n_default.t("CharacterSetValidator.lengthMustBeLessThanOrEqualTo", {
1481
+ ns: utilityNS,
1482
+ length,
1483
+ maximumLength: _CharacterSetCreator.MAXIMUM_STRING_LENGTH
1484
+ }));
1485
+ }
1486
+ }
1487
+ // eslint-disable-next-line jsdoc/require-jsdoc -- Implementation of overloaded signatures.
1488
+ create(length, valueOrValues, exclusion = 0 /* None */, tweak, creationCallback) {
1489
+ this.validateLength(length);
1490
+ this.validateExclusion(exclusion);
1491
+ const allZerosValue = exclusion === 2 /* AllNumeric */ ? this._allZerosValues[length] : 0n;
1492
+ const transformer = Transformer.get(this._exclusionDomains[exclusion][length], tweak);
1493
+ return transformer.forward(valueOrValues, (transformedValue, index) => {
1494
+ let s = "";
1495
+ if (length !== 0) {
1496
+ let convertValue = transformedValue;
1497
+ if (exclusion === 2 /* AllNumeric */ && convertValue >= allZerosValue) {
1498
+ convertValue = convertValue + this.allNumericShift(true, length, convertValue - allZerosValue);
1499
+ }
1500
+ for (let position = length - 1; position > 0; position--) {
1501
+ const nextConvertValue = convertValue / this._characterSetSizeN;
1502
+ s = this.character(Number(convertValue - nextConvertValue * this._characterSetSizeN)) + s;
1503
+ convertValue = nextConvertValue;
1504
+ }
1505
+ s = this.character(exclusion === 1 /* FirstZero */ ? Number(convertValue % this._characterSetSizeMinusOneN) + 1 : Number(convertValue % this._characterSetSizeN)) + s;
1506
+ }
1507
+ return creationCallback !== void 0 ? creationCallback(s, index) : s;
1508
+ });
1509
+ }
1510
+ /**
1511
+ * Determine the value for a string.
1512
+ *
1513
+ * @param s
1514
+ * String.
1515
+ *
1516
+ * @param exclusion
1517
+ * Strings excluded from the range of inputs. See {@link Exclusion} for possible values and their meaning.
1518
+ *
1519
+ * @param tweak
1520
+ * If provided, the numerical value of the string was "tweaked" using an {@link EncryptionTransformer | encryption
1521
+ * transformer}.
1522
+ *
1523
+ * @returns
1524
+ * Numeric value of the string.
1525
+ */
1526
+ valueFor(s, exclusion = 0 /* None */, tweak) {
1527
+ const length = s.length;
1528
+ this.validateLength(length);
1529
+ this.validateExclusion(exclusion);
1530
+ const characterSetSizeN = BigInt(this.characterSetSize);
1531
+ let value = this.characterIndexes(s).reduce((accumulator, characterIndex, index) => {
1532
+ if (characterIndex === void 0) {
1533
+ throw new RangeError(i18n_default.t("CharacterSetValidator.invalidCharacterAtPosition", {
1534
+ ns: utilityNS,
1535
+ c: s.charAt(index),
1536
+ position: index + 1
1537
+ }));
1538
+ }
1539
+ let value2;
1540
+ if (index === 0 && exclusion === 1 /* FirstZero */) {
1541
+ if (characterIndex === 0) {
1542
+ throw new RangeError(i18n_default.t("CharacterSetValidator.invalidCharacterAtPosition", {
1543
+ ns: utilityNS,
1544
+ c: "0",
1545
+ position: 1
1546
+ }));
1547
+ }
1548
+ value2 = BigInt(characterIndex - 1);
1549
+ } else {
1550
+ value2 = accumulator * characterSetSizeN + BigInt(characterIndex);
1551
+ }
1552
+ return value2;
1553
+ }, 0n);
1554
+ if (exclusion === 2 /* AllNumeric */) {
1555
+ const allZerosValue = this._allZerosValues[length];
1556
+ if (value >= allZerosValue) {
1557
+ value -= this.allNumericShift(false, length, value - allZerosValue);
1558
+ }
1559
+ }
1560
+ return Transformer.get(this._exclusionDomains[exclusion][length], tweak).reverse(value);
1561
+ }
1562
+ };
1563
+ var NUMERIC_CREATOR = new CharacterSetCreator([
1564
+ "0",
1565
+ "1",
1566
+ "2",
1567
+ "3",
1568
+ "4",
1569
+ "5",
1570
+ "6",
1571
+ "7",
1572
+ "8",
1573
+ "9"
1574
+ ], 1 /* FirstZero */);
1575
+ var HEXADECIMAL_CREATOR = new CharacterSetCreator([
1576
+ "0",
1577
+ "1",
1578
+ "2",
1579
+ "3",
1580
+ "4",
1581
+ "5",
1582
+ "6",
1583
+ "7",
1584
+ "8",
1585
+ "9",
1586
+ "A",
1587
+ "B",
1588
+ "C",
1589
+ "D",
1590
+ "E",
1591
+ "F"
1592
+ ], 1 /* FirstZero */, 2 /* AllNumeric */);
1593
+ var ALPHABETIC_CREATOR = new CharacterSetCreator([
1594
+ "A",
1595
+ "B",
1596
+ "C",
1597
+ "D",
1598
+ "E",
1599
+ "F",
1600
+ "G",
1601
+ "H",
1602
+ "I",
1603
+ "J",
1604
+ "K",
1605
+ "L",
1606
+ "M",
1607
+ "N",
1608
+ "O",
1609
+ "P",
1610
+ "Q",
1611
+ "R",
1612
+ "S",
1613
+ "T",
1614
+ "U",
1615
+ "V",
1616
+ "W",
1617
+ "X",
1618
+ "Y",
1619
+ "Z"
1620
+ ]);
1621
+ var ALPHANUMERIC_CREATOR = new CharacterSetCreator([
1622
+ "0",
1623
+ "1",
1624
+ "2",
1625
+ "3",
1626
+ "4",
1627
+ "5",
1628
+ "6",
1629
+ "7",
1630
+ "8",
1631
+ "9",
1632
+ "A",
1633
+ "B",
1634
+ "C",
1635
+ "D",
1636
+ "E",
1637
+ "F",
1638
+ "G",
1639
+ "H",
1640
+ "I",
1641
+ "J",
1642
+ "K",
1643
+ "L",
1644
+ "M",
1645
+ "N",
1646
+ "O",
1647
+ "P",
1648
+ "Q",
1649
+ "R",
1650
+ "S",
1651
+ "T",
1652
+ "U",
1653
+ "V",
1654
+ "W",
1655
+ "X",
1656
+ "Y",
1657
+ "Z"
1658
+ ], 1 /* FirstZero */, 2 /* AllNumeric */);
1659
+ export {
1660
+ ALPHABETIC_CREATOR,
1661
+ ALPHANUMERIC_CREATOR,
1662
+ CharacterSetCreator,
1663
+ CharacterSetValidator,
1664
+ EncryptionTransformer,
1665
+ Exclusion,
1666
+ HEXADECIMAL_CREATOR,
1667
+ IdentityTransformer,
1668
+ IteratorProxy,
1669
+ NUMERIC_CREATOR,
1670
+ RecordValidator,
1671
+ RegExpValidator,
1672
+ Sequencer,
1673
+ Transformer
1674
+ };