@aidc-toolkit/gs1 0.9.0 → 0.9.1
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/.idea/runConfigurations/build_dev.xml +12 -0
- package/eslint.config.js +3 -0
- package/package.json +15 -14
- package/src/check.ts +0 -4
- package/src/idkey.ts +188 -180
- package/test/check.test.ts +1 -1
- package/test/idkey.test.ts +25 -25
- package/typedoc.json +2 -1
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
<component name="ProjectRunConfigurationManager">
|
|
2
|
+
<configuration default="false" name="build-dev" type="js.build_tools.npm" nameIsGenerated="true">
|
|
3
|
+
<package-json value="$PROJECT_DIR$/package.json" />
|
|
4
|
+
<command value="run" />
|
|
5
|
+
<scripts>
|
|
6
|
+
<script value="build-dev" />
|
|
7
|
+
</scripts>
|
|
8
|
+
<node-interpreter value="project" />
|
|
9
|
+
<envs />
|
|
10
|
+
<method v="2" />
|
|
11
|
+
</configuration>
|
|
12
|
+
</component>
|
package/eslint.config.js
CHANGED
|
@@ -6,6 +6,9 @@ import esLintConfigLove from "eslint-config-love";
|
|
|
6
6
|
import { esLintConfigAIDCToolkit } from "@aidc-toolkit/dev";
|
|
7
7
|
|
|
8
8
|
export default tseslint.config(
|
|
9
|
+
{
|
|
10
|
+
ignores: ["eslint.config.js", "dist"]
|
|
11
|
+
},
|
|
9
12
|
js.configs.recommended,
|
|
10
13
|
...tseslint.configs.strictTypeChecked,
|
|
11
14
|
stylistic.configs["recommended-flat"],
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aidc-toolkit/gs1",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.1",
|
|
4
4
|
"description": "GS1 AIDC Toolkit",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -20,25 +20,26 @@
|
|
|
20
20
|
},
|
|
21
21
|
"scripts": {
|
|
22
22
|
"eslint": "eslint .",
|
|
23
|
-
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
23
|
+
"build": "tsup src/index.ts --clean --format cjs,esm --dts",
|
|
24
|
+
"build-dev": "npm run build && tsc src/index.ts --outDir dist --target esnext --moduleResolution nodenext --module nodenext --emitDeclarationOnly --declaration --declarationMap",
|
|
24
25
|
"test": "vitest run"
|
|
25
26
|
},
|
|
26
27
|
"devDependencies": {
|
|
27
|
-
"@aidc-toolkit/dev": "^0.9.
|
|
28
|
-
"@eslint/js": "^9.
|
|
29
|
-
"@stylistic/eslint-plugin": "^2.
|
|
30
|
-
"eslint-config-love": "^
|
|
31
|
-
"eslint-plugin-jsdoc": "^50.
|
|
28
|
+
"@aidc-toolkit/dev": "^0.9.1",
|
|
29
|
+
"@eslint/js": "^9.11.1",
|
|
30
|
+
"@stylistic/eslint-plugin": "^2.8.0",
|
|
31
|
+
"eslint-config-love": "^71.0.0",
|
|
32
|
+
"eslint-plugin-jsdoc": "^50.3.0",
|
|
32
33
|
"ts-node": "^10.9.2",
|
|
33
|
-
"tsup": "^8.
|
|
34
|
-
"typescript": "^5.
|
|
35
|
-
"typescript-eslint": "^8.
|
|
36
|
-
"vitest": "^2.
|
|
34
|
+
"tsup": "^8.3.0",
|
|
35
|
+
"typescript": "^5.6.2",
|
|
36
|
+
"typescript-eslint": "^8.7.0",
|
|
37
|
+
"vitest": "^2.1.1"
|
|
37
38
|
},
|
|
38
39
|
"dependencies": {
|
|
39
|
-
"@aidc-toolkit/core": "^0.9.
|
|
40
|
-
"@aidc-toolkit/utility": "^0.9.
|
|
41
|
-
"i18next": "^23.
|
|
40
|
+
"@aidc-toolkit/core": "^0.9.1",
|
|
41
|
+
"@aidc-toolkit/utility": "^0.9.1",
|
|
42
|
+
"i18next": "^23.15.1",
|
|
42
43
|
"ts-mixer": "^6.0.4"
|
|
43
44
|
}
|
|
44
45
|
}
|
package/src/check.ts
CHANGED
|
@@ -108,8 +108,6 @@ export function hasValidCheckDigit(s: string): boolean {
|
|
|
108
108
|
*
|
|
109
109
|
* @returns
|
|
110
110
|
* Accumulated sum of the weight result for each digit at the digit's position.
|
|
111
|
-
*
|
|
112
|
-
* @throws RangeError
|
|
113
111
|
*/
|
|
114
112
|
function priceWeightSum(weightsResults: ReadonlyArray<readonly number[]>, s: string): number {
|
|
115
113
|
if (s.length !== weightsResults.length) {
|
|
@@ -184,8 +182,6 @@ const CHECK_CHARACTERS = [
|
|
|
184
182
|
*
|
|
185
183
|
* @returns
|
|
186
184
|
* Check character pair.
|
|
187
|
-
*
|
|
188
|
-
* @throws RangeError
|
|
189
185
|
*/
|
|
190
186
|
export function checkCharacterPair(s: string): string {
|
|
191
187
|
// Weights are applied from right to left.
|
package/src/idkey.ts
CHANGED
|
@@ -2,7 +2,8 @@ import {
|
|
|
2
2
|
CharacterSetCreator,
|
|
3
3
|
type CharacterSetValidation,
|
|
4
4
|
type CharacterSetValidator,
|
|
5
|
-
Exclusion,
|
|
5
|
+
Exclusion,
|
|
6
|
+
NUMERIC_CREATOR,
|
|
6
7
|
RegExpValidator,
|
|
7
8
|
type StringValidation,
|
|
8
9
|
type StringValidator
|
|
@@ -139,7 +140,7 @@ export interface IdentificationKeyValidation extends StringValidation {
|
|
|
139
140
|
* Identification key validator. Validates an identification key against its definition in section 3 of the {@link
|
|
140
141
|
* https://www.gs1.org/genspecs | GS1 General Specifications}.
|
|
141
142
|
*/
|
|
142
|
-
export interface IdentificationKeyValidator extends StringValidator {
|
|
143
|
+
export interface IdentificationKeyValidator<V extends IdentificationKeyValidation = IdentificationKeyValidation> extends StringValidator<V> {
|
|
143
144
|
/**
|
|
144
145
|
* Get the identification key type. Per the GS1 General Specifications, the identification key type determines
|
|
145
146
|
* the remaining properties.
|
|
@@ -177,13 +178,13 @@ export interface IdentificationKeyValidator extends StringValidator {
|
|
|
177
178
|
* @param validation
|
|
178
179
|
* Identification key validation parameters.
|
|
179
180
|
*/
|
|
180
|
-
validate: (identificationKey: string, validation?:
|
|
181
|
+
validate: (identificationKey: string, validation?: V) => void;
|
|
181
182
|
}
|
|
182
183
|
|
|
183
184
|
/**
|
|
184
185
|
* Abstract identification key validator. Implements common functionality for an identification key validator.
|
|
185
186
|
*/
|
|
186
|
-
abstract class AbstractIdentificationKeyValidator implements IdentificationKeyValidator {
|
|
187
|
+
abstract class AbstractIdentificationKeyValidator<V extends IdentificationKeyValidation = IdentificationKeyValidation> implements IdentificationKeyValidator<V> {
|
|
187
188
|
/**
|
|
188
189
|
* Identification key type.
|
|
189
190
|
*/
|
|
@@ -261,22 +262,37 @@ abstract class AbstractIdentificationKeyValidator implements IdentificationKeyVa
|
|
|
261
262
|
this._referenceValidator = AbstractIdentificationKeyValidator.validatorFor(referenceCharacterSet);
|
|
262
263
|
}
|
|
263
264
|
|
|
265
|
+
/**
|
|
266
|
+
* @inheritDoc
|
|
267
|
+
*/
|
|
264
268
|
get identificationKeyType(): IdentificationKeyType {
|
|
265
269
|
return this._identificationKeyType;
|
|
266
270
|
}
|
|
267
271
|
|
|
272
|
+
/**
|
|
273
|
+
* @inheritDoc
|
|
274
|
+
*/
|
|
268
275
|
get prefixType(): PrefixType {
|
|
269
276
|
return this._prefixType;
|
|
270
277
|
}
|
|
271
278
|
|
|
279
|
+
/**
|
|
280
|
+
* @inheritDoc
|
|
281
|
+
*/
|
|
272
282
|
get length(): number {
|
|
273
283
|
return this._length;
|
|
274
284
|
}
|
|
275
285
|
|
|
286
|
+
/**
|
|
287
|
+
* @inheritDoc
|
|
288
|
+
*/
|
|
276
289
|
get referenceCharacterSet(): CharacterSet {
|
|
277
290
|
return this._referenceCharacterSet;
|
|
278
291
|
}
|
|
279
292
|
|
|
293
|
+
/**
|
|
294
|
+
* @inheritDoc
|
|
295
|
+
*/
|
|
280
296
|
get referenceValidator(): CharacterSetValidator {
|
|
281
297
|
return this._referenceValidator;
|
|
282
298
|
}
|
|
@@ -313,7 +329,7 @@ abstract class AbstractIdentificationKeyValidator implements IdentificationKeyVa
|
|
|
313
329
|
PrefixManager.validatePrefix(this.prefixType, true, false, partialIdentificationKey, true, this.referenceCharacterSet === CharacterSet.Numeric, positionOffset);
|
|
314
330
|
}
|
|
315
331
|
|
|
316
|
-
abstract validate(identificationKey: string, validation?:
|
|
332
|
+
abstract validate(identificationKey: string, validation?: V): void;
|
|
317
333
|
}
|
|
318
334
|
|
|
319
335
|
/**
|
|
@@ -383,10 +399,16 @@ abstract class AbstractNumericIdentificationKeyValidator extends AbstractIdentif
|
|
|
383
399
|
this._prefixPosition = Number(this.leaderType === LeaderType.ExtensionDigit);
|
|
384
400
|
}
|
|
385
401
|
|
|
402
|
+
/**
|
|
403
|
+
* @inheritDoc
|
|
404
|
+
*/
|
|
386
405
|
get leaderType(): LeaderType {
|
|
387
406
|
return this._leaderType;
|
|
388
407
|
}
|
|
389
408
|
|
|
409
|
+
/**
|
|
410
|
+
* @inheritDoc
|
|
411
|
+
*/
|
|
390
412
|
validate(identificationKey: string, validation?: IdentificationKeyValidation): void {
|
|
391
413
|
// Validate the prefix, with care taken for its position within the identification key.
|
|
392
414
|
if (this._prefixPosition === 0) {
|
|
@@ -501,11 +523,17 @@ export class GTINValidator extends AbstractNumericIdentificationKeyValidator {
|
|
|
501
523
|
super(IdentificationKeyType.GTIN, prefixType, gtinType, LeaderType.IndicatorDigit);
|
|
502
524
|
}
|
|
503
525
|
|
|
526
|
+
/**
|
|
527
|
+
* @inheritDoc
|
|
528
|
+
*/
|
|
504
529
|
get gtinType(): GTINType {
|
|
505
530
|
// Length maps to GTIN type enumeration.
|
|
506
|
-
return this.length
|
|
531
|
+
return this.length satisfies GTINType;
|
|
507
532
|
}
|
|
508
533
|
|
|
534
|
+
/**
|
|
535
|
+
* @inheritDoc
|
|
536
|
+
*/
|
|
509
537
|
protected override validatePrefix(partialIdentificationKey: string, positionOffset?: number): void {
|
|
510
538
|
// Delegate to prefix manager requiring exact match for prefix type.
|
|
511
539
|
PrefixManager.validatePrefix(this.prefixType, false, false, partialIdentificationKey, true, true, positionOffset);
|
|
@@ -519,8 +547,6 @@ export class GTINValidator extends AbstractNumericIdentificationKeyValidator {
|
|
|
519
547
|
*
|
|
520
548
|
* @returns
|
|
521
549
|
* GTIN-12.
|
|
522
|
-
*
|
|
523
|
-
* @throws RangeError
|
|
524
550
|
*/
|
|
525
551
|
static zeroExpand(zeroSuppressedGTIN12: string): string {
|
|
526
552
|
NUMERIC_CREATOR.validate(zeroSuppressedGTIN12, GTINValidator.ZERO_SUPPRESSED_GTIN12_VALIDATION);
|
|
@@ -563,8 +589,6 @@ export class GTINValidator extends AbstractNumericIdentificationKeyValidator {
|
|
|
563
589
|
*
|
|
564
590
|
* @param gtinLevel
|
|
565
591
|
* Level at which GTIN is to be validated.
|
|
566
|
-
*
|
|
567
|
-
* @throws RangeError
|
|
568
592
|
*/
|
|
569
593
|
static validateAny(gtin: string, gtinLevel: GTINLevel = GTINLevel.Any): void {
|
|
570
594
|
// Assume length-validated GTIN is the GTIN (true for all except zero-suppressed GTIN-12).
|
|
@@ -638,11 +662,9 @@ export class GTINValidator extends AbstractNumericIdentificationKeyValidator {
|
|
|
638
662
|
*
|
|
639
663
|
* @param gtin14
|
|
640
664
|
* GTIN-14.
|
|
641
|
-
*
|
|
642
|
-
* @throws RangeError
|
|
643
665
|
*/
|
|
644
666
|
static validateGTIN14(gtin14: string): void {
|
|
645
|
-
if (gtin14.length !== GTINType.GTIN14
|
|
667
|
+
if (gtin14.length as GTINType !== GTINType.GTIN14) {
|
|
646
668
|
throw new RangeError(i18next.t("IdentificationKey.invalidGTIN14Length", {
|
|
647
669
|
ns: gs1NS
|
|
648
670
|
}));
|
|
@@ -758,6 +780,9 @@ export class SerializableNumericIdentificationKeyValidator extends NonGTINNumeri
|
|
|
758
780
|
return this._serialComponentValidator;
|
|
759
781
|
}
|
|
760
782
|
|
|
783
|
+
/**
|
|
784
|
+
* @inheritDoc
|
|
785
|
+
*/
|
|
761
786
|
override validate(identificationKey: string, validation?: IdentificationKeyValidation): void {
|
|
762
787
|
super.validate(identificationKey.substring(0, this.length), validation);
|
|
763
788
|
|
|
@@ -771,17 +796,24 @@ export class SerializableNumericIdentificationKeyValidator extends NonGTINNumeri
|
|
|
771
796
|
* Non-numeric identification key validation parameters.
|
|
772
797
|
*/
|
|
773
798
|
export interface NonNumericIdentificationKeyValidation extends IdentificationKeyValidation {
|
|
799
|
+
/**
|
|
800
|
+
* Exclusion support for reference. Prevents non-numeric identification key from being mistaken for numeric
|
|
801
|
+
* identification key.
|
|
802
|
+
*/
|
|
774
803
|
exclusion?: Exclusion.None | Exclusion.AllNumeric | undefined;
|
|
775
804
|
}
|
|
776
805
|
|
|
777
806
|
/**
|
|
778
807
|
* Non-numeric identification key validator.
|
|
779
808
|
*/
|
|
780
|
-
export class NonNumericIdentificationKeyValidator extends AbstractIdentificationKeyValidator {
|
|
809
|
+
export class NonNumericIdentificationKeyValidator extends AbstractIdentificationKeyValidator<NonNumericIdentificationKeyValidation> {
|
|
781
810
|
/**
|
|
782
811
|
* Validator to ensure that an identification key (minus check character pair) is not all numeric.
|
|
783
812
|
*/
|
|
784
813
|
private static readonly NOT_ALL_NUMERIC_VALIDATOR = new class extends RegExpValidator {
|
|
814
|
+
/**
|
|
815
|
+
* @inheritDoc
|
|
816
|
+
*/
|
|
785
817
|
protected override createErrorMessage(_s: string): string {
|
|
786
818
|
return i18next.t("IdentificationKey.referenceCantBeAllNumeric", {
|
|
787
819
|
ns: gs1NS
|
|
@@ -822,6 +854,15 @@ export class NonNumericIdentificationKeyValidator extends AbstractIdentification
|
|
|
822
854
|
return this._requiresCheckCharacterPair;
|
|
823
855
|
}
|
|
824
856
|
|
|
857
|
+
/**
|
|
858
|
+
* Validate a non-numeric identification key and throw an exception if validation fails.
|
|
859
|
+
*
|
|
860
|
+
* @param identificationKey
|
|
861
|
+
* Identification key.
|
|
862
|
+
*
|
|
863
|
+
* @param validation
|
|
864
|
+
* Validation parameters.
|
|
865
|
+
*/
|
|
825
866
|
validate(identificationKey: string, validation?: NonNumericIdentificationKeyValidation): void {
|
|
826
867
|
const partialIdentificationKey = this.requiresCheckCharacterPair ? identificationKey.substring(0, identificationKey.length - 2) : identificationKey;
|
|
827
868
|
|
|
@@ -979,8 +1020,6 @@ abstract class AbstractIdentificationKeyCreator implements IdentificationKeyCrea
|
|
|
979
1020
|
*
|
|
980
1021
|
* @param checkAllowance
|
|
981
1022
|
* Number of characters to allow for check digit or check character pair.
|
|
982
|
-
*
|
|
983
|
-
* @throws Error
|
|
984
1023
|
*/
|
|
985
1024
|
protected init(prefixManager: PrefixManager, prefix: string, checkAllowance: number): void {
|
|
986
1025
|
// Verify that prefix manager has not already been assigned.
|
|
@@ -1005,19 +1044,32 @@ abstract class AbstractIdentificationKeyCreator implements IdentificationKeyCrea
|
|
|
1005
1044
|
|
|
1006
1045
|
abstract get referenceValidator(): CharacterSetValidator;
|
|
1007
1046
|
|
|
1047
|
+
/**
|
|
1048
|
+
* @inheritDoc
|
|
1049
|
+
*/
|
|
1008
1050
|
get referenceCreator(): CharacterSetCreator {
|
|
1009
1051
|
return this.referenceValidator as CharacterSetCreator;
|
|
1010
1052
|
}
|
|
1011
1053
|
|
|
1054
|
+
/**
|
|
1055
|
+
* @inheritDoc
|
|
1056
|
+
*/
|
|
1012
1057
|
get prefixManager(): PrefixManager {
|
|
1058
|
+
// Prefix manager is expected to have been initialized by this point.
|
|
1013
1059
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
1014
1060
|
return this._prefixManager!;
|
|
1015
1061
|
}
|
|
1016
1062
|
|
|
1063
|
+
/**
|
|
1064
|
+
* @inheritDoc
|
|
1065
|
+
*/
|
|
1017
1066
|
get prefix(): string {
|
|
1018
1067
|
return this.prefixManager.gs1CompanyPrefix;
|
|
1019
1068
|
}
|
|
1020
1069
|
|
|
1070
|
+
/**
|
|
1071
|
+
* @inheritDoc
|
|
1072
|
+
*/
|
|
1021
1073
|
get referenceLength(): number {
|
|
1022
1074
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
1023
1075
|
return this._referenceLength!;
|
|
@@ -1035,58 +1087,37 @@ export interface NumericIdentificationKeyCreator extends NumericIdentificationKe
|
|
|
1035
1087
|
*/
|
|
1036
1088
|
get capacity(): number;
|
|
1037
1089
|
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
* @returns
|
|
1070
|
-
* Iterable iterator over created identification keys.
|
|
1071
|
-
*/
|
|
1072
|
-
createSequence: (startValue: number, count: number, sparse?: boolean) => IterableIterator<string>;
|
|
1073
|
-
|
|
1074
|
-
/**
|
|
1075
|
-
* Create multiple identification keys with references based on numeric values. The values are converted to
|
|
1076
|
-
* references of the appropriate length using {@linkcode NUMERIC_CREATOR}.
|
|
1077
|
-
*
|
|
1078
|
-
* The implementation uses {@link CharacterSetCreator.createMultiple}, so the values are created only as needed.
|
|
1079
|
-
*
|
|
1080
|
-
* @param valuesSource
|
|
1081
|
-
* Source of values.
|
|
1082
|
-
*
|
|
1083
|
-
* @param sparse
|
|
1084
|
-
* If true, the values are mapped to a sparse sequence resistant to discovery. Default is false.
|
|
1085
|
-
*
|
|
1086
|
-
* @returns
|
|
1087
|
-
* Iterable iterator over created identification keys.
|
|
1088
|
-
*/
|
|
1089
|
-
createMultiple: (valuesSource: IterationSource<number>, sparse?: boolean) => IterableIterator<string>;
|
|
1090
|
+
create: {
|
|
1091
|
+
/**
|
|
1092
|
+
* Create an identification key with a reference based on a numeric value. The value is converted to a reference of
|
|
1093
|
+
* the appropriate length using {@linkcode NUMERIC_CREATOR}.
|
|
1094
|
+
*
|
|
1095
|
+
* @param value
|
|
1096
|
+
* Numeric value.
|
|
1097
|
+
*
|
|
1098
|
+
* @param sparse
|
|
1099
|
+
* If true, the value is mapped to a sparse sequence resistant to discovery. Default is false.
|
|
1100
|
+
*
|
|
1101
|
+
* @returns
|
|
1102
|
+
* Identification key.
|
|
1103
|
+
*/
|
|
1104
|
+
(value: number | bigint, sparse?: boolean): string;
|
|
1105
|
+
|
|
1106
|
+
/**
|
|
1107
|
+
* Create identification keys with references based on numeric values. The values are converted to references of
|
|
1108
|
+
* the appropriate length using {@linkcode NUMERIC_CREATOR}.
|
|
1109
|
+
*
|
|
1110
|
+
* @param values
|
|
1111
|
+
* Numeric values.
|
|
1112
|
+
*
|
|
1113
|
+
* @param sparse
|
|
1114
|
+
* If true, the values are mapped to a sparse sequence resistant to discovery. Default is false.
|
|
1115
|
+
*
|
|
1116
|
+
* @returns
|
|
1117
|
+
* Identification keys.
|
|
1118
|
+
*/
|
|
1119
|
+
(values: Iterable<number | bigint>, sparse?: boolean): IterableIterator<string>;
|
|
1120
|
+
};
|
|
1090
1121
|
|
|
1091
1122
|
/**
|
|
1092
1123
|
* Create all identification keys for the prefix from `0` to `capacity - 1`.
|
|
@@ -1122,8 +1153,6 @@ abstract class AbstractNumericIdentificationKeyCreator extends AbstractIdentific
|
|
|
1122
1153
|
*
|
|
1123
1154
|
* @param prefix
|
|
1124
1155
|
* Prefix within prefix manager to use to calculate reference length.
|
|
1125
|
-
*
|
|
1126
|
-
* @throws Error
|
|
1127
1156
|
*/
|
|
1128
1157
|
protected override init(prefixManager: PrefixManager, prefix: string): void {
|
|
1129
1158
|
super.init(prefixManager, prefix, 1);
|
|
@@ -1134,6 +1163,9 @@ abstract class AbstractNumericIdentificationKeyCreator extends AbstractIdentific
|
|
|
1134
1163
|
|
|
1135
1164
|
abstract get leaderType(): LeaderType;
|
|
1136
1165
|
|
|
1166
|
+
/**
|
|
1167
|
+
* @inheritDoc
|
|
1168
|
+
*/
|
|
1137
1169
|
get capacity(): number {
|
|
1138
1170
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
1139
1171
|
return this._capacity!;
|
|
@@ -1168,16 +1200,19 @@ abstract class AbstractNumericIdentificationKeyCreator extends AbstractIdentific
|
|
|
1168
1200
|
return partialIdentificationKey + checkDigit(partialIdentificationKey);
|
|
1169
1201
|
}
|
|
1170
1202
|
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1203
|
+
/**
|
|
1204
|
+
* @inheritDoc
|
|
1205
|
+
*/
|
|
1206
|
+
create(value: number | bigint, sparse?: boolean): string;
|
|
1174
1207
|
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1208
|
+
/**
|
|
1209
|
+
* @inheritDoc
|
|
1210
|
+
*/
|
|
1211
|
+
create(values: Iterable<number | bigint>, sparse?: boolean): IterableIterator<string>;
|
|
1178
1212
|
|
|
1179
|
-
|
|
1180
|
-
|
|
1213
|
+
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
1214
|
+
create(valueOrValues: number | bigint | Iterable<number | bigint>, sparse = false): string | IterableIterator<string> {
|
|
1215
|
+
return NUMERIC_CREATOR.create(this.referenceLength, valueOrValues, Exclusion.None, sparse ? this.tweak : undefined, reference => this.buildIdentificationKey(reference));
|
|
1181
1216
|
}
|
|
1182
1217
|
|
|
1183
1218
|
/**
|
|
@@ -1232,6 +1267,9 @@ abstract class AbstractNumericIdentificationKeyCreator extends AbstractIdentific
|
|
|
1232
1267
|
}
|
|
1233
1268
|
}
|
|
1234
1269
|
|
|
1270
|
+
/**
|
|
1271
|
+
* @inheritDoc
|
|
1272
|
+
*/
|
|
1235
1273
|
createAll(): IterableIterator<string> {
|
|
1236
1274
|
const hasExtensionDigit = this.leaderType === LeaderType.ExtensionDigit;
|
|
1237
1275
|
const prefix = this.prefix;
|
|
@@ -1285,26 +1323,11 @@ export class GTINCreator extends Mixin(GTINValidator, AbstractNumericIdentificat
|
|
|
1285
1323
|
this.init(prefixManager, prefixManager.prefix);
|
|
1286
1324
|
}
|
|
1287
1325
|
|
|
1288
|
-
override get prefix(): string {
|
|
1289
|
-
return this.prefixManager.prefix;
|
|
1290
|
-
}
|
|
1291
|
-
|
|
1292
1326
|
/**
|
|
1293
|
-
*
|
|
1294
|
-
*
|
|
1295
|
-
* @param indicatorDigit
|
|
1296
|
-
* Indicator digit.
|
|
1297
|
-
*
|
|
1298
|
-
* @param reference
|
|
1299
|
-
* Reference.
|
|
1300
|
-
*
|
|
1301
|
-
* @returns
|
|
1302
|
-
* GTIN-14.
|
|
1327
|
+
* @inheritDoc
|
|
1303
1328
|
*/
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
return partialIdentificationKey + checkDigit(partialIdentificationKey);
|
|
1329
|
+
override get prefix(): string {
|
|
1330
|
+
return this.prefixManager.prefix;
|
|
1308
1331
|
}
|
|
1309
1332
|
|
|
1310
1333
|
/**
|
|
@@ -1315,7 +1338,7 @@ export class GTINCreator extends Mixin(GTINValidator, AbstractNumericIdentificat
|
|
|
1315
1338
|
* Indicator digit.
|
|
1316
1339
|
*
|
|
1317
1340
|
* @param value
|
|
1318
|
-
* Numeric value.
|
|
1341
|
+
* Numeric value of the reference.
|
|
1319
1342
|
*
|
|
1320
1343
|
* @param sparse
|
|
1321
1344
|
* If true, the value is mapped to a sparse sequence resistant to discovery. Default is false.
|
|
@@ -1323,51 +1346,19 @@ export class GTINCreator extends Mixin(GTINValidator, AbstractNumericIdentificat
|
|
|
1323
1346
|
* @returns
|
|
1324
1347
|
* GTIN-14.
|
|
1325
1348
|
*/
|
|
1326
|
-
createGTIN14(indicatorDigit: string, value: number, sparse
|
|
1327
|
-
NUMERIC_CREATOR.validate(indicatorDigit, GTINCreator.REQUIRED_INDICATOR_DIGIT_VALIDATION);
|
|
1328
|
-
|
|
1329
|
-
return NUMERIC_CREATOR.create(GTINType.GTIN13 - this.prefixManager.gs1CompanyPrefix.length - 1, value, Exclusion.None, sparse ? this.tweak : undefined, reference => this.buildGTIN14(indicatorDigit, reference));
|
|
1330
|
-
}
|
|
1331
|
-
|
|
1332
|
-
/**
|
|
1333
|
-
* Create a sequence of GTIN-14s with an indicator digit and references based on a sequence of numeric values.
|
|
1334
|
-
* The values are converted to references of the appropriate length using {@linkcode NUMERIC_CREATOR}. References
|
|
1335
|
-
* are created with values from `startValue` to `startValue + count - 1`.
|
|
1336
|
-
*
|
|
1337
|
-
* The implementation uses {@link CharacterSetCreator.createSequence}, so the values are created only as needed.
|
|
1338
|
-
*
|
|
1339
|
-
* @param indicatorDigit
|
|
1340
|
-
* Indicator digit.
|
|
1341
|
-
*
|
|
1342
|
-
* @param startValue
|
|
1343
|
-
* Start numeric value.
|
|
1344
|
-
*
|
|
1345
|
-
* @param count
|
|
1346
|
-
* Count of identification keys to create.
|
|
1347
|
-
*
|
|
1348
|
-
* @param sparse
|
|
1349
|
-
* If true, the values are mapped to a sparse sequence resistant to discovery. Default is false.
|
|
1350
|
-
*
|
|
1351
|
-
* @returns
|
|
1352
|
-
* Iterable iterator over created GTIN-14s.
|
|
1353
|
-
*/
|
|
1354
|
-
createGTIN14Sequence(indicatorDigit: string, startValue: number, count: number, sparse = false): IterableIterator<string> {
|
|
1355
|
-
NUMERIC_CREATOR.validate(indicatorDigit, GTINCreator.REQUIRED_INDICATOR_DIGIT_VALIDATION);
|
|
1356
|
-
|
|
1357
|
-
return NUMERIC_CREATOR.createSequence(GTINType.GTIN13 - this.prefixManager.gs1CompanyPrefix.length - 1, startValue, count, Exclusion.None, sparse ? this.tweak : undefined, reference => this.buildGTIN14(indicatorDigit, reference));
|
|
1358
|
-
}
|
|
1349
|
+
createGTIN14(indicatorDigit: string, value: number | bigint, sparse?: boolean): string;
|
|
1359
1350
|
|
|
1360
1351
|
/**
|
|
1361
1352
|
* Create multiple GTIN-14s with an indicator digit and references based on numeric values. The values are converted
|
|
1362
1353
|
* to references of the appropriate length using {@linkcode NUMERIC_CREATOR}.
|
|
1363
1354
|
*
|
|
1364
|
-
* The implementation uses {@link CharacterSetCreator.
|
|
1355
|
+
* The implementation uses {@link CharacterSetCreator.create}, so the values are created only as needed.
|
|
1365
1356
|
*
|
|
1366
1357
|
* @param indicatorDigit
|
|
1367
1358
|
* Indicator digit.
|
|
1368
1359
|
*
|
|
1369
|
-
* @param
|
|
1370
|
-
*
|
|
1360
|
+
* @param values
|
|
1361
|
+
* Values.
|
|
1371
1362
|
*
|
|
1372
1363
|
* @param sparse
|
|
1373
1364
|
* If true, the values are mapped to a sparse sequence resistant to discovery. Default is false.
|
|
@@ -1375,10 +1366,17 @@ export class GTINCreator extends Mixin(GTINValidator, AbstractNumericIdentificat
|
|
|
1375
1366
|
* @returns
|
|
1376
1367
|
* Iterable iterator over created GTIN-14s.
|
|
1377
1368
|
*/
|
|
1378
|
-
|
|
1369
|
+
createGTIN14(indicatorDigit: string, values: Iterable<number | bigint>, sparse?: boolean): IterableIterator<string>;
|
|
1370
|
+
|
|
1371
|
+
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
1372
|
+
createGTIN14(indicatorDigit: string, valueOrValues: number | bigint | Iterable<number | bigint>, sparse = false): string | IterableIterator<string> {
|
|
1379
1373
|
NUMERIC_CREATOR.validate(indicatorDigit, GTINCreator.REQUIRED_INDICATOR_DIGIT_VALIDATION);
|
|
1380
1374
|
|
|
1381
|
-
return NUMERIC_CREATOR.
|
|
1375
|
+
return NUMERIC_CREATOR.create(GTINType.GTIN13 - this.prefixManager.gs1CompanyPrefix.length - 1, valueOrValues, Exclusion.None, sparse ? this.tweak : undefined, (reference) => {
|
|
1376
|
+
const partialIdentificationKey = indicatorDigit + this.prefixManager.gs1CompanyPrefix + reference;
|
|
1377
|
+
|
|
1378
|
+
return partialIdentificationKey + checkDigit(partialIdentificationKey);
|
|
1379
|
+
});
|
|
1382
1380
|
}
|
|
1383
1381
|
|
|
1384
1382
|
/**
|
|
@@ -1389,8 +1387,6 @@ export class GTINCreator extends Mixin(GTINValidator, AbstractNumericIdentificat
|
|
|
1389
1387
|
*
|
|
1390
1388
|
* @returns
|
|
1391
1389
|
* Zero-suppressed GTIN-12.
|
|
1392
|
-
*
|
|
1393
|
-
* @throws RangeError
|
|
1394
1390
|
*/
|
|
1395
1391
|
static zeroSuppress(gtin12: string): string {
|
|
1396
1392
|
GTIN12_VALIDATOR.validate(gtin12);
|
|
@@ -1466,8 +1462,6 @@ export class GTINCreator extends Mixin(GTINValidator, AbstractNumericIdentificat
|
|
|
1466
1462
|
*
|
|
1467
1463
|
* @returns
|
|
1468
1464
|
* Normalized GTIN.
|
|
1469
|
-
*
|
|
1470
|
-
* @throws RangeError
|
|
1471
1465
|
*/
|
|
1472
1466
|
static normalize(gtin: string): string {
|
|
1473
1467
|
const gtinLength = gtin.length;
|
|
@@ -1615,11 +1609,7 @@ export class SerializableNumericIdentificationKeyCreator extends Mixin(Serializa
|
|
|
1615
1609
|
* @returns
|
|
1616
1610
|
* Serialized identification key.
|
|
1617
1611
|
*/
|
|
1618
|
-
private concatenateValidated(baseIdentificationKey: string, serialComponent: string): string
|
|
1619
|
-
this.serialComponentCreator.validate(serialComponent, this.serialComponentValidation);
|
|
1620
|
-
|
|
1621
|
-
return baseIdentificationKey + serialComponent;
|
|
1622
|
-
}
|
|
1612
|
+
private concatenateValidated(baseIdentificationKey: string, serialComponent: string): string;
|
|
1623
1613
|
|
|
1624
1614
|
/**
|
|
1625
1615
|
* Concatenate a validated base identification key with multiple serial components.
|
|
@@ -1627,14 +1617,27 @@ export class SerializableNumericIdentificationKeyCreator extends Mixin(Serializa
|
|
|
1627
1617
|
* @param baseIdentificationKey
|
|
1628
1618
|
* Base identification key.
|
|
1629
1619
|
*
|
|
1630
|
-
* @param
|
|
1631
|
-
*
|
|
1620
|
+
* @param serialComponents
|
|
1621
|
+
* Serial components.
|
|
1632
1622
|
*
|
|
1633
1623
|
* @returns
|
|
1634
1624
|
* Serialized identification keys.
|
|
1635
1625
|
*/
|
|
1636
|
-
private
|
|
1637
|
-
|
|
1626
|
+
private concatenateValidated(baseIdentificationKey: string, serialComponents: Iterable<string>): IterableIterator<string>;
|
|
1627
|
+
|
|
1628
|
+
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
1629
|
+
private concatenateValidated(baseIdentificationKey: string, serialComponent: string | Iterable<string>): string | IterableIterator<string> {
|
|
1630
|
+
let result: string | IterableIterator<string>;
|
|
1631
|
+
|
|
1632
|
+
if (typeof serialComponent === "string") {
|
|
1633
|
+
this.serialComponentCreator.validate(serialComponent, this.serialComponentValidation);
|
|
1634
|
+
|
|
1635
|
+
result = baseIdentificationKey + serialComponent;
|
|
1636
|
+
} else {
|
|
1637
|
+
result = Iterator.from(serialComponent).map(serialComponent => this.concatenateValidated(baseIdentificationKey, serialComponent));
|
|
1638
|
+
}
|
|
1639
|
+
|
|
1640
|
+
return result;
|
|
1638
1641
|
}
|
|
1639
1642
|
|
|
1640
1643
|
/**
|
|
@@ -1642,7 +1645,7 @@ export class SerializableNumericIdentificationKeyCreator extends Mixin(Serializa
|
|
|
1642
1645
|
* value is converted to a reference of the appropriate length using {@linkcode NUMERIC_CREATOR}.
|
|
1643
1646
|
*
|
|
1644
1647
|
* @param value
|
|
1645
|
-
* Numeric value.
|
|
1648
|
+
* Numeric value of the references.
|
|
1646
1649
|
*
|
|
1647
1650
|
* @param serialComponent
|
|
1648
1651
|
* Serial component.
|
|
@@ -1653,9 +1656,7 @@ export class SerializableNumericIdentificationKeyCreator extends Mixin(Serializa
|
|
|
1653
1656
|
* @returns
|
|
1654
1657
|
* Serialized identification key.
|
|
1655
1658
|
*/
|
|
1656
|
-
createSerialized(value: number, serialComponent: string, sparse
|
|
1657
|
-
return this.concatenateValidated(this.create(value, sparse), serialComponent);
|
|
1658
|
-
}
|
|
1659
|
+
createSerialized(value: number, serialComponent: string, sparse?: boolean): string;
|
|
1659
1660
|
|
|
1660
1661
|
/**
|
|
1661
1662
|
* Create multiple serialized identification keys with a reference based on a numeric value and multiple serial
|
|
@@ -1664,8 +1665,8 @@ export class SerializableNumericIdentificationKeyCreator extends Mixin(Serializa
|
|
|
1664
1665
|
* @param value
|
|
1665
1666
|
* Numeric value.
|
|
1666
1667
|
*
|
|
1667
|
-
* @param
|
|
1668
|
-
*
|
|
1668
|
+
* @param serialComponents
|
|
1669
|
+
* Serial components.
|
|
1669
1670
|
*
|
|
1670
1671
|
* @param sparse
|
|
1671
1672
|
* If true, the value is mapped to a sparse sequence resistant to discovery. Default is false.
|
|
@@ -1673,8 +1674,11 @@ export class SerializableNumericIdentificationKeyCreator extends Mixin(Serializa
|
|
|
1673
1674
|
* @returns
|
|
1674
1675
|
* Serialized identification keys.
|
|
1675
1676
|
*/
|
|
1676
|
-
|
|
1677
|
-
|
|
1677
|
+
createSerialized(value: number, serialComponents: Iterable<string>, sparse?: boolean): IterableIterator<string>;
|
|
1678
|
+
|
|
1679
|
+
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
1680
|
+
createSerialized(value: number, serialComponent: string | Iterable<string>, sparse = false): string | IterableIterator<string> {
|
|
1681
|
+
return this.concatenateValidated(this.create(value, sparse), serialComponent);
|
|
1678
1682
|
}
|
|
1679
1683
|
|
|
1680
1684
|
/**
|
|
@@ -1689,11 +1693,7 @@ export class SerializableNumericIdentificationKeyCreator extends Mixin(Serializa
|
|
|
1689
1693
|
* @returns
|
|
1690
1694
|
* Serialized identification key.
|
|
1691
1695
|
*/
|
|
1692
|
-
concatenate(baseIdentificationKey: string, serialComponent: string): string
|
|
1693
|
-
this.validate(baseIdentificationKey);
|
|
1694
|
-
|
|
1695
|
-
return this.concatenateValidated(baseIdentificationKey, serialComponent);
|
|
1696
|
-
}
|
|
1696
|
+
concatenate(baseIdentificationKey: string, serialComponent: string): string;
|
|
1697
1697
|
|
|
1698
1698
|
/**
|
|
1699
1699
|
* Concatenate a base identification key with multiple serial components.
|
|
@@ -1701,16 +1701,19 @@ export class SerializableNumericIdentificationKeyCreator extends Mixin(Serializa
|
|
|
1701
1701
|
* @param baseIdentificationKey
|
|
1702
1702
|
* Base identification key.
|
|
1703
1703
|
*
|
|
1704
|
-
* @param
|
|
1705
|
-
*
|
|
1704
|
+
* @param serialComponents
|
|
1705
|
+
* Serial components.
|
|
1706
1706
|
*
|
|
1707
1707
|
* @returns
|
|
1708
1708
|
* Serialized identification keys.
|
|
1709
1709
|
*/
|
|
1710
|
-
|
|
1710
|
+
concatenate(baseIdentificationKey: string, serialComponents: Iterable<string>): IterableIterator<string>;
|
|
1711
|
+
|
|
1712
|
+
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
1713
|
+
concatenate(baseIdentificationKey: string, serialComponent: string | Iterable<string>): string | IterableIterator<string> {
|
|
1711
1714
|
this.validate(baseIdentificationKey);
|
|
1712
1715
|
|
|
1713
|
-
return this.
|
|
1716
|
+
return this.concatenateValidated(baseIdentificationKey, serialComponent);
|
|
1714
1717
|
}
|
|
1715
1718
|
}
|
|
1716
1719
|
|
|
@@ -1766,25 +1769,34 @@ export class NonNumericIdentificationKeyCreator extends Mixin(NonNumericIdentifi
|
|
|
1766
1769
|
* @returns
|
|
1767
1770
|
* Identification key.
|
|
1768
1771
|
*/
|
|
1769
|
-
create(reference: string): string
|
|
1770
|
-
this.referenceValidator.validate(reference, this._referenceValidation);
|
|
1771
|
-
|
|
1772
|
-
const partialIdentificationKey = this.prefix + reference;
|
|
1773
|
-
|
|
1774
|
-
return this.requiresCheckCharacterPair ? partialIdentificationKey + checkCharacterPair(partialIdentificationKey) : partialIdentificationKey;
|
|
1775
|
-
}
|
|
1772
|
+
create(reference: string): string;
|
|
1776
1773
|
|
|
1777
1774
|
/**
|
|
1778
1775
|
* Create multiple identification keys with references.
|
|
1779
1776
|
*
|
|
1780
|
-
* @param
|
|
1781
|
-
*
|
|
1777
|
+
* @param references
|
|
1778
|
+
* References.
|
|
1782
1779
|
*
|
|
1783
1780
|
* @returns
|
|
1784
1781
|
* Identification keys.
|
|
1785
1782
|
*/
|
|
1786
|
-
|
|
1787
|
-
|
|
1783
|
+
create(references: Iterable<string>): IterableIterator<string>;
|
|
1784
|
+
|
|
1785
|
+
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
1786
|
+
create(referenceOrReferences: string | Iterable<string>): string | IterableIterator<string> {
|
|
1787
|
+
let result: string | IterableIterator<string>;
|
|
1788
|
+
|
|
1789
|
+
if (typeof referenceOrReferences === "string") {
|
|
1790
|
+
this.referenceValidator.validate(referenceOrReferences, this._referenceValidation);
|
|
1791
|
+
|
|
1792
|
+
const partialIdentificationKey = this.prefix + referenceOrReferences;
|
|
1793
|
+
|
|
1794
|
+
result = this.requiresCheckCharacterPair ? partialIdentificationKey + checkCharacterPair(partialIdentificationKey) : partialIdentificationKey;
|
|
1795
|
+
} else {
|
|
1796
|
+
result = Iterator.from(referenceOrReferences).map(reference => this.create(reference));
|
|
1797
|
+
}
|
|
1798
|
+
|
|
1799
|
+
return result;
|
|
1788
1800
|
}
|
|
1789
1801
|
}
|
|
1790
1802
|
|
|
@@ -2124,8 +2136,6 @@ export class PrefixManager {
|
|
|
2124
2136
|
*
|
|
2125
2137
|
* @param positionOffset
|
|
2126
2138
|
* Position offset within a larger string.
|
|
2127
|
-
*
|
|
2128
|
-
* @throws RangeError
|
|
2129
2139
|
*/
|
|
2130
2140
|
static validatePrefix(prefixType: PrefixType, allowUPCCompanyPrefix: boolean, allowGS18Prefix: boolean, prefix: string, isFromIdentificationKey = false, isNumericIdentificationKey = false, positionOffset?: number): void {
|
|
2131
2141
|
let baseValidation: CharacterSetValidation;
|
|
@@ -2205,8 +2215,6 @@ export class PrefixManager {
|
|
|
2205
2215
|
*
|
|
2206
2216
|
* @returns
|
|
2207
2217
|
* Identification key creator.
|
|
2208
|
-
*
|
|
2209
|
-
* @throws RangeError
|
|
2210
2218
|
*/
|
|
2211
2219
|
private getIdentificationKeyCreator<T extends IdentificationKeyCreator>(identificationKeyType: IdentificationKeyType, constructorCallback: () => T): T {
|
|
2212
2220
|
let creator = this._identificationKeyCreatorsMap.get(identificationKeyType) as (T | undefined);
|
package/test/check.test.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import { I18NEnvironment, i18nInit } from "@aidc-toolkit/core";
|
|
1
2
|
import { NUMERIC_CREATOR } from "@aidc-toolkit/utility";
|
|
2
3
|
import { describe, expect, test } from "vitest";
|
|
3
|
-
import { I18NEnvironment, i18nInit } from "../../core/dist/index.js";
|
|
4
4
|
import {
|
|
5
5
|
checkCharacterPair,
|
|
6
6
|
checkDigit,
|
package/test/idkey.test.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { I18NEnvironment, i18nInit } from "@aidc-toolkit/core";
|
|
2
|
-
import { CharacterSetCreator, Exclusion,
|
|
2
|
+
import { CharacterSetCreator, Exclusion, NUMERIC_CREATOR, Sequencer } from "@aidc-toolkit/utility";
|
|
3
3
|
import { describe, expect, test } from "vitest";
|
|
4
4
|
import {
|
|
5
5
|
AI39_CREATOR,
|
|
@@ -373,8 +373,8 @@ describe("Sparse creation", () => {
|
|
|
373
373
|
|
|
374
374
|
prefixManager.tweakFactor = 0;
|
|
375
375
|
|
|
376
|
-
const sparseGTINs = Array.from(prefixManager.gtinCreator.
|
|
377
|
-
const straightGTINs = Array.from(prefixManager.gtinCreator.
|
|
376
|
+
const sparseGTINs = Array.from(prefixManager.gtinCreator.create(new Sequencer(0, 10), true));
|
|
377
|
+
const straightGTINs = Array.from(prefixManager.gtinCreator.create(new Sequencer(0, 10)));
|
|
378
378
|
|
|
379
379
|
test("Tweak factor 0", () => {
|
|
380
380
|
expect(sparseGTINs).toStrictEqual(straightGTINs);
|
|
@@ -421,11 +421,11 @@ function testNumericIdentificationKeyCreator(creator: NumericIdentificationKeyCr
|
|
|
421
421
|
expect(creator.referenceLength).toBe(referenceLength);
|
|
422
422
|
expect(creator.capacity).toBe(Number(CharacterSetCreator.powerOf10(referenceLength)));
|
|
423
423
|
|
|
424
|
-
const sequenceIterator = creator.
|
|
424
|
+
const sequenceIterator = Iterator.from(creator.create(new Sequencer(0, referenceCount)));
|
|
425
425
|
|
|
426
426
|
let allCount = 0;
|
|
427
427
|
|
|
428
|
-
|
|
428
|
+
Iterator.from(creator.createAll()).forEach((identificationKey, index) => {
|
|
429
429
|
validate(identificationKey, index, false);
|
|
430
430
|
|
|
431
431
|
expect(Number((hasExtensionDigit ? identificationKey.charAt(0) : "") + identificationKey.substring(referenceSubstringStart, referenceSubstringEnd))).toBe(index);
|
|
@@ -447,7 +447,7 @@ function testNumericIdentificationKeyCreator(creator: NumericIdentificationKeyCr
|
|
|
447
447
|
identificationKeys.push(creator.create(randomValue));
|
|
448
448
|
}
|
|
449
449
|
|
|
450
|
-
expect(Array.from(creator.
|
|
450
|
+
expect(Array.from(creator.create(randomValues))).toStrictEqual(identificationKeys);
|
|
451
451
|
});
|
|
452
452
|
|
|
453
453
|
test("Sparse", () => {
|
|
@@ -460,7 +460,7 @@ function testNumericIdentificationKeyCreator(creator: NumericIdentificationKeyCr
|
|
|
460
460
|
|
|
461
461
|
let sequenceCount = 0;
|
|
462
462
|
|
|
463
|
-
|
|
463
|
+
Iterator.from(creator.create(new Sequencer(0, sparseReferenceCount), true)).forEach((identificationKey, index) => {
|
|
464
464
|
validate(identificationKey, index, true);
|
|
465
465
|
|
|
466
466
|
sequential = sequential && Number((hasExtensionDigit ? identificationKey.charAt(0) : "") + identificationKey.substring(referenceSubstringStart, referenceSubstringEnd)) === index;
|
|
@@ -484,7 +484,7 @@ function testNumericIdentificationKeyCreator(creator: NumericIdentificationKeyCr
|
|
|
484
484
|
identificationKeys.push(creator.create(randomValue, true));
|
|
485
485
|
}
|
|
486
486
|
|
|
487
|
-
expect(Array.from(creator.
|
|
487
|
+
expect(Array.from(creator.create(randomValues, true))).toStrictEqual(identificationKeys);
|
|
488
488
|
});
|
|
489
489
|
|
|
490
490
|
test("Validation position", () => {
|
|
@@ -555,7 +555,7 @@ function testGTINCreator(creator: GTINCreator): void {
|
|
|
555
555
|
test("GTIN-14 straight", () => {
|
|
556
556
|
let sequenceCount = 0;
|
|
557
557
|
|
|
558
|
-
|
|
558
|
+
Iterator.from(creator.createGTIN14("5", new Sequencer(0, referenceCount))).forEach((gtin, index) => {
|
|
559
559
|
expect(Number(gtin.substring(referenceSubstringStart, referenceSubstringEnd))).toBe(index);
|
|
560
560
|
|
|
561
561
|
validate(gtin, index, false);
|
|
@@ -575,7 +575,7 @@ function testGTINCreator(creator: GTINCreator): void {
|
|
|
575
575
|
identificationKeys.push(creator.createGTIN14("5", randomValue));
|
|
576
576
|
}
|
|
577
577
|
|
|
578
|
-
expect(Array.from(creator.
|
|
578
|
+
expect(Array.from(creator.createGTIN14("5", randomValues))).toStrictEqual(identificationKeys);
|
|
579
579
|
});
|
|
580
580
|
|
|
581
581
|
test("GTIN-14 sparse", () => {
|
|
@@ -588,7 +588,7 @@ function testGTINCreator(creator: GTINCreator): void {
|
|
|
588
588
|
|
|
589
589
|
let sequenceCount = 0;
|
|
590
590
|
|
|
591
|
-
|
|
591
|
+
Iterator.from(creator.createGTIN14("5", new Sequencer(0, sparseReferenceCount), true)).forEach((gtin, index) => {
|
|
592
592
|
sequential = sequential && Number(gtin.substring(referenceSubstringStart, referenceSubstringEnd)) === index;
|
|
593
593
|
|
|
594
594
|
validate(gtin, index, true);
|
|
@@ -612,7 +612,7 @@ function testGTINCreator(creator: GTINCreator): void {
|
|
|
612
612
|
identificationKeys.push(creator.createGTIN14("5", randomValue, true));
|
|
613
613
|
}
|
|
614
614
|
|
|
615
|
-
expect(Array.from(creator.
|
|
615
|
+
expect(Array.from(creator.createGTIN14("5", randomValues, true))).toStrictEqual(identificationKeys);
|
|
616
616
|
});
|
|
617
617
|
|
|
618
618
|
if (creator.gtinType === GTINType.GTIN12) {
|
|
@@ -878,8 +878,8 @@ function testSerializableNumericIdentificationKeyCreator(creator: SerializableNu
|
|
|
878
878
|
|
|
879
879
|
expect(creator.createSerialized(0, serial, true)).toBe(serializedIdentificationKey);
|
|
880
880
|
expect(creator.concatenate(identificationKey, serial)).toBe(serializedIdentificationKey);
|
|
881
|
-
expect(Array.from(creator.
|
|
882
|
-
expect(Array.from(creator.
|
|
881
|
+
expect(Array.from(creator.createSerialized(0, serials, true))).toStrictEqual(serializedIdentificationKeys);
|
|
882
|
+
expect(Array.from(creator.concatenate(identificationKey, serials))).toStrictEqual(serializedIdentificationKeys);
|
|
883
883
|
|
|
884
884
|
const fullLengthSerial = "0".repeat(creator.serialComponentLength);
|
|
885
885
|
const fullLengthPlusOneSerial = fullLengthSerial + "0";
|
|
@@ -887,12 +887,12 @@ function testSerializableNumericIdentificationKeyCreator(creator: SerializableNu
|
|
|
887
887
|
|
|
888
888
|
expect(() => creator.createSerialized(0, fullLengthSerial, true)).not.toThrow(RangeError);
|
|
889
889
|
expect(() => creator.concatenate(identificationKey, fullLengthSerial)).not.toThrow(RangeError);
|
|
890
|
-
expect(() => Array.from(creator.
|
|
891
|
-
expect(() => Array.from(creator.
|
|
890
|
+
expect(() => Array.from(creator.createSerialized(0, [...serials, fullLengthSerial], true))).not.toThrow(RangeError);
|
|
891
|
+
expect(() => Array.from(creator.concatenate(identificationKey, [...serials, fullLengthSerial]))).not.toThrow(RangeError);
|
|
892
892
|
expect(() => creator.createSerialized(0, fullLengthPlusOneSerial, true)).toThrow(fullLengthPlusOneSerialErrorMessage);
|
|
893
893
|
expect(() => creator.concatenate(identificationKey, fullLengthPlusOneSerial)).toThrow(fullLengthPlusOneSerialErrorMessage);
|
|
894
|
-
expect(() => Array.from(creator.
|
|
895
|
-
expect(() => Array.from(creator.
|
|
894
|
+
expect(() => Array.from(creator.createSerialized(0, [...serials, fullLengthPlusOneSerial], true))).toThrow(fullLengthPlusOneSerialErrorMessage);
|
|
895
|
+
expect(() => Array.from(creator.concatenate(identificationKey, [...serials, fullLengthPlusOneSerial]))).toThrow(fullLengthPlusOneSerialErrorMessage);
|
|
896
896
|
|
|
897
897
|
let invalidSerial: string;
|
|
898
898
|
|
|
@@ -914,8 +914,8 @@ function testSerializableNumericIdentificationKeyCreator(creator: SerializableNu
|
|
|
914
914
|
|
|
915
915
|
expect(() => creator.createSerialized(0, invalidSerial, true)).toThrow(invalidSerialErrorMessage);
|
|
916
916
|
expect(() => creator.concatenate(identificationKey, invalidSerial)).toThrow(invalidSerialErrorMessage);
|
|
917
|
-
expect(() => Array.from(creator.
|
|
918
|
-
expect(() => Array.from(creator.
|
|
917
|
+
expect(() => Array.from(creator.createSerialized(0, [...serials, invalidSerial], true))).toThrow(invalidSerialErrorMessage);
|
|
918
|
+
expect(() => Array.from(creator.concatenate(identificationKey, [...serials, invalidSerial]))).toThrow(invalidSerialErrorMessage);
|
|
919
919
|
});
|
|
920
920
|
});
|
|
921
921
|
}
|
|
@@ -937,12 +937,12 @@ function testNonNumericIdentificationKeyCreator<T extends NonNumericIdentificati
|
|
|
937
937
|
|
|
938
938
|
let sequenceCount = 0;
|
|
939
939
|
|
|
940
|
-
|
|
940
|
+
Iterator.from(creator.create(creator.referenceCreator.create(TEST_REFERENCE_LENGTH, new Sequencer(0, referenceCount)))).forEach((identificationKey, index) => {
|
|
941
941
|
expect(() => {
|
|
942
942
|
creator.validate(identificationKey);
|
|
943
943
|
}).not.toThrow(RangeError);
|
|
944
944
|
|
|
945
|
-
expect(Number(creator.referenceCreator.
|
|
945
|
+
expect(Number(creator.referenceCreator.valueFor(identificationKey.substring(referenceSubstringStart, referenceSubstringEnd)))).toBe(index);
|
|
946
946
|
|
|
947
947
|
expect(identificationKey.length).toBeLessThanOrEqual(creator.length);
|
|
948
948
|
expect(identificationKey.substring(0, prefixLength)).toBe(prefix);
|
|
@@ -961,14 +961,14 @@ function testNonNumericIdentificationKeyCreator<T extends NonNumericIdentificati
|
|
|
961
961
|
|
|
962
962
|
let sequenceCount = 0;
|
|
963
963
|
|
|
964
|
-
|
|
964
|
+
Iterator.from(creator.create(creator.referenceCreator.create(TEST_REFERENCE_LENGTH, new Sequencer(0, referenceCount), Exclusion.None, 123456n))).forEach((identificationKey, index) => {
|
|
965
965
|
expect(() => {
|
|
966
966
|
creator.validate(identificationKey);
|
|
967
967
|
}).not.toThrow(RangeError);
|
|
968
968
|
|
|
969
|
-
expect(Number(creator.referenceCreator.
|
|
969
|
+
expect(Number(creator.referenceCreator.valueFor(identificationKey.substring(referenceSubstringStart, referenceSubstringEnd), Exclusion.None, 123456n))).toBe(index);
|
|
970
970
|
|
|
971
|
-
sequential = sequential && Number(creator.referenceCreator.
|
|
971
|
+
sequential = sequential && Number(creator.referenceCreator.valueFor(identificationKey.substring(referenceSubstringStart, referenceSubstringEnd))) === index;
|
|
972
972
|
|
|
973
973
|
expect(identificationKey.length).toBeLessThanOrEqual(creator.length);
|
|
974
974
|
expect(identificationKey.substring(0, prefixLength)).toBe(prefix);
|