@aidc-toolkit/utility 0.9.7-beta → 0.9.8-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 +91 -482
- package/dist/index.d.cts +611 -65
- package/dist/index.d.ts +611 -65
- package/dist/index.js +77 -481
- package/package.json +5 -4
- package/src/{character_set.ts → character-set.ts} +17 -36
- package/src/index.ts +3 -5
- package/src/locale/i18n.ts +37 -6
- package/src/locale/i18next.d.ts +3 -3
- package/src/record.ts +3 -4
- package/src/{reg_exp.ts → reg-exp.ts} +2 -3
- package/src/sequencer.ts +8 -50
- package/src/transformer.ts +89 -23
- package/test/{character_set.test.ts → character-set.test.ts} +3 -2
- package/test/record.test.ts +3 -3
- package/test/{reg_exp.test.ts → reg-exp.test.ts} +3 -3
- package/test/sequencer.test.ts +10 -21
- package/test/transformer.test.ts +31 -3
- package/src/iterator_proxy.ts +0 -517
- package/src/types.ts +0 -46
- package/test/iterator_proxy.test.ts +0 -252
- /package/src/locale/en/{locale_strings.ts → locale-strings.ts} +0 -0
- /package/src/locale/fr/{locale_strings.ts → locale-strings.ts} +0 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aidc-toolkit/utility",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.8-beta",
|
|
4
4
|
"description": "Foundational utilities for AIDC Toolkit",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"test": "vitest run"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"@aidc-toolkit/dev": "^0.9.
|
|
28
|
+
"@aidc-toolkit/dev": "^0.9.8-beta",
|
|
29
29
|
"eslint": "^9.16.0",
|
|
30
30
|
"ts-node": "^10.9.2",
|
|
31
31
|
"tsup": "^8.3.5",
|
|
@@ -33,7 +33,8 @@
|
|
|
33
33
|
"vitest": "^2.1.8"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@aidc-toolkit/core": "^0.9.
|
|
37
|
-
"@rollup/rollup-linux-x64-gnu": "^4.28.1"
|
|
36
|
+
"@aidc-toolkit/core": "^0.9.8-beta",
|
|
37
|
+
"@rollup/rollup-linux-x64-gnu": "^4.28.1",
|
|
38
|
+
"i18next": "^24.1.0"
|
|
38
39
|
}
|
|
39
40
|
}
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { RegExpValidator } from "./
|
|
1
|
+
import { i18nextUtility } from "./locale/i18n.js";
|
|
2
|
+
import { RegExpValidator } from "./reg-exp.js";
|
|
3
3
|
import type { StringValidation, StringValidator } from "./string.js";
|
|
4
|
-
import { Transformer } from "./transformer.js";
|
|
5
|
-
import type { TransformerCallback, TransformerInput, TransformerOutput } from "./types.js";
|
|
4
|
+
import { Transformer, type TransformerCallback, type TransformerInput, type TransformerOutput } from "./transformer.js";
|
|
6
5
|
|
|
7
6
|
/**
|
|
8
7
|
* Exclusion options for validating and creating strings based on character sets.
|
|
@@ -72,9 +71,7 @@ export class CharacterSetValidator implements StringValidator<CharacterSetValida
|
|
|
72
71
|
* Error message.
|
|
73
72
|
*/
|
|
74
73
|
protected override createErrorMessage(_s: string): string {
|
|
75
|
-
return
|
|
76
|
-
ns: utilityNS
|
|
77
|
-
});
|
|
74
|
+
return i18nextUtility.t("CharacterSetValidator.stringMustNotBeAllNumeric");
|
|
78
75
|
}
|
|
79
76
|
}(/\D/);
|
|
80
77
|
|
|
@@ -200,8 +197,7 @@ export class CharacterSetValidator implements StringValidator<CharacterSetValida
|
|
|
200
197
|
*/
|
|
201
198
|
protected validateExclusion(exclusion: Exclusion): void {
|
|
202
199
|
if (exclusion !== Exclusion.None && !this._exclusionSupport.includes(exclusion)) {
|
|
203
|
-
throw new RangeError(
|
|
204
|
-
ns: utilityNS,
|
|
200
|
+
throw new RangeError(i18nextUtility.t("CharacterSetValidator.exclusionNotSupported", {
|
|
205
201
|
exclusion
|
|
206
202
|
}));
|
|
207
203
|
}
|
|
@@ -227,15 +223,13 @@ export class CharacterSetValidator implements StringValidator<CharacterSetValida
|
|
|
227
223
|
let errorMessage: string;
|
|
228
224
|
|
|
229
225
|
if (maximumLength !== undefined && maximumLength === minimumLength) {
|
|
230
|
-
errorMessage =
|
|
231
|
-
ns: utilityNS,
|
|
226
|
+
errorMessage = i18nextUtility.t(validation?.component === undefined ? "CharacterSetValidator.lengthMustBeEqualTo" : "CharacterSetValidator.lengthOfComponentMustBeEqualTo", {
|
|
232
227
|
component: CharacterSetValidator.componentToString(validation?.component),
|
|
233
228
|
length,
|
|
234
229
|
exactLength: minimumLength
|
|
235
230
|
});
|
|
236
231
|
} else {
|
|
237
|
-
errorMessage =
|
|
238
|
-
ns: utilityNS,
|
|
232
|
+
errorMessage = i18nextUtility.t(validation?.component === undefined ? "CharacterSetValidator.lengthMustBeGreaterThanOrEqualTo" : "CharacterSetValidator.lengthOfComponentMustBeGreaterThanOrEqualTo", {
|
|
239
233
|
component: CharacterSetValidator.componentToString(validation?.component),
|
|
240
234
|
length,
|
|
241
235
|
minimumLength
|
|
@@ -246,8 +240,7 @@ export class CharacterSetValidator implements StringValidator<CharacterSetValida
|
|
|
246
240
|
}
|
|
247
241
|
|
|
248
242
|
if (maximumLength !== undefined && length > maximumLength) {
|
|
249
|
-
throw new RangeError(
|
|
250
|
-
ns: utilityNS,
|
|
243
|
+
throw new RangeError(i18nextUtility.t(validation?.component === undefined ? "CharacterSetValidator.lengthMustBeLessThanOrEqualTo" : "CharacterSetValidator.lengthOfComponentMustBeLessThanOrEqualTo", {
|
|
251
244
|
component: CharacterSetValidator.componentToString(validation?.component),
|
|
252
245
|
length,
|
|
253
246
|
maximumLength
|
|
@@ -258,8 +251,7 @@ export class CharacterSetValidator implements StringValidator<CharacterSetValida
|
|
|
258
251
|
const index = this.characterIndexes(s).findIndex(characterIndex => characterIndex === undefined);
|
|
259
252
|
|
|
260
253
|
if (index !== -1) {
|
|
261
|
-
throw new RangeError(
|
|
262
|
-
ns: utilityNS,
|
|
254
|
+
throw new RangeError(i18nextUtility.t(validation?.component === undefined ? "CharacterSetValidator.invalidCharacterAtPosition" : "CharacterSetValidator.invalidCharacterAtPositionOfComponent", {
|
|
263
255
|
component: CharacterSetValidator.componentToString(validation?.component),
|
|
264
256
|
c: s.charAt(index),
|
|
265
257
|
position: index + (validation?.positionOffset ?? 0) + 1
|
|
@@ -275,8 +267,7 @@ export class CharacterSetValidator implements StringValidator<CharacterSetValida
|
|
|
275
267
|
|
|
276
268
|
case Exclusion.FirstZero:
|
|
277
269
|
if (s.startsWith("0")) {
|
|
278
|
-
throw new RangeError(
|
|
279
|
-
ns: utilityNS,
|
|
270
|
+
throw new RangeError(i18nextUtility.t(validation.component === undefined ? "CharacterSetValidator.invalidCharacterAtPosition" : "CharacterSetValidator.invalidCharacterAtPositionOfComponent", {
|
|
280
271
|
component: CharacterSetValidator.componentToString(validation.component),
|
|
281
272
|
c: "0",
|
|
282
273
|
position: (validation.positionOffset ?? 0) + 1
|
|
@@ -384,9 +375,7 @@ export class CharacterSetCreator extends CharacterSetValidator {
|
|
|
384
375
|
|
|
385
376
|
if (exclusionSupport.includes(Exclusion.FirstZero)) {
|
|
386
377
|
if (characterSet[0] !== "0") {
|
|
387
|
-
throw new RangeError(
|
|
388
|
-
ns: utilityNS
|
|
389
|
-
}));
|
|
378
|
+
throw new RangeError(i18nextUtility.t("CharacterSetValidator.firstZeroFirstCharacter"));
|
|
390
379
|
}
|
|
391
380
|
|
|
392
381
|
const exclusionFirstZeroDomains = new Array<bigint>(CharacterSetCreator.MAXIMUM_STRING_LENGTH + 1);
|
|
@@ -417,9 +406,7 @@ export class CharacterSetCreator extends CharacterSetValidator {
|
|
|
417
406
|
// Make sure that all numeric characters are present and in sequence.
|
|
418
407
|
for (const numberIndex of numberIndexes) {
|
|
419
408
|
if (numberIndex === undefined || numberIndex !== expectedNumberIndex) {
|
|
420
|
-
throw new RangeError(
|
|
421
|
-
ns: utilityNS
|
|
422
|
-
}));
|
|
409
|
+
throw new RangeError(i18nextUtility.t("CharacterSetValidator.allNumericAllNumericCharacters"));
|
|
423
410
|
}
|
|
424
411
|
|
|
425
412
|
expectedNumberIndex = numberIndex + 1;
|
|
@@ -491,9 +478,7 @@ export class CharacterSetCreator extends CharacterSetValidator {
|
|
|
491
478
|
if (length === 0) {
|
|
492
479
|
if (!shiftForward && value < 10n) {
|
|
493
480
|
// If calculation gets this far, string is all-numeric.
|
|
494
|
-
throw new RangeError(
|
|
495
|
-
ns: utilityNS
|
|
496
|
-
}));
|
|
481
|
+
throw new RangeError(i18nextUtility.t("CharacterSetValidator.stringMustNotBeAllNumeric"));
|
|
497
482
|
}
|
|
498
483
|
|
|
499
484
|
// Now dealing with individual characters; shift by 10 to skip numeric characters.
|
|
@@ -528,16 +513,14 @@ export class CharacterSetCreator extends CharacterSetValidator {
|
|
|
528
513
|
*/
|
|
529
514
|
private validateLength(length: number): void {
|
|
530
515
|
if (length < 0) {
|
|
531
|
-
throw new RangeError(
|
|
532
|
-
ns: utilityNS,
|
|
516
|
+
throw new RangeError(i18nextUtility.t("CharacterSetValidator.lengthMustBeGreaterThanOrEqualTo", {
|
|
533
517
|
length,
|
|
534
518
|
minimumLength: 0
|
|
535
519
|
}));
|
|
536
520
|
}
|
|
537
521
|
|
|
538
522
|
if (length > CharacterSetCreator.MAXIMUM_STRING_LENGTH) {
|
|
539
|
-
throw new RangeError(
|
|
540
|
-
ns: utilityNS,
|
|
523
|
+
throw new RangeError(i18nextUtility.t("CharacterSetValidator.lengthMustBeLessThanOrEqualTo", {
|
|
541
524
|
length,
|
|
542
525
|
maximumLength: CharacterSetCreator.MAXIMUM_STRING_LENGTH
|
|
543
526
|
}));
|
|
@@ -633,8 +616,7 @@ export class CharacterSetCreator extends CharacterSetValidator {
|
|
|
633
616
|
// Convert string to its value character by character.
|
|
634
617
|
let value = this.characterIndexes(s).reduce((accumulator, characterIndex, index) => {
|
|
635
618
|
if (characterIndex === undefined) {
|
|
636
|
-
throw new RangeError(
|
|
637
|
-
ns: utilityNS,
|
|
619
|
+
throw new RangeError(i18nextUtility.t("CharacterSetValidator.invalidCharacterAtPosition", {
|
|
638
620
|
c: s.charAt(index),
|
|
639
621
|
position: index + 1
|
|
640
622
|
}));
|
|
@@ -644,8 +626,7 @@ export class CharacterSetCreator extends CharacterSetValidator {
|
|
|
644
626
|
|
|
645
627
|
if (index === 0 && exclusion === Exclusion.FirstZero) {
|
|
646
628
|
if (characterIndex === 0) {
|
|
647
|
-
throw new RangeError(
|
|
648
|
-
ns: utilityNS,
|
|
629
|
+
throw new RangeError(i18nextUtility.t("CharacterSetValidator.invalidCharacterAtPosition", {
|
|
649
630
|
c: "0",
|
|
650
631
|
position: 1
|
|
651
632
|
}));
|
package/src/index.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
export
|
|
2
|
-
export type * from "./types.js";
|
|
3
|
-
export * from "./iterator_proxy.js";
|
|
1
|
+
export * from "./locale/i18n.js";
|
|
4
2
|
export * from "./sequencer.js";
|
|
5
3
|
export * from "./transformer.js";
|
|
6
4
|
export type * from "./string.js";
|
|
7
|
-
export * from "./
|
|
5
|
+
export * from "./reg-exp.js";
|
|
8
6
|
export * from "./record.js";
|
|
9
|
-
export * from "./
|
|
7
|
+
export * from "./character-set.js";
|
package/src/locale/i18n.ts
CHANGED
|
@@ -1,12 +1,43 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { localeStrings as
|
|
1
|
+
import { i18nAssertValidResources, i18nCoreInit, type I18NEnvironment } from "@aidc-toolkit/core";
|
|
2
|
+
import i18next, { type Resource } from "i18next";
|
|
3
|
+
import { localeStrings as enLocaleStrings } from "./en/locale-strings.js";
|
|
4
|
+
import { localeStrings as frLocaleStrings } from "./fr/locale-strings.js";
|
|
4
5
|
|
|
5
6
|
export const utilityNS = "aidct_utility";
|
|
6
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Locale strings type is extracted from the English locale strings object.
|
|
10
|
+
*/
|
|
11
|
+
export type UtilityLocaleStrings = typeof enLocaleStrings;
|
|
12
|
+
|
|
7
13
|
i18nAssertValidResources(enLocaleStrings, "fr", frLocaleStrings);
|
|
8
14
|
|
|
9
|
-
|
|
10
|
-
|
|
15
|
+
/**
|
|
16
|
+
* Utility resources.
|
|
17
|
+
*/
|
|
18
|
+
export const utilityResources: Resource = {
|
|
19
|
+
en: {
|
|
20
|
+
aidct_utility: enLocaleStrings
|
|
21
|
+
},
|
|
22
|
+
fr: {
|
|
23
|
+
aidct_utility: frLocaleStrings
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const i18nextUtility = i18next.createInstance();
|
|
11
28
|
|
|
12
|
-
|
|
29
|
+
/**
|
|
30
|
+
* Initialize internationalization.
|
|
31
|
+
*
|
|
32
|
+
* @param environment
|
|
33
|
+
* Environment in which the application is running.
|
|
34
|
+
*
|
|
35
|
+
* @param debug
|
|
36
|
+
* Debug setting.
|
|
37
|
+
*
|
|
38
|
+
* @returns
|
|
39
|
+
* Void promise.
|
|
40
|
+
*/
|
|
41
|
+
export async function i18nUtilityInit(environment: I18NEnvironment, debug = false): Promise<void> {
|
|
42
|
+
await i18nCoreInit(i18nextUtility, environment, debug, utilityNS, utilityResources);
|
|
43
|
+
}
|
package/src/locale/i18next.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { UtilityLocaleStrings } from "./i18n.js";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Internationalization module.
|
|
@@ -8,9 +8,9 @@ declare module "i18next" {
|
|
|
8
8
|
* Custom type options for this package.
|
|
9
9
|
*/
|
|
10
10
|
interface CustomTypeOptions {
|
|
11
|
+
defaultNS: "aidct_utility";
|
|
11
12
|
resources: {
|
|
12
|
-
|
|
13
|
-
aidct_utility: typeof localeStrings;
|
|
13
|
+
aidct_utility: UtilityLocaleStrings;
|
|
14
14
|
};
|
|
15
15
|
}
|
|
16
16
|
}
|
package/src/record.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { i18nextUtility } from "./locale/i18n.js";
|
|
2
2
|
import type { StringValidator } from "./string.js";
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -51,9 +51,8 @@ export class RecordValidator<T> implements StringValidator {
|
|
|
51
51
|
* Record key.
|
|
52
52
|
*/
|
|
53
53
|
validate(key: string): void {
|
|
54
|
-
if (this.record
|
|
55
|
-
throw new RangeError(
|
|
56
|
-
ns: utilityNS,
|
|
54
|
+
if (!(key in this.record)) {
|
|
55
|
+
throw new RangeError(i18nextUtility.t("RecordValidator.typeNameKeyNotFound", {
|
|
57
56
|
typeName: this.typeName,
|
|
58
57
|
key
|
|
59
58
|
}));
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { i18nextUtility } from "./locale/i18n.js";
|
|
2
2
|
import type { StringValidator } from "./string.js";
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -44,8 +44,7 @@ export class RegExpValidator implements StringValidator {
|
|
|
44
44
|
* Error message.
|
|
45
45
|
*/
|
|
46
46
|
protected createErrorMessage(s: string): string {
|
|
47
|
-
return
|
|
48
|
-
ns: utilityNS,
|
|
47
|
+
return i18nextUtility.t("RegExpValidator.stringDoesNotMatchPattern", {
|
|
49
48
|
s
|
|
50
49
|
});
|
|
51
50
|
}
|
package/src/sequencer.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Sequencer. Defines an ascending or descending sequence of big integers implemented as an iterable
|
|
2
|
+
* Sequencer. Defines an ascending or descending sequence of big integers implemented as an iterable.
|
|
3
3
|
*/
|
|
4
|
-
export class Sequencer implements Iterable<bigint
|
|
4
|
+
export class Sequencer implements Iterable<bigint> {
|
|
5
5
|
/**
|
|
6
6
|
* Start value (inclusive).
|
|
7
7
|
*/
|
|
@@ -32,11 +32,6 @@ export class Sequencer implements Iterable<bigint>, IterableIterator<bigint> {
|
|
|
32
32
|
*/
|
|
33
33
|
private readonly _maxValue: bigint;
|
|
34
34
|
|
|
35
|
-
/**
|
|
36
|
-
* Next value.
|
|
37
|
-
*/
|
|
38
|
-
private _nextValue: bigint;
|
|
39
|
-
|
|
40
35
|
/**
|
|
41
36
|
* Constructor.
|
|
42
37
|
*
|
|
@@ -52,9 +47,7 @@ export class Sequencer implements Iterable<bigint>, IterableIterator<bigint> {
|
|
|
52
47
|
this._endValue = this._startValue + BigInt(count);
|
|
53
48
|
this._count = count;
|
|
54
49
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
if (ascending) {
|
|
50
|
+
if (count >= 0) {
|
|
58
51
|
this._nextDelta = 1n;
|
|
59
52
|
this._minValue = this._startValue;
|
|
60
53
|
this._maxValue = this._endValue - 1n;
|
|
@@ -63,8 +56,6 @@ export class Sequencer implements Iterable<bigint>, IterableIterator<bigint> {
|
|
|
63
56
|
this._minValue = this._endValue + 1n;
|
|
64
57
|
this._maxValue = this._startValue;
|
|
65
58
|
}
|
|
66
|
-
|
|
67
|
-
this._nextValue = this._startValue;
|
|
68
59
|
}
|
|
69
60
|
|
|
70
61
|
/**
|
|
@@ -105,45 +96,12 @@ export class Sequencer implements Iterable<bigint>, IterableIterator<bigint> {
|
|
|
105
96
|
/**
|
|
106
97
|
* Iterable implementation.
|
|
107
98
|
*
|
|
108
|
-
* @
|
|
109
|
-
*
|
|
110
|
-
*/
|
|
111
|
-
[Symbol.iterator](): this {
|
|
112
|
-
return this;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Iterator implementation.
|
|
117
|
-
*
|
|
118
|
-
* @returns
|
|
119
|
-
* Iterator result. If iterator is exhausted, the value is absolute value of the count.
|
|
99
|
+
* @yields
|
|
100
|
+
* Next value in sequence.
|
|
120
101
|
*/
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
let result: IteratorResult<bigint, number>;
|
|
125
|
-
|
|
126
|
-
if (!done) {
|
|
127
|
-
result = {
|
|
128
|
-
value: this._nextValue
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
this._nextValue += this._nextDelta;
|
|
132
|
-
} else {
|
|
133
|
-
result = {
|
|
134
|
-
done: true,
|
|
135
|
-
value: Math.abs(this._count)
|
|
136
|
-
};
|
|
102
|
+
* [Symbol.iterator](): Generator<bigint> {
|
|
103
|
+
for (let value = this._startValue; value !== this._endValue; value += this._nextDelta) {
|
|
104
|
+
yield value;
|
|
137
105
|
}
|
|
138
|
-
|
|
139
|
-
return result;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* Reset the iterator.
|
|
144
|
-
*/
|
|
145
|
-
reset(): void {
|
|
146
|
-
// Reset simply returns to the start.
|
|
147
|
-
this._nextValue = this._startValue;
|
|
148
106
|
}
|
|
149
107
|
}
|
package/src/transformer.ts
CHANGED
|
@@ -1,7 +1,75 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import i18next, { utilityNS } from "./locale/i18n.js";
|
|
1
|
+
import { i18nextUtility } from "./locale/i18n.js";
|
|
3
2
|
import { Sequencer } from "./sequencer.js";
|
|
4
|
-
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Transformer input, one of:
|
|
6
|
+
*
|
|
7
|
+
* - T (primitive type)
|
|
8
|
+
* - Iterable<T>
|
|
9
|
+
*
|
|
10
|
+
* @template T
|
|
11
|
+
* Primitive type.
|
|
12
|
+
*/
|
|
13
|
+
export type TransformerInput<T extends string | number | bigint | boolean> = T | Iterable<T>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Transformer callback, used to convert transformed value to its final value.
|
|
17
|
+
*
|
|
18
|
+
* @template TInput
|
|
19
|
+
* Type of input to callback.
|
|
20
|
+
*
|
|
21
|
+
* @template TOutput
|
|
22
|
+
* Type of output to callback.
|
|
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, TOutput> = (input: TInput, index: number) => TOutput;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Transformer output, based on transformer input:
|
|
37
|
+
*
|
|
38
|
+
* - If type T is primitive, result is type TOutput.
|
|
39
|
+
* - If type T is Iterable, result is type Iterable<TOutput>.
|
|
40
|
+
*
|
|
41
|
+
* @template T
|
|
42
|
+
* Transformer input type.
|
|
43
|
+
*
|
|
44
|
+
* @template TOutput
|
|
45
|
+
* Output base type.
|
|
46
|
+
*/
|
|
47
|
+
export type TransformerOutput<T extends TransformerInput<string | number | bigint | boolean>, TOutput> =
|
|
48
|
+
T extends (T extends TransformerInput<infer TInput> ? TInput : never) ? TOutput : Iterable<TOutput>;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Transform an iterable by applying a transformer callback to each entry.
|
|
52
|
+
*
|
|
53
|
+
* @param iterable
|
|
54
|
+
* Input iterable.
|
|
55
|
+
*
|
|
56
|
+
* @param transformerCallback
|
|
57
|
+
* Callback to transform input value to output value.
|
|
58
|
+
*
|
|
59
|
+
* @returns
|
|
60
|
+
* Output iterable.
|
|
61
|
+
*/
|
|
62
|
+
export function transformIterable<TInput, TOutput>(iterable: Iterable<TInput>, transformerCallback: TransformerCallback<TInput, TOutput>): Iterable<TOutput> {
|
|
63
|
+
return {
|
|
64
|
+
* [Symbol.iterator](): Generator<TOutput> {
|
|
65
|
+
let index = 0;
|
|
66
|
+
|
|
67
|
+
for (const input of iterable) {
|
|
68
|
+
yield transformerCallback(input, index++);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
}
|
|
5
73
|
|
|
6
74
|
/**
|
|
7
75
|
* Transformer that transforms values in a numeric domain to values in a range equal to the domain or to another range
|
|
@@ -41,8 +109,7 @@ export abstract class Transformer {
|
|
|
41
109
|
this._domain = BigInt(domain);
|
|
42
110
|
|
|
43
111
|
if (this._domain <= 0n) {
|
|
44
|
-
throw new RangeError(
|
|
45
|
-
ns: utilityNS,
|
|
112
|
+
throw new RangeError(i18nextUtility.t("Transformer.domainMustBeGreaterThanZero", {
|
|
46
113
|
domain
|
|
47
114
|
}));
|
|
48
115
|
}
|
|
@@ -100,15 +167,13 @@ export abstract class Transformer {
|
|
|
100
167
|
*/
|
|
101
168
|
private validate(value: bigint): void {
|
|
102
169
|
if (value < 0n) {
|
|
103
|
-
throw new RangeError(
|
|
104
|
-
ns: utilityNS,
|
|
170
|
+
throw new RangeError(i18nextUtility.t("Transformer.valueMustBeGreaterThanOrEqualToZero", {
|
|
105
171
|
value
|
|
106
172
|
}));
|
|
107
173
|
}
|
|
108
174
|
|
|
109
175
|
if (value >= this.domain) {
|
|
110
|
-
throw new RangeError(
|
|
111
|
-
ns: utilityNS,
|
|
176
|
+
throw new RangeError(i18nextUtility.t("Transformer.valueMustBeLessThan", {
|
|
112
177
|
value,
|
|
113
178
|
domain: this.domain
|
|
114
179
|
}));
|
|
@@ -165,7 +230,7 @@ export abstract class Transformer {
|
|
|
165
230
|
// eslint-disable-next-line jsdoc/require-jsdoc -- Implementation of overloaded signatures.
|
|
166
231
|
forward<T extends TransformerInput<number | bigint>, U>(valueOrValues: T, transformerCallback?: TransformerCallback<bigint, U>): TransformerOutput<T, U> {
|
|
167
232
|
// TODO Refactor type when https://github.com/microsoft/TypeScript/pull/56941 released.
|
|
168
|
-
let result: bigint | U |
|
|
233
|
+
let result: bigint | U | Iterable<bigint> | Iterable<U>;
|
|
169
234
|
|
|
170
235
|
if (typeof valueOrValues !== "object") {
|
|
171
236
|
const valueN = BigInt(valueOrValues);
|
|
@@ -177,39 +242,41 @@ export abstract class Transformer {
|
|
|
177
242
|
result = transformerCallback === undefined ? transformedValue : transformerCallback(transformedValue, 0);
|
|
178
243
|
} else if (valueOrValues instanceof Sequencer) {
|
|
179
244
|
if (valueOrValues.minValue < 0n) {
|
|
180
|
-
throw new RangeError(
|
|
181
|
-
ns: utilityNS,
|
|
245
|
+
throw new RangeError(i18nextUtility.t("Transformer.minValueMustBeGreaterThanOrEqualToZero", {
|
|
182
246
|
minValue: valueOrValues.minValue
|
|
183
247
|
}));
|
|
184
248
|
}
|
|
185
249
|
|
|
186
250
|
if (valueOrValues.maxValue >= this.domain) {
|
|
187
|
-
throw new RangeError(
|
|
188
|
-
ns: utilityNS,
|
|
251
|
+
throw new RangeError(i18nextUtility.t("Transformer.maxValueMustBeLessThan", {
|
|
189
252
|
maxValue: valueOrValues.maxValue,
|
|
190
253
|
domain: this.domain
|
|
191
254
|
}));
|
|
192
255
|
}
|
|
193
256
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
257
|
+
if (transformerCallback === undefined) {
|
|
258
|
+
result = transformIterable(valueOrValues, value => this.doForward(value));
|
|
259
|
+
} else {
|
|
260
|
+
result = transformIterable(valueOrValues, (value, index) => transformerCallback(this.doForward(value), index));
|
|
261
|
+
}
|
|
197
262
|
} else {
|
|
198
|
-
|
|
199
|
-
|
|
263
|
+
if (transformerCallback === undefined) {
|
|
264
|
+
result = transformIterable(valueOrValues, (value) => {
|
|
200
265
|
const valueN = BigInt(value);
|
|
201
266
|
|
|
202
267
|
this.validate(valueN);
|
|
203
268
|
|
|
204
269
|
return this.doForward(valueN);
|
|
205
|
-
})
|
|
206
|
-
|
|
270
|
+
});
|
|
271
|
+
} else {
|
|
272
|
+
result = transformIterable(valueOrValues, (value, index) => {
|
|
207
273
|
const valueN = BigInt(value);
|
|
208
274
|
|
|
209
275
|
this.validate(valueN);
|
|
210
276
|
|
|
211
277
|
return transformerCallback(this.doForward(valueN), index);
|
|
212
278
|
});
|
|
279
|
+
}
|
|
213
280
|
}
|
|
214
281
|
|
|
215
282
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- Type determination is handled above.
|
|
@@ -342,8 +409,7 @@ export class EncryptionTransformer extends Transformer {
|
|
|
342
409
|
super(domain);
|
|
343
410
|
|
|
344
411
|
if (tweak < 0n) {
|
|
345
|
-
throw new RangeError(
|
|
346
|
-
ns: utilityNS,
|
|
412
|
+
throw new RangeError(i18nextUtility.t("Transformer.tweakMustBeGreaterThanOrEqualToZero", {
|
|
347
413
|
tweak
|
|
348
414
|
}));
|
|
349
415
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { I18NEnvironment
|
|
1
|
+
import { I18NEnvironment } from "@aidc-toolkit/core";
|
|
2
2
|
import { describe, expect, test } from "vitest";
|
|
3
3
|
import {
|
|
4
4
|
ALPHABETIC_CREATOR,
|
|
@@ -6,11 +6,12 @@ import {
|
|
|
6
6
|
CharacterSetCreator,
|
|
7
7
|
Exclusion,
|
|
8
8
|
HEXADECIMAL_CREATOR,
|
|
9
|
+
i18nUtilityInit,
|
|
9
10
|
NUMERIC_CREATOR,
|
|
10
11
|
Sequencer
|
|
11
12
|
} from "../src/index.js";
|
|
12
13
|
|
|
13
|
-
await
|
|
14
|
+
await i18nUtilityInit(I18NEnvironment.CLI);
|
|
14
15
|
|
|
15
16
|
function testCharacterSetCreator(name: string, characterSetCreator: CharacterSetCreator, characterSetSize: number, length: number, excludeFirstZero: boolean, excludeAllNumeric: boolean): void {
|
|
16
17
|
describe(name, () => {
|
package/test/record.test.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { I18NEnvironment
|
|
1
|
+
import { I18NEnvironment } from "@aidc-toolkit/core";
|
|
2
2
|
import { describe, expect, test } from "vitest";
|
|
3
|
-
import { RecordValidator } from "../src/index.js";
|
|
3
|
+
import { i18nUtilityInit, RecordValidator } from "../src/index.js";
|
|
4
4
|
|
|
5
|
-
await
|
|
5
|
+
await i18nUtilityInit(I18NEnvironment.CLI);
|
|
6
6
|
|
|
7
7
|
describe("Record validator", () => {
|
|
8
8
|
enum StringEnum {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { I18NEnvironment
|
|
1
|
+
import { I18NEnvironment } from "@aidc-toolkit/core";
|
|
2
2
|
import { describe, expect, test } from "vitest";
|
|
3
|
-
import { RegExpValidator } from "../src/index.js";
|
|
3
|
+
import { i18nUtilityInit, RegExpValidator } from "../src/index.js";
|
|
4
4
|
|
|
5
|
-
await
|
|
5
|
+
await i18nUtilityInit(I18NEnvironment.CLI);
|
|
6
6
|
|
|
7
7
|
describe("Regular expression validator", () => {
|
|
8
8
|
test("Validation", () => {
|
package/test/sequencer.test.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { I18NEnvironment
|
|
1
|
+
import { I18NEnvironment } from "@aidc-toolkit/core";
|
|
2
2
|
import { describe, expect, test } from "vitest";
|
|
3
|
-
import { Sequencer } from "../src/index.js";
|
|
3
|
+
import { i18nUtilityInit, Sequencer } from "../src/index.js";
|
|
4
4
|
|
|
5
|
-
await
|
|
5
|
+
await i18nUtilityInit(I18NEnvironment.CLI);
|
|
6
6
|
|
|
7
7
|
describe("Sequence", () => {
|
|
8
8
|
const sequencer1 = new Sequencer(10, 20);
|
|
@@ -22,7 +22,7 @@ describe("Sequence", () => {
|
|
|
22
22
|
expect(sequencer2.maxValue).toBe(29n);
|
|
23
23
|
});
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
function iterate(): void {
|
|
26
26
|
let expectedValue: bigint;
|
|
27
27
|
let count: number;
|
|
28
28
|
|
|
@@ -49,24 +49,13 @@ describe("Sequence", () => {
|
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
expect(count).toBe(20);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
test("Reset", () => {
|
|
55
|
-
let expectedValue: bigint;
|
|
56
|
-
let count: number;
|
|
57
|
-
|
|
58
|
-
expectedValue = 10n;
|
|
59
|
-
count = 0;
|
|
60
|
-
|
|
61
|
-
sequencer1.reset();
|
|
52
|
+
}
|
|
62
53
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
expectedValue++;
|
|
67
|
-
count++;
|
|
68
|
-
}
|
|
54
|
+
test("Iteration", () => {
|
|
55
|
+
iterate();
|
|
56
|
+
});
|
|
69
57
|
|
|
70
|
-
|
|
58
|
+
test("Repeat", () => {
|
|
59
|
+
iterate();
|
|
71
60
|
});
|
|
72
61
|
});
|