@aidc-toolkit/utility 0.9.14-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.
- package/dist/character-set.d.ts +301 -0
- package/dist/character-set.d.ts.map +1 -0
- package/dist/index.cjs +1342 -1
- package/dist/index.d.ts +24 -909
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1288 -1
- package/dist/locale/en/locale-strings.d.ts +33 -0
- package/dist/locale/en/locale-strings.d.ts.map +1 -0
- package/dist/locale/fr/locale-strings.d.ts +33 -0
- package/dist/locale/fr/locale-strings.d.ts.map +1 -0
- package/dist/locale/i18n.d.ts +27 -0
- package/dist/locale/i18n.d.ts.map +1 -0
- package/dist/record.d.ts +41 -0
- package/dist/record.d.ts.map +1 -0
- package/dist/reg-exp.d.ts +43 -0
- package/dist/reg-exp.d.ts.map +1 -0
- package/dist/sequence.d.ts +68 -0
- package/dist/sequence.d.ts.map +1 -0
- package/dist/string.d.ts +22 -0
- package/dist/string.d.ts.map +1 -0
- package/dist/transformer.d.ts +377 -0
- package/dist/transformer.d.ts.map +1 -0
- package/package.json +5 -5
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export declare const localeStrings: {
|
|
2
|
+
readonly Transformer: {
|
|
3
|
+
readonly domainMustBeGreaterThanZero: "Domain {{domain}} must be greater than 0";
|
|
4
|
+
readonly tweakMustBeGreaterThanOrEqualToZero: "Tweak {{tweak}} must be greater than or equal to 0";
|
|
5
|
+
readonly valueMustBeGreaterThanOrEqualToZero: "Value {{value}} must be greater than or equal to 0";
|
|
6
|
+
readonly valueMustBeLessThan: "Value {{value}} must be less than {{domain}}";
|
|
7
|
+
readonly minimumValueMustBeGreaterThanOrEqualToZero: "Minimum value {{minimumValue}} must be greater than or equal to 0";
|
|
8
|
+
readonly maximumValueMustBeLessThan: "Maximum value {{maximumValue}} must be less than {{domain}}";
|
|
9
|
+
};
|
|
10
|
+
readonly RegExpValidator: {
|
|
11
|
+
readonly stringDoesNotMatchPattern: "String {{s}} does not match pattern";
|
|
12
|
+
};
|
|
13
|
+
readonly CharacterSetValidator: {
|
|
14
|
+
readonly firstZeroFirstCharacter: "Character set must support zero as first character";
|
|
15
|
+
readonly allNumericAllNumericCharacters: "Character set must support all numeric characters in sequence";
|
|
16
|
+
readonly stringMustNotBeAllNumeric: "String must not be all numeric";
|
|
17
|
+
readonly lengthMustBeGreaterThanOrEqualTo: "Length {{length}} must be greater than or equal to {{minimumLength}}";
|
|
18
|
+
readonly lengthMustBeLessThanOrEqualTo: "Length {{length}} must be less than or equal to {{maximumLength}}";
|
|
19
|
+
readonly lengthMustBeEqualTo: "Length {{length}} must be equal to {{exactLength}}";
|
|
20
|
+
readonly lengthOfComponentMustBeGreaterThanOrEqualTo: "Length {{length}} of {{component}} must be greater than or equal to {{minimumLength}}";
|
|
21
|
+
readonly lengthOfComponentMustBeLessThanOrEqualTo: "Length {{length}} of {{component}} must be less than or equal to {{maximumLength}}";
|
|
22
|
+
readonly lengthOfComponentMustBeEqualTo: "Length {{length}} of {{component}} must be equal to {{exactLength}}";
|
|
23
|
+
readonly invalidCharacterAtPosition: "Invalid character '{{c}}' at position {{position}}";
|
|
24
|
+
readonly invalidCharacterAtPositionOfComponent: "Invalid character '{{c}}' at position {{position}} of {{component}}";
|
|
25
|
+
readonly exclusionNotSupported: "Exclusion value of {{exclusion}} is not supported";
|
|
26
|
+
readonly invalidTweakWithAllNumericExclusion: "Tweak must not be used with all-numeric exclusion";
|
|
27
|
+
readonly endSequenceValueMustBeLessThanOrEqualTo: "End sequence value (start sequence value + count - 1) must be less than {{domain}}";
|
|
28
|
+
};
|
|
29
|
+
readonly RecordValidator: {
|
|
30
|
+
readonly typeNameKeyNotFound: "{{typeName}} \"{{key}}\" not found";
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
//# sourceMappingURL=locale-strings.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"locale-strings.d.ts","sourceRoot":"","sources":["../../../src/locale/en/locale-strings.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA+BhB,CAAC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export declare const localeStrings: {
|
|
2
|
+
readonly Transformer: {
|
|
3
|
+
readonly domainMustBeGreaterThanZero: "Le domaine {{domain}} doit être supérieur à 0";
|
|
4
|
+
readonly tweakMustBeGreaterThanOrEqualToZero: "Le réglage {{tweak}} doit être supérieur ou égal à 0";
|
|
5
|
+
readonly valueMustBeGreaterThanOrEqualToZero: "La valeur {{value}} doit être supérieure ou égale à 0";
|
|
6
|
+
readonly valueMustBeLessThan: "La valeur {{value}} doit être inférieure à {{domain}}";
|
|
7
|
+
readonly minimumValueMustBeGreaterThanOrEqualToZero: "La valeur minimale {{minimumValue}} doit être supérieure ou égale à 0";
|
|
8
|
+
readonly maximumValueMustBeLessThan: "La valeur maximale {{maximumValue}} doit être inférieure à {{domain}}";
|
|
9
|
+
};
|
|
10
|
+
readonly RegExpValidator: {
|
|
11
|
+
readonly stringDoesNotMatchPattern: "La chaîne {{s}} ne correspond pas au modèle";
|
|
12
|
+
};
|
|
13
|
+
readonly CharacterSetValidator: {
|
|
14
|
+
readonly firstZeroFirstCharacter: "Le jeu de caractères doit prendre en charge zéro comme premier caractère";
|
|
15
|
+
readonly allNumericAllNumericCharacters: "Le jeu de caractères doit prendre en charge tous les caractères numériques en séquence";
|
|
16
|
+
readonly stringMustNotBeAllNumeric: "La chaîne ne doit pas être entièrement numérique";
|
|
17
|
+
readonly lengthMustBeGreaterThanOrEqualTo: "La longueur {{length}} doit être supérieure ou égale à {{minimumLength}}";
|
|
18
|
+
readonly lengthMustBeLessThanOrEqualTo: "La longueur {{length}} doit être inférieure ou égale à {{maximumLength}}";
|
|
19
|
+
readonly lengthMustBeEqualTo: "La longueur {{length}} doit être égale à {{exactLength}}";
|
|
20
|
+
readonly lengthOfComponentMustBeGreaterThanOrEqualTo: "La longueur {{length}} de {{component}} doit être supérieure ou égale à {{minimumLength}}";
|
|
21
|
+
readonly lengthOfComponentMustBeLessThanOrEqualTo: "La longueur {{length}} de {{component}} doit être inférieure ou égale à {{maximumLength}}";
|
|
22
|
+
readonly lengthOfComponentMustBeEqualTo: "La longueur {{length}} de {{component}} doit être égale à {{exactLength}}";
|
|
23
|
+
readonly invalidCharacterAtPosition: "Caractère non valide '{{c}}' à la position {{position}}";
|
|
24
|
+
readonly invalidCharacterAtPositionOfComponent: "Caractère non valide '{{c}}' à la position {{position}} de {{component}}";
|
|
25
|
+
readonly exclusionNotSupported: "La valeur d'exclusion de {{exclusion}} n'est pas prise en charge";
|
|
26
|
+
readonly invalidTweakWithAllNumericExclusion: "Le réglage ne doit pas être utilisé avec une exclusion entièrement numérique";
|
|
27
|
+
readonly endSequenceValueMustBeLessThanOrEqualTo: "La valeur de la séquence de fin (valeur de la séquence de début + nombre - 1) doit être inférieure à {{domaine}}";
|
|
28
|
+
};
|
|
29
|
+
readonly RecordValidator: {
|
|
30
|
+
readonly typeNameKeyNotFound: "{{typeName}} \"{{key}}\" introuvable";
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
//# sourceMappingURL=locale-strings.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"locale-strings.d.ts","sourceRoot":"","sources":["../../../src/locale/fr/locale-strings.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA+BhB,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type I18NEnvironment } from "@aidc-toolkit/core";
|
|
2
|
+
import { type i18n, type Resource } from "i18next";
|
|
3
|
+
import { localeStrings as enLocaleStrings } from "./en/locale-strings.js";
|
|
4
|
+
export declare const utilityNS = "aidct_utility";
|
|
5
|
+
/**
|
|
6
|
+
* Locale strings type is extracted from the English locale strings object.
|
|
7
|
+
*/
|
|
8
|
+
export type UtilityLocaleStrings = typeof enLocaleStrings;
|
|
9
|
+
/**
|
|
10
|
+
* Utility resources.
|
|
11
|
+
*/
|
|
12
|
+
export declare const utilityResources: Resource;
|
|
13
|
+
export declare const i18nextUtility: i18n;
|
|
14
|
+
/**
|
|
15
|
+
* Initialize internationalization.
|
|
16
|
+
*
|
|
17
|
+
* @param environment
|
|
18
|
+
* Environment in which the application is running.
|
|
19
|
+
*
|
|
20
|
+
* @param debug
|
|
21
|
+
* Debug setting.
|
|
22
|
+
*
|
|
23
|
+
* @returns
|
|
24
|
+
* Void promise.
|
|
25
|
+
*/
|
|
26
|
+
export declare function i18nUtilityInit(environment: I18NEnvironment, debug?: boolean): Promise<void>;
|
|
27
|
+
//# sourceMappingURL=i18n.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"i18n.d.ts","sourceRoot":"","sources":["../../src/locale/i18n.ts"],"names":[],"mappings":"AAAA,OAAO,EAA0C,KAAK,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAClG,OAAgB,EAAE,KAAK,IAAI,EAAE,KAAK,QAAQ,EAAE,MAAM,SAAS,CAAC;AAC5D,OAAO,EAAE,aAAa,IAAI,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAG1E,eAAO,MAAM,SAAS,kBAAkB,CAAC;AAEzC;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAAG,OAAO,eAAe,CAAC;AAI1D;;GAEG;AACH,eAAO,MAAM,gBAAgB,EAAE,QAO9B,CAAC;AAGF,eAAO,MAAM,cAAc,EAAE,IAA+B,CAAC;AAE7D;;;;;;;;;;;GAWG;AACH,wBAAsB,eAAe,CAAC,WAAW,EAAE,eAAe,EAAE,KAAK,UAAQ,GAAG,OAAO,CAAC,IAAI,CAAC,CAEhG"}
|
package/dist/record.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { StringValidator } from "./string.js";
|
|
2
|
+
/**
|
|
3
|
+
* Record validator. Validation is performed against a record with a string key type and throws an error if the key is
|
|
4
|
+
* not found.
|
|
5
|
+
*/
|
|
6
|
+
export declare class RecordValidator<T> implements StringValidator {
|
|
7
|
+
/**
|
|
8
|
+
* Type name for error message.
|
|
9
|
+
*/
|
|
10
|
+
private readonly _typeName;
|
|
11
|
+
/**
|
|
12
|
+
* Record in which to look up keys.
|
|
13
|
+
*/
|
|
14
|
+
private readonly _record;
|
|
15
|
+
/**
|
|
16
|
+
* Constructor.
|
|
17
|
+
*
|
|
18
|
+
* @param typeName
|
|
19
|
+
* Type name for error message.
|
|
20
|
+
*
|
|
21
|
+
* @param record
|
|
22
|
+
* Record in which to look up keys.
|
|
23
|
+
*/
|
|
24
|
+
constructor(typeName: string, record: Readonly<Record<string, T>>);
|
|
25
|
+
/**
|
|
26
|
+
* Get the type name.
|
|
27
|
+
*/
|
|
28
|
+
get typeName(): string;
|
|
29
|
+
/**
|
|
30
|
+
* Get the record.
|
|
31
|
+
*/
|
|
32
|
+
get record(): Readonly<Record<string, T>>;
|
|
33
|
+
/**
|
|
34
|
+
* Validate a key by looking it up in the record.
|
|
35
|
+
*
|
|
36
|
+
* @param key
|
|
37
|
+
* Record key.
|
|
38
|
+
*/
|
|
39
|
+
validate(key: string): void;
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=record.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"record.d.ts","sourceRoot":"","sources":["../src/record.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAEnD;;;GAGG;AACH,qBAAa,eAAe,CAAC,CAAC,CAAE,YAAW,eAAe;IACtD;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;IAEnC;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA8B;IAEtD;;;;;;;;OAQG;gBACS,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;IAKjE;;OAEG;IACH,IAAI,QAAQ,IAAI,MAAM,CAErB;IAED;;OAEG;IACH,IAAI,MAAM,IAAI,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,CAExC;IAED;;;;;OAKG;IACH,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;CAQ9B"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { StringValidator } from "./string.js";
|
|
2
|
+
/**
|
|
3
|
+
* Regular expression validator. The regular expression applies to the full string only if constructed as such. For
|
|
4
|
+
* example, <code>/\d*/</code> (0 or more digits) matches every string, <code>/\d+/</code>
|
|
5
|
+
* (1 or more digits) matches strings with at least one digit, <code>/^\d*$/</code> matches strings that
|
|
6
|
+
* are all digits or empty, and <code>/^\d+$/</code> matches strings that are all digits and not empty.
|
|
7
|
+
*
|
|
8
|
+
* Clients of this class are recommended to override the {@link createErrorMessage} method create a more suitable error
|
|
9
|
+
* message for their use case.
|
|
10
|
+
*/
|
|
11
|
+
export declare class RegExpValidator implements StringValidator {
|
|
12
|
+
/**
|
|
13
|
+
* Regular expression.
|
|
14
|
+
*/
|
|
15
|
+
private readonly _regExp;
|
|
16
|
+
/**
|
|
17
|
+
* Constructor.
|
|
18
|
+
*
|
|
19
|
+
* @param regExp
|
|
20
|
+
* Regular expression. See {@link RegExpValidator | class documentation} for notes.
|
|
21
|
+
*/
|
|
22
|
+
constructor(regExp: RegExp);
|
|
23
|
+
/**
|
|
24
|
+
* Get the regular expression.
|
|
25
|
+
*/
|
|
26
|
+
get regExp(): RegExp;
|
|
27
|
+
/**
|
|
28
|
+
* Create an error message for a string. The generic error message is sufficient for many use cases but a more
|
|
29
|
+
* domain-specific error message, possibly including the pattern itself, is often required.
|
|
30
|
+
*
|
|
31
|
+
* @param s
|
|
32
|
+
* String.
|
|
33
|
+
*
|
|
34
|
+
* @returns
|
|
35
|
+
* Error message.
|
|
36
|
+
*/
|
|
37
|
+
protected createErrorMessage(s: string): string;
|
|
38
|
+
/**
|
|
39
|
+
* @inheritDoc
|
|
40
|
+
*/
|
|
41
|
+
validate(s: string): void;
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=reg-exp.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reg-exp.d.ts","sourceRoot":"","sources":["../src/reg-exp.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAEnD;;;;;;;;GAQG;AACH,qBAAa,eAAgB,YAAW,eAAe;IACnD;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IAEjC;;;;;OAKG;gBACS,MAAM,EAAE,MAAM;IAI1B;;OAEG;IACH,IAAI,MAAM,IAAI,MAAM,CAEnB;IAED;;;;;;;;;OASG;IACH,SAAS,CAAC,kBAAkB,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM;IAM/C;;OAEG;IACH,QAAQ,CAAC,CAAC,EAAE,MAAM,GAAG,IAAI;CAK5B"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sequence. Defines an ascending or descending sequence of big integers implemented as an iterable.
|
|
3
|
+
*/
|
|
4
|
+
export declare class Sequence implements Iterable<bigint> {
|
|
5
|
+
/**
|
|
6
|
+
* Start value (inclusive).
|
|
7
|
+
*/
|
|
8
|
+
private readonly _startValue;
|
|
9
|
+
/**
|
|
10
|
+
* End value (exclusive).
|
|
11
|
+
*/
|
|
12
|
+
private readonly _endValue;
|
|
13
|
+
/**
|
|
14
|
+
* Count of values.
|
|
15
|
+
*/
|
|
16
|
+
private readonly _count;
|
|
17
|
+
/**
|
|
18
|
+
* Delta to the next value; equal to the sign of the count.
|
|
19
|
+
*/
|
|
20
|
+
private readonly _nextDelta;
|
|
21
|
+
/**
|
|
22
|
+
* Minimum value (inclusive).
|
|
23
|
+
*/
|
|
24
|
+
private readonly _minimumValue;
|
|
25
|
+
/**
|
|
26
|
+
* Maximum value (inclusive).
|
|
27
|
+
*/
|
|
28
|
+
private readonly _maximumValue;
|
|
29
|
+
/**
|
|
30
|
+
* Constructor.
|
|
31
|
+
*
|
|
32
|
+
* @param startValue
|
|
33
|
+
* Start value.
|
|
34
|
+
*
|
|
35
|
+
* @param count
|
|
36
|
+
* Count of values. If count is zero or positive, iteration ascends from start value, otherwise it descends from
|
|
37
|
+
* start value.
|
|
38
|
+
*/
|
|
39
|
+
constructor(startValue: number | bigint, count: number);
|
|
40
|
+
/**
|
|
41
|
+
* Get the start value (inclusive).
|
|
42
|
+
*/
|
|
43
|
+
get startValue(): bigint;
|
|
44
|
+
/**
|
|
45
|
+
* Get the end value (exclusive).
|
|
46
|
+
*/
|
|
47
|
+
get endValue(): bigint;
|
|
48
|
+
/**
|
|
49
|
+
* Get the count of values.
|
|
50
|
+
*/
|
|
51
|
+
get count(): number;
|
|
52
|
+
/**
|
|
53
|
+
* Get the minimum value (inclusive).
|
|
54
|
+
*/
|
|
55
|
+
get minimumValue(): bigint;
|
|
56
|
+
/**
|
|
57
|
+
* Get the maximum value (inclusive).
|
|
58
|
+
*/
|
|
59
|
+
get maximumValue(): bigint;
|
|
60
|
+
/**
|
|
61
|
+
* Iterable implementation.
|
|
62
|
+
*
|
|
63
|
+
* @yields
|
|
64
|
+
* Next value in sequence.
|
|
65
|
+
*/
|
|
66
|
+
[Symbol.iterator](): Generator<bigint>;
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=sequence.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sequence.d.ts","sourceRoot":"","sources":["../src/sequence.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,qBAAa,QAAS,YAAW,QAAQ,CAAC,MAAM,CAAC;IAC7C;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAS;IAErC;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;IAEnC;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAEhC;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAW;IAEtC;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAS;IAEvC;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAS;IAEvC;;;;;;;;;OASG;gBACS,UAAU,EAAE,MAAM,GAAG,MAAM,EAAE,KAAK,EAAE,MAAM;IAgBtD;;OAEG;IACH,IAAI,UAAU,IAAI,MAAM,CAEvB;IAED;;OAEG;IACH,IAAI,QAAQ,IAAI,MAAM,CAErB;IAED;;OAEG;IACH,IAAI,KAAK,IAAI,MAAM,CAElB;IAED;;OAEG;IACH,IAAI,YAAY,IAAI,MAAM,CAEzB;IAED;;OAEG;IACH,IAAI,YAAY,IAAI,MAAM,CAEzB;IAED;;;;;OAKG;IACD,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,SAAS,CAAC,MAAM,CAAC;CAK3C"}
|
package/dist/string.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* String validation interface. To ensure signature compatibility in implementing classes, string validation is
|
|
3
|
+
* controlled by validation interfaces specific to each validator type.
|
|
4
|
+
*/
|
|
5
|
+
export interface StringValidation {
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* String validator interface.
|
|
9
|
+
*/
|
|
10
|
+
export interface StringValidator<V extends StringValidation = StringValidation> {
|
|
11
|
+
/**
|
|
12
|
+
* Validate a string and throw an error if validation fails.
|
|
13
|
+
*
|
|
14
|
+
* @param s
|
|
15
|
+
* String.
|
|
16
|
+
*
|
|
17
|
+
* @param validation
|
|
18
|
+
* String validation parameters.
|
|
19
|
+
*/
|
|
20
|
+
validate: (s: string, validation?: V) => void;
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=string.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"string.d.ts","sourceRoot":"","sources":["../src/string.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,WAAW,gBAAgB;CAChC;AAED;;GAEG;AACH,MAAM,WAAW,eAAe,CAAC,CAAC,SAAS,gBAAgB,GAAG,gBAAgB;IAC1E;;;;;;;;OAQG;IACH,QAAQ,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,CAAC,KAAK,IAAI,CAAC;CACjD"}
|
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transformer primitive type.
|
|
3
|
+
*/
|
|
4
|
+
export type TransformerPrimitive = string | number | bigint | boolean;
|
|
5
|
+
/**
|
|
6
|
+
* Transformer input type, one of:
|
|
7
|
+
*
|
|
8
|
+
* - TInput (primitive type)
|
|
9
|
+
* - Iterable<TInput>
|
|
10
|
+
*
|
|
11
|
+
* @template TInput
|
|
12
|
+
* Transformer input primitive type.
|
|
13
|
+
*/
|
|
14
|
+
export type TransformerInput<TInput extends TransformerPrimitive> = TInput | Iterable<TInput>;
|
|
15
|
+
/**
|
|
16
|
+
* Transformer callback, used to convert transformed value to its final value.
|
|
17
|
+
*
|
|
18
|
+
* @template TInput
|
|
19
|
+
* Transformer input primitive type.
|
|
20
|
+
*
|
|
21
|
+
* @template TOutput
|
|
22
|
+
* Transformer output type.
|
|
23
|
+
*
|
|
24
|
+
* @param input
|
|
25
|
+
* Input value.
|
|
26
|
+
*
|
|
27
|
+
* @param index
|
|
28
|
+
* Index in sequence (0 for single transformation).
|
|
29
|
+
*
|
|
30
|
+
* @returns
|
|
31
|
+
* Output value.
|
|
32
|
+
*/
|
|
33
|
+
export type TransformerCallback<TInput extends TransformerPrimitive, TOutput> = (input: TInput, index: number) => TOutput;
|
|
34
|
+
/**
|
|
35
|
+
* Transformer output, based on transformer input:
|
|
36
|
+
*
|
|
37
|
+
* - If type TTransformerInput is primitive, result is type TOutput.
|
|
38
|
+
* - If type TTransformerInput is Iterable, result is type Iterable<TOutput>.
|
|
39
|
+
*
|
|
40
|
+
* @template TTransformerInput
|
|
41
|
+
* Transformer input type.
|
|
42
|
+
*
|
|
43
|
+
* @template TOutput
|
|
44
|
+
* Output base type.
|
|
45
|
+
*/
|
|
46
|
+
export type TransformerOutput<TTransformerInput extends TransformerInput<TransformerPrimitive>, TOutput> = TTransformerInput extends (TTransformerInput extends TransformerInput<infer TInput> ? TInput : never) ? TOutput : Iterable<TOutput>;
|
|
47
|
+
/**
|
|
48
|
+
* Transform an input iterable to an output iterable that applies a transformer callback to each value in the input.
|
|
49
|
+
*
|
|
50
|
+
* @param values
|
|
51
|
+
* Input values iterable.
|
|
52
|
+
*
|
|
53
|
+
* @param transformerCallback
|
|
54
|
+
* Callback to transform input value to output value.
|
|
55
|
+
*
|
|
56
|
+
* @returns
|
|
57
|
+
* Output values iterable.
|
|
58
|
+
*/
|
|
59
|
+
export declare function transformIterable<TInput extends TransformerPrimitive, TOutput>(values: Iterable<TInput>, transformerCallback: TransformerCallback<TInput, TOutput>): Iterable<TOutput>;
|
|
60
|
+
/**
|
|
61
|
+
* Transformer that transforms values in a numeric domain to values in a range equal to the domain or to another range
|
|
62
|
+
* defined by a callback function. In other words, the domain determines valid input values and, without a callback, the
|
|
63
|
+
* range of valid output values.
|
|
64
|
+
*
|
|
65
|
+
* The concept is similar to {@link https://en.wikipedia.org/wiki/Format-preserving_encryption | format-preserving
|
|
66
|
+
* encryption}, where input values within a specified domain (e.g., {@link
|
|
67
|
+
* https://en.wikipedia.org/wiki/Payment_card_number | payment card numbers} ranging from 8-19 digits) are transformed
|
|
68
|
+
* into values in the same domain, typically for storage in a database where the data type and length are already fixed
|
|
69
|
+
* and exfiltration of the data can have significant repercussions.
|
|
70
|
+
*
|
|
71
|
+
* Two subclasses are supported directly by this class: {@link IdentityTransformer} (which operates based on a domain
|
|
72
|
+
* only) and {@link EncryptionTransformer} (which operates based on a domain and a tweak). If an application is expected
|
|
73
|
+
* to make repeated use of a transformer with the same domain and (optional) tweak and can't manage the transformer
|
|
74
|
+
* object, an in-memory cache is available via the {@link get} method. Properties in {@link IdentityTransformer} and
|
|
75
|
+
* {@link EncryptionTransformer} are read-only once constructed, so there is no issue with their shared use.
|
|
76
|
+
*/
|
|
77
|
+
export declare abstract class Transformer {
|
|
78
|
+
/**
|
|
79
|
+
* Transformers cache, mapping a domain to another map, which maps an optional tweak to a transformer.
|
|
80
|
+
*/
|
|
81
|
+
private static readonly TRANSFORMER_MAPS_MAP;
|
|
82
|
+
/**
|
|
83
|
+
* Domain.
|
|
84
|
+
*/
|
|
85
|
+
private readonly _domain;
|
|
86
|
+
/**
|
|
87
|
+
* Constructor.
|
|
88
|
+
*
|
|
89
|
+
* @param domain
|
|
90
|
+
* Domain.
|
|
91
|
+
*/
|
|
92
|
+
constructor(domain: number | bigint);
|
|
93
|
+
/**
|
|
94
|
+
* Get a transformer, constructing it if necessary. The type returned is {@link IdentityTransformer} if tweak is
|
|
95
|
+
* undefined, {@link EncryptionTransformer} if tweak is defined. Note that although an {@link EncryptionTransformer}
|
|
96
|
+
* with a zero tweak operates as an {@link IdentityTransformer}, {@link EncryptionTransformer} is still the type
|
|
97
|
+
* returned if a zero tweak is explicitly specified.
|
|
98
|
+
*
|
|
99
|
+
* @param domain
|
|
100
|
+
* Domain.
|
|
101
|
+
*
|
|
102
|
+
* @param tweak
|
|
103
|
+
* Tweak.
|
|
104
|
+
*
|
|
105
|
+
* @returns
|
|
106
|
+
* {@link IdentityTransformer} if tweak is undefined, {@link EncryptionTransformer} if tweak is defined.
|
|
107
|
+
*/
|
|
108
|
+
static get(domain: number | bigint, tweak?: number | bigint): Transformer;
|
|
109
|
+
/**
|
|
110
|
+
* Get the domain.
|
|
111
|
+
*/
|
|
112
|
+
get domain(): bigint;
|
|
113
|
+
/**
|
|
114
|
+
* Validate that a value is within the domain.
|
|
115
|
+
*
|
|
116
|
+
* @param value
|
|
117
|
+
* Value.
|
|
118
|
+
*/
|
|
119
|
+
private validate;
|
|
120
|
+
/**
|
|
121
|
+
* Do the work of transforming a value forward.
|
|
122
|
+
*
|
|
123
|
+
* @param value
|
|
124
|
+
* Value.
|
|
125
|
+
*
|
|
126
|
+
* @returns
|
|
127
|
+
* Transformed value.
|
|
128
|
+
*/
|
|
129
|
+
protected abstract doForward(value: bigint): bigint;
|
|
130
|
+
/**
|
|
131
|
+
* Validate that a value is within the domain and do the work of transforming it forward.
|
|
132
|
+
*
|
|
133
|
+
* @param value
|
|
134
|
+
* Value.
|
|
135
|
+
*
|
|
136
|
+
* @returns
|
|
137
|
+
* Transformed value.
|
|
138
|
+
*/
|
|
139
|
+
private validateDoForward;
|
|
140
|
+
/**
|
|
141
|
+
* Validate that a value is within the domain, do the work of transforming it forward, and apply a callback.
|
|
142
|
+
*
|
|
143
|
+
* @param transformerCallback
|
|
144
|
+
* Called after each value is transformed to convert it to its final value.
|
|
145
|
+
*
|
|
146
|
+
* @param value
|
|
147
|
+
* Value.
|
|
148
|
+
*
|
|
149
|
+
* @param index
|
|
150
|
+
* Index in sequence (0 for single transformation).
|
|
151
|
+
*
|
|
152
|
+
* @returns
|
|
153
|
+
* Transformed value.
|
|
154
|
+
*/
|
|
155
|
+
private validateDoForwardCallback;
|
|
156
|
+
/**
|
|
157
|
+
* Transform value(s) forward.
|
|
158
|
+
*
|
|
159
|
+
* @template TTransformerInput
|
|
160
|
+
* Value(s) input type.
|
|
161
|
+
*
|
|
162
|
+
* @param valueOrValues
|
|
163
|
+
* Value(s). If this is an instance of {@link Sequence}, the minimum and maximum values are validated prior to
|
|
164
|
+
* transformation. Otherwise, the individual value(s) is/are validated at the time of transformation.
|
|
165
|
+
*
|
|
166
|
+
* @returns
|
|
167
|
+
* Transformed value(s).
|
|
168
|
+
*/
|
|
169
|
+
forward<TTransformerInput extends TransformerInput<number | bigint>>(valueOrValues: TTransformerInput): TransformerOutput<TTransformerInput, bigint>;
|
|
170
|
+
/**
|
|
171
|
+
* Transform value(s) forward, optionally applying a transformation.
|
|
172
|
+
*
|
|
173
|
+
* @template TTransformerInput
|
|
174
|
+
* Value(s) input type.
|
|
175
|
+
*
|
|
176
|
+
* @template TOutput
|
|
177
|
+
* Transformation callback output type.
|
|
178
|
+
*
|
|
179
|
+
* @param valueOrValues
|
|
180
|
+
* Value(s). If this is an instance of {@link Sequence}, the minimum and maximum values are validated prior to
|
|
181
|
+
* transformation. Otherwise, the individual value(s) is/are validated at the time of transformation.
|
|
182
|
+
*
|
|
183
|
+
* @param transformerCallback
|
|
184
|
+
* Called after each value is transformed to convert it to its final value.
|
|
185
|
+
*
|
|
186
|
+
* @returns
|
|
187
|
+
* Transformed value(s).
|
|
188
|
+
*/
|
|
189
|
+
forward<TTransformerInput extends TransformerInput<number | bigint>, TOutput>(valueOrValues: TTransformerInput, transformerCallback: TransformerCallback<bigint, TOutput>): TransformerOutput<TTransformerInput, TOutput>;
|
|
190
|
+
/**
|
|
191
|
+
* Do the work of transforming a value in reverse.
|
|
192
|
+
*
|
|
193
|
+
* @param transformedValue
|
|
194
|
+
* Transformed value.
|
|
195
|
+
*
|
|
196
|
+
* @returns
|
|
197
|
+
* Value.
|
|
198
|
+
*/
|
|
199
|
+
protected abstract doReverse(transformedValue: bigint): bigint;
|
|
200
|
+
/**
|
|
201
|
+
* Transform a value in reverse.
|
|
202
|
+
*
|
|
203
|
+
* @param transformedValue
|
|
204
|
+
* Transformed value.
|
|
205
|
+
*
|
|
206
|
+
* @returns
|
|
207
|
+
* Value.
|
|
208
|
+
*/
|
|
209
|
+
reverse(transformedValue: number | bigint): bigint;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Identity transformer. Values are transformed to themselves.
|
|
213
|
+
*/
|
|
214
|
+
export declare class IdentityTransformer extends Transformer {
|
|
215
|
+
/**
|
|
216
|
+
* @inheritDoc
|
|
217
|
+
*/
|
|
218
|
+
protected doForward(value: bigint): bigint;
|
|
219
|
+
/**
|
|
220
|
+
* @inheritDoc
|
|
221
|
+
*/
|
|
222
|
+
protected doReverse(transformedValue: bigint): bigint;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Encryption transformer. Values are transformed using repeated shuffle and xor operations, similar to those found in
|
|
226
|
+
* many cryptography algorithms, particularly AES. While sufficient for obfuscation of numeric sequences (e.g., serial
|
|
227
|
+
* number generation, below), if true format-preserving encryption is required, a more robust algorithm such as
|
|
228
|
+
* {@link https://doi.org/10.6028/NIST.SP.800-38Gr1-draft | FF1} is recommended. Furthermore, no work has been done to
|
|
229
|
+
* mitigate {@link https://timing.attacks.cr.yp.to/index.html | timing attacks} for key detection.
|
|
230
|
+
*
|
|
231
|
+
* The purpose of the encryption transformer is to generate pseudo-random values in a deterministic manner to obscure
|
|
232
|
+
* the sequence of values generated over time. A typical example is for serial number generation, where knowledge of the
|
|
233
|
+
* sequence can infer production volumes (e.g., serial number 1000 implies that at least 1,000 units have been
|
|
234
|
+
* manufactured) or can be used in counterfeiting (e.g., a counterfeiter can generate serial numbers 1001, 1002, ...
|
|
235
|
+
* with reasonable confidence that they would be valid if queried).
|
|
236
|
+
*
|
|
237
|
+
* The domain and the tweak together determine the encryption key, which in turn determines the number of rounds of
|
|
238
|
+
* shuffle and xor operations. The minimum number of rounds is 4, except where the domain is less than or equal to 256,
|
|
239
|
+
* which results in single-byte operations. To ensure that the operations are effective for single-byte domains, the
|
|
240
|
+
* number of rounds is 1 and only the xor operation is applied (shuffling a single byte is an identity operation).
|
|
241
|
+
*
|
|
242
|
+
* Another exception is when there is a tweak value of 0; this results in identity operations where the output value is
|
|
243
|
+
* identical to the input value, as no shuffle or xor takes place.
|
|
244
|
+
*/
|
|
245
|
+
export declare class EncryptionTransformer extends Transformer {
|
|
246
|
+
/**
|
|
247
|
+
* Individual bits, pre-calculated for performance.
|
|
248
|
+
*/
|
|
249
|
+
private static readonly BITS;
|
|
250
|
+
/**
|
|
251
|
+
* Inverse individual bits, pre-calculated for performance.
|
|
252
|
+
*/
|
|
253
|
+
private static readonly INVERSE_BITS;
|
|
254
|
+
/**
|
|
255
|
+
* Number of bytes covered by the domain.
|
|
256
|
+
*/
|
|
257
|
+
private readonly _domainBytes;
|
|
258
|
+
/**
|
|
259
|
+
* Xor bytes array generated from the domain and tweak.
|
|
260
|
+
*/
|
|
261
|
+
private readonly _xorBytes;
|
|
262
|
+
/**
|
|
263
|
+
* Bits array generated from the domain and tweak.
|
|
264
|
+
*/
|
|
265
|
+
private readonly _bits;
|
|
266
|
+
/**
|
|
267
|
+
* Inverse bits array generated from the domain and tweak.
|
|
268
|
+
*/
|
|
269
|
+
private readonly _inverseBits;
|
|
270
|
+
/**
|
|
271
|
+
* Number of rounds (length of arrays) generated from the domain and tweak.
|
|
272
|
+
*/
|
|
273
|
+
private readonly _rounds;
|
|
274
|
+
/**
|
|
275
|
+
* Constructor.
|
|
276
|
+
*
|
|
277
|
+
* @param domain
|
|
278
|
+
* Domain.
|
|
279
|
+
*
|
|
280
|
+
* @param tweak
|
|
281
|
+
* Tweak.
|
|
282
|
+
*/
|
|
283
|
+
constructor(domain: number | bigint, tweak: number | bigint);
|
|
284
|
+
/**
|
|
285
|
+
* Convert a value to a byte array big enough to handle the entire domain.
|
|
286
|
+
*
|
|
287
|
+
* @param value
|
|
288
|
+
* Value.
|
|
289
|
+
*
|
|
290
|
+
* @returns
|
|
291
|
+
* Big-endian byte array equivalent to the value.
|
|
292
|
+
*/
|
|
293
|
+
private valueToBytes;
|
|
294
|
+
/**
|
|
295
|
+
* Convert a byte array to a value.
|
|
296
|
+
*
|
|
297
|
+
* @param bytes
|
|
298
|
+
* Big-endian byte array equivalent to the value.
|
|
299
|
+
*
|
|
300
|
+
* @returns
|
|
301
|
+
* Value.
|
|
302
|
+
*/
|
|
303
|
+
private static bytesToValue;
|
|
304
|
+
/**
|
|
305
|
+
* Shuffle a byte array.
|
|
306
|
+
*
|
|
307
|
+
* The input array to the forward operation (output from the reverse operation) is `bytes` and the output array from
|
|
308
|
+
* the forward operation (input to the reverse operation) is `bytes'`.
|
|
309
|
+
*
|
|
310
|
+
* The shuffle operation starts by testing the bit at `bits[round]` for each `byte` in `bytes`. The indexes for all
|
|
311
|
+
* bytes with that bit set are put into one array (`shuffleIndexes1`) and the rest are put into another
|
|
312
|
+
* (`shuffleIndexes0`). The two arrays are concatenated and used to shuffle the input array, using their values
|
|
313
|
+
* (`shuffleIndex`) and the indexes of those values (`index`) in the concatenated array.
|
|
314
|
+
*
|
|
315
|
+
* Forward shuffling moves the entry at `shuffleIndex` to the `index` position.
|
|
316
|
+
*
|
|
317
|
+
* Reverse shuffling moves the entry at `index` to the `shuffleIndex` position.
|
|
318
|
+
*
|
|
319
|
+
* As each byte is moved, the bit at `bits[round]` is preserved in its original position. This ensures that the
|
|
320
|
+
* process is reversible.
|
|
321
|
+
*
|
|
322
|
+
* @param bytes
|
|
323
|
+
* Byte array.
|
|
324
|
+
*
|
|
325
|
+
* @param round
|
|
326
|
+
* Round number.
|
|
327
|
+
*
|
|
328
|
+
* @param forward
|
|
329
|
+
* True if operating forward (encrypting), false if operating in reverse (decrypting).
|
|
330
|
+
*
|
|
331
|
+
* @returns
|
|
332
|
+
* Shuffled byte array.
|
|
333
|
+
*/
|
|
334
|
+
private shuffle;
|
|
335
|
+
/**
|
|
336
|
+
* Xor a byte array.
|
|
337
|
+
*
|
|
338
|
+
* The input array to the forward operation (output from the reverse operation) is `bytes` and the output array from
|
|
339
|
+
* the forward operation (input to the reverse operation) is `bytes'`.
|
|
340
|
+
*
|
|
341
|
+
* Forward:
|
|
342
|
+
* - `bytes'[0] = bytes[0] ^ xorBytes[round]`
|
|
343
|
+
* - `bytes'[1] = bytes[1] ^ bytes'[0]`
|
|
344
|
+
* - `bytes'[2] = bytes[2] ^ bytes'[1]`
|
|
345
|
+
* - `...`
|
|
346
|
+
* - `bytes'[domainBytes - 1] = bytes[domainBytes - 1] ^ bytes'[domainBytes - 2]`
|
|
347
|
+
*
|
|
348
|
+
* Reverse:
|
|
349
|
+
* - `bytes[0] = bytes'[0] ^ xorBytes[round]`
|
|
350
|
+
* - `bytes[1] = bytes'[1] ^ bytes'[0]`
|
|
351
|
+
* - `bytes[2] = bytes'[2] ^ bytes'[1]`
|
|
352
|
+
* - `...`
|
|
353
|
+
* - `bytes[domainBytes - 1] = bytes'[domainBytes - 1] ^ bytes'[domainBytes - 2]`
|
|
354
|
+
*
|
|
355
|
+
* @param bytes
|
|
356
|
+
* Byte array.
|
|
357
|
+
*
|
|
358
|
+
* @param round
|
|
359
|
+
* Round number.
|
|
360
|
+
*
|
|
361
|
+
* @param forward
|
|
362
|
+
* True if operating forward (encrypting), false if operating in reverse (decrypting).
|
|
363
|
+
*
|
|
364
|
+
* @returns
|
|
365
|
+
* Xored byte array.
|
|
366
|
+
*/
|
|
367
|
+
private xor;
|
|
368
|
+
/**
|
|
369
|
+
* @inheritDoc
|
|
370
|
+
*/
|
|
371
|
+
protected doForward(value: bigint): bigint;
|
|
372
|
+
/**
|
|
373
|
+
* @inheritDoc
|
|
374
|
+
*/
|
|
375
|
+
protected doReverse(transformedValue: bigint): bigint;
|
|
376
|
+
}
|
|
377
|
+
//# sourceMappingURL=transformer.d.ts.map
|