@aidc-toolkit/utility 0.9.4 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aidc-toolkit/utility",
3
- "version": "0.9.4",
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,21 +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.4",
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.4",
37
- "i18next": "^23.16.5"
36
+ "@aidc-toolkit/core": "^0.9.6-beta",
37
+ "@rollup/rollup-linux-x64-gnu": "^4.28.0"
38
38
  }
39
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";