@aidc-toolkit/gs1 0.9.0 → 0.9.2
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/.idea/runConfigurations/eslint.xml +12 -0
- package/eslint.config.js +1 -3
- package/package.json +14 -14
- package/src/check.ts +9 -19
- package/src/idkey.ts +242 -235
- package/src/locale/i18next.d.ts +6 -0
- package/test/check.test.ts +7 -13
- package/test/idkey.test.ts +55 -44
- package/typedoc.json +2 -1
package/src/locale/i18next.d.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import type { localeStrings } from "./en/locale_strings.js";
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Internationalization module.
|
|
5
|
+
*/
|
|
3
6
|
declare module "i18next" {
|
|
7
|
+
/**
|
|
8
|
+
* Custom type options for this package.
|
|
9
|
+
*/
|
|
4
10
|
interface CustomTypeOptions {
|
|
5
11
|
resources: {
|
|
6
12
|
// Extract the type from the English locale strings object.
|
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,
|
|
@@ -40,48 +40,42 @@ describe("Check digit", () => {
|
|
|
40
40
|
});
|
|
41
41
|
|
|
42
42
|
describe("Price/weight check digit", () => {
|
|
43
|
-
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
44
43
|
function weight2Minus(characterIndex: number): number {
|
|
45
44
|
const product = characterIndex * 2;
|
|
46
45
|
|
|
47
46
|
return (product - Math.trunc(product / 10)) % 10;
|
|
48
47
|
}
|
|
49
48
|
|
|
50
|
-
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
51
49
|
function weight3(characterIndex: number): number {
|
|
52
50
|
return characterIndex * 3 % 10;
|
|
53
51
|
}
|
|
54
52
|
|
|
55
|
-
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
56
53
|
function weight5Plus(characterIndex: number): number {
|
|
57
54
|
const product = characterIndex * 5;
|
|
58
55
|
|
|
59
56
|
return (product + Math.trunc(product / 10)) % 10;
|
|
60
57
|
}
|
|
61
58
|
|
|
62
|
-
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
63
59
|
function weight5Minus(characterIndex: number): number {
|
|
64
60
|
const product = characterIndex * 5;
|
|
65
61
|
|
|
66
62
|
return (product - Math.trunc(product / 10)) % 10;
|
|
67
63
|
}
|
|
68
64
|
|
|
69
|
-
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
70
65
|
function testFourDigitPriceWeightCheckDigit(s: string): void {
|
|
71
|
-
|
|
66
|
+
// All character indexes are known to be defined.
|
|
67
|
+
const characterIndexes = NUMERIC_CREATOR.characterIndexes(s) as number[];
|
|
72
68
|
|
|
73
|
-
|
|
74
|
-
const sum = weight2Minus(characterIndexes[0]!) + weight2Minus(characterIndexes[1]!) + weight3(characterIndexes[2]!) + weight5Minus(characterIndexes[3]!);
|
|
69
|
+
const sum = weight2Minus(characterIndexes[0]) + weight2Minus(characterIndexes[1]) + weight3(characterIndexes[2]) + weight5Minus(characterIndexes[3]);
|
|
75
70
|
|
|
76
71
|
expect(fourDigitPriceWeightCheckDigit(s)).toBe(NUMERIC_CREATOR.character(sum * 3 % 10));
|
|
77
72
|
}
|
|
78
73
|
|
|
79
|
-
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
80
74
|
function testFiveDigitPriceWeightCheckDigit(s: string): void {
|
|
81
|
-
|
|
75
|
+
// All character indexes are known to be defined.
|
|
76
|
+
const characterIndexes = NUMERIC_CREATOR.characterIndexes(s) as number[];
|
|
82
77
|
|
|
83
|
-
|
|
84
|
-
const sum = weight5Plus(characterIndexes[0]!) + weight2Minus(characterIndexes[1]!) + weight5Minus(characterIndexes[2]!) + weight5Plus(characterIndexes[3]!) + weight2Minus(characterIndexes[4]!);
|
|
78
|
+
const sum = weight5Plus(characterIndexes[0]) + weight2Minus(characterIndexes[1]) + weight5Minus(characterIndexes[2]) + weight5Plus(characterIndexes[3]) + weight2Minus(characterIndexes[4]);
|
|
85
79
|
|
|
86
80
|
expect(weight5Minus(Number(fiveDigitPriceWeightCheckDigit(s)))).toBe(9 - (sum + 9) % 10);
|
|
87
81
|
}
|
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,
|
|
@@ -42,7 +42,6 @@ import {
|
|
|
42
42
|
|
|
43
43
|
await i18nInit(I18NEnvironment.CLI, true);
|
|
44
44
|
|
|
45
|
-
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
46
45
|
function creatorFor(characterSet: CharacterSet): CharacterSetCreator {
|
|
47
46
|
let creator: CharacterSetCreator;
|
|
48
47
|
|
|
@@ -63,14 +62,12 @@ function creatorFor(characterSet: CharacterSet): CharacterSetCreator {
|
|
|
63
62
|
return creator;
|
|
64
63
|
}
|
|
65
64
|
|
|
66
|
-
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
67
65
|
function validateIdentificationKeyValidator(creator: IdentificationKeyValidator, identificationKeyType: IdentificationKeyType, prefixType: PrefixType, length: number): void {
|
|
68
66
|
expect(creator.identificationKeyType).toBe(identificationKeyType);
|
|
69
67
|
expect(creator.prefixType).toBe(prefixType);
|
|
70
68
|
expect(creator.length).toBe(length);
|
|
71
69
|
}
|
|
72
70
|
|
|
73
|
-
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
74
71
|
function validateNumericIdentificationKeyValidator(validator: NumericIdentificationKeyValidator, isCreator: boolean, identificationKeyType: IdentificationKeyType, prefixType: PrefixType, length: number, leaderType: LeaderType): void {
|
|
75
72
|
validateIdentificationKeyValidator(validator, identificationKeyType, prefixType, length);
|
|
76
73
|
|
|
@@ -83,7 +80,6 @@ function validateNumericIdentificationKeyValidator(validator: NumericIdentificat
|
|
|
83
80
|
}
|
|
84
81
|
}
|
|
85
82
|
|
|
86
|
-
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
87
83
|
function validateGTINValidator(validator: GTINValidator, isCreator: boolean, gtinType: GTINType): void {
|
|
88
84
|
let prefixType: PrefixType;
|
|
89
85
|
|
|
@@ -109,12 +105,10 @@ function validateGTINValidator(validator: GTINValidator, isCreator: boolean, gti
|
|
|
109
105
|
expect(validator.gtinType).toBe(gtinType);
|
|
110
106
|
}
|
|
111
107
|
|
|
112
|
-
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
113
108
|
function validateNonGTINNumericIdentificationKeyValidator<T extends NonGTINNumericIdentificationKeyValidator>(validator: T, isCreator: boolean, identificationKeyType: IdentificationKeyType, length: number, leaderType: LeaderType): void {
|
|
114
109
|
validateNumericIdentificationKeyValidator(validator, isCreator, identificationKeyType, PrefixType.GS1CompanyPrefix, length, leaderType);
|
|
115
110
|
}
|
|
116
111
|
|
|
117
|
-
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
118
112
|
function validateSerializableNumericIdentificationKeyValidator(validator: SerializableNumericIdentificationKeyValidator, isCreator: boolean, identificationKeyType: IdentificationKeyType, length: number, leaderType: LeaderType, serialLength: number, serialCharacterSet: CharacterSet): void {
|
|
119
113
|
validateNonGTINNumericIdentificationKeyValidator(validator, isCreator, identificationKeyType, length, leaderType);
|
|
120
114
|
|
|
@@ -129,7 +123,6 @@ function validateSerializableNumericIdentificationKeyValidator(validator: Serial
|
|
|
129
123
|
}
|
|
130
124
|
}
|
|
131
125
|
|
|
132
|
-
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
133
126
|
function validateNonNumericIdentificationKeyValidator(validator: NonNumericIdentificationKeyValidator, isCreator: boolean, identificationKeyType: IdentificationKeyType, length: number, referenceCharacterSet: CharacterSet, requiresCheckCharacterPair: boolean): void {
|
|
134
127
|
validateIdentificationKeyValidator(validator, identificationKeyType, PrefixType.GS1CompanyPrefix, length);
|
|
135
128
|
|
|
@@ -169,7 +162,6 @@ describe("Validators", () => {
|
|
|
169
162
|
});
|
|
170
163
|
});
|
|
171
164
|
|
|
172
|
-
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
173
165
|
function validateIdentificationKeyCreators(prefixManager: PrefixManager): void {
|
|
174
166
|
let gtinType: GTINType;
|
|
175
167
|
|
|
@@ -190,9 +182,23 @@ function validateIdentificationKeyCreators(prefixManager: PrefixManager): void {
|
|
|
190
182
|
break;
|
|
191
183
|
}
|
|
192
184
|
|
|
185
|
+
expect(prefixManager.gtinCreator).toBe(prefixManager.gtinCreator);
|
|
186
|
+
|
|
193
187
|
validateGTINValidator(prefixManager.gtinCreator, true, gtinType);
|
|
194
188
|
|
|
195
189
|
if (prefixManager.prefixType !== PrefixType.GS18Prefix) {
|
|
190
|
+
expect(prefixManager.glnCreator).toBe(prefixManager.glnCreator);
|
|
191
|
+
expect(prefixManager.ssccCreator).toBe(prefixManager.ssccCreator);
|
|
192
|
+
expect(prefixManager.graiCreator).toBe(prefixManager.graiCreator);
|
|
193
|
+
expect(prefixManager.giaiCreator).toBe(prefixManager.giaiCreator);
|
|
194
|
+
expect(prefixManager.gsrnCreator).toBe(prefixManager.gsrnCreator);
|
|
195
|
+
expect(prefixManager.gdtiCreator).toBe(prefixManager.gdtiCreator);
|
|
196
|
+
expect(prefixManager.gincCreator).toBe(prefixManager.gincCreator);
|
|
197
|
+
expect(prefixManager.gsinCreator).toBe(prefixManager.gsinCreator);
|
|
198
|
+
expect(prefixManager.gcnCreator).toBe(prefixManager.gcnCreator);
|
|
199
|
+
expect(prefixManager.cpidCreator).toBe(prefixManager.cpidCreator);
|
|
200
|
+
expect(prefixManager.gmnCreator).toBe(prefixManager.gmnCreator);
|
|
201
|
+
|
|
196
202
|
validateNonGTINNumericIdentificationKeyValidator(prefixManager.glnCreator, true, IdentificationKeyType.GLN, 13, LeaderType.None);
|
|
197
203
|
validateNonGTINNumericIdentificationKeyValidator(prefixManager.ssccCreator, true, IdentificationKeyType.SSCC, 18, LeaderType.ExtensionDigit);
|
|
198
204
|
validateSerializableNumericIdentificationKeyValidator(prefixManager.graiCreator, true, IdentificationKeyType.GRAI, 13, LeaderType.None, 16, CharacterSet.AI82);
|
|
@@ -222,7 +228,6 @@ function validateIdentificationKeyCreators(prefixManager: PrefixManager): void {
|
|
|
222
228
|
describe("Prefix manager", () => {
|
|
223
229
|
let prefixManager: PrefixManager;
|
|
224
230
|
|
|
225
|
-
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
226
231
|
function validateGTINStartsWithPrefix(length: number): void {
|
|
227
232
|
expect(prefixManager.gtinCreator.length).toBe(length);
|
|
228
233
|
|
|
@@ -237,7 +242,6 @@ describe("Prefix manager", () => {
|
|
|
237
242
|
expect(gtin14.length).toBe(14);
|
|
238
243
|
}
|
|
239
244
|
|
|
240
|
-
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
241
245
|
function validateNonGTINStartsWithGS1CompanyPrefix(): void {
|
|
242
246
|
const gln = prefixManager.glnCreator.create(0);
|
|
243
247
|
|
|
@@ -373,8 +377,8 @@ describe("Sparse creation", () => {
|
|
|
373
377
|
|
|
374
378
|
prefixManager.tweakFactor = 0;
|
|
375
379
|
|
|
376
|
-
const sparseGTINs = Array.from(prefixManager.gtinCreator.
|
|
377
|
-
const straightGTINs = Array.from(prefixManager.gtinCreator.
|
|
380
|
+
const sparseGTINs = Array.from(prefixManager.gtinCreator.create(new Sequencer(0, 10), true));
|
|
381
|
+
const straightGTINs = Array.from(prefixManager.gtinCreator.create(new Sequencer(0, 10)));
|
|
378
382
|
|
|
379
383
|
test("Tweak factor 0", () => {
|
|
380
384
|
expect(sparseGTINs).toStrictEqual(straightGTINs);
|
|
@@ -383,14 +387,12 @@ describe("Sparse creation", () => {
|
|
|
383
387
|
prefixManager.resetTweakFactor();
|
|
384
388
|
});
|
|
385
389
|
|
|
386
|
-
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
387
390
|
function testIdentificationKeyCreatorCallback(callback?: () => void): void {
|
|
388
391
|
if (callback !== undefined) {
|
|
389
392
|
callback();
|
|
390
393
|
}
|
|
391
394
|
}
|
|
392
395
|
|
|
393
|
-
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
394
396
|
function testNumericIdentificationKeyCreator(creator: NumericIdentificationKeyCreator, preTestCallback?: () => void, postTestCallback?: () => void): void {
|
|
395
397
|
describe(creator.identificationKeyType === IdentificationKeyType.GTIN ? `${creator.identificationKeyType}-${creator.length}` : creator.identificationKeyType, () => {
|
|
396
398
|
testIdentificationKeyCreatorCallback(preTestCallback);
|
|
@@ -405,7 +407,6 @@ function testNumericIdentificationKeyCreator(creator: NumericIdentificationKeyCr
|
|
|
405
407
|
const referenceSubstringStart = prefixSubstringEnd;
|
|
406
408
|
const referenceSubstringEnd = referenceSubstringStart + referenceLength - prefixSubstringStart;
|
|
407
409
|
|
|
408
|
-
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
409
410
|
function validate(identificationKey: string, index: number, sparse: boolean): void {
|
|
410
411
|
expect(() => {
|
|
411
412
|
creator.validate(identificationKey);
|
|
@@ -421,11 +422,11 @@ function testNumericIdentificationKeyCreator(creator: NumericIdentificationKeyCr
|
|
|
421
422
|
expect(creator.referenceLength).toBe(referenceLength);
|
|
422
423
|
expect(creator.capacity).toBe(Number(CharacterSetCreator.powerOf10(referenceLength)));
|
|
423
424
|
|
|
424
|
-
const sequenceIterator = creator.
|
|
425
|
+
const sequenceIterator = Iterator.from(creator.create(new Sequencer(0, referenceCount)));
|
|
425
426
|
|
|
426
427
|
let allCount = 0;
|
|
427
428
|
|
|
428
|
-
|
|
429
|
+
Iterator.from(creator.createAll()).forEach((identificationKey, index) => {
|
|
429
430
|
validate(identificationKey, index, false);
|
|
430
431
|
|
|
431
432
|
expect(Number((hasExtensionDigit ? identificationKey.charAt(0) : "") + identificationKey.substring(referenceSubstringStart, referenceSubstringEnd))).toBe(index);
|
|
@@ -447,7 +448,7 @@ function testNumericIdentificationKeyCreator(creator: NumericIdentificationKeyCr
|
|
|
447
448
|
identificationKeys.push(creator.create(randomValue));
|
|
448
449
|
}
|
|
449
450
|
|
|
450
|
-
expect(Array.from(creator.
|
|
451
|
+
expect(Array.from(creator.create(randomValues))).toStrictEqual(identificationKeys);
|
|
451
452
|
});
|
|
452
453
|
|
|
453
454
|
test("Sparse", () => {
|
|
@@ -460,7 +461,7 @@ function testNumericIdentificationKeyCreator(creator: NumericIdentificationKeyCr
|
|
|
460
461
|
|
|
461
462
|
let sequenceCount = 0;
|
|
462
463
|
|
|
463
|
-
|
|
464
|
+
Iterator.from(creator.create(new Sequencer(0, sparseReferenceCount), true)).forEach((identificationKey, index) => {
|
|
464
465
|
validate(identificationKey, index, true);
|
|
465
466
|
|
|
466
467
|
sequential = sequential && Number((hasExtensionDigit ? identificationKey.charAt(0) : "") + identificationKey.substring(referenceSubstringStart, referenceSubstringEnd)) === index;
|
|
@@ -484,7 +485,7 @@ function testNumericIdentificationKeyCreator(creator: NumericIdentificationKeyCr
|
|
|
484
485
|
identificationKeys.push(creator.create(randomValue, true));
|
|
485
486
|
}
|
|
486
487
|
|
|
487
|
-
expect(Array.from(creator.
|
|
488
|
+
expect(Array.from(creator.create(randomValues, true))).toStrictEqual(identificationKeys);
|
|
488
489
|
});
|
|
489
490
|
|
|
490
491
|
test("Validation position", () => {
|
|
@@ -505,11 +506,18 @@ function testNumericIdentificationKeyCreator(creator: NumericIdentificationKeyCr
|
|
|
505
506
|
}).toThrow("Invalid character 'O' at position 3");
|
|
506
507
|
});
|
|
507
508
|
|
|
509
|
+
test("Position offset", () => {
|
|
510
|
+
expect(() => {
|
|
511
|
+
creator.validate(creator.create(0), {
|
|
512
|
+
positionOffset: 4
|
|
513
|
+
});
|
|
514
|
+
}).not.toThrow(RangeError);
|
|
515
|
+
});
|
|
516
|
+
|
|
508
517
|
testIdentificationKeyCreatorCallback(postTestCallback);
|
|
509
518
|
});
|
|
510
519
|
}
|
|
511
520
|
|
|
512
|
-
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
513
521
|
function testGTINCreator(creator: GTINCreator): void {
|
|
514
522
|
testNumericIdentificationKeyCreator(creator, () => {
|
|
515
523
|
test("Length", () => {
|
|
@@ -538,7 +546,6 @@ function testGTINCreator(creator: GTINCreator): void {
|
|
|
538
546
|
const referenceSubstringStart = prefixSubstringEnd;
|
|
539
547
|
const referenceSubstringEnd = referenceSubstringStart + referenceLength;
|
|
540
548
|
|
|
541
|
-
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
542
549
|
function validate(gtin: string, index: number, sparse: boolean): void {
|
|
543
550
|
expect(() => {
|
|
544
551
|
GTINCreator.validateGTIN14(gtin);
|
|
@@ -555,7 +562,7 @@ function testGTINCreator(creator: GTINCreator): void {
|
|
|
555
562
|
test("GTIN-14 straight", () => {
|
|
556
563
|
let sequenceCount = 0;
|
|
557
564
|
|
|
558
|
-
|
|
565
|
+
Iterator.from(creator.createGTIN14("5", new Sequencer(0, referenceCount))).forEach((gtin, index) => {
|
|
559
566
|
expect(Number(gtin.substring(referenceSubstringStart, referenceSubstringEnd))).toBe(index);
|
|
560
567
|
|
|
561
568
|
validate(gtin, index, false);
|
|
@@ -575,7 +582,7 @@ function testGTINCreator(creator: GTINCreator): void {
|
|
|
575
582
|
identificationKeys.push(creator.createGTIN14("5", randomValue));
|
|
576
583
|
}
|
|
577
584
|
|
|
578
|
-
expect(Array.from(creator.
|
|
585
|
+
expect(Array.from(creator.createGTIN14("5", randomValues))).toStrictEqual(identificationKeys);
|
|
579
586
|
});
|
|
580
587
|
|
|
581
588
|
test("GTIN-14 sparse", () => {
|
|
@@ -588,7 +595,7 @@ function testGTINCreator(creator: GTINCreator): void {
|
|
|
588
595
|
|
|
589
596
|
let sequenceCount = 0;
|
|
590
597
|
|
|
591
|
-
|
|
598
|
+
Iterator.from(creator.createGTIN14("5", new Sequencer(0, sparseReferenceCount), true)).forEach((gtin, index) => {
|
|
592
599
|
sequential = sequential && Number(gtin.substring(referenceSubstringStart, referenceSubstringEnd)) === index;
|
|
593
600
|
|
|
594
601
|
validate(gtin, index, true);
|
|
@@ -612,7 +619,7 @@ function testGTINCreator(creator: GTINCreator): void {
|
|
|
612
619
|
identificationKeys.push(creator.createGTIN14("5", randomValue, true));
|
|
613
620
|
}
|
|
614
621
|
|
|
615
|
-
expect(Array.from(creator.
|
|
622
|
+
expect(Array.from(creator.createGTIN14("5", randomValues, true))).toStrictEqual(identificationKeys);
|
|
616
623
|
});
|
|
617
624
|
|
|
618
625
|
if (creator.gtinType === GTINType.GTIN12) {
|
|
@@ -746,7 +753,6 @@ function testGTINCreator(creator: GTINCreator): void {
|
|
|
746
753
|
});
|
|
747
754
|
}
|
|
748
755
|
|
|
749
|
-
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
750
756
|
function testGTINValidationAndNormalization(): void {
|
|
751
757
|
describe("GTIN validation and normalization", () => {
|
|
752
758
|
test("Validation", () => {
|
|
@@ -861,12 +867,10 @@ function testGTINValidationAndNormalization(): void {
|
|
|
861
867
|
});
|
|
862
868
|
}
|
|
863
869
|
|
|
864
|
-
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
865
870
|
function testNonGTINNumericIdentificationKeyCreator(creator: NonGTINNumericIdentificationKeyCreator, preTestCallback?: () => void, postTestCallback?: () => void): void {
|
|
866
871
|
testNumericIdentificationKeyCreator(creator, preTestCallback, postTestCallback);
|
|
867
872
|
}
|
|
868
873
|
|
|
869
|
-
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
870
874
|
function testSerializableNumericIdentificationKeyCreator(creator: SerializableNumericIdentificationKeyCreator): void {
|
|
871
875
|
testNonGTINNumericIdentificationKeyCreator(creator, undefined, () => {
|
|
872
876
|
test("Serialization", () => {
|
|
@@ -878,8 +882,8 @@ function testSerializableNumericIdentificationKeyCreator(creator: SerializableNu
|
|
|
878
882
|
|
|
879
883
|
expect(creator.createSerialized(0, serial, true)).toBe(serializedIdentificationKey);
|
|
880
884
|
expect(creator.concatenate(identificationKey, serial)).toBe(serializedIdentificationKey);
|
|
881
|
-
expect(Array.from(creator.
|
|
882
|
-
expect(Array.from(creator.
|
|
885
|
+
expect(Array.from(creator.createSerialized(0, serials, true))).toStrictEqual(serializedIdentificationKeys);
|
|
886
|
+
expect(Array.from(creator.concatenate(identificationKey, serials))).toStrictEqual(serializedIdentificationKeys);
|
|
883
887
|
|
|
884
888
|
const fullLengthSerial = "0".repeat(creator.serialComponentLength);
|
|
885
889
|
const fullLengthPlusOneSerial = fullLengthSerial + "0";
|
|
@@ -887,12 +891,12 @@ function testSerializableNumericIdentificationKeyCreator(creator: SerializableNu
|
|
|
887
891
|
|
|
888
892
|
expect(() => creator.createSerialized(0, fullLengthSerial, true)).not.toThrow(RangeError);
|
|
889
893
|
expect(() => creator.concatenate(identificationKey, fullLengthSerial)).not.toThrow(RangeError);
|
|
890
|
-
expect(() => Array.from(creator.
|
|
891
|
-
expect(() => Array.from(creator.
|
|
894
|
+
expect(() => Array.from(creator.createSerialized(0, [...serials, fullLengthSerial], true))).not.toThrow(RangeError);
|
|
895
|
+
expect(() => Array.from(creator.concatenate(identificationKey, [...serials, fullLengthSerial]))).not.toThrow(RangeError);
|
|
892
896
|
expect(() => creator.createSerialized(0, fullLengthPlusOneSerial, true)).toThrow(fullLengthPlusOneSerialErrorMessage);
|
|
893
897
|
expect(() => creator.concatenate(identificationKey, fullLengthPlusOneSerial)).toThrow(fullLengthPlusOneSerialErrorMessage);
|
|
894
|
-
expect(() => Array.from(creator.
|
|
895
|
-
expect(() => Array.from(creator.
|
|
898
|
+
expect(() => Array.from(creator.createSerialized(0, [...serials, fullLengthPlusOneSerial], true))).toThrow(fullLengthPlusOneSerialErrorMessage);
|
|
899
|
+
expect(() => Array.from(creator.concatenate(identificationKey, [...serials, fullLengthPlusOneSerial]))).toThrow(fullLengthPlusOneSerialErrorMessage);
|
|
896
900
|
|
|
897
901
|
let invalidSerial: string;
|
|
898
902
|
|
|
@@ -914,15 +918,14 @@ function testSerializableNumericIdentificationKeyCreator(creator: SerializableNu
|
|
|
914
918
|
|
|
915
919
|
expect(() => creator.createSerialized(0, invalidSerial, true)).toThrow(invalidSerialErrorMessage);
|
|
916
920
|
expect(() => creator.concatenate(identificationKey, invalidSerial)).toThrow(invalidSerialErrorMessage);
|
|
917
|
-
expect(() => Array.from(creator.
|
|
918
|
-
expect(() => Array.from(creator.
|
|
921
|
+
expect(() => Array.from(creator.createSerialized(0, [...serials, invalidSerial], true))).toThrow(invalidSerialErrorMessage);
|
|
922
|
+
expect(() => Array.from(creator.concatenate(identificationKey, [...serials, invalidSerial]))).toThrow(invalidSerialErrorMessage);
|
|
919
923
|
});
|
|
920
924
|
});
|
|
921
925
|
}
|
|
922
926
|
|
|
923
927
|
const TEST_REFERENCE_LENGTH = 2;
|
|
924
928
|
|
|
925
|
-
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
926
929
|
function testNonNumericIdentificationKeyCreator<T extends NonNumericIdentificationKeyCreator>(creator: T): void {
|
|
927
930
|
describe(creator.identificationKeyType, () => {
|
|
928
931
|
const prefix = creator.prefix;
|
|
@@ -937,12 +940,12 @@ function testNonNumericIdentificationKeyCreator<T extends NonNumericIdentificati
|
|
|
937
940
|
|
|
938
941
|
let sequenceCount = 0;
|
|
939
942
|
|
|
940
|
-
|
|
943
|
+
Iterator.from(creator.create(creator.referenceCreator.create(TEST_REFERENCE_LENGTH, new Sequencer(0, referenceCount)))).forEach((identificationKey, index) => {
|
|
941
944
|
expect(() => {
|
|
942
945
|
creator.validate(identificationKey);
|
|
943
946
|
}).not.toThrow(RangeError);
|
|
944
947
|
|
|
945
|
-
expect(Number(creator.referenceCreator.
|
|
948
|
+
expect(Number(creator.referenceCreator.valueFor(identificationKey.substring(referenceSubstringStart, referenceSubstringEnd)))).toBe(index);
|
|
946
949
|
|
|
947
950
|
expect(identificationKey.length).toBeLessThanOrEqual(creator.length);
|
|
948
951
|
expect(identificationKey.substring(0, prefixLength)).toBe(prefix);
|
|
@@ -961,14 +964,14 @@ function testNonNumericIdentificationKeyCreator<T extends NonNumericIdentificati
|
|
|
961
964
|
|
|
962
965
|
let sequenceCount = 0;
|
|
963
966
|
|
|
964
|
-
|
|
967
|
+
Iterator.from(creator.create(creator.referenceCreator.create(TEST_REFERENCE_LENGTH, new Sequencer(0, referenceCount), Exclusion.None, 123456n))).forEach((identificationKey, index) => {
|
|
965
968
|
expect(() => {
|
|
966
969
|
creator.validate(identificationKey);
|
|
967
970
|
}).not.toThrow(RangeError);
|
|
968
971
|
|
|
969
|
-
expect(Number(creator.referenceCreator.
|
|
972
|
+
expect(Number(creator.referenceCreator.valueFor(identificationKey.substring(referenceSubstringStart, referenceSubstringEnd), Exclusion.None, 123456n))).toBe(index);
|
|
970
973
|
|
|
971
|
-
sequential = sequential && Number(creator.referenceCreator.
|
|
974
|
+
sequential = sequential && Number(creator.referenceCreator.valueFor(identificationKey.substring(referenceSubstringStart, referenceSubstringEnd))) === index;
|
|
972
975
|
|
|
973
976
|
expect(identificationKey.length).toBeLessThanOrEqual(creator.length);
|
|
974
977
|
expect(identificationKey.substring(0, prefixLength)).toBe(prefix);
|
|
@@ -983,6 +986,14 @@ function testNonNumericIdentificationKeyCreator<T extends NonNumericIdentificati
|
|
|
983
986
|
expect(sequenceCount).toBe(referenceCount);
|
|
984
987
|
});
|
|
985
988
|
|
|
989
|
+
test("Position offset", () => {
|
|
990
|
+
expect(() => {
|
|
991
|
+
creator.validate(creator.create("ABC123"), {
|
|
992
|
+
positionOffset: 4
|
|
993
|
+
});
|
|
994
|
+
}).not.toThrow(RangeError);
|
|
995
|
+
});
|
|
996
|
+
|
|
986
997
|
test("Not all numeric", () => {
|
|
987
998
|
expect(() => {
|
|
988
999
|
creator.validate(creator.create("01234"), {
|