@aidc-toolkit/utility 0.9.5 → 0.9.6-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.js CHANGED
@@ -660,28 +660,14 @@ var Transformer = class _Transformer {
660
660
  }));
661
661
  }
662
662
  }
663
- /**
664
- * Transform a value or values forward.
665
- *
666
- * @template T
667
- * Type returned by transformation callback.
668
- *
669
- * @param valueOrValues
670
- * Value(s).
671
- *
672
- * @param transformationCallback
673
- * Called after value(s) is/are transformed to convert it/them to its/their final value(s).
674
- *
675
- * @returns
676
- * Value(s) transformed into object(s).
677
- */
678
- forward(valueOrValues, transformationCallback) {
663
+ // eslint-disable-next-line jsdoc/require-jsdoc -- Implementation of overloaded signatures.
664
+ forward(valueOrValues, transformerCallback) {
679
665
  let result;
680
666
  if (typeof valueOrValues !== "object") {
681
667
  const valueN = BigInt(valueOrValues);
682
668
  this.validate(valueN);
683
669
  const transformedValue = this.doForward(valueN);
684
- result = transformationCallback === void 0 ? transformedValue : transformationCallback(transformedValue, 0);
670
+ result = transformerCallback === void 0 ? transformedValue : transformerCallback(transformedValue, 0);
685
671
  } else if (valueOrValues instanceof Sequencer) {
686
672
  if (valueOrValues.minValue < 0n) {
687
673
  throw new RangeError(i18n_default.t("Transformer.minValueMustBeGreaterThanOrEqualToZero", {
@@ -696,16 +682,16 @@ var Transformer = class _Transformer {
696
682
  domain: this.domain
697
683
  }));
698
684
  }
699
- result = transformationCallback === void 0 ? IteratorProxy.from(valueOrValues).map((value) => this.doForward(value)) : IteratorProxy.from(valueOrValues).map((value, index) => transformationCallback(this.doForward(value), index));
685
+ result = transformerCallback === void 0 ? IteratorProxy.from(valueOrValues).map((value) => this.doForward(value)) : IteratorProxy.from(valueOrValues).map((value, index) => transformerCallback(this.doForward(value), index));
700
686
  } else {
701
- result = transformationCallback === void 0 ? IteratorProxy.from(valueOrValues).map((value) => {
687
+ result = transformerCallback === void 0 ? IteratorProxy.from(valueOrValues).map((value) => {
702
688
  const valueN = BigInt(value);
703
689
  this.validate(valueN);
704
690
  return this.doForward(valueN);
705
691
  }) : IteratorProxy.from(valueOrValues).map((value, index) => {
706
692
  const valueN = BigInt(value);
707
693
  this.validate(valueN);
708
- return transformationCallback(this.doForward(valueN), index);
694
+ return transformerCallback(this.doForward(valueN), index);
709
695
  });
710
696
  }
711
697
  return result;
@@ -1387,17 +1373,21 @@ var CharacterSetCreator = class _CharacterSetCreator extends CharacterSetValidat
1387
1373
  exclusionDomains[1 /* FirstZero */] = exclusionFirstZeroDomains;
1388
1374
  }
1389
1375
  if (exclusionSupport.includes(2 /* AllNumeric */)) {
1376
+ let validateNumberIndexes2 = function(numberIndexes2) {
1377
+ let expectedNumberIndex = numberIndexes2[0];
1378
+ for (const numberIndex of numberIndexes2) {
1379
+ if (numberIndex === void 0 || numberIndex !== expectedNumberIndex) {
1380
+ throw new RangeError(i18n_default.t("CharacterSetValidator.allNumericAllNumericCharacters", {
1381
+ ns: utilityNS
1382
+ }));
1383
+ }
1384
+ expectedNumberIndex = numberIndex + 1;
1385
+ }
1386
+ };
1387
+ var validateNumberIndexes = validateNumberIndexes2;
1390
1388
  const exclusionAllNumericDomains = new Array(_CharacterSetCreator.MAXIMUM_STRING_LENGTH + 1);
1391
1389
  const numberIndexes = this.characterIndexes("0123456789");
1392
- let expectedNumberIndex = numberIndexes[0];
1393
- for (const numberIndex of numberIndexes) {
1394
- if (numberIndex === void 0 || numberIndex !== expectedNumberIndex) {
1395
- throw new RangeError(i18n_default.t("CharacterSetValidator.allNumericAllNumericCharacters", {
1396
- ns: utilityNS
1397
- }));
1398
- }
1399
- expectedNumberIndex = numberIndex + 1;
1400
- }
1390
+ validateNumberIndexes2(numberIndexes);
1401
1391
  const zeroIndex = BigInt(numberIndexes[0]);
1402
1392
  const allZerosValues = new Array(_CharacterSetCreator.MAXIMUM_STRING_LENGTH + 1);
1403
1393
  let allZerosValue = 0n;
@@ -1484,8 +1474,30 @@ var CharacterSetCreator = class _CharacterSetCreator extends CharacterSetValidat
1484
1474
  }));
1485
1475
  }
1486
1476
  }
1487
- // eslint-disable-next-line jsdoc/require-jsdoc -- Implementation of overloaded signatures.
1488
- create(length, valueOrValues, exclusion = 0 /* None */, tweak, creationCallback) {
1477
+ /**
1478
+ * Create string(s) by mapping value(s) to the equivalent characters in the character set across the length of the
1479
+ * string.
1480
+ *
1481
+ * @param length
1482
+ * Required string length.
1483
+ *
1484
+ * @param valueOrValues
1485
+ * Numeric value(s) of the string(s).
1486
+ *
1487
+ * @param exclusion
1488
+ * String(s) to be excluded from the range of outputs. See {@link Exclusion} for possible values and their meaning.
1489
+ *
1490
+ * @param tweak
1491
+ * If provided, the numerical value of the string(s) is/are "tweaked" using an {@link EncryptionTransformer |
1492
+ * encryption transformer}.
1493
+ *
1494
+ * @param creatorCallback
1495
+ * If provided, called after each string is constructed to create the final value.
1496
+ *
1497
+ * @returns
1498
+ * String(s) created from the value(s).
1499
+ */
1500
+ create(length, valueOrValues, exclusion = 0 /* None */, tweak, creatorCallback) {
1489
1501
  this.validateLength(length);
1490
1502
  this.validateExclusion(exclusion);
1491
1503
  const allZerosValue = exclusion === 2 /* AllNumeric */ ? this._allZerosValues[length] : 0n;
@@ -1504,7 +1516,7 @@ var CharacterSetCreator = class _CharacterSetCreator extends CharacterSetValidat
1504
1516
  }
1505
1517
  s = this.character(exclusion === 1 /* FirstZero */ ? Number(convertValue % this._characterSetSizeMinusOneN) + 1 : Number(convertValue % this._characterSetSizeN)) + s;
1506
1518
  }
1507
- return creationCallback !== void 0 ? creationCallback(s, index) : s;
1519
+ return creatorCallback !== void 0 ? creatorCallback(s, index) : s;
1508
1520
  });
1509
1521
  }
1510
1522
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aidc-toolkit/utility",
3
- "version": "0.9.5",
3
+ "version": "0.9.6-beta",
4
4
  "description": "Foundational utilities for AIDC Toolkit",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -19,22 +19,21 @@
19
19
  "url": "https://www.linkedin.com/in/kdean"
20
20
  },
21
21
  "scripts": {
22
- "eslint": "eslint .",
22
+ "lint": "eslint .",
23
23
  "build": "tsup src/index.ts --clean --format cjs,esm --dts",
24
24
  "build-doc": "npm run build && tsc src/index.ts --outDir dist --target esnext --moduleResolution nodenext --module nodenext --emitDeclarationOnly --declaration --declarationMap",
25
25
  "test": "vitest run"
26
26
  },
27
27
  "devDependencies": {
28
- "@aidc-toolkit/dev": "^0.9.5",
29
- "eslint": "^9.15.0",
28
+ "@aidc-toolkit/dev": "^0.9.6-beta",
29
+ "eslint": "^9.16.0",
30
30
  "ts-node": "^10.9.2",
31
31
  "tsup": "^8.3.5",
32
- "typescript": "^5.6.3",
33
- "vitest": "^2.1.5"
32
+ "typescript": "^5.7.2",
33
+ "vitest": "^2.1.7"
34
34
  },
35
35
  "dependencies": {
36
- "@aidc-toolkit/core": "^0.9.5",
37
- "@rollup/rollup-linux-x64-gnu": "^4.27.3",
38
- "i18next": "^23.16.8"
36
+ "@aidc-toolkit/core": "^0.9.6-beta",
37
+ "@rollup/rollup-linux-x64-gnu": "^4.28.0"
39
38
  }
40
39
  }
@@ -2,6 +2,7 @@ import i18next, { utilityNS } from "./locale/i18n.js";
2
2
  import { RegExpValidator } from "./reg_exp.js";
3
3
  import type { StringValidation, StringValidator } from "./string.js";
4
4
  import { Transformer } from "./transformer.js";
5
+ import type { TransformerCallback, TransformerInput, TransformerOutput } from "./types.js";
5
6
 
6
7
  /**
7
8
  * Exclusion options for validating and creating strings based on character sets.
@@ -291,20 +292,6 @@ export class CharacterSetValidator implements StringValidator<CharacterSetValida
291
292
  }
292
293
  }
293
294
 
294
- /**
295
- * Creation callback, used to convert created string to its final value.
296
- *
297
- * @param s
298
- * Created string.
299
- *
300
- * @param index
301
- * Index in sequence creation (0 for single creation).
302
- *
303
- * @returns
304
- * Final value.
305
- */
306
- export type CreationCallback = (s: string, index: number) => string;
307
-
308
295
  /**
309
296
  * Character set creator. Maps numeric values to strings using the character set as digits.
310
297
  */
@@ -418,23 +405,33 @@ export class CharacterSetCreator extends CharacterSetValidator {
418
405
  if (exclusionSupport.includes(Exclusion.AllNumeric)) {
419
406
  const exclusionAllNumericDomains = new Array<bigint>(CharacterSetCreator.MAXIMUM_STRING_LENGTH + 1);
420
407
 
421
- const numberIndexes = this.characterIndexes("0123456789");
422
-
423
- let expectedNumberIndex = numberIndexes[0];
408
+ /**
409
+ * Validate that number indexes are defined and sequential.
410
+ *
411
+ * @param numberIndexes
412
+ * Number indexes.
413
+ */
414
+ function validateNumberIndexes(numberIndexes: ReadonlyArray<number | undefined>): asserts numberIndexes is number[] {
415
+ let expectedNumberIndex = numberIndexes[0];
416
+
417
+ // Make sure that all numeric characters are present and in sequence.
418
+ for (const numberIndex of numberIndexes) {
419
+ if (numberIndex === undefined || numberIndex !== expectedNumberIndex) {
420
+ throw new RangeError(i18next.t("CharacterSetValidator.allNumericAllNumericCharacters", {
421
+ ns: utilityNS
422
+ }));
423
+ }
424
424
 
425
- // Make sure that all numeric characters are present and in sequence.
426
- for (const numberIndex of numberIndexes) {
427
- if (numberIndex === undefined || numberIndex !== expectedNumberIndex) {
428
- throw new RangeError(i18next.t("CharacterSetValidator.allNumericAllNumericCharacters", {
429
- ns: utilityNS
430
- }));
425
+ expectedNumberIndex = numberIndex + 1;
431
426
  }
432
-
433
- expectedNumberIndex = numberIndex + 1;
434
427
  }
435
428
 
429
+ const numberIndexes = this.characterIndexes("0123456789");
430
+
431
+ validateNumberIndexes(numberIndexes);
432
+
436
433
  // Zero index is the all-zero value for a single-character string.
437
- const zeroIndex = BigInt((numberIndexes as number[])[0]);
434
+ const zeroIndex = BigInt(numberIndexes[0]);
438
435
 
439
436
  const allZerosValues = new Array<bigint>(CharacterSetCreator.MAXIMUM_STRING_LENGTH + 1);
440
437
  let allZerosValue = 0n;
@@ -548,79 +545,33 @@ export class CharacterSetCreator extends CharacterSetValidator {
548
545
  }
549
546
 
550
547
  /**
551
- * Create a string by mapping a value to the equivalent characters in the character set across the length of the
548
+ * Create string(s) by mapping value(s) to the equivalent characters in the character set across the length of the
552
549
  * string.
553
550
  *
554
551
  * @param length
555
552
  * Required string length.
556
553
  *
557
- * @param value
558
- * Numeric value of the string.
559
- *
560
- * @param exclusion
561
- * Strings to be excluded from the range of outputs. See {@link Exclusion} for possible values and their meaning.
562
- *
563
- * @param tweak
564
- * If provided, the numerical value of the string is "tweaked" using an {@link EncryptionTransformer | encryption
565
- * transformer}.
566
- *
567
- * @param creationCallback
568
- * If provided, called after the string is constructed to create the final value.
569
- *
570
- * @returns
571
- * String created from the value.
572
- */
573
- create(length: number, value: number | bigint, exclusion?: Exclusion, tweak?: number | bigint, creationCallback?: CreationCallback): string;
574
-
575
- /**
576
- * Create multiple strings by mapping each value to the equivalent characters in the character set across the length
577
- * of the string. Equivalent to calling this method for each individual value.
578
- *
579
- * @param length
580
- * Required string length.
581
- *
582
- * @param values
583
- * Numeric values of the strings.
584
- *
585
- * @param exclusion
586
- * Strings to be excluded from the range of outputs. See {@link Exclusion} for possible values and their meaning.
587
- *
588
- * @param tweak
589
- * If provided, the numerical value of the strings are "tweaked" using an {@link EncryptionTransformer | encryption
590
- * transformer}.
591
- *
592
- * @param creationCallback
593
- * If provided, called after each string is constructed to create the final value.
594
- *
595
- * @returns
596
- * Iterable iterator over strings created from the values.
597
- */
598
- create(length: number, values: Iterable<number | bigint>, exclusion?: Exclusion, tweak?: number | bigint, creationCallback?: CreationCallback): IterableIterator<string>;
599
-
600
- /**
601
- * Create a string or multiple strings. This signature exists to allow similar overloaded methods in other classes
602
- * to call this method correctly.
603
- *
604
- * @param length
605
- *
606
554
  * @param valueOrValues
555
+ * Numeric value(s) of the string(s).
607
556
  *
608
557
  * @param exclusion
558
+ * String(s) to be excluded from the range of outputs. See {@link Exclusion} for possible values and their meaning.
609
559
  *
610
560
  * @param tweak
561
+ * If provided, the numerical value of the string(s) is/are "tweaked" using an {@link EncryptionTransformer |
562
+ * encryption transformer}.
611
563
  *
612
- * @param creationCallback
564
+ * @param creatorCallback
565
+ * If provided, called after each string is constructed to create the final value.
613
566
  *
614
567
  * @returns
568
+ * String(s) created from the value(s).
615
569
  */
616
- create(length: number, valueOrValues: number | bigint | Iterable<number | bigint>, exclusion?: Exclusion, tweak?: number | bigint, creationCallback?: CreationCallback): string | IterableIterator<string>;
617
-
618
- // eslint-disable-next-line jsdoc/require-jsdoc -- Implementation of overloaded signatures.
619
- create(length: number, valueOrValues: number | bigint | Iterable<number | bigint>, exclusion: Exclusion = Exclusion.None, tweak?: number | bigint, creationCallback?: CreationCallback): string | IterableIterator<string> {
570
+ create<T extends TransformerInput<number | bigint>>(length: number, valueOrValues: T, exclusion: Exclusion = Exclusion.None, tweak?: number | bigint, creatorCallback?: TransformerCallback<string, string>): TransformerOutput<T, string> {
620
571
  this.validateLength(length);
621
572
  this.validateExclusion(exclusion);
622
573
 
623
- // Zero value obviates need for non-null assertion.
574
+ // Zero value in ternary else obviates need for non-null assertion.
624
575
  const allZerosValue = exclusion === Exclusion.AllNumeric ? this._allZerosValues[length] : 0n;
625
576
 
626
577
  const transformer = Transformer.get(this._exclusionDomains[exclusion][length], tweak);
@@ -651,7 +602,7 @@ export class CharacterSetCreator extends CharacterSetValidator {
651
602
  s = this.character(exclusion === Exclusion.FirstZero ? Number(convertValue % this._characterSetSizeMinusOneN) + 1 : Number(convertValue % this._characterSetSizeN)) + s;
652
603
  }
653
604
 
654
- return creationCallback !== undefined ? creationCallback(s, index) : s;
605
+ return creatorCallback !== undefined ? creatorCallback(s, index) : s;
655
606
  });
656
607
  }
657
608
 
package/src/index.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export type * from "./types.js";
1
2
  export * from "./iterator_proxy.js";
2
3
  export * from "./sequencer.js";
3
4
  export * from "./transformer.js";
@@ -150,10 +150,10 @@ abstract class IteratorProxyBase<TInitial, TFinal> implements IteratorObject<TFi
150
150
  for (const value of this) {
151
151
  // Need to check arguments length as U could include undefined.
152
152
  if (index === 0 && arguments.length === 1) {
153
- // Initial value is not supplied only when U is identical to TFinal.
153
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- Initial value is not supplied only when U is identical to TFinal.
154
154
  result = value as unknown as U;
155
155
  } else {
156
- // Iteration has occurred at least once so result is of the expected type.
156
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- Iteration has occurred at least once so result is of the expected type.
157
157
  result = callback(result as U, value, index);
158
158
  }
159
159
 
@@ -164,7 +164,7 @@ abstract class IteratorProxyBase<TInitial, TFinal> implements IteratorObject<TFi
164
164
  throw new Error("reduce() of empty iterator with no initial value");
165
165
  }
166
166
 
167
- // Iteration has occurred at least once so result is of the expected type.
167
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- Iteration has occurred at least once so result is of the expected type.
168
168
  return result as U;
169
169
  }
170
170
 
@@ -508,8 +508,8 @@ function iteratorProxy(): Pick<typeof Iterator, "from"> {
508
508
  /**
509
509
  * Iterator proxy. In environments where
510
510
  * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator#iterator_helpers |
511
- * iterator helpers} are supported, this references the {@link Iterator} variable directly. Otherwise, it references an
512
- * implementation of "from" that uses an internally-defined iterator proxy object.
511
+ * iterator helpers} are supported, this references the {@linkcode Iterator} variable directly. Otherwise, it references
512
+ * an implementation of "from" that uses an internally-defined iterator proxy object.
513
513
  *
514
514
  * Client applications should **not** rely on long-term availability of this variable as it will be removed once there
515
515
  * is widespread support for iterator helpers.
@@ -1,23 +1,7 @@
1
1
  import { IteratorProxy } from "./iterator_proxy.js";
2
2
  import i18next, { utilityNS } from "./locale/i18n.js";
3
3
  import { Sequencer } from "./sequencer.js";
4
-
5
- /**
6
- * Transformation callback, used to convert transformed value to its final value.
7
- *
8
- * @template T
9
- * Type returned by callback.
10
- *
11
- * @param transformedValue
12
- * Transformed value.
13
- *
14
- * @param index
15
- * Index in sequence transformation (0 for single transformation).
16
- *
17
- * @returns
18
- * Final value.
19
- */
20
- export type TransformationCallback<T> = (transformedValue: bigint, index: number) => T;
4
+ import type { TransformerCallback, TransformerInput, TransformerOutput } from "./types.js";
21
5
 
22
6
  /**
23
7
  * Transformer that transforms values in a numeric domain to values in a range equal to the domain or to another range
@@ -143,104 +127,45 @@ export abstract class Transformer {
143
127
  protected abstract doForward(value: bigint): bigint;
144
128
 
145
129
  /**
146
- * Transform a value forward.
147
- *
148
- * @param value
149
- * Value.
150
- *
151
- * @returns
152
- * Transformed value.
153
- */
154
- forward(value: number | bigint): bigint;
155
-
156
- /**
157
- * Transform a value forward.
158
- *
159
- * @template T
160
- * Type returned by transformation callback.
161
- *
162
- * @param value
163
- * Value.
164
- *
165
- * @param transformationCallback
166
- * Called after the value is transformed to convert it to its final value.
167
- *
168
- * @returns
169
- * Value transformed into object.
170
- */
171
- forward<T>(value: number | bigint, transformationCallback: TransformationCallback<T>): T;
172
-
173
- /**
174
- * Transform values forward.
175
- *
176
- * @param values
177
- * Values. If this is an instance of {@link Sequencer}, the minimum and maximum values are validated prior to
178
- * transformation. Otherwise, the individual values are validated at the time of transformation.
179
- *
180
- * @returns
181
- * Transformed values.
182
- */
183
- forward(values: Iterable<number | bigint>): IterableIterator<bigint>;
184
-
185
- /**
186
- * Transform values forward.
130
+ * Transform value(s) forward.
187
131
  *
188
132
  * @template T
189
- * Type returned by transformation callback.
190
- *
191
- * @param values
192
- * Values. If this is an instance of {@link Sequencer}, the minimum and maximum values are validated prior to
193
- * transformation. Otherwise, the individual values are validated at the time of transformation.
194
- *
195
- * @param transformationCallback
196
- * Called after each value is transformed to convert it to its final value.
197
- *
198
- * @returns
199
- * Values transformed into objects.
200
- */
201
- forward<T>(values: Iterable<number | bigint>, transformationCallback: TransformationCallback<T>): IterableIterator<T>;
202
-
203
- /**
204
- * Transform a value or values forward. This signature exists to allow similar overloaded methods in other classes
205
- * to call this method correctly.
133
+ * Value(s) input type.
206
134
  *
207
135
  * @param valueOrValues
136
+ * Value(s). If this is an instance of {@link Sequencer}, the minimum and maximum values are validated prior to
137
+ * transformation. Otherwise, the individual value(s) is/are validated at the time of transformation.
208
138
  *
209
139
  * @returns
140
+ * Transformed value(s).
210
141
  */
211
- forward(valueOrValues: number | bigint | Iterable<number | bigint>): bigint | IterableIterator<bigint>;
142
+ forward<T extends TransformerInput<number | bigint>>(valueOrValues: T): TransformerOutput<T, bigint>;
212
143
 
213
144
  /**
214
- * Transform a value or values forward. This signature exists to allow similar overloaded methods in other classes
215
- * to call this method correctly.
145
+ * Transform value(s) forward, optionally applying a transformation.
216
146
  *
217
147
  * @template T
148
+ * Value(s) input type.
218
149
  *
219
- * @param valueOrValues
220
- *
221
- * @param transformationCallback
222
- *
223
- * @returns
224
- */
225
- forward<T>(valueOrValues: number | bigint | Iterable<number | bigint>, transformationCallback: TransformationCallback<T>): T | IterableIterator<T>;
226
-
227
- /**
228
- * Transform a value or values forward.
229
- *
230
- * @template T
231
- * Type returned by transformation callback.
150
+ * @template U
151
+ * Transformation callback output type.
232
152
  *
233
153
  * @param valueOrValues
234
- * Value(s).
154
+ * Value(s). If this is an instance of {@link Sequencer}, the minimum and maximum values are validated prior to
155
+ * transformation. Otherwise, the individual value(s) is/are validated at the time of transformation.
235
156
  *
236
- * @param transformationCallback
237
- * Called after value(s) is/are transformed to convert it/them to its/their final value(s).
157
+ * @param transformerCallback
158
+ * Called after each value is transformed to convert it to its final value.
238
159
  *
239
160
  * @returns
240
- * Value(s) transformed into object(s).
161
+ * Transformed value(s).
241
162
  */
242
- forward<T>(valueOrValues: number | bigint | Iterable<number | bigint>, transformationCallback?: TransformationCallback<T>): bigint | T | IterableIterator<bigint> | IterableIterator<T> {
243
- let result: bigint | T | IterableIterator<bigint> | IterableIterator<T>;
163
+ forward<T extends TransformerInput<number | bigint>, U>(valueOrValues: T, transformerCallback: TransformerCallback<bigint, U>): TransformerOutput<T, U>;
164
+
165
+ // eslint-disable-next-line jsdoc/require-jsdoc -- Implementation of overloaded signatures.
166
+ forward<T extends TransformerInput<number | bigint>, U>(valueOrValues: T, transformerCallback?: TransformerCallback<bigint, U>): TransformerOutput<T, U> {
167
+ // TODO Refactor type when https://github.com/microsoft/TypeScript/pull/56941 released.
168
+ let result: bigint | U | IterableIterator<bigint> | IterableIterator<U>;
244
169
 
245
170
  if (typeof valueOrValues !== "object") {
246
171
  const valueN = BigInt(valueOrValues);
@@ -249,7 +174,7 @@ export abstract class Transformer {
249
174
 
250
175
  const transformedValue = this.doForward(valueN);
251
176
 
252
- result = transformationCallback === undefined ? transformedValue : transformationCallback(transformedValue, 0);
177
+ result = transformerCallback === undefined ? transformedValue : transformerCallback(transformedValue, 0);
253
178
  } else if (valueOrValues instanceof Sequencer) {
254
179
  if (valueOrValues.minValue < 0n) {
255
180
  throw new RangeError(i18next.t("Transformer.minValueMustBeGreaterThanOrEqualToZero", {
@@ -266,11 +191,11 @@ export abstract class Transformer {
266
191
  }));
267
192
  }
268
193
 
269
- result = transformationCallback === undefined ?
194
+ result = transformerCallback === undefined ?
270
195
  IteratorProxy.from(valueOrValues).map(value => this.doForward(value)) :
271
- IteratorProxy.from(valueOrValues).map((value, index) => transformationCallback(this.doForward(value), index));
196
+ IteratorProxy.from(valueOrValues).map((value, index) => transformerCallback(this.doForward(value), index));
272
197
  } else {
273
- result = transformationCallback === undefined ?
198
+ result = transformerCallback === undefined ?
274
199
  IteratorProxy.from(valueOrValues).map((value) => {
275
200
  const valueN = BigInt(value);
276
201
 
@@ -283,11 +208,12 @@ export abstract class Transformer {
283
208
 
284
209
  this.validate(valueN);
285
210
 
286
- return transformationCallback(this.doForward(valueN), index);
211
+ return transformerCallback(this.doForward(valueN), index);
287
212
  });
288
213
  }
289
214
 
290
- return result;
215
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- Type determination is handled above.
216
+ return result as TransformerOutput<T, U>;
291
217
  }
292
218
 
293
219
  /**
package/src/types.ts ADDED
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Transformer input, one of:
3
+ *
4
+ * - T (primitive type)
5
+ * - Iterable<T>
6
+ *
7
+ * @template T
8
+ * Primitive type.
9
+ */
10
+ export type TransformerInput<T extends string | number | bigint | boolean> =
11
+ T | Iterable<T>;
12
+
13
+ /**
14
+ * Transformer callback, used to convert transformed value to its final value.
15
+ *
16
+ * @template TInput
17
+ * Type of input to callback.
18
+ *
19
+ * @template TOutput
20
+ * Type of output to callback.
21
+ *
22
+ * @param input
23
+ * Input value.
24
+ *
25
+ * @param index
26
+ * Index in sequence (0 for single transformation).
27
+ *
28
+ * @returns
29
+ * Output value.
30
+ */
31
+ export type TransformerCallback<TInput, TOutput> = (input: TInput, index: number) => TOutput;
32
+
33
+ /**
34
+ * Transformer output, based on transformer input:
35
+ *
36
+ * - If type T is primitive type, result is type U.
37
+ * - If type T is Iterable type, result is type IterableIterator<U>.
38
+ *
39
+ * @template T
40
+ * Transformer input type.
41
+ *
42
+ * @template U
43
+ * Output base type.
44
+ */
45
+ export type TransformerOutput<T extends TransformerInput<string | number | bigint | boolean>, U> =
46
+ T extends (T extends TransformerInput<infer V> ? V : never) ? U : IterableIterator<U>;
@@ -6,7 +6,6 @@ import {
6
6
  CharacterSetCreator,
7
7
  Exclusion,
8
8
  HEXADECIMAL_CREATOR,
9
- IteratorProxy,
10
9
  NUMERIC_CREATOR,
11
10
  Sequencer
12
11
  } from "../src/index.js";
@@ -65,7 +64,7 @@ function testCharacterSetCreator(name: string, characterSetCreator: CharacterSet
65
64
  break;
66
65
  }
67
66
 
68
- const sequence = IteratorProxy.from(characterSetCreator.create(length, new Sequencer(0n, domain), exclusion));
67
+ const sequence = Iterator.from(characterSetCreator.create(length, new Sequencer(0n, domain), exclusion));
69
68
 
70
69
  let previousS = "";
71
70
 
@@ -86,7 +85,7 @@ function testCharacterSetCreator(name: string, characterSetCreator: CharacterSet
86
85
 
87
86
  expect(() => characterSetCreator.create(length, domain, exclusion)).toThrow(`Value ${domain} must be less than ${domain}`);
88
87
 
89
- const sparseSequence = IteratorProxy.from(characterSetCreator.create(length, new Sequencer(domain - 1, -domain), exclusion, 123456n));
88
+ const sparseSequence = Iterator.from(characterSetCreator.create(length, new Sequencer(domain - 1, -domain), exclusion, 123456n));
90
89
 
91
90
  let sequential = true;
92
91
  previousS = "~";
@@ -1,6 +1,6 @@
1
1
  import { I18NEnvironment, i18nInit } from "@aidc-toolkit/core";
2
2
  import { describe, expect, test } from "vitest";
3
- import { IteratorProxy, Sequencer } from "../src/index.js";
3
+ import { Sequencer } from "../src/index.js";
4
4
 
5
5
  await i18nInit(I18NEnvironment.CLI);
6
6
 
@@ -29,7 +29,7 @@ describe("Sequence", () => {
29
29
  expectedValue = 10n;
30
30
  count = 0;
31
31
 
32
- for (const value of IteratorProxy.from(sequencer1)) {
32
+ for (const value of Iterator.from(sequencer1)) {
33
33
  expect(value).toBe(expectedValue);
34
34
 
35
35
  expectedValue++;
@@ -41,7 +41,7 @@ describe("Sequence", () => {
41
41
  expectedValue = 29n;
42
42
  count = 0;
43
43
 
44
- for (const value of IteratorProxy.from(sequencer2)) {
44
+ for (const value of Iterator.from(sequencer2)) {
45
45
  expect(value).toBe(expectedValue);
46
46
 
47
47
  expectedValue--;
@@ -60,7 +60,7 @@ describe("Sequence", () => {
60
60
 
61
61
  sequencer1.reset();
62
62
 
63
- for (const value of IteratorProxy.from(sequencer1)) {
63
+ for (const value of Iterator.from(sequencer1)) {
64
64
  expect(value).toBe(expectedValue);
65
65
 
66
66
  expectedValue++;