@aidc-toolkit/utility 1.0.25-beta → 1.0.26-beta

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