@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.
@@ -0,0 +1,909 @@
1
+ /**
2
+ * Iterator proxy. In environments where
3
+ * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator#iterator_helpers |
4
+ * iterator helpers} are supported, this references the {@link Iterator} variable directly. Otherwise, it references an
5
+ * implementation of "from" that uses an internally-defined iterator proxy object.
6
+ *
7
+ * Client applications should **not** rely on long-term availability of this variable as it will be removed once there
8
+ * is widespread support for iterator helpers.
9
+ */
10
+ declare const IteratorProxy: Pick<IteratorConstructor, "from">;
11
+
12
+ /**
13
+ * Sequencer. Defines an ascending or descending sequence of big integers implemented as an iterable iterator.
14
+ */
15
+ declare class Sequencer implements Iterable<bigint>, IterableIterator<bigint> {
16
+ /**
17
+ * Start value (inclusive).
18
+ */
19
+ private readonly _startValue;
20
+ /**
21
+ * End value (exclusive).
22
+ */
23
+ private readonly _endValue;
24
+ /**
25
+ * Count of values.
26
+ */
27
+ private readonly _count;
28
+ /**
29
+ * Delta to the next value; equal to the sign of the count.
30
+ */
31
+ private readonly _nextDelta;
32
+ /**
33
+ * Minimum value (inclusive).
34
+ */
35
+ private readonly _minValue;
36
+ /**
37
+ * Maximum value (inclusive).
38
+ */
39
+ private readonly _maxValue;
40
+ /**
41
+ * Next value.
42
+ */
43
+ private _nextValue;
44
+ /**
45
+ * Constructor.
46
+ *
47
+ * @param startValue
48
+ * Start value.
49
+ *
50
+ * @param count
51
+ * Count of values. If count is zero or positive, iteration ascends from start value, otherwise it descends from
52
+ * start value.
53
+ */
54
+ constructor(startValue: number | bigint, count: number);
55
+ /**
56
+ * Get the start value (inclusive).
57
+ */
58
+ get startValue(): bigint;
59
+ /**
60
+ * Get the end value (exclusive).
61
+ */
62
+ get endValue(): bigint;
63
+ /**
64
+ * Get the count of values.
65
+ */
66
+ get count(): number;
67
+ /**
68
+ * Get the minimum value (inclusive).
69
+ */
70
+ get minValue(): bigint;
71
+ /**
72
+ * Get the maximum value (inclusive).
73
+ */
74
+ get maxValue(): bigint;
75
+ /**
76
+ * Iterable implementation.
77
+ *
78
+ * @returns
79
+ * this
80
+ */
81
+ [Symbol.iterator](): this;
82
+ /**
83
+ * Iterator implementation.
84
+ *
85
+ * @returns
86
+ * Iterator result. If iterator is exhausted, the value is absolute value of the count.
87
+ */
88
+ next(): IteratorResult<bigint, number>;
89
+ /**
90
+ * Reset the iterator.
91
+ */
92
+ reset(): void;
93
+ }
94
+
95
+ /**
96
+ * Transformation callback, used to convert transformed value to its final value.
97
+ *
98
+ * @template T
99
+ * Type returned by callback.
100
+ *
101
+ * @param transformedValue
102
+ * Transformed value.
103
+ *
104
+ * @param index
105
+ * Index in sequence transformation (0 for single transformation).
106
+ *
107
+ * @returns
108
+ * Final value.
109
+ */
110
+ type TransformationCallback<T> = (transformedValue: bigint, index: number) => T;
111
+ /**
112
+ * Transformer that transforms values in a numeric domain to values in a range equal to the domain or to another range
113
+ * defined by a callback function. In other words, the domain determines valid input values and, without a callback, the
114
+ * range of valid output values.
115
+ *
116
+ * The concept is similar to {@link https://en.wikipedia.org/wiki/Format-preserving_encryption | format-preserving
117
+ * encryption}, where input values within a specified domain (e.g., {@link
118
+ * https://en.wikipedia.org/wiki/Payment_card_number | payment card numbers} ranging from 8-19 digits) are transformed
119
+ * into values in the same domain, typically for storage in a database where the data type and length are already fixed
120
+ * and exfiltration of the data can have significant repercussions.
121
+ *
122
+ * Two subclasses are supported directly by this class: {@link IdentityTransformer} (which operates based on a domain
123
+ * only) and {@link EncryptionTransformer} (which operates based on a domain and a tweak). If an application is expected
124
+ * to make repeated use of a transformer with the same domain and (optional) tweak and can't manage the transformer
125
+ * object, an in-memory cache is available via the {@link get} method. Properties in {@link IdentityTransformer} and
126
+ * {@link EncryptionTransformer} are read-only once constructed, so there is no issue with their shared use.
127
+ */
128
+ declare abstract class Transformer {
129
+ /**
130
+ * Transformers cache, mapping a domain to another map, which maps an optional tweak to a transformer.
131
+ */
132
+ private static readonly TRANSFORMER_MAPS_MAP;
133
+ /**
134
+ * Domain.
135
+ */
136
+ private readonly _domain;
137
+ /**
138
+ * Constructor.
139
+ *
140
+ * @param domain
141
+ * Domain.
142
+ */
143
+ constructor(domain: number | bigint);
144
+ /**
145
+ * Get a transformer, constructing it if necessary. The type returned is {@link IdentityTransformer} if tweak is
146
+ * undefined, {@link EncryptionTransformer} if tweak is defined. Note that although an {@link EncryptionTransformer}
147
+ * with a zero tweak operates as an {@link IdentityTransformer}, {@link EncryptionTransformer} is still the type
148
+ * returned if a zero tweak is explicitly specified.
149
+ *
150
+ * @param domain
151
+ * Domain.
152
+ *
153
+ * @param tweak
154
+ * Tweak.
155
+ *
156
+ * @returns
157
+ * {@link IdentityTransformer} if tweak is undefined, {@link EncryptionTransformer} if tweak is defined.
158
+ */
159
+ static get(domain: number | bigint, tweak?: number | bigint): Transformer;
160
+ /**
161
+ * Get the domain.
162
+ */
163
+ get domain(): bigint;
164
+ /**
165
+ * Validate that a value is within the domain.
166
+ *
167
+ * @param value
168
+ * Value.
169
+ */
170
+ private validate;
171
+ /**
172
+ * Do the work of transforming a value forward.
173
+ *
174
+ * @param value
175
+ * Value.
176
+ *
177
+ * @returns
178
+ * Transformed value.
179
+ */
180
+ protected abstract doForward(value: bigint): bigint;
181
+ /**
182
+ * Transform a value forward.
183
+ *
184
+ * @param value
185
+ * Value.
186
+ *
187
+ * @returns
188
+ * Transformed value.
189
+ */
190
+ forward(value: number | bigint): bigint;
191
+ /**
192
+ * Transform a value forward.
193
+ *
194
+ * @template T
195
+ * Type returned by transformation callback.
196
+ *
197
+ * @param value
198
+ * Value.
199
+ *
200
+ * @param transformationCallback
201
+ * Called after the value is transformed to convert it to its final value.
202
+ *
203
+ * @returns
204
+ * Value transformed into object.
205
+ */
206
+ forward<T>(value: number | bigint, transformationCallback: TransformationCallback<T>): T;
207
+ /**
208
+ * Transform values forward.
209
+ *
210
+ * @param values
211
+ * Values. If this is an instance of {@link Sequencer}, the minimum and maximum values are validated prior to
212
+ * transformation. Otherwise, the individual values are validated at the time of transformation.
213
+ *
214
+ * @returns
215
+ * Transformed values.
216
+ */
217
+ forward(values: Iterable<number | bigint>): IterableIterator<bigint>;
218
+ /**
219
+ * Transform values forward.
220
+ *
221
+ * @template T
222
+ * Type returned by transformation callback.
223
+ *
224
+ * @param values
225
+ * Values. If this is an instance of {@link Sequencer}, the minimum and maximum values are validated prior to
226
+ * transformation. Otherwise, the individual values are validated at the time of transformation.
227
+ *
228
+ * @param transformationCallback
229
+ * Called after each value is transformed to convert it to its final value.
230
+ *
231
+ * @returns
232
+ * Values transformed into objects.
233
+ */
234
+ forward<T>(values: Iterable<number | bigint>, transformationCallback: TransformationCallback<T>): IterableIterator<T>;
235
+ /**
236
+ * Transform a value or values forward. This signature exists to allow similar overloaded methods in other classes
237
+ * to call this method correctly.
238
+ *
239
+ * @param valueOrValues
240
+ *
241
+ * @returns
242
+ */
243
+ forward(valueOrValues: number | bigint | Iterable<number | bigint>): bigint | IterableIterator<bigint>;
244
+ /**
245
+ * Transform a value or values forward. This signature exists to allow similar overloaded methods in other classes
246
+ * to call this method correctly.
247
+ *
248
+ * @template T
249
+ *
250
+ * @param valueOrValues
251
+ *
252
+ * @param transformationCallback
253
+ *
254
+ * @returns
255
+ */
256
+ forward<T>(valueOrValues: number | bigint | Iterable<number | bigint>, transformationCallback: TransformationCallback<T>): T | IterableIterator<T>;
257
+ /**
258
+ * Do the work of transforming a value in reverse.
259
+ *
260
+ * @param transformedValue
261
+ * Transformed value.
262
+ *
263
+ * @returns
264
+ * Value.
265
+ */
266
+ protected abstract doReverse(transformedValue: bigint): bigint;
267
+ /**
268
+ * Transform a value in reverse.
269
+ *
270
+ * @param transformedValue
271
+ * Transformed value.
272
+ *
273
+ * @returns
274
+ * Value.
275
+ */
276
+ reverse(transformedValue: number | bigint): bigint;
277
+ }
278
+ /**
279
+ * Identity transformer. Values are transformed to themselves.
280
+ */
281
+ declare class IdentityTransformer extends Transformer {
282
+ /**
283
+ * @inheritDoc
284
+ */
285
+ protected doForward(value: bigint): bigint;
286
+ /**
287
+ * @inheritDoc
288
+ */
289
+ protected doReverse(transformedValue: bigint): bigint;
290
+ }
291
+ /**
292
+ * Encryption transformer. Values are transformed using repeated shuffle and xor operations. The underlying operations
293
+ * are similar to those found in many cryptography algorithms, particularly AES. While sufficient for obfuscation of
294
+ * numeric sequences (e.g., serial number generation, below), if true format-preserving encryption is required, a more
295
+ * robust algorithm such as {@link https://doi.org/10.6028/NIST.SP.800-38Gr1-draft | FF1} is recommended.
296
+ *
297
+ * The purpose of the encryption transformer is to generate pseudo-random values in a deterministic manner to obscure
298
+ * the sequence of values generated over time. A typical example is for serial number generation, where knowledge of the
299
+ * sequence can infer production volumes (e.g., serial number 1000 implies that at least 1,000 units have been
300
+ * manufactured) or can be used in counterfeiting (e.g., a counterfeiter can generate serial numbers 1001, 1002, ...
301
+ * with reasonable confidence that they would be valid if queried).
302
+ *
303
+ * The domain and the tweak together determine the encryption key, which in turn determines the number of rounds of
304
+ * shuffle and xor operations. The minimum number of rounds is 4, except where the domain is less than or equal to 256,
305
+ * which results in single-byte operations. To ensure that the operations are effective for single-byte domains, the
306
+ * number of rounds is 1 and only the xor operation is applied (shuffling a single byte is an identity operation).
307
+ *
308
+ * Another exception is when there is a tweak value of 0; this results in identity operations where the output value is
309
+ * identical to the input value, as no shuffle or xor takes place.
310
+ */
311
+ declare class EncryptionTransformer extends Transformer {
312
+ /**
313
+ * Individual bits, pre-calculated for performance.
314
+ */
315
+ private static readonly BITS;
316
+ /**
317
+ * Inverse individual bits, pre-calculated for performance.
318
+ */
319
+ private static readonly INVERSE_BITS;
320
+ /**
321
+ * Number of bytes covered by the domain.
322
+ */
323
+ private readonly _domainBytes;
324
+ /**
325
+ * Tweak.
326
+ */
327
+ private readonly _tweak;
328
+ /**
329
+ * Xor bytes array generated from the domain and tweak.
330
+ */
331
+ private readonly _xorBytes;
332
+ /**
333
+ * Bits array generated from the domain and tweak.
334
+ */
335
+ private readonly _bits;
336
+ /**
337
+ * Inverse bits array generated from the domain and tweak.
338
+ */
339
+ private readonly _inverseBits;
340
+ /**
341
+ * Number of rounds (length of arrays) generated from the domain and tweak.
342
+ */
343
+ private readonly _rounds;
344
+ /**
345
+ * Constructor.
346
+ *
347
+ * @param domain
348
+ * Domain.
349
+ *
350
+ * @param tweak
351
+ * Tweak.
352
+ */
353
+ constructor(domain: number | bigint, tweak: number | bigint);
354
+ /**
355
+ * Get the tweak.
356
+ */
357
+ get tweak(): bigint;
358
+ /**
359
+ * Convert a value to a byte array big enough to handle the entire domain.
360
+ *
361
+ * @param value
362
+ * Value.
363
+ *
364
+ * @returns
365
+ * Big-endian byte array equivalent to the value.
366
+ */
367
+ private valueToBytes;
368
+ /**
369
+ * Convert a byte array to a value.
370
+ *
371
+ * @param bytes
372
+ * Big-endian byte array equivalent to the value.
373
+ *
374
+ * @returns
375
+ * Value.
376
+ */
377
+ private static bytesToValue;
378
+ /**
379
+ * Shuffle a byte array.
380
+ *
381
+ * The input array to the forward operation (output from the reverse operation) is `bytes` and the output array from
382
+ * the forward operation (input to the reverse operation) is `bytes'`.
383
+ *
384
+ * The shuffle operation starts by testing the bit at `bits[round]` for each `byte` in `bytes`. The indexes for all
385
+ * bytes with that bit set are put into one array (`shuffleIndexes1`) and the rest are put into another
386
+ * (`shuffleIndexes0`). The two arrays are concatenated and used to shuffle the input array, using their values
387
+ * (`shuffleIndex`) and the indexes of those values (`index`) in the concatenated array.
388
+ *
389
+ * Forward shuffling moves the entry at `shuffleIndex` to the `index` position.
390
+ *
391
+ * Reverse shuffling moves the entry at `index` to the `shuffleIndex` position.
392
+ *
393
+ * As each byte is moved, the bit at `bits[round]` is preserved in its original position. This ensures that the
394
+ * process is reversible.
395
+ *
396
+ * @param bytes
397
+ * Byte array.
398
+ *
399
+ * @param round
400
+ * Round number.
401
+ *
402
+ * @param forward
403
+ * True if operating forward (encrypting), false if operating in reverse (decrypting).
404
+ *
405
+ * @returns
406
+ * Shuffled byte array.
407
+ */
408
+ private shuffle;
409
+ /**
410
+ * Xor a byte array.
411
+ *
412
+ * The input array to the forward operation (output from the reverse operation) is `bytes` and the output array from
413
+ * the forward operation (input to the reverse operation) is `bytes'`.
414
+ *
415
+ * Forward:
416
+ * - `bytes'[0] = bytes[0] ^ xorBytes[round]`
417
+ * - `bytes'[1] = bytes[1] ^ bytes'[0]`
418
+ * - `bytes'[2] = bytes[2] ^ bytes'[1]`
419
+ * - `...`
420
+ * - `bytes'[domainBytes - 1] = bytes[domainBytes - 1] ^ bytes'[domainBytes - 2]`
421
+ *
422
+ * Reverse:
423
+ * - `bytes[0] = bytes'[0] ^ xorBytes[round]`
424
+ * - `bytes[1] = bytes'[1] ^ bytes'[0]`
425
+ * - `bytes[2] = bytes'[2] ^ bytes'[1]`
426
+ * - `...`
427
+ * - `bytes[domainBytes - 1] = bytes'[domainBytes - 1] ^ bytes'[domainBytes - 2]`
428
+ *
429
+ * @param bytes
430
+ * Byte array.
431
+ *
432
+ * @param round
433
+ * Round number.
434
+ *
435
+ * @param forward
436
+ * True if operating forward (encrypting), false if operating in reverse (decrypting).
437
+ *
438
+ * @returns
439
+ * Xored byte array.
440
+ */
441
+ private xor;
442
+ /**
443
+ * @inheritDoc
444
+ */
445
+ protected doForward(value: bigint): bigint;
446
+ /**
447
+ * @inheritDoc
448
+ */
449
+ protected doReverse(transformedValue: bigint): bigint;
450
+ }
451
+
452
+ /**
453
+ * String validation interface. To ensure signature compatibility in implementing classes, string validation is
454
+ * controlled by validation interfaces specific to each validator type.
455
+ */
456
+ interface StringValidation {
457
+ }
458
+ /**
459
+ * String validator interface.
460
+ */
461
+ interface StringValidator<V extends StringValidation = StringValidation> {
462
+ /**
463
+ * Validate a string and throw an error if validation fails.
464
+ *
465
+ * @param s
466
+ * String.
467
+ *
468
+ * @param validation
469
+ * String validation parameters.
470
+ */
471
+ validate: (s: string, validation?: V) => void;
472
+ }
473
+
474
+ /**
475
+ * Regular expression validator. The regular expression applies to the full string only if constructed as such. For
476
+ * example, <code>&#x2F;\d&#x2A;&#x2F;</code> (0 or more digits) matches every string, <code>&#x2F;\d+&#x2F;</code>
477
+ * (1 or more digits) matches strings with at least one digit, <code>&#x2F;^\d&#x2A;$&#x2F;</code> matches strings that
478
+ * are all digits or empty, and <code>&#x2F;^\d+$&#x2F;</code> matches strings that are all digits and not empty.
479
+ *
480
+ * Clients of this class are recommended to override the {@link createErrorMessage} method create a more suitable error
481
+ * message for their use case.
482
+ */
483
+ declare class RegExpValidator implements StringValidator {
484
+ /**
485
+ * Regular expression.
486
+ */
487
+ private readonly _regExp;
488
+ /**
489
+ * Constructor.
490
+ *
491
+ * @param regExp
492
+ * Regular expression. See {@link RegExpValidator | class documentation} for notes.
493
+ */
494
+ constructor(regExp: RegExp);
495
+ /**
496
+ * Get the regular expression.
497
+ */
498
+ get regExp(): RegExp;
499
+ /**
500
+ * Create an error message for a string. The generic error message is sufficient for many use cases but a more
501
+ * domain-specific error message, possibly including the pattern itself, is often required.
502
+ *
503
+ * @param s
504
+ * String.
505
+ *
506
+ * @returns
507
+ * Error message.
508
+ */
509
+ protected createErrorMessage(s: string): string;
510
+ /**
511
+ * @inheritDoc
512
+ */
513
+ validate(s: string): void;
514
+ }
515
+
516
+ /**
517
+ * Record validator. Validation is performed against a record with a string key type and throws an error if the key is
518
+ * not found.
519
+ */
520
+ declare class RecordValidator<T> implements StringValidator {
521
+ /**
522
+ * Type name for error message.
523
+ */
524
+ private readonly _typeName;
525
+ /**
526
+ * Record in which to look up keys.
527
+ */
528
+ private readonly _record;
529
+ /**
530
+ * Constructor.
531
+ *
532
+ * @param typeName
533
+ * Type name for error message.
534
+ *
535
+ * @param record
536
+ * Record in which to look up keys.
537
+ */
538
+ constructor(typeName: string, record: Readonly<Record<string, T>>);
539
+ /**
540
+ * Get the type name.
541
+ */
542
+ get typeName(): string;
543
+ /**
544
+ * Get the record.
545
+ */
546
+ get record(): Readonly<Record<string, T>>;
547
+ /**
548
+ * Validate a key by looking it up in the record.
549
+ *
550
+ * @param key
551
+ * Record key.
552
+ */
553
+ validate(key: string): void;
554
+ }
555
+
556
+ /**
557
+ * Exclusion options for validating and creating strings based on character sets.
558
+ */
559
+ declare enum Exclusion {
560
+ /**
561
+ * No strings excluded.
562
+ */
563
+ None = 0,
564
+ /**
565
+ * Strings that start with zero ('0') excluded.
566
+ */
567
+ FirstZero = 1,
568
+ /**
569
+ * Strings that are all-numeric (e.g., "123456") excluded.
570
+ */
571
+ AllNumeric = 2
572
+ }
573
+ /**
574
+ * Character set validation parameters.
575
+ */
576
+ interface CharacterSetValidation extends StringValidation {
577
+ /**
578
+ * Minimum length. If defined and the string is less than this length, an error is thrown.
579
+ */
580
+ minimumLength?: number | undefined;
581
+ /**
582
+ * Maximum length. If defined and the string is greater than this length, an error is thrown.
583
+ */
584
+ maximumLength?: number | undefined;
585
+ /**
586
+ * Exclusion from the string. If defined and the string is within the exclusion range, an error is thrown.
587
+ */
588
+ exclusion?: Exclusion | undefined;
589
+ /**
590
+ * Position offset within a larger string. Strings are sometimes composed of multiple substrings; this parameter
591
+ * ensures that the error notes the proper position in the string.
592
+ */
593
+ positionOffset?: number | undefined;
594
+ /**
595
+ * Name of component, typically but not exclusively within a larger string. This parameter ensure that the
596
+ * error notes the component that triggered it. Value may be a string or a callback that returns a string, the
597
+ * latter allowing for localization changes.
598
+ */
599
+ component?: string | (() => string) | undefined;
600
+ }
601
+ /**
602
+ * Character set validator. Validates a string against a specified character set.
603
+ */
604
+ declare class CharacterSetValidator implements StringValidator<CharacterSetValidation> {
605
+ private static readonly NOT_ALL_NUMERIC_VALIDATOR;
606
+ /**
607
+ * Character set.
608
+ */
609
+ private readonly _characterSet;
610
+ /**
611
+ * Character set map, mapping each character in the character set to its index such that
612
+ * `_characterSetMap.get(_characterSet[index]) === index`.
613
+ */
614
+ private readonly _characterSetMap;
615
+ /**
616
+ * Exclusions supported by the character set.
617
+ */
618
+ private readonly _exclusionSupport;
619
+ /**
620
+ * Constructor.
621
+ *
622
+ * @param characterSet
623
+ * Character set. Each element is a single-character string, unique within the array, that defines the character
624
+ * set.
625
+ *
626
+ * @param exclusionSupport
627
+ * Exclusions supported by the character set. All character sets implicitly support {@link Exclusion.None}.
628
+ */
629
+ constructor(characterSet: readonly string[], ...exclusionSupport: readonly Exclusion[]);
630
+ /**
631
+ * Get the character set.
632
+ */
633
+ get characterSet(): readonly string[];
634
+ /**
635
+ * Get the character set size.
636
+ */
637
+ get characterSetSize(): number;
638
+ /**
639
+ * Get the exclusions supported by the character set.
640
+ */
641
+ get exclusionSupport(): readonly Exclusion[];
642
+ /**
643
+ * Get the character at an index.
644
+ *
645
+ * @param index
646
+ * Index into the character set.
647
+ *
648
+ * @returns
649
+ * Character at the index.
650
+ */
651
+ character(index: number): string;
652
+ /**
653
+ * Get the index for a character.
654
+ *
655
+ * @param c
656
+ * Character.
657
+ *
658
+ * @returns
659
+ * Index for the character or undefined if the character is not in the character set.
660
+ */
661
+ characterIndex(c: string): number | undefined;
662
+ /**
663
+ * Get the indexes for all characters in a string.
664
+ *
665
+ * @param s
666
+ * String.
667
+ *
668
+ * @returns
669
+ * Array of indexes for each character or undefined if the character is not in the character set.
670
+ */
671
+ characterIndexes(s: string): ReadonlyArray<number | undefined>;
672
+ /**
673
+ * Convert a component definition to a string or undefined. Checks the type of the component and makes the callback
674
+ * if required.
675
+ *
676
+ * @param component
677
+ * Component definition as a string, callback, or undefined.
678
+ *
679
+ * @returns
680
+ * Component as a string or undefined.
681
+ */
682
+ private static componentToString;
683
+ /**
684
+ * Validate that an exclusion is supported. If not, an error is thrown.
685
+ *
686
+ * @param exclusion
687
+ * Exclusion.
688
+ */
689
+ protected validateExclusion(exclusion: Exclusion): void;
690
+ /**
691
+ * Validate a string. If the string violates the character set or any of the character set validation parameters, an
692
+ * error is thrown.
693
+ *
694
+ * @param s
695
+ * String.
696
+ *
697
+ * @param validation
698
+ * Character set validation parameters.
699
+ */
700
+ validate(s: string, validation?: CharacterSetValidation): void;
701
+ }
702
+ /**
703
+ * Creation callback, used to convert created string to its final value.
704
+ *
705
+ * @param s
706
+ * Created string.
707
+ *
708
+ * @param index
709
+ * Index in sequence creation (0 for single creation).
710
+ *
711
+ * @returns
712
+ * Final value.
713
+ */
714
+ type CreationCallback = (s: string, index: number) => string;
715
+ /**
716
+ * Character set creator. Maps numeric values to strings using the character set as digits.
717
+ */
718
+ declare class CharacterSetCreator extends CharacterSetValidator {
719
+ /**
720
+ * Maximum string length supported.
721
+ */
722
+ static readonly MAXIMUM_STRING_LENGTH = 40;
723
+ /**
724
+ * Powers of 10 from 1 (`10**0`) to `10**MAXIMUM_STRING_LENGTH`.
725
+ */
726
+ private static readonly _powersOf10;
727
+ /**
728
+ * Create powers of a given base from 1 (`base**0`) to `base**MAXIMUM_STRING_LENGTH`.
729
+ *
730
+ * @param base
731
+ * Number base.
732
+ *
733
+ * @returns
734
+ * Array of powers of base.
735
+ */
736
+ private static createPowersOf;
737
+ /**
738
+ * Get a power of 10.
739
+ *
740
+ * @param power
741
+ * Power.
742
+ *
743
+ * @returns
744
+ * `10**power`.
745
+ */
746
+ static powerOf10(power: number): bigint;
747
+ /**
748
+ * Character set size as big integer, cached for performance purposes.
749
+ */
750
+ private readonly _characterSetSizeN;
751
+ /**
752
+ * Character set size minus 1 as big integer, cached for performance purposes.
753
+ */
754
+ private readonly _characterSetSizeMinusOneN;
755
+ /**
756
+ * Domains for every length for every supported {@link Exclusion}.
757
+ */
758
+ private readonly _exclusionDomains;
759
+ /**
760
+ * Values that would generate all zeros in the created string.
761
+ */
762
+ private readonly _allZerosValues;
763
+ /**
764
+ * Constructor.
765
+ *
766
+ * @param characterSet
767
+ * Character set. Each element is a single-character string, unique within the array, that defines the character
768
+ * set.
769
+ *
770
+ * @param exclusionSupport
771
+ * Exclusions supported by the character set. All character sets implicitly support {@link Exclusion.None}.
772
+ */
773
+ constructor(characterSet: readonly string[], ...exclusionSupport: readonly Exclusion[]);
774
+ /**
775
+ * Get a power of character set size.
776
+ *
777
+ * @param power
778
+ * Power.
779
+ *
780
+ * @returns
781
+ * `characterSetSize**power`.
782
+ */
783
+ private powerOfSize;
784
+ /**
785
+ * Determine the shift required to skip all all-numeric strings up to the value.
786
+ *
787
+ * @param shiftForward
788
+ * True to shift forward (value to string), false to shift backward (string to value).
789
+ *
790
+ * @param length
791
+ * Length of string for which to get the all-numeric shift.
792
+ *
793
+ * @param value
794
+ * Value for which to get the all-numeric shift.
795
+ *
796
+ * @returns
797
+ * Shift required to skip all all-numeric strings.
798
+ */
799
+ private allNumericShift;
800
+ /**
801
+ * Validate that a length is less than or equal to {@link MAXIMUM_STRING_LENGTH}. If not, an error is thrown.
802
+ *
803
+ * @param length
804
+ * Length.
805
+ */
806
+ private validateLength;
807
+ /**
808
+ * Create a string by mapping a value to the equivalent characters in the character set across the length of the
809
+ * string.
810
+ *
811
+ * @param length
812
+ * Required string length.
813
+ *
814
+ * @param value
815
+ * Numeric value of the string.
816
+ *
817
+ * @param exclusion
818
+ * Strings to be excluded from the range of outputs. See {@link Exclusion} for possible values and their meaning.
819
+ *
820
+ * @param tweak
821
+ * If provided, the numerical value of the string is "tweaked" using an {@link EncryptionTransformer | encryption
822
+ * transformer}.
823
+ *
824
+ * @param creationCallback
825
+ * If provided, called after the string is constructed to create the final value.
826
+ *
827
+ * @returns
828
+ * String created from the value.
829
+ */
830
+ create(length: number, value: number | bigint, exclusion?: Exclusion, tweak?: number | bigint, creationCallback?: CreationCallback): string;
831
+ /**
832
+ * Create multiple strings by mapping each value to the equivalent characters in the character set across the length
833
+ * of the string. Equivalent to calling this method for each individual value.
834
+ *
835
+ * @param length
836
+ * Required string length.
837
+ *
838
+ * @param values
839
+ * Numeric values of the strings.
840
+ *
841
+ * @param exclusion
842
+ * Strings to be excluded from the range of outputs. See {@link Exclusion} for possible values and their meaning.
843
+ *
844
+ * @param tweak
845
+ * If provided, the numerical value of the strings are "tweaked" using an {@link EncryptionTransformer | encryption
846
+ * transformer}.
847
+ *
848
+ * @param creationCallback
849
+ * If provided, called after each string is constructed to create the final value.
850
+ *
851
+ * @returns
852
+ * Iterable iterator over strings created from the values.
853
+ */
854
+ create(length: number, values: Iterable<number | bigint>, exclusion?: Exclusion, tweak?: number | bigint, creationCallback?: CreationCallback): IterableIterator<string>;
855
+ /**
856
+ * Create a string or multiple strings. This signature exists to allow similar overloaded methods in other classes
857
+ * to call this method correctly.
858
+ *
859
+ * @param length
860
+ *
861
+ * @param valueOrValues
862
+ *
863
+ * @param exclusion
864
+ *
865
+ * @param tweak
866
+ *
867
+ * @param creationCallback
868
+ *
869
+ * @returns
870
+ */
871
+ create(length: number, valueOrValues: number | bigint | Iterable<number | bigint>, exclusion?: Exclusion, tweak?: number | bigint, creationCallback?: CreationCallback): string | IterableIterator<string>;
872
+ /**
873
+ * Determine the value for a string.
874
+ *
875
+ * @param s
876
+ * String.
877
+ *
878
+ * @param exclusion
879
+ * Strings excluded from the range of inputs. See {@link Exclusion} for possible values and their meaning.
880
+ *
881
+ * @param tweak
882
+ * If provided, the numerical value of the string was "tweaked" using an {@link EncryptionTransformer | encryption
883
+ * transformer}.
884
+ *
885
+ * @returns
886
+ * Numeric value of the string.
887
+ */
888
+ valueFor(s: string, exclusion?: Exclusion, tweak?: number | bigint): bigint;
889
+ }
890
+ /**
891
+ * Numeric creator. Character set is 0-9. Supports {@link Exclusion.FirstZero}.
892
+ */
893
+ declare const NUMERIC_CREATOR: CharacterSetCreator;
894
+ /**
895
+ * Hexadecimal creator. Character set is 0-9, A-F. Supports {@link Exclusion.FirstZero} and {@link
896
+ * Exclusion.AllNumeric}.
897
+ */
898
+ declare const HEXADECIMAL_CREATOR: CharacterSetCreator;
899
+ /**
900
+ * Alphabetic creator. Character set is A-Z.
901
+ */
902
+ declare const ALPHABETIC_CREATOR: CharacterSetCreator;
903
+ /**
904
+ * Alphanumeric creator. Character set is 0-9, A-Z. Supports {@link Exclusion.FirstZero} and {@link
905
+ * Exclusion.AllNumeric}.
906
+ */
907
+ declare const ALPHANUMERIC_CREATOR: CharacterSetCreator;
908
+
909
+ export { ALPHABETIC_CREATOR, ALPHANUMERIC_CREATOR, CharacterSetCreator, type CharacterSetValidation, CharacterSetValidator, type CreationCallback, EncryptionTransformer, Exclusion, HEXADECIMAL_CREATOR, IdentityTransformer, IteratorProxy, NUMERIC_CREATOR, RecordValidator, RegExpValidator, Sequencer, type StringValidation, type StringValidator, type TransformationCallback, Transformer };