@aidc-toolkit/gs1 0.9.6-beta → 0.9.8-beta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +2398 -214
- package/dist/index.d.cts +659 -17
- package/dist/index.d.ts +659 -17
- package/dist/index.js +2370 -190
- package/package.json +7 -6
- package/src/check.ts +24 -9
- package/src/idkey.ts +72 -121
- package/src/index.ts +2 -1
- package/src/locale/en/{locale_strings.ts → locale-strings.ts} +2 -0
- package/src/locale/fr/{locale_strings.ts → locale-strings.ts} +2 -0
- package/src/locale/i18n.ts +39 -6
- package/src/locale/i18next.d.ts +5 -3
- package/test/check.test.ts +9 -8
- package/test/idkey.test.ts +30 -29
- /package/src/{character_set.ts → character-set.ts} +0 -0
package/src/idkey.ts
CHANGED
|
@@ -2,16 +2,16 @@ import {
|
|
|
2
2
|
CharacterSetCreator,
|
|
3
3
|
type CharacterSetValidation,
|
|
4
4
|
Exclusion,
|
|
5
|
-
IteratorProxy,
|
|
6
5
|
NUMERIC_CREATOR,
|
|
7
6
|
RegExpValidator,
|
|
8
7
|
type StringValidation,
|
|
9
8
|
type StringValidator,
|
|
10
9
|
type TransformerInput,
|
|
11
|
-
type TransformerOutput
|
|
10
|
+
type TransformerOutput,
|
|
11
|
+
transformIterable
|
|
12
12
|
} from "@aidc-toolkit/utility";
|
|
13
13
|
import { Mixin } from "ts-mixer";
|
|
14
|
-
import { AI39_CREATOR, AI82_CREATOR } from "./
|
|
14
|
+
import { AI39_CREATOR, AI82_CREATOR } from "./character-set.js";
|
|
15
15
|
import {
|
|
16
16
|
checkCharacterPair,
|
|
17
17
|
checkDigit,
|
|
@@ -19,7 +19,7 @@ import {
|
|
|
19
19
|
hasValidCheckCharacterPair,
|
|
20
20
|
hasValidCheckDigit
|
|
21
21
|
} from "./check.js";
|
|
22
|
-
import
|
|
22
|
+
import { i18nextGS1 } from "./locale/i18n.js";
|
|
23
23
|
|
|
24
24
|
/**
|
|
25
25
|
* Identification key type.
|
|
@@ -110,7 +110,7 @@ export enum PrefixType {
|
|
|
110
110
|
* Character set supported by the reference portion of an identification key or the serial component of a numeric
|
|
111
111
|
* identification key.
|
|
112
112
|
*/
|
|
113
|
-
export enum
|
|
113
|
+
export enum ContentCharacterSet {
|
|
114
114
|
/**
|
|
115
115
|
* Numeric.
|
|
116
116
|
*/
|
|
@@ -164,7 +164,7 @@ export interface IdentificationKeyValidator<V extends IdentificationKeyValidatio
|
|
|
164
164
|
/**
|
|
165
165
|
* Get the reference character set.
|
|
166
166
|
*/
|
|
167
|
-
get referenceCharacterSet():
|
|
167
|
+
get referenceCharacterSet(): ContentCharacterSet;
|
|
168
168
|
|
|
169
169
|
/**
|
|
170
170
|
* Get the reference validator.
|
|
@@ -209,7 +209,7 @@ abstract class AbstractIdentificationKeyValidator<V extends IdentificationKeyVal
|
|
|
209
209
|
/**
|
|
210
210
|
* Reference character set.
|
|
211
211
|
*/
|
|
212
|
-
private readonly _referenceCharacterSet:
|
|
212
|
+
private readonly _referenceCharacterSet: ContentCharacterSet;
|
|
213
213
|
|
|
214
214
|
/**
|
|
215
215
|
* Reference creator.
|
|
@@ -225,7 +225,7 @@ abstract class AbstractIdentificationKeyValidator<V extends IdentificationKeyVal
|
|
|
225
225
|
* @returns
|
|
226
226
|
* Character set creator.
|
|
227
227
|
*/
|
|
228
|
-
protected static creatorFor(characterSet:
|
|
228
|
+
protected static creatorFor(characterSet: ContentCharacterSet): CharacterSetCreator {
|
|
229
229
|
return AbstractIdentificationKeyValidator.CHARACTER_SET_CREATORS[characterSet];
|
|
230
230
|
}
|
|
231
231
|
|
|
@@ -244,7 +244,7 @@ abstract class AbstractIdentificationKeyValidator<V extends IdentificationKeyVal
|
|
|
244
244
|
* @param referenceCharacterSet
|
|
245
245
|
* Reference character set.
|
|
246
246
|
*/
|
|
247
|
-
protected constructor(identificationKeyType: IdentificationKeyType, prefixType: PrefixType, length: number, referenceCharacterSet:
|
|
247
|
+
protected constructor(identificationKeyType: IdentificationKeyType, prefixType: PrefixType, length: number, referenceCharacterSet: ContentCharacterSet) {
|
|
248
248
|
this._identificationKeyType = identificationKeyType;
|
|
249
249
|
this._prefixType = prefixType;
|
|
250
250
|
this._length = length;
|
|
@@ -276,7 +276,7 @@ abstract class AbstractIdentificationKeyValidator<V extends IdentificationKeyVal
|
|
|
276
276
|
/**
|
|
277
277
|
* @inheritDoc
|
|
278
278
|
*/
|
|
279
|
-
get referenceCharacterSet():
|
|
279
|
+
get referenceCharacterSet(): ContentCharacterSet {
|
|
280
280
|
return this._referenceCharacterSet;
|
|
281
281
|
}
|
|
282
282
|
|
|
@@ -316,7 +316,7 @@ abstract class AbstractIdentificationKeyValidator<V extends IdentificationKeyVal
|
|
|
316
316
|
*/
|
|
317
317
|
protected validatePrefix(partialIdentificationKey: string, positionOffset?: number): void {
|
|
318
318
|
// Delegate to prefix manager with support for U.P.C. Company Prefix but not GS1-8 Prefix.
|
|
319
|
-
PrefixManager.validatePrefix(this.prefixType, true, false, partialIdentificationKey, true, this.referenceCharacterSet ===
|
|
319
|
+
PrefixManager.validatePrefix(this.prefixType, true, false, partialIdentificationKey, true, this.referenceCharacterSet === ContentCharacterSet.Numeric, positionOffset);
|
|
320
320
|
}
|
|
321
321
|
|
|
322
322
|
abstract validate(identificationKey: string, validation?: V): void;
|
|
@@ -383,7 +383,7 @@ abstract class AbstractNumericIdentificationKeyValidator extends AbstractIdentif
|
|
|
383
383
|
* Leader type.
|
|
384
384
|
*/
|
|
385
385
|
protected constructor(identificationKeyType: IdentificationKeyType, prefixType: PrefixType, length: number, leaderType: LeaderType) {
|
|
386
|
-
super(identificationKeyType, prefixType, length,
|
|
386
|
+
super(identificationKeyType, prefixType, length, ContentCharacterSet.Numeric);
|
|
387
387
|
|
|
388
388
|
this._leaderType = leaderType;
|
|
389
389
|
this._prefixPosition = Number(this.leaderType === LeaderType.ExtensionDigit);
|
|
@@ -409,8 +409,7 @@ abstract class AbstractNumericIdentificationKeyValidator extends AbstractIdentif
|
|
|
409
409
|
|
|
410
410
|
// Validate the length.
|
|
411
411
|
if (identificationKey.length !== this.length) {
|
|
412
|
-
throw new RangeError(
|
|
413
|
-
ns: gs1NS,
|
|
412
|
+
throw new RangeError(i18nextGS1.t("IdentificationKey.identificationKeyTypeLength", {
|
|
414
413
|
identificationKeyType: this.identificationKeyType,
|
|
415
414
|
length: this.length
|
|
416
415
|
}));
|
|
@@ -418,9 +417,7 @@ abstract class AbstractNumericIdentificationKeyValidator extends AbstractIdentif
|
|
|
418
417
|
|
|
419
418
|
// Validating the check digit will also validate the characters.
|
|
420
419
|
if (!hasValidCheckDigit(this.padIdentificationKey(identificationKey, validation))) {
|
|
421
|
-
throw new RangeError(
|
|
422
|
-
ns: gs1NS
|
|
423
|
-
}));
|
|
420
|
+
throw new RangeError(i18nextGS1.t("IdentificationKey.invalidCheckDigit"));
|
|
424
421
|
}
|
|
425
422
|
}
|
|
426
423
|
}
|
|
@@ -560,9 +557,7 @@ export class GTINValidator extends AbstractNumericIdentificationKeyValidator {
|
|
|
560
557
|
}
|
|
561
558
|
|
|
562
559
|
if (gtin12 === undefined) {
|
|
563
|
-
throw new RangeError(
|
|
564
|
-
ns: gs1NS
|
|
565
|
-
}));
|
|
560
|
+
throw new RangeError(i18nextGS1.t("IdentificationKey.invalidZeroSuppressedGTIN12"));
|
|
566
561
|
}
|
|
567
562
|
|
|
568
563
|
// Make sure that resulting GTIN-12 is valid.
|
|
@@ -589,9 +584,7 @@ export class GTINValidator extends AbstractNumericIdentificationKeyValidator {
|
|
|
589
584
|
switch (gtin.length) {
|
|
590
585
|
case GTINType.GTIN13 as number:
|
|
591
586
|
if (gtin.startsWith("0")) {
|
|
592
|
-
throw new RangeError(
|
|
593
|
-
ns: gs1NS
|
|
594
|
-
}));
|
|
587
|
+
throw new RangeError(i18nextGS1.t("IdentificationKey.invalidGTIN13AtRetail"));
|
|
595
588
|
}
|
|
596
589
|
|
|
597
590
|
// Validate prefix requiring exact match for prefix type.
|
|
@@ -627,23 +620,17 @@ export class GTINValidator extends AbstractNumericIdentificationKeyValidator {
|
|
|
627
620
|
break;
|
|
628
621
|
|
|
629
622
|
default:
|
|
630
|
-
throw new RangeError(
|
|
631
|
-
ns: gs1NS
|
|
632
|
-
}));
|
|
623
|
+
throw new RangeError(i18nextGS1.t("IdentificationKey.invalidGTINLength"));
|
|
633
624
|
}
|
|
634
625
|
|
|
635
626
|
// Validating the check digit will also validate the characters.
|
|
636
627
|
if (!hasValidCheckDigit(lengthValidatedGTIN)) {
|
|
637
|
-
throw new RangeError(
|
|
638
|
-
ns: gs1NS
|
|
639
|
-
}));
|
|
628
|
+
throw new RangeError(i18nextGS1.t("IdentificationKey.invalidCheckDigit"));
|
|
640
629
|
}
|
|
641
630
|
|
|
642
631
|
// Validate against level if required.
|
|
643
632
|
if (gtinLevel !== GTINLevel.Any && gtinLevelRestriction !== GTINLevel.Any && gtinLevelRestriction !== gtinLevel) {
|
|
644
|
-
throw new RangeError(
|
|
645
|
-
ns: gs1NS
|
|
646
|
-
}));
|
|
633
|
+
throw new RangeError(i18nextGS1.t(gtinLevel === GTINLevel.RetailConsumer ? "IdentificationKey.invalidGTINAtRetail" : "IdentificationKey.invalidGTINAtOtherThanRetail"));
|
|
647
634
|
}
|
|
648
635
|
}
|
|
649
636
|
|
|
@@ -655,9 +642,7 @@ export class GTINValidator extends AbstractNumericIdentificationKeyValidator {
|
|
|
655
642
|
*/
|
|
656
643
|
static validateGTIN14(gtin14: string): void {
|
|
657
644
|
if (gtin14.length as GTINType !== GTINType.GTIN14) {
|
|
658
|
-
throw new RangeError(
|
|
659
|
-
ns: gs1NS
|
|
660
|
-
}));
|
|
645
|
+
throw new RangeError(i18nextGS1.t("IdentificationKey.invalidGTIN14Length"));
|
|
661
646
|
}
|
|
662
647
|
|
|
663
648
|
GTINCreator.validateAny(gtin14);
|
|
@@ -698,7 +683,7 @@ export class SerializableNumericIdentificationKeyValidator extends NonGTINNumeri
|
|
|
698
683
|
/**
|
|
699
684
|
* Serial component character set.
|
|
700
685
|
*/
|
|
701
|
-
private readonly _serialComponentCharacterSet:
|
|
686
|
+
private readonly _serialComponentCharacterSet: ContentCharacterSet;
|
|
702
687
|
|
|
703
688
|
/**
|
|
704
689
|
* Serial component validation parameters.
|
|
@@ -725,7 +710,7 @@ export class SerializableNumericIdentificationKeyValidator extends NonGTINNumeri
|
|
|
725
710
|
* @param serialComponentCharacterSet
|
|
726
711
|
* Serial component character set.
|
|
727
712
|
*/
|
|
728
|
-
constructor(identificationKeyType: IdentificationKeyType, length: number, serialComponentLength: number, serialComponentCharacterSet:
|
|
713
|
+
constructor(identificationKeyType: IdentificationKeyType, length: number, serialComponentLength: number, serialComponentCharacterSet: ContentCharacterSet) {
|
|
729
714
|
super(identificationKeyType, length, LeaderType.None);
|
|
730
715
|
|
|
731
716
|
this._serialComponentLength = serialComponentLength;
|
|
@@ -734,9 +719,7 @@ export class SerializableNumericIdentificationKeyValidator extends NonGTINNumeri
|
|
|
734
719
|
this._serialComponentValidation = {
|
|
735
720
|
minimumLength: 1,
|
|
736
721
|
maximumLength: serialComponentLength,
|
|
737
|
-
component: () =>
|
|
738
|
-
ns: gs1NS
|
|
739
|
-
})
|
|
722
|
+
component: () => i18nextGS1.t("IdentificationKey.serialComponent")
|
|
740
723
|
};
|
|
741
724
|
|
|
742
725
|
this._serialComponentCreator = SerializableNumericIdentificationKeyValidator.creatorFor(serialComponentCharacterSet);
|
|
@@ -752,7 +735,7 @@ export class SerializableNumericIdentificationKeyValidator extends NonGTINNumeri
|
|
|
752
735
|
/**
|
|
753
736
|
* Get the serial component character set.
|
|
754
737
|
*/
|
|
755
|
-
get serialComponentCharacterSet():
|
|
738
|
+
get serialComponentCharacterSet(): ContentCharacterSet {
|
|
756
739
|
return this._serialComponentCharacterSet;
|
|
757
740
|
}
|
|
758
741
|
|
|
@@ -805,9 +788,7 @@ export class NonNumericIdentificationKeyValidator extends AbstractIdentification
|
|
|
805
788
|
* @inheritDoc
|
|
806
789
|
*/
|
|
807
790
|
protected override createErrorMessage(_s: string): string {
|
|
808
|
-
return
|
|
809
|
-
ns: gs1NS
|
|
810
|
-
});
|
|
791
|
+
return i18nextGS1.t("IdentificationKey.referenceCantBeAllNumeric");
|
|
811
792
|
}
|
|
812
793
|
}(/\D/);
|
|
813
794
|
|
|
@@ -831,7 +812,7 @@ export class NonNumericIdentificationKeyValidator extends AbstractIdentification
|
|
|
831
812
|
* @param requiresCheckCharacterPair
|
|
832
813
|
* True if the identification key requires a check character pair.
|
|
833
814
|
*/
|
|
834
|
-
constructor(identificationKeyType: IdentificationKeyType, length: number, referenceCharacterSet:
|
|
815
|
+
constructor(identificationKeyType: IdentificationKeyType, length: number, referenceCharacterSet: ContentCharacterSet, requiresCheckCharacterPair = false) {
|
|
835
816
|
super(identificationKeyType, PrefixType.GS1CompanyPrefix, length, referenceCharacterSet);
|
|
836
817
|
|
|
837
818
|
this._requiresCheckCharacterPair = requiresCheckCharacterPair;
|
|
@@ -865,9 +846,7 @@ export class NonNumericIdentificationKeyValidator extends AbstractIdentification
|
|
|
865
846
|
});
|
|
866
847
|
// Validating the check character pair will also validate the characters.
|
|
867
848
|
} else if (!hasValidCheckCharacterPair(this.padIdentificationKey(identificationKey, validation))) {
|
|
868
|
-
throw new RangeError(
|
|
869
|
-
ns: gs1NS
|
|
870
|
-
}));
|
|
849
|
+
throw new RangeError(i18nextGS1.t("IdentificationKey.invalidCheckCharacterPair"));
|
|
871
850
|
}
|
|
872
851
|
|
|
873
852
|
// Check for all-numeric identification key (minus check character pair) if excluded.
|
|
@@ -912,12 +891,12 @@ export const SSCC_VALIDATOR = new NonGTINNumericIdentificationKeyValidator(Ident
|
|
|
912
891
|
/**
|
|
913
892
|
* GRAI validator.
|
|
914
893
|
*/
|
|
915
|
-
export const GRAI_VALIDATOR = new SerializableNumericIdentificationKeyValidator(IdentificationKeyType.GRAI, 13, 16,
|
|
894
|
+
export const GRAI_VALIDATOR = new SerializableNumericIdentificationKeyValidator(IdentificationKeyType.GRAI, 13, 16, ContentCharacterSet.AI82);
|
|
916
895
|
|
|
917
896
|
/**
|
|
918
897
|
* GIAI validator.
|
|
919
898
|
*/
|
|
920
|
-
export const GIAI_VALIDATOR = new NonNumericIdentificationKeyValidator(IdentificationKeyType.GIAI, 30,
|
|
899
|
+
export const GIAI_VALIDATOR = new NonNumericIdentificationKeyValidator(IdentificationKeyType.GIAI, 30, ContentCharacterSet.AI82);
|
|
921
900
|
|
|
922
901
|
/**
|
|
923
902
|
* GSRN validator.
|
|
@@ -927,12 +906,12 @@ export const GSRN_VALIDATOR = new NonGTINNumericIdentificationKeyValidator(Ident
|
|
|
927
906
|
/**
|
|
928
907
|
* GDTI validator.
|
|
929
908
|
*/
|
|
930
|
-
export const GDTI_VALIDATOR = new SerializableNumericIdentificationKeyValidator(IdentificationKeyType.GDTI, 13, 17,
|
|
909
|
+
export const GDTI_VALIDATOR = new SerializableNumericIdentificationKeyValidator(IdentificationKeyType.GDTI, 13, 17, ContentCharacterSet.AI82);
|
|
931
910
|
|
|
932
911
|
/**
|
|
933
912
|
* GINC validator.
|
|
934
913
|
*/
|
|
935
|
-
export const GINC_VALIDATOR = new NonNumericIdentificationKeyValidator(IdentificationKeyType.GINC, 30,
|
|
914
|
+
export const GINC_VALIDATOR = new NonNumericIdentificationKeyValidator(IdentificationKeyType.GINC, 30, ContentCharacterSet.AI82);
|
|
936
915
|
|
|
937
916
|
/**
|
|
938
917
|
* GSIN validator.
|
|
@@ -942,17 +921,17 @@ export const GSIN_VALIDATOR = new NonGTINNumericIdentificationKeyValidator(Ident
|
|
|
942
921
|
/**
|
|
943
922
|
* GCN validator.
|
|
944
923
|
*/
|
|
945
|
-
export const GCN_VALIDATOR = new SerializableNumericIdentificationKeyValidator(IdentificationKeyType.GCN, 13, 12,
|
|
924
|
+
export const GCN_VALIDATOR = new SerializableNumericIdentificationKeyValidator(IdentificationKeyType.GCN, 13, 12, ContentCharacterSet.Numeric);
|
|
946
925
|
|
|
947
926
|
/**
|
|
948
927
|
* CPID validator.
|
|
949
928
|
*/
|
|
950
|
-
export const CPID_VALIDATOR = new NonNumericIdentificationKeyValidator(IdentificationKeyType.CPID, 30,
|
|
929
|
+
export const CPID_VALIDATOR = new NonNumericIdentificationKeyValidator(IdentificationKeyType.CPID, 30, ContentCharacterSet.AI39);
|
|
951
930
|
|
|
952
931
|
/**
|
|
953
932
|
* GMN validator.
|
|
954
933
|
*/
|
|
955
|
-
export const GMN_VALIDATOR = new NonNumericIdentificationKeyValidator(IdentificationKeyType.GMN, 25,
|
|
934
|
+
export const GMN_VALIDATOR = new NonNumericIdentificationKeyValidator(IdentificationKeyType.GMN, 25, ContentCharacterSet.AI82, true);
|
|
956
935
|
|
|
957
936
|
/**
|
|
958
937
|
* Identification key creator. Creates an identification key based on its definition in section 3 of the {@link
|
|
@@ -1019,7 +998,7 @@ abstract class AbstractIdentificationKeyCreator implements IdentificationKeyCrea
|
|
|
1019
998
|
|
|
1020
999
|
abstract get length(): number;
|
|
1021
1000
|
|
|
1022
|
-
abstract get referenceCharacterSet():
|
|
1001
|
+
abstract get referenceCharacterSet(): ContentCharacterSet;
|
|
1023
1002
|
|
|
1024
1003
|
abstract get referenceCreator(): CharacterSetCreator;
|
|
1025
1004
|
|
|
@@ -1074,13 +1053,14 @@ export interface NumericIdentificationKeyCreator extends NumericIdentificationKe
|
|
|
1074
1053
|
/**
|
|
1075
1054
|
* Create all identification keys for the prefix from `0` to `capacity - 1`.
|
|
1076
1055
|
*
|
|
1077
|
-
* The implementation creates the strings as needed using an internal generator function
|
|
1078
|
-
*
|
|
1056
|
+
* The implementation creates the strings only as needed using an internal generator function. Although the result
|
|
1057
|
+
* is equivalent to calling `creator.create(new Sequencer(0, creator.capacity - 1))`, this method is significantly
|
|
1058
|
+
* faster.
|
|
1079
1059
|
*
|
|
1080
1060
|
* @returns
|
|
1081
|
-
*
|
|
1061
|
+
* All identification keys for the prefix.
|
|
1082
1062
|
*/
|
|
1083
|
-
createAll: () =>
|
|
1063
|
+
createAll: () => Iterable<string>;
|
|
1084
1064
|
}
|
|
1085
1065
|
|
|
1086
1066
|
/**
|
|
@@ -1213,15 +1193,21 @@ abstract class AbstractNumericIdentificationKeyCreator extends AbstractIdentific
|
|
|
1213
1193
|
/**
|
|
1214
1194
|
* @inheritDoc
|
|
1215
1195
|
*/
|
|
1216
|
-
createAll():
|
|
1196
|
+
createAll(): Iterable<string> {
|
|
1217
1197
|
const hasExtensionDigit = this.leaderType === LeaderType.ExtensionDigit;
|
|
1218
1198
|
const prefix = this.prefix;
|
|
1199
|
+
const length = this.length;
|
|
1219
1200
|
const referenceLength = this.referenceLength;
|
|
1220
1201
|
|
|
1221
1202
|
// Start weight is for reference excluding extension digit, which has its weight calculated separately.
|
|
1222
1203
|
const startWeight = 3 - 2 * ((referenceLength + 1 - Number(hasExtensionDigit)) % 2);
|
|
1223
1204
|
|
|
1224
|
-
|
|
1205
|
+
// Returning separate Iterable object makes iteration repeatable.
|
|
1206
|
+
return {
|
|
1207
|
+
[Symbol.iterator]() {
|
|
1208
|
+
return AbstractNumericIdentificationKeyCreator.createAllPartial(prefix, referenceLength, hasExtensionDigit ? 3 - 2 * length % 2 : 0, startWeight, checkDigitSum(startWeight === 3, prefix));
|
|
1209
|
+
}
|
|
1210
|
+
};
|
|
1225
1211
|
}
|
|
1226
1212
|
}
|
|
1227
1213
|
|
|
@@ -1235,9 +1221,7 @@ export class GTINCreator extends Mixin(GTINValidator, AbstractNumericIdentificat
|
|
|
1235
1221
|
private static readonly REQUIRED_INDICATOR_DIGIT_VALIDATION: CharacterSetValidation = {
|
|
1236
1222
|
minimumLength: 1,
|
|
1237
1223
|
maximumLength: 1,
|
|
1238
|
-
component: () =>
|
|
1239
|
-
ns: gs1NS
|
|
1240
|
-
})
|
|
1224
|
+
component: () => i18nextGS1.t("IdentificationKey.indicatorDigit")
|
|
1241
1225
|
};
|
|
1242
1226
|
|
|
1243
1227
|
/**
|
|
@@ -1246,9 +1230,7 @@ export class GTINCreator extends Mixin(GTINValidator, AbstractNumericIdentificat
|
|
|
1246
1230
|
private static readonly OPTIONAL_INDICATOR_DIGIT_VALIDATION: CharacterSetValidation = {
|
|
1247
1231
|
minimumLength: 0,
|
|
1248
1232
|
maximumLength: 1,
|
|
1249
|
-
component: () =>
|
|
1250
|
-
ns: gs1NS
|
|
1251
|
-
})
|
|
1233
|
+
component: () => i18nextGS1.t("IdentificationKey.indicatorDigit")
|
|
1252
1234
|
};
|
|
1253
1235
|
|
|
1254
1236
|
/**
|
|
@@ -1330,9 +1312,7 @@ export class GTINCreator extends Mixin(GTINValidator, AbstractNumericIdentificat
|
|
|
1330
1312
|
}
|
|
1331
1313
|
|
|
1332
1314
|
if (zeroSuppressedGTIN12 === undefined) {
|
|
1333
|
-
throw new RangeError(
|
|
1334
|
-
ns: gs1NS
|
|
1335
|
-
}));
|
|
1315
|
+
throw new RangeError(i18nextGS1.t("IdentificationKey.invalidZeroSuppressibleGTIN12"));
|
|
1336
1316
|
}
|
|
1337
1317
|
|
|
1338
1318
|
return zeroSuppressedGTIN12;
|
|
@@ -1400,9 +1380,7 @@ export class GTINCreator extends Mixin(GTINValidator, AbstractNumericIdentificat
|
|
|
1400
1380
|
// GTIN is GTIN-8.
|
|
1401
1381
|
normalizedGTIN = gtin.substring(5);
|
|
1402
1382
|
} else {
|
|
1403
|
-
throw new RangeError(
|
|
1404
|
-
ns: gs1NS
|
|
1405
|
-
}));
|
|
1383
|
+
throw new RangeError(i18nextGS1.t("IdentificationKey.invalidZeroSuppressedGTIN12AsGTIN13"));
|
|
1406
1384
|
}
|
|
1407
1385
|
break;
|
|
1408
1386
|
|
|
@@ -1435,16 +1413,12 @@ export class GTINCreator extends Mixin(GTINValidator, AbstractNumericIdentificat
|
|
|
1435
1413
|
// GTIN is GTIN-8.
|
|
1436
1414
|
normalizedGTIN = gtin.substring(6);
|
|
1437
1415
|
} else {
|
|
1438
|
-
throw new RangeError(
|
|
1439
|
-
ns: gs1NS
|
|
1440
|
-
}));
|
|
1416
|
+
throw new RangeError(i18nextGS1.t("IdentificationKey.invalidZeroSuppressedGTIN12AsGTIN14"));
|
|
1441
1417
|
}
|
|
1442
1418
|
break;
|
|
1443
1419
|
|
|
1444
1420
|
default:
|
|
1445
|
-
throw new RangeError(
|
|
1446
|
-
ns: gs1NS
|
|
1447
|
-
}));
|
|
1421
|
+
throw new RangeError(i18nextGS1.t("IdentificationKey.invalidGTINLength"));
|
|
1448
1422
|
}
|
|
1449
1423
|
|
|
1450
1424
|
// Validation applies to the normalized GTIN.
|
|
@@ -1504,7 +1478,7 @@ export class SerializableNumericIdentificationKeyCreator extends Mixin(Serializa
|
|
|
1504
1478
|
* @param serialComponentCharacterSet
|
|
1505
1479
|
* Serial component character set.
|
|
1506
1480
|
*/
|
|
1507
|
-
constructor(prefixManager: PrefixManager, identificationKeyType: IdentificationKeyType, length: number, serialComponentLength: number, serialComponentCharacterSet:
|
|
1481
|
+
constructor(prefixManager: PrefixManager, identificationKeyType: IdentificationKeyType, length: number, serialComponentLength: number, serialComponentCharacterSet: ContentCharacterSet) {
|
|
1508
1482
|
super(identificationKeyType, length, serialComponentLength, serialComponentCharacterSet);
|
|
1509
1483
|
|
|
1510
1484
|
this.init(prefixManager, prefixManager.gs1CompanyPrefix);
|
|
@@ -1524,7 +1498,7 @@ export class SerializableNumericIdentificationKeyCreator extends Mixin(Serializa
|
|
|
1524
1498
|
*/
|
|
1525
1499
|
private concatenateValidated<T extends TransformerInput<string>>(baseIdentificationKey: string, serialComponentOrComponents: T): TransformerOutput<T, string> {
|
|
1526
1500
|
// TODO Refactor type when https://github.com/microsoft/TypeScript/pull/56941 released.
|
|
1527
|
-
let result: string |
|
|
1501
|
+
let result: string | Iterable<string>;
|
|
1528
1502
|
|
|
1529
1503
|
const serialComponentCreator = this.serialComponentCreator;
|
|
1530
1504
|
const serialComponentValidation = this.serialComponentValidation;
|
|
@@ -1547,7 +1521,7 @@ export class SerializableNumericIdentificationKeyCreator extends Mixin(Serializa
|
|
|
1547
1521
|
if (typeof serialComponentOrComponents !== "object") {
|
|
1548
1522
|
result = validateAndConcatenate(serialComponentOrComponents);
|
|
1549
1523
|
} else {
|
|
1550
|
-
result =
|
|
1524
|
+
result = transformIterable(serialComponentOrComponents, validateAndConcatenate);
|
|
1551
1525
|
}
|
|
1552
1526
|
|
|
1553
1527
|
return result as TransformerOutput<T, string>;
|
|
@@ -1620,7 +1594,7 @@ export class NonNumericIdentificationKeyCreator extends Mixin(NonNumericIdentifi
|
|
|
1620
1594
|
* @param requiresCheckCharacterPair
|
|
1621
1595
|
* True if the identification key requires a check character pair.
|
|
1622
1596
|
*/
|
|
1623
|
-
constructor(prefixManager: PrefixManager, identificationKeyType: IdentificationKeyType, length: number, referenceCharacterSet:
|
|
1597
|
+
constructor(prefixManager: PrefixManager, identificationKeyType: IdentificationKeyType, length: number, referenceCharacterSet: ContentCharacterSet, requiresCheckCharacterPair = false) {
|
|
1624
1598
|
super(identificationKeyType, length, referenceCharacterSet, requiresCheckCharacterPair);
|
|
1625
1599
|
|
|
1626
1600
|
this.init(prefixManager, prefixManager.gs1CompanyPrefix, 2 * Number(requiresCheckCharacterPair));
|
|
@@ -1629,9 +1603,7 @@ export class NonNumericIdentificationKeyCreator extends Mixin(NonNumericIdentifi
|
|
|
1629
1603
|
minimumLength: 1,
|
|
1630
1604
|
// Maximum reference length has to account for prefix and check character pair.
|
|
1631
1605
|
maximumLength: this.referenceLength,
|
|
1632
|
-
component: () =>
|
|
1633
|
-
ns: gs1NS
|
|
1634
|
-
})
|
|
1606
|
+
component: () => i18nextGS1.t("IdentificationKey.reference")
|
|
1635
1607
|
};
|
|
1636
1608
|
}
|
|
1637
1609
|
|
|
@@ -1653,7 +1625,7 @@ export class NonNumericIdentificationKeyCreator extends Mixin(NonNumericIdentifi
|
|
|
1653
1625
|
*/
|
|
1654
1626
|
create<T extends TransformerInput<string>>(referenceOrReferences: T): TransformerOutput<T, string> {
|
|
1655
1627
|
// TODO Refactor type when https://github.com/microsoft/TypeScript/pull/56941 released.
|
|
1656
|
-
let result: string |
|
|
1628
|
+
let result: string | Iterable<string>;
|
|
1657
1629
|
|
|
1658
1630
|
const referenceCreator = this.referenceCreator;
|
|
1659
1631
|
const referenceValidation = this.referenceValidation;
|
|
@@ -1680,7 +1652,7 @@ export class NonNumericIdentificationKeyCreator extends Mixin(NonNumericIdentifi
|
|
|
1680
1652
|
if (typeof referenceOrReferences !== "object") {
|
|
1681
1653
|
result = validateAndCreate(referenceOrReferences);
|
|
1682
1654
|
} else {
|
|
1683
|
-
result =
|
|
1655
|
+
result = transformIterable(referenceOrReferences, validateAndCreate);
|
|
1684
1656
|
}
|
|
1685
1657
|
|
|
1686
1658
|
return result as TransformerOutput<T, string>;
|
|
@@ -1770,9 +1742,7 @@ export class PrefixManager {
|
|
|
1770
1742
|
private static readonly GS1_COMPANY_PREFIX_VALIDATION: PrefixValidation = {
|
|
1771
1743
|
minimumLength: PrefixManager.GS1_COMPANY_PREFIX_MINIMUM_LENGTH,
|
|
1772
1744
|
maximumLength: PrefixManager.GS1_COMPANY_PREFIX_MAXIMUM_LENGTH,
|
|
1773
|
-
component: () =>
|
|
1774
|
-
ns: gs1NS
|
|
1775
|
-
})
|
|
1745
|
+
component: () => i18nextGS1.t("Prefix.gs1CompanyPrefix")
|
|
1776
1746
|
};
|
|
1777
1747
|
|
|
1778
1748
|
/**
|
|
@@ -1781,9 +1751,7 @@ export class PrefixManager {
|
|
|
1781
1751
|
private static readonly UPC_COMPANY_PREFIX_AS_GS1_COMPANY_PREFIX_VALIDATION: PrefixValidation = {
|
|
1782
1752
|
minimumLength: PrefixManager.UPC_COMPANY_PREFIX_MINIMUM_LENGTH + 1,
|
|
1783
1753
|
maximumLength: PrefixManager.UPC_COMPANY_PREFIX_MAXIMUM_LENGTH + 1,
|
|
1784
|
-
component: () =>
|
|
1785
|
-
ns: gs1NS
|
|
1786
|
-
})
|
|
1754
|
+
component: () => i18nextGS1.t("Prefix.gs1CompanyPrefix")
|
|
1787
1755
|
};
|
|
1788
1756
|
|
|
1789
1757
|
/**
|
|
@@ -1792,9 +1760,7 @@ export class PrefixManager {
|
|
|
1792
1760
|
private static readonly GS1_8_PREFIX_AS_GS1_COMPANY_PREFIX_VALIDATION: PrefixValidation = {
|
|
1793
1761
|
minimumLength: PrefixManager.GS1_8_PREFIX_MINIMUM_LENGTH + 5,
|
|
1794
1762
|
maximumLength: PrefixManager.GS1_8_PREFIX_MAXIMUM_LENGTH + 5,
|
|
1795
|
-
component: () =>
|
|
1796
|
-
ns: gs1NS
|
|
1797
|
-
})
|
|
1763
|
+
component: () => i18nextGS1.t("Prefix.gs1CompanyPrefix")
|
|
1798
1764
|
};
|
|
1799
1765
|
|
|
1800
1766
|
/**
|
|
@@ -1803,9 +1769,7 @@ export class PrefixManager {
|
|
|
1803
1769
|
private static readonly UPC_COMPANY_PREFIX_VALIDATION: PrefixValidation = {
|
|
1804
1770
|
minimumLength: PrefixManager.UPC_COMPANY_PREFIX_MINIMUM_LENGTH,
|
|
1805
1771
|
maximumLength: PrefixManager.UPC_COMPANY_PREFIX_MAXIMUM_LENGTH,
|
|
1806
|
-
component: () =>
|
|
1807
|
-
ns: gs1NS
|
|
1808
|
-
})
|
|
1772
|
+
component: () => i18nextGS1.t("Prefix.upcCompanyPrefix")
|
|
1809
1773
|
};
|
|
1810
1774
|
|
|
1811
1775
|
/**
|
|
@@ -1814,9 +1778,7 @@ export class PrefixManager {
|
|
|
1814
1778
|
private static readonly GS1_8_PREFIX_VALIDATION: PrefixValidation = {
|
|
1815
1779
|
minimumLength: PrefixManager.GS1_8_PREFIX_MINIMUM_LENGTH,
|
|
1816
1780
|
maximumLength: PrefixManager.GS1_8_PREFIX_MAXIMUM_LENGTH,
|
|
1817
|
-
component: () =>
|
|
1818
|
-
ns: gs1NS
|
|
1819
|
-
})
|
|
1781
|
+
component: () => i18nextGS1.t("Prefix.gs18Prefix")
|
|
1820
1782
|
};
|
|
1821
1783
|
|
|
1822
1784
|
/**
|
|
@@ -2054,32 +2016,24 @@ export class PrefixManager {
|
|
|
2054
2016
|
baseValidation = PrefixManager.GS1_COMPANY_PREFIX_VALIDATION;
|
|
2055
2017
|
} else if (!prefix.startsWith("00000")) {
|
|
2056
2018
|
if (!allowUPCCompanyPrefix) {
|
|
2057
|
-
throw new RangeError(
|
|
2058
|
-
ns: gs1NS
|
|
2059
|
-
}));
|
|
2019
|
+
throw new RangeError(i18nextGS1.t("Prefix.gs1CompanyPrefixCantStartWith0"));
|
|
2060
2020
|
}
|
|
2061
2021
|
|
|
2062
2022
|
baseValidation = PrefixManager.UPC_COMPANY_PREFIX_AS_GS1_COMPANY_PREFIX_VALIDATION;
|
|
2063
2023
|
} else if (!prefix.startsWith("000000")) {
|
|
2064
2024
|
if (!allowGS18Prefix) {
|
|
2065
|
-
throw new RangeError(
|
|
2066
|
-
ns: gs1NS
|
|
2067
|
-
}));
|
|
2025
|
+
throw new RangeError(i18nextGS1.t("Prefix.gs1CompanyPrefixCantStartWith00000"));
|
|
2068
2026
|
}
|
|
2069
2027
|
|
|
2070
2028
|
baseValidation = PrefixManager.GS1_8_PREFIX_AS_GS1_COMPANY_PREFIX_VALIDATION;
|
|
2071
2029
|
} else {
|
|
2072
|
-
throw new RangeError(
|
|
2073
|
-
ns: gs1NS
|
|
2074
|
-
}));
|
|
2030
|
+
throw new RangeError(i18nextGS1.t("Prefix.gs1CompanyPrefixCantStartWith000000"));
|
|
2075
2031
|
}
|
|
2076
2032
|
break;
|
|
2077
2033
|
|
|
2078
2034
|
case PrefixType.UPCCompanyPrefix:
|
|
2079
2035
|
if (prefix.startsWith("0000")) {
|
|
2080
|
-
throw new RangeError(
|
|
2081
|
-
ns: gs1NS
|
|
2082
|
-
}));
|
|
2036
|
+
throw new RangeError(i18nextGS1.t("Prefix.upcCompanyPrefixCantStartWith0000"));
|
|
2083
2037
|
}
|
|
2084
2038
|
|
|
2085
2039
|
baseValidation = PrefixManager.UPC_COMPANY_PREFIX_VALIDATION;
|
|
@@ -2087,9 +2041,7 @@ export class PrefixManager {
|
|
|
2087
2041
|
|
|
2088
2042
|
case PrefixType.GS18Prefix:
|
|
2089
2043
|
if (prefix.startsWith("0")) {
|
|
2090
|
-
throw new RangeError(
|
|
2091
|
-
ns: gs1NS
|
|
2092
|
-
}));
|
|
2044
|
+
throw new RangeError(i18nextGS1.t("Prefix.gs18PrefixCantStartWith0"));
|
|
2093
2045
|
}
|
|
2094
2046
|
|
|
2095
2047
|
baseValidation = PrefixManager.GS1_8_PREFIX_VALIDATION;
|
|
@@ -2127,8 +2079,7 @@ export class PrefixManager {
|
|
|
2127
2079
|
|
|
2128
2080
|
if (creator === undefined) {
|
|
2129
2081
|
if (this.prefixType === PrefixType.GS18Prefix && identificationKeyType !== IdentificationKeyType.GTIN) {
|
|
2130
|
-
throw new RangeError(
|
|
2131
|
-
ns: gs1NS,
|
|
2082
|
+
throw new RangeError(i18nextGS1.t("Prefix.identificationKeyTypeNotSupportedByGS18Prefix", {
|
|
2132
2083
|
identificationKeyType
|
|
2133
2084
|
}));
|
|
2134
2085
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export const localeStrings = {
|
|
2
2
|
Check: {
|
|
3
|
+
lengthOfStringForPriceOrWeightMustBeExactly: "Length {{length}} of string for price or weight sum must be exactly {{exactLength}}",
|
|
4
|
+
priceOrWeightComponent: "price or weight",
|
|
3
5
|
lengthOfStringForCheckCharacterPairMustBeLessThanOrEqualTo: "Length {{length}} of string for check character pair must be less than or equal to {{maximumLength}}"
|
|
4
6
|
},
|
|
5
7
|
IdentificationKey: {
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export const localeStrings = {
|
|
2
2
|
Check: {
|
|
3
|
+
lengthOfStringForPriceOrWeightMustBeExactly: "La longueur {{longueur}} de la chaîne pour le prix ou la somme du poids doit être exactement {{exactLength}}",
|
|
4
|
+
priceOrWeightComponent: "prix ou poids",
|
|
3
5
|
lengthOfStringForCheckCharacterPairMustBeLessThanOrEqualTo: "La longueur {{length}} de la chaîne pour la paire de caractères de vérification doit être inférieure ou égale à {{maximum Length}}"
|
|
4
6
|
},
|
|
5
7
|
IdentificationKey: {
|
package/src/locale/i18n.ts
CHANGED
|
@@ -1,12 +1,45 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
1
|
+
import { i18nAssertValidResources, i18nCoreInit, type I18NEnvironment } from "@aidc-toolkit/core";
|
|
2
|
+
import { i18nUtilityInit, utilityResources } from "@aidc-toolkit/utility";
|
|
3
|
+
import i18next from "i18next";
|
|
4
|
+
import { localeStrings as enLocaleStrings } from "./en/locale-strings.js";
|
|
5
|
+
import { localeStrings as frLocaleStrings } from "./fr/locale-strings.js";
|
|
4
6
|
|
|
5
7
|
export const gs1NS = "aidct_gs1";
|
|
6
8
|
|
|
9
|
+
/**
|
|
10
|
+
* Locale strings type is extracted from the English locale strings object.
|
|
11
|
+
*/
|
|
12
|
+
export type GS1LocaleStrings = typeof enLocaleStrings;
|
|
13
|
+
|
|
7
14
|
i18nAssertValidResources(enLocaleStrings, "fr", frLocaleStrings);
|
|
8
15
|
|
|
9
|
-
|
|
10
|
-
|
|
16
|
+
/**
|
|
17
|
+
* GS1 resources.
|
|
18
|
+
*/
|
|
19
|
+
export const gs1Resources = {
|
|
20
|
+
en: {
|
|
21
|
+
aidct_gs1: enLocaleStrings
|
|
22
|
+
},
|
|
23
|
+
fr: {
|
|
24
|
+
aidct_gs1: frLocaleStrings
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export const i18nextGS1 = i18next.createInstance();
|
|
11
29
|
|
|
12
|
-
|
|
30
|
+
/**
|
|
31
|
+
* Initialize internationalization.
|
|
32
|
+
*
|
|
33
|
+
* @param environment
|
|
34
|
+
* Environment in which the application is running.
|
|
35
|
+
*
|
|
36
|
+
* @param debug
|
|
37
|
+
* Debug setting.
|
|
38
|
+
*
|
|
39
|
+
* @returns
|
|
40
|
+
* Void promise.
|
|
41
|
+
*/
|
|
42
|
+
export async function i18nGS1Init(environment: I18NEnvironment, debug = false): Promise<void> {
|
|
43
|
+
await i18nUtilityInit(environment, debug);
|
|
44
|
+
await i18nCoreInit(i18nextGS1, environment, debug, gs1NS, utilityResources, gs1Resources);
|
|
45
|
+
}
|