@aidc-toolkit/utility 0.9.13-beta → 0.9.15-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.
@@ -0,0 +1,301 @@
1
+ import type { StringValidation, StringValidator } from "./string.js";
2
+ import { type TransformerCallback, type TransformerInput, type TransformerOutput } from "./transformer.js";
3
+ /**
4
+ * Exclusion options for validating and creating strings based on character sets.
5
+ */
6
+ export declare enum Exclusion {
7
+ /**
8
+ * No strings excluded.
9
+ */
10
+ None = 0,
11
+ /**
12
+ * Strings that start with zero ('0') excluded.
13
+ */
14
+ FirstZero = 1,
15
+ /**
16
+ * Strings that are all-numeric (e.g., "123456") excluded.
17
+ */
18
+ AllNumeric = 2
19
+ }
20
+ /**
21
+ * Character set validation parameters.
22
+ */
23
+ export interface CharacterSetValidation extends StringValidation {
24
+ /**
25
+ * Minimum length. If defined and the string is less than this length, an error is thrown.
26
+ */
27
+ minimumLength?: number | undefined;
28
+ /**
29
+ * Maximum length. If defined and the string is greater than this length, an error is thrown.
30
+ */
31
+ maximumLength?: number | undefined;
32
+ /**
33
+ * Exclusion from the string. If defined and the string is within the exclusion range, an error is thrown.
34
+ */
35
+ exclusion?: Exclusion | undefined;
36
+ /**
37
+ * Position offset within a larger string. Strings are sometimes composed of multiple substrings; this parameter
38
+ * ensures that the error notes the proper position in the string.
39
+ */
40
+ positionOffset?: number | undefined;
41
+ /**
42
+ * Name of component, typically but not exclusively within a larger string. This parameter ensure that the
43
+ * error notes the component that triggered it. Value may be a string or a callback that returns a string, the
44
+ * latter allowing for localization changes.
45
+ */
46
+ component?: string | (() => string) | undefined;
47
+ }
48
+ /**
49
+ * Character set validator. Validates a string against a specified character set.
50
+ */
51
+ export declare class CharacterSetValidator implements StringValidator<CharacterSetValidation> {
52
+ private static readonly NOT_ALL_NUMERIC_VALIDATOR;
53
+ /**
54
+ * Character set.
55
+ */
56
+ private readonly _characterSet;
57
+ /**
58
+ * Character set map, mapping each character in the character set to its index such that
59
+ * `_characterSetMap.get(_characterSet[index]) === index`.
60
+ */
61
+ private readonly _characterSetMap;
62
+ /**
63
+ * Exclusions supported by the character set.
64
+ */
65
+ private readonly _exclusionSupport;
66
+ /**
67
+ * Constructor.
68
+ *
69
+ * @param characterSet
70
+ * Character set. Each element is a single-character string, unique within the array, that defines the character
71
+ * set.
72
+ *
73
+ * @param exclusionSupport
74
+ * Exclusions supported by the character set. All character sets implicitly support {@link Exclusion.None}.
75
+ */
76
+ constructor(characterSet: readonly string[], ...exclusionSupport: readonly Exclusion[]);
77
+ /**
78
+ * Get the character set.
79
+ */
80
+ get characterSet(): readonly string[];
81
+ /**
82
+ * Get the character set size.
83
+ */
84
+ get characterSetSize(): number;
85
+ /**
86
+ * Get the exclusions supported by the character set.
87
+ */
88
+ get exclusionSupport(): readonly Exclusion[];
89
+ /**
90
+ * Get the character at an index.
91
+ *
92
+ * @param index
93
+ * Index into the character set.
94
+ *
95
+ * @returns
96
+ * Character at the index.
97
+ */
98
+ character(index: number): string;
99
+ /**
100
+ * Get the index for a character.
101
+ *
102
+ * @param c
103
+ * Character.
104
+ *
105
+ * @returns
106
+ * Index for the character or undefined if the character is not in the character set.
107
+ */
108
+ characterIndex(c: string): number | undefined;
109
+ /**
110
+ * Get the indexes for all characters in a string.
111
+ *
112
+ * @param s
113
+ * String.
114
+ *
115
+ * @returns
116
+ * Array of indexes for each character or undefined if the character is not in the character set.
117
+ */
118
+ characterIndexes(s: string): ReadonlyArray<number | undefined>;
119
+ /**
120
+ * Convert a component definition to a string or undefined. Checks the type of the component and makes the callback
121
+ * if required.
122
+ *
123
+ * @param component
124
+ * Component definition as a string, callback, or undefined.
125
+ *
126
+ * @returns
127
+ * Component as a string or undefined.
128
+ */
129
+ private static componentToString;
130
+ /**
131
+ * Validate that an exclusion is supported. If not, an error is thrown.
132
+ *
133
+ * @param exclusion
134
+ * Exclusion.
135
+ */
136
+ protected validateExclusion(exclusion: Exclusion): void;
137
+ /**
138
+ * Validate a string. If the string violates the character set or any of the character set validation parameters, an
139
+ * error is thrown.
140
+ *
141
+ * @param s
142
+ * String.
143
+ *
144
+ * @param validation
145
+ * Character set validation parameters.
146
+ */
147
+ validate(s: string, validation?: CharacterSetValidation): void;
148
+ }
149
+ /**
150
+ * Character set creator. Maps numeric values to strings using the character set as digits.
151
+ */
152
+ export declare class CharacterSetCreator extends CharacterSetValidator {
153
+ /**
154
+ * Maximum string length supported.
155
+ */
156
+ static readonly MAXIMUM_STRING_LENGTH = 40;
157
+ /**
158
+ * Powers of 10 from 1 (`10**0`) to `10**MAXIMUM_STRING_LENGTH`.
159
+ */
160
+ private static readonly _powersOf10;
161
+ /**
162
+ * Create powers of a given base from 1 (`base**0`) to `base**MAXIMUM_STRING_LENGTH`.
163
+ *
164
+ * @param base
165
+ * Number base.
166
+ *
167
+ * @returns
168
+ * Array of powers of base.
169
+ */
170
+ private static createPowersOf;
171
+ /**
172
+ * Get a power of 10.
173
+ *
174
+ * @param power
175
+ * Power.
176
+ *
177
+ * @returns
178
+ * `10**power`.
179
+ */
180
+ static powerOf10(power: number): bigint;
181
+ /**
182
+ * Character set size as big integer, cached for performance purposes.
183
+ */
184
+ private readonly _characterSetSizeN;
185
+ /**
186
+ * Character set size minus 1 as big integer, cached for performance purposes.
187
+ */
188
+ private readonly _characterSetSizeMinusOneN;
189
+ /**
190
+ * Domains for every length for every supported {@link Exclusion}.
191
+ */
192
+ private readonly _exclusionDomains;
193
+ /**
194
+ * Values that would generate all zeros in the created string.
195
+ */
196
+ private readonly _allZerosValues;
197
+ /**
198
+ * Constructor.
199
+ *
200
+ * @param characterSet
201
+ * Character set. Each element is a single-character string, unique within the array, that defines the character
202
+ * set.
203
+ *
204
+ * @param exclusionSupport
205
+ * Exclusions supported by the character set. All character sets implicitly support {@link Exclusion.None}.
206
+ */
207
+ constructor(characterSet: readonly string[], ...exclusionSupport: readonly Exclusion[]);
208
+ /**
209
+ * Get a power of character set size.
210
+ *
211
+ * @param power
212
+ * Power.
213
+ *
214
+ * @returns
215
+ * `characterSetSize**power`.
216
+ */
217
+ private powerOfSize;
218
+ /**
219
+ * Determine the shift required to skip all all-numeric strings up to the value.
220
+ *
221
+ * @param shiftForward
222
+ * True to shift forward (value to string), false to shift backward (string to value).
223
+ *
224
+ * @param length
225
+ * Length of string for which to get the all-numeric shift.
226
+ *
227
+ * @param value
228
+ * Value for which to get the all-numeric shift.
229
+ *
230
+ * @returns
231
+ * Shift required to skip all all-numeric strings.
232
+ */
233
+ private allNumericShift;
234
+ /**
235
+ * Validate that a length is less than or equal to {@link MAXIMUM_STRING_LENGTH}. If not, an error is thrown.
236
+ *
237
+ * @param length
238
+ * Length.
239
+ */
240
+ private validateLength;
241
+ /**
242
+ * Create string(s) by mapping value(s) to the equivalent characters in the character set across the length of the
243
+ * string.
244
+ *
245
+ * @param length
246
+ * Required string length.
247
+ *
248
+ * @param valueOrValues
249
+ * Numeric value(s) of the string(s).
250
+ *
251
+ * @param exclusion
252
+ * String(s) to be excluded from the range of outputs. See {@link Exclusion} for possible values and their meaning.
253
+ *
254
+ * @param tweak
255
+ * If provided, the numerical value of the string(s) is/are "tweaked" using an {@link EncryptionTransformer |
256
+ * encryption transformer}.
257
+ *
258
+ * @param creatorCallback
259
+ * If provided, called after each string is constructed to create the final value.
260
+ *
261
+ * @returns
262
+ * String(s) created from the value(s).
263
+ */
264
+ create<TTransformerInput extends TransformerInput<number | bigint>>(length: number, valueOrValues: TTransformerInput, exclusion?: Exclusion, tweak?: number | bigint, creatorCallback?: TransformerCallback<string, string>): TransformerOutput<TTransformerInput, string>;
265
+ /**
266
+ * Determine the value for a string.
267
+ *
268
+ * @param s
269
+ * String.
270
+ *
271
+ * @param exclusion
272
+ * Strings excluded from the range of inputs. See {@link Exclusion} for possible values and their meaning.
273
+ *
274
+ * @param tweak
275
+ * If provided, the numerical value of the string was "tweaked" using an {@link EncryptionTransformer | encryption
276
+ * transformer}.
277
+ *
278
+ * @returns
279
+ * Numeric value of the string.
280
+ */
281
+ valueFor(s: string, exclusion?: Exclusion, tweak?: number | bigint): bigint;
282
+ }
283
+ /**
284
+ * Numeric creator. Character set is 0-9. Supports {@link Exclusion.FirstZero}.
285
+ */
286
+ export declare const NUMERIC_CREATOR: CharacterSetCreator;
287
+ /**
288
+ * Hexadecimal creator. Character set is 0-9, A-F. Supports {@link Exclusion.FirstZero} and {@link
289
+ * Exclusion.AllNumeric}.
290
+ */
291
+ export declare const HEXADECIMAL_CREATOR: CharacterSetCreator;
292
+ /**
293
+ * Alphabetic creator. Character set is A-Z.
294
+ */
295
+ export declare const ALPHABETIC_CREATOR: CharacterSetCreator;
296
+ /**
297
+ * Alphanumeric creator. Character set is 0-9, A-Z. Supports {@link Exclusion.FirstZero} and {@link
298
+ * Exclusion.AllNumeric}.
299
+ */
300
+ export declare const ALPHANUMERIC_CREATOR: CharacterSetCreator;
301
+ //# sourceMappingURL=character-set.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"character-set.d.ts","sourceRoot":"","sources":["../src/character-set.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACrE,OAAO,EAAe,KAAK,mBAAmB,EAAE,KAAK,gBAAgB,EAAE,KAAK,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAExH;;GAEG;AACH,oBAAY,SAAS;IACjB;;OAEG;IACH,IAAI,IAAA;IAEJ;;OAEG;IACH,SAAS,IAAA;IAET;;OAEG;IACH,UAAU,IAAA;CACb;AAED;;GAEG;AACH,MAAM,WAAW,sBAAuB,SAAQ,gBAAgB;IAC5D;;OAEG;IACH,aAAa,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAEnC;;OAEG;IACH,aAAa,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAEnC;;OAEG;IACH,SAAS,CAAC,EAAE,SAAS,GAAG,SAAS,CAAC;IAElC;;;OAGG;IACH,cAAc,CAAC,EAAE,MAAM,GAAG,SA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