@aidc-toolkit/utility 1.0.24-beta → 1.0.25-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.
- package/dist/index.cjs +14 -13
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +43 -37
- package/dist/index.d.ts +43 -37
- package/dist/index.js +14 -13
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
- package/src/character-set.ts +14 -13
- package/src/exclusion.ts +6 -1
- package/src/reg-exp.ts +5 -5
- package/src/string.ts +3 -3
- package/src/transformer.ts +16 -15
- package/test/record.test.ts +3 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aidc-toolkit/utility",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.25-beta",
|
|
4
4
|
"description": "Foundational utilities for AIDC Toolkit",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -23,10 +23,10 @@
|
|
|
23
23
|
"test": "vitest run"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
|
-
"@aidc-toolkit/dev": "beta",
|
|
27
|
-
"vitest": "^4.0.
|
|
26
|
+
"@aidc-toolkit/dev": "1.0.25-beta",
|
|
27
|
+
"vitest": "^4.0.15"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@aidc-toolkit/core": "beta"
|
|
30
|
+
"@aidc-toolkit/core": "1.0.25-beta"
|
|
31
31
|
}
|
|
32
32
|
}
|
package/src/character-set.ts
CHANGED
|
@@ -81,7 +81,7 @@ export class CharacterSetValidator implements StringValidator<CharacterSetValida
|
|
|
81
81
|
* set.
|
|
82
82
|
*
|
|
83
83
|
* @param exclusionSupport
|
|
84
|
-
* Exclusions supported by the character set. All character sets implicitly support {@
|
|
84
|
+
* Exclusions supported by the character set. All character sets implicitly support {@linkcode Exclusions.None}.
|
|
85
85
|
*/
|
|
86
86
|
constructor(characterSet: readonly string[], ...exclusionSupport: readonly Exclusion[]) {
|
|
87
87
|
this._characterSet = characterSet;
|
|
@@ -324,7 +324,7 @@ export class CharacterSetCreator extends CharacterSetValidator {
|
|
|
324
324
|
private readonly _characterSetSizeMinusOneN: bigint;
|
|
325
325
|
|
|
326
326
|
/**
|
|
327
|
-
* Domains for every length for every supported {@
|
|
327
|
+
* Domains for every length for every supported {@linkcode Exclusions}.
|
|
328
328
|
*/
|
|
329
329
|
private readonly _exclusionDomains: ReadonlyArray<readonly bigint[]>;
|
|
330
330
|
|
|
@@ -341,7 +341,7 @@ export class CharacterSetCreator extends CharacterSetValidator {
|
|
|
341
341
|
* set.
|
|
342
342
|
*
|
|
343
343
|
* @param exclusionSupport
|
|
344
|
-
* Exclusions supported by the character set. All character sets implicitly support {@
|
|
344
|
+
* Exclusions supported by the character set. All character sets implicitly support {@linkcode Exclusions.None}.
|
|
345
345
|
*/
|
|
346
346
|
constructor(characterSet: readonly string[], ...exclusionSupport: readonly Exclusion[]) {
|
|
347
347
|
super(characterSet, ...exclusionSupport);
|
|
@@ -488,7 +488,7 @@ export class CharacterSetCreator extends CharacterSetValidator {
|
|
|
488
488
|
}
|
|
489
489
|
|
|
490
490
|
/**
|
|
491
|
-
* Validate that a length is less than or equal to {@
|
|
491
|
+
* Validate that a length is less than or equal to {@linkcode MAXIMUM_STRING_LENGTH}. If not, an error is thrown.
|
|
492
492
|
*
|
|
493
493
|
* @param length
|
|
494
494
|
* Length.
|
|
@@ -523,7 +523,8 @@ export class CharacterSetCreator extends CharacterSetValidator {
|
|
|
523
523
|
* Numeric value(s) of the string(s).
|
|
524
524
|
*
|
|
525
525
|
* @param exclusion
|
|
526
|
-
* String(s) to be excluded from the range of outputs. See {@
|
|
526
|
+
* String(s) to be excluded from the range of outputs. See {@linkcode Exclusions} for possible values and their
|
|
527
|
+
* meaning.
|
|
527
528
|
*
|
|
528
529
|
* @param tweak
|
|
529
530
|
* If provided, the numerical value of the string(s) is/are "tweaked" using an {@link EncryptionTransformer |
|
|
@@ -553,7 +554,7 @@ export class CharacterSetCreator extends CharacterSetValidator {
|
|
|
553
554
|
|
|
554
555
|
if (exclusion === Exclusions.AllNumeric && convertValue >= allZerosValue) {
|
|
555
556
|
// Value to convert is shifted by the number of all-numeric strings that occur at or prior to it.
|
|
556
|
-
convertValue
|
|
557
|
+
convertValue += this.allNumericShift(true, length, convertValue - allZerosValue);
|
|
557
558
|
}
|
|
558
559
|
|
|
559
560
|
// Build string from right to left excluding the first character.
|
|
@@ -581,7 +582,7 @@ export class CharacterSetCreator extends CharacterSetValidator {
|
|
|
581
582
|
* String.
|
|
582
583
|
*
|
|
583
584
|
* @param exclusion
|
|
584
|
-
* Strings excluded from the range of inputs. See {@
|
|
585
|
+
* Strings excluded from the range of inputs. See {@linkcode Exclusions} for possible values and their meaning.
|
|
585
586
|
*
|
|
586
587
|
* @param tweak
|
|
587
588
|
* If provided, the numerical value of the string was "tweaked" using an {@link EncryptionTransformer | encryption
|
|
@@ -640,19 +641,19 @@ export class CharacterSetCreator extends CharacterSetValidator {
|
|
|
640
641
|
}
|
|
641
642
|
|
|
642
643
|
/**
|
|
643
|
-
* Numeric creator. Character set is 0-9. Supports {@
|
|
644
|
+
* Numeric creator. Character set is 0-9. Supports {@linkcode Exclusions.FirstZero}.
|
|
644
645
|
*/
|
|
645
646
|
export const NUMERIC_CREATOR = new CharacterSetCreator([
|
|
646
647
|
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
|
|
647
648
|
], Exclusions.FirstZero);
|
|
648
649
|
|
|
649
650
|
/**
|
|
650
|
-
* Numeric validator. Character set is 0-9. Supports {@
|
|
651
|
+
* Numeric validator. Character set is 0-9. Supports {@linkcode Exclusions.FirstZero}.
|
|
651
652
|
*/
|
|
652
653
|
export const NUMERIC_VALIDATOR = NUMERIC_CREATOR as CharacterSetValidator;
|
|
653
654
|
|
|
654
655
|
/**
|
|
655
|
-
* Hexadecimal creator. Character set is 0-9, A-F. Supports {@
|
|
656
|
+
* Hexadecimal creator. Character set is 0-9, A-F. Supports {@linkcode Exclusions.FirstZero} and {@linkcode
|
|
656
657
|
* Exclusions.AllNumeric}.
|
|
657
658
|
*/
|
|
658
659
|
export const HEXADECIMAL_CREATOR = new CharacterSetCreator([
|
|
@@ -661,7 +662,7 @@ export const HEXADECIMAL_CREATOR = new CharacterSetCreator([
|
|
|
661
662
|
], Exclusions.FirstZero, Exclusions.AllNumeric);
|
|
662
663
|
|
|
663
664
|
/**
|
|
664
|
-
* Hexadecimal validator. Character set is 0-9, A-F. Supports {@
|
|
665
|
+
* Hexadecimal validator. Character set is 0-9, A-F. Supports {@linkcode Exclusions.FirstZero} and {@linkcode
|
|
665
666
|
* Exclusions.AllNumeric}.
|
|
666
667
|
*/
|
|
667
668
|
export const HEXADECIMAL_VALIDATOR = HEXADECIMAL_CREATOR as CharacterSetValidator;
|
|
@@ -680,7 +681,7 @@ export const ALPHABETIC_CREATOR = new CharacterSetCreator([
|
|
|
680
681
|
export const ALPHABETIC_VALIDATOR = ALPHABETIC_CREATOR as CharacterSetValidator;
|
|
681
682
|
|
|
682
683
|
/**
|
|
683
|
-
* Alphanumeric creator. Character set is 0-9, A-Z. Supports {@
|
|
684
|
+
* Alphanumeric creator. Character set is 0-9, A-Z. Supports {@linkcode Exclusions.FirstZero} and {@linkcode
|
|
684
685
|
* Exclusions.AllNumeric}.
|
|
685
686
|
*/
|
|
686
687
|
export const ALPHANUMERIC_CREATOR = new CharacterSetCreator([
|
|
@@ -690,7 +691,7 @@ export const ALPHANUMERIC_CREATOR = new CharacterSetCreator([
|
|
|
690
691
|
], Exclusions.FirstZero, Exclusions.AllNumeric);
|
|
691
692
|
|
|
692
693
|
/**
|
|
693
|
-
* Alphanumeric validator. Character set is 0-9, A-Z. Supports {@
|
|
694
|
+
* Alphanumeric validator. Character set is 0-9, A-Z. Supports {@linkcode Exclusions.FirstZero} and {@linkcode
|
|
694
695
|
* Exclusions.AllNumeric}.
|
|
695
696
|
*/
|
|
696
697
|
export const ALPHANUMERIC_VALIDATOR = ALPHANUMERIC_CREATOR as CharacterSetValidator;
|
package/src/exclusion.ts
CHANGED
|
@@ -18,7 +18,12 @@ export const Exclusions = {
|
|
|
18
18
|
AllNumeric: 2
|
|
19
19
|
} as const;
|
|
20
20
|
|
|
21
|
+
/**
|
|
22
|
+
* Exclusion key.
|
|
23
|
+
*/
|
|
24
|
+
export type ExclusionKey = keyof typeof Exclusions;
|
|
25
|
+
|
|
21
26
|
/**
|
|
22
27
|
* Exclusion.
|
|
23
28
|
*/
|
|
24
|
-
export type Exclusion = typeof Exclusions[
|
|
29
|
+
export type Exclusion = typeof Exclusions[ExclusionKey];
|
package/src/reg-exp.ts
CHANGED
|
@@ -3,12 +3,12 @@ import type { StringValidator } from "./string";
|
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Regular expression validator. The regular expression applies to the full string only if constructed as such. For
|
|
6
|
-
* example, <code>/\d*/</code> (0 or more digits) matches every string, <code>/\d+/</code>
|
|
7
|
-
*
|
|
8
|
-
*
|
|
6
|
+
* example, <code>/\d*/</code> (0 or more digits) matches every string, <code>/\d+/</code> (1
|
|
7
|
+
* or more digits) matches strings with at least one digit, <code>/^\d*$/</code> matches strings that are
|
|
8
|
+
* all digits or empty, and <code>/^\d+$/</code> matches strings that are all digits and not empty.
|
|
9
9
|
*
|
|
10
|
-
* Clients of this class are recommended to override the {@
|
|
11
|
-
* message for their use case.
|
|
10
|
+
* Clients of this class are recommended to override the {@linkcode createErrorMessage | createErrorMessage()} method
|
|
11
|
+
* to create a more suitable error message for their use case.
|
|
12
12
|
*/
|
|
13
13
|
export class RegExpValidator implements StringValidator {
|
|
14
14
|
/**
|
package/src/string.ts
CHANGED
|
@@ -8,10 +8,10 @@ export interface StringValidation {
|
|
|
8
8
|
/**
|
|
9
9
|
* String validator interface.
|
|
10
10
|
*
|
|
11
|
-
* @template
|
|
11
|
+
* @template TStringValidation
|
|
12
12
|
* String validation type.
|
|
13
13
|
*/
|
|
14
|
-
export interface StringValidator<
|
|
14
|
+
export interface StringValidator<TStringValidation extends StringValidation = StringValidation> {
|
|
15
15
|
/**
|
|
16
16
|
* Validate a string and throw an error if validation fails.
|
|
17
17
|
*
|
|
@@ -21,5 +21,5 @@ export interface StringValidator<V extends StringValidation = StringValidation>
|
|
|
21
21
|
* @param validation
|
|
22
22
|
* String validation parameters.
|
|
23
23
|
*/
|
|
24
|
-
validate: (s: string, validation?:
|
|
24
|
+
validate: (s: string, validation?: TStringValidation) => void;
|
|
25
25
|
}
|
package/src/transformer.ts
CHANGED
|
@@ -43,11 +43,12 @@ export type TransformerOutput<TTransformerInput extends TransformerInput<Transfo
|
|
|
43
43
|
* into values in the same domain, typically for storage in a database where the data type and length are already fixed
|
|
44
44
|
* and exfiltration of the data can have significant repercussions.
|
|
45
45
|
*
|
|
46
|
-
* Two subclasses are supported directly by this class: {@
|
|
47
|
-
* only) and {@
|
|
48
|
-
* to make repeated use of a transformer with the same domain and (optional) tweak and can't manage the
|
|
49
|
-
* object, an in-memory cache is available via the {@
|
|
50
|
-
* {@
|
|
46
|
+
* Two subclasses are supported directly by this class: {@linkcode IdentityTransformer} (which operates based on a
|
|
47
|
+
* domain only) and {@linkcode EncryptionTransformer} (which operates based on a domain and a tweak). If an application
|
|
48
|
+
* is expected to make repeated use of a transformer with the same domain and (optional) tweak and can't manage the
|
|
49
|
+
* transformer object, an in-memory cache is available via the {@linkcode get | get()} method. Properties in {@linkcode
|
|
50
|
+
* IdentityTransformer} and {@linkcode EncryptionTransformer} are read-only once constructed, so there is no issue with
|
|
51
|
+
* their shared use.
|
|
51
52
|
*/
|
|
52
53
|
export abstract class Transformer {
|
|
53
54
|
/**
|
|
@@ -77,10 +78,10 @@ export abstract class Transformer {
|
|
|
77
78
|
}
|
|
78
79
|
|
|
79
80
|
/**
|
|
80
|
-
* Get a transformer, constructing it if necessary. The type returned is {@
|
|
81
|
-
* undefined, {@
|
|
82
|
-
* with a zero tweak operates as an {@
|
|
83
|
-
* returned if a zero tweak is explicitly specified.
|
|
81
|
+
* Get a transformer, constructing it if necessary. The type returned is {@linkcode IdentityTransformer} if tweak is
|
|
82
|
+
* undefined, {@linkcode EncryptionTransformer} if tweak is defined. Note that although an {@linkcode
|
|
83
|
+
* EncryptionTransformer} with a zero tweak operates as an {@linkcode IdentityTransformer}, {@linkcode
|
|
84
|
+
* EncryptionTransformer} is still the type returned if a zero tweak is explicitly specified.
|
|
84
85
|
*
|
|
85
86
|
* @param domain
|
|
86
87
|
* Domain.
|
|
@@ -89,7 +90,7 @@ export abstract class Transformer {
|
|
|
89
90
|
* Tweak.
|
|
90
91
|
*
|
|
91
92
|
* @returns
|
|
92
|
-
* {@
|
|
93
|
+
* {@linkcode IdentityTransformer} if tweak is undefined, {@linkcode EncryptionTransformer} if tweak is defined.
|
|
93
94
|
*/
|
|
94
95
|
static get(domain: number | bigint, tweak?: number | bigint): Transformer {
|
|
95
96
|
const domainN = BigInt(domain);
|
|
@@ -195,7 +196,7 @@ export abstract class Transformer {
|
|
|
195
196
|
* Value(s) input type.
|
|
196
197
|
*
|
|
197
198
|
* @param valueOrValues
|
|
198
|
-
* Value(s). If this is an instance of {@
|
|
199
|
+
* Value(s). If this is an instance of {@linkcode Sequence}, the minimum and maximum values are validated prior to
|
|
199
200
|
* transformation. Otherwise, the individual value(s) is/are validated at the time of transformation.
|
|
200
201
|
*
|
|
201
202
|
* @returns
|
|
@@ -213,7 +214,7 @@ export abstract class Transformer {
|
|
|
213
214
|
* Transformation callback output type.
|
|
214
215
|
*
|
|
215
216
|
* @param valueOrValues
|
|
216
|
-
* Value(s). If this is an instance of {@
|
|
217
|
+
* Value(s). If this is an instance of {@linkcode Sequence}, the minimum and maximum values are validated prior to
|
|
217
218
|
* transformation. Otherwise, the individual value(s) is/are validated at the time of transformation.
|
|
218
219
|
*
|
|
219
220
|
* @param transformerCallback
|
|
@@ -305,9 +306,9 @@ export class IdentityTransformer extends Transformer {
|
|
|
305
306
|
/**
|
|
306
307
|
* Encryption transformer. Values are transformed using repeated shuffle and xor operations, similar to those found in
|
|
307
308
|
* many cryptography algorithms, particularly AES. While sufficient for obfuscation of numeric sequences (e.g., serial
|
|
308
|
-
* number generation, below), if true format-preserving encryption is required, a more robust algorithm such as
|
|
309
|
-
*
|
|
310
|
-
*
|
|
309
|
+
* number generation, below), if true format-preserving encryption is required, a more robust algorithm such as {@link
|
|
310
|
+
* https://doi.org/10.6028/NIST.SP.800-38Gr1.2pd | FF1} is recommended. Furthermore, no work has been done to mitigate
|
|
311
|
+
* {@link https://timing.attacks.cr.yp.to/index.html | timing attacks} for key detection.
|
|
311
312
|
*
|
|
312
313
|
* The purpose of the encryption transformer is to generate pseudo-random values in a deterministic manner to obscure
|
|
313
314
|
* the sequence of values generated over time. A typical example is for serial number generation, where knowledge of the
|
package/test/record.test.ts
CHANGED
|
@@ -9,7 +9,9 @@ describe("Record validator", () => {
|
|
|
9
9
|
ValueD: "D"
|
|
10
10
|
} as const;
|
|
11
11
|
|
|
12
|
-
type
|
|
12
|
+
type StringIndexKey = keyof typeof StringIndexes;
|
|
13
|
+
|
|
14
|
+
type StringIndex = typeof StringIndexes[StringIndexKey];
|
|
13
15
|
|
|
14
16
|
const stringRecord: Record<StringIndex, string> = {
|
|
15
17
|
[StringIndexes.ValueA]: "This is for Value A",
|