@aidc-toolkit/gs1 0.9.10-beta → 0.9.12-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 +129 -119
- package/dist/index.d.cts +59 -52
- package/dist/index.d.ts +59 -52
- package/dist/index.js +129 -119
- package/package.json +10 -9
- package/src/idkey.ts +184 -173
- package/src/locale/en/locale-strings.ts +1 -0
- package/src/locale/fr/locale-strings.ts +1 -0
- package/test/idkey.test.ts +118 -115
- /package/{eslint.config.js → eslint.config.ts} +0 -0
package/dist/index.d.ts
CHANGED
|
@@ -31,6 +31,7 @@ declare const localeStrings: {
|
|
|
31
31
|
readonly gs1CompanyPrefix: "GS1 Company Prefix";
|
|
32
32
|
readonly upcCompanyPrefix: "U.P.C. Company Prefix";
|
|
33
33
|
readonly gs18Prefix: "GS1-8 Prefix";
|
|
34
|
+
readonly invalidPrefixType: "Invalid prefix type";
|
|
34
35
|
readonly gs1CompanyPrefixCantStartWith0: "GS1 Company Prefix can't start with \"0\"";
|
|
35
36
|
readonly gs1CompanyPrefixCantStartWith00000: "GS1 Company Prefix can't start with \"00000\"";
|
|
36
37
|
readonly gs1CompanyPrefixCantStartWith000000: "GS1 Company Prefix can't start with \"000000\"";
|
|
@@ -78,6 +79,7 @@ declare const gs1Resources: {
|
|
|
78
79
|
readonly gs1CompanyPrefix: "GS1 Company Prefix";
|
|
79
80
|
readonly upcCompanyPrefix: "U.P.C. Company Prefix";
|
|
80
81
|
readonly gs18Prefix: "GS1-8 Prefix";
|
|
82
|
+
readonly invalidPrefixType: "Invalid prefix type";
|
|
81
83
|
readonly gs1CompanyPrefixCantStartWith0: "GS1 Company Prefix can't start with \"0\"";
|
|
82
84
|
readonly gs1CompanyPrefixCantStartWith00000: "GS1 Company Prefix can't start with \"00000\"";
|
|
83
85
|
readonly gs1CompanyPrefixCantStartWith000000: "GS1 Company Prefix can't start with \"000000\"";
|
|
@@ -116,6 +118,7 @@ declare const gs1Resources: {
|
|
|
116
118
|
readonly gs1CompanyPrefix: "Préfixe de l'entreprise GS1";
|
|
117
119
|
readonly upcCompanyPrefix: "Préfixe de l'entreprise U.P.C.";
|
|
118
120
|
readonly gs18Prefix: "Préfixe GS1-8";
|
|
121
|
+
readonly invalidPrefixType: "Type de préfixe invalide";
|
|
119
122
|
readonly gs1CompanyPrefixCantStartWith0: "Le préfixe de l'entreprise GS1 ne peut pas commencer par \"0\"";
|
|
120
123
|
readonly gs1CompanyPrefixCantStartWith00000: "Le préfixe de l'entreprise GS1 ne peut pas commencer par \"00000\"";
|
|
121
124
|
readonly gs1CompanyPrefixCantStartWith000000: "Le préfixe de l'entreprise GS1 ne peut pas commencer par \"000000\"";
|
|
@@ -333,7 +336,7 @@ interface IdentificationKeyValidation extends StringValidation {
|
|
|
333
336
|
* Identification key validator. Validates an identification key against its definition in section 3 of the {@link
|
|
334
337
|
* https://www.gs1.org/genspecs | GS1 General Specifications}.
|
|
335
338
|
*/
|
|
336
|
-
interface IdentificationKeyValidator<
|
|
339
|
+
interface IdentificationKeyValidator<TIdentificationKeyValidation extends IdentificationKeyValidation = IdentificationKeyValidation> extends StringValidator<TIdentificationKeyValidation> {
|
|
337
340
|
/**
|
|
338
341
|
* Get the identification key type. Per the GS1 General Specifications, the identification key type determines
|
|
339
342
|
* the remaining properties.
|
|
@@ -366,12 +369,12 @@ interface IdentificationKeyValidator<V extends IdentificationKeyValidation = Ide
|
|
|
366
369
|
* @param validation
|
|
367
370
|
* Identification key validation parameters.
|
|
368
371
|
*/
|
|
369
|
-
validate: (identificationKey: string, validation?:
|
|
372
|
+
validate: (identificationKey: string, validation?: TIdentificationKeyValidation) => void;
|
|
370
373
|
}
|
|
371
374
|
/**
|
|
372
375
|
* Abstract identification key validator. Implements common functionality for an identification key validator.
|
|
373
376
|
*/
|
|
374
|
-
declare abstract class AbstractIdentificationKeyValidator<
|
|
377
|
+
declare abstract class AbstractIdentificationKeyValidator<TIdentificationKeyValidation extends IdentificationKeyValidation = IdentificationKeyValidation> implements IdentificationKeyValidator<TIdentificationKeyValidation> {
|
|
375
378
|
private static readonly CHARACTER_SET_CREATORS;
|
|
376
379
|
/**
|
|
377
380
|
* Identification key type.
|
|
@@ -463,7 +466,7 @@ declare abstract class AbstractIdentificationKeyValidator<V extends Identificati
|
|
|
463
466
|
* Position offset within a larger string.
|
|
464
467
|
*/
|
|
465
468
|
protected validatePrefix(partialIdentificationKey: string, positionOffset?: number): void;
|
|
466
|
-
abstract validate(identificationKey: string, validation?:
|
|
469
|
+
abstract validate(identificationKey: string, validation?: TIdentificationKeyValidation): void;
|
|
467
470
|
}
|
|
468
471
|
/**
|
|
469
472
|
* Leader type.
|
|
@@ -572,7 +575,11 @@ declare enum GTINLevel {
|
|
|
572
575
|
*/
|
|
573
576
|
declare class GTINValidator extends AbstractNumericIdentificationKeyValidator {
|
|
574
577
|
/**
|
|
575
|
-
*
|
|
578
|
+
* Validation parameters for optional indicator digit.
|
|
579
|
+
*/
|
|
580
|
+
private static readonly OPTIONAL_INDICATOR_DIGIT_VALIDATION;
|
|
581
|
+
/**
|
|
582
|
+
* Validation parameters for zero-suppressed GTIN-12.
|
|
576
583
|
*/
|
|
577
584
|
private static readonly ZERO_SUPPRESSED_GTIN12_VALIDATION;
|
|
578
585
|
/**
|
|
@@ -590,6 +597,16 @@ declare class GTINValidator extends AbstractNumericIdentificationKeyValidator {
|
|
|
590
597
|
* @inheritDoc
|
|
591
598
|
*/
|
|
592
599
|
protected validatePrefix(partialIdentificationKey: string, positionOffset?: number): void;
|
|
600
|
+
/**
|
|
601
|
+
* Zero suppress a GTIN-12.
|
|
602
|
+
*
|
|
603
|
+
* @param gtin12
|
|
604
|
+
* GTIN-12.
|
|
605
|
+
*
|
|
606
|
+
* @returns
|
|
607
|
+
* Zero-suppressed GTIN-12.
|
|
608
|
+
*/
|
|
609
|
+
static zeroSuppress(gtin12: string): string;
|
|
593
610
|
/**
|
|
594
611
|
* Zero expand a zero-suppressed GTIN-12.
|
|
595
612
|
*
|
|
@@ -600,6 +617,33 @@ declare class GTINValidator extends AbstractNumericIdentificationKeyValidator {
|
|
|
600
617
|
* GTIN-12.
|
|
601
618
|
*/
|
|
602
619
|
static zeroExpand(zeroSuppressedGTIN12: string): string;
|
|
620
|
+
/**
|
|
621
|
+
* Convert a GTIN of any length to a GTIN-14 with an optional indicator digit.
|
|
622
|
+
*
|
|
623
|
+
* @param indicatorDigit
|
|
624
|
+
* Indicator digit. If blank, assumes "0" if the GTIN is not already a GTIN-14.
|
|
625
|
+
*
|
|
626
|
+
* @param gtin
|
|
627
|
+
* GTIN.
|
|
628
|
+
*
|
|
629
|
+
* @returns
|
|
630
|
+
* GTIN-14.
|
|
631
|
+
*/
|
|
632
|
+
static convertToGTIN14(indicatorDigit: string, gtin: string): string;
|
|
633
|
+
/**
|
|
634
|
+
* Normalize a GTIN of any length.
|
|
635
|
+
* - A GTIN-14 that starts with six zeros or a GTIN-13 that starts with five zeros is normalized to GTIN-8.
|
|
636
|
+
* - A GTIN-14 that starts with two zeros or a GTIN-13 that starts with one zero is normalized to GTIN-12.
|
|
637
|
+
* - A GTIN-14 that starts with one zero is normalized to GTIN-13.
|
|
638
|
+
* - Otherwise, the GTIN is unchanged.
|
|
639
|
+
*
|
|
640
|
+
* @param gtin
|
|
641
|
+
* GTIN.
|
|
642
|
+
*
|
|
643
|
+
* @returns
|
|
644
|
+
* Normalized GTIN.
|
|
645
|
+
*/
|
|
646
|
+
static normalize(gtin: string): string;
|
|
603
647
|
/**
|
|
604
648
|
* Validate any GTIN, optionally against a level.
|
|
605
649
|
*
|
|
@@ -895,7 +939,7 @@ interface NumericIdentificationKeyCreator extends NumericIdentificationKeyValida
|
|
|
895
939
|
* @returns
|
|
896
940
|
* Identification key(s).
|
|
897
941
|
*/
|
|
898
|
-
create: <
|
|
942
|
+
create: <TTransformerInput extends TransformerInput<number | bigint>>(valueOrValues: TTransformerInput, sparse?: boolean) => TransformerOutput<TTransformerInput, string>;
|
|
899
943
|
/**
|
|
900
944
|
* Create all identification keys for the prefix from `0` to `capacity - 1`.
|
|
901
945
|
*
|
|
@@ -956,7 +1000,7 @@ declare abstract class AbstractNumericIdentificationKeyCreator extends AbstractI
|
|
|
956
1000
|
/**
|
|
957
1001
|
* @inheritDoc
|
|
958
1002
|
*/
|
|
959
|
-
create<
|
|
1003
|
+
create<TTransformerInput extends TransformerInput<number | bigint>>(valueOrValues: TTransformerInput, sparse?: boolean): TransformerOutput<TTransformerInput, string>;
|
|
960
1004
|
/**
|
|
961
1005
|
* Create all identification keys from a partial identification key. Call is recursive until remaining reference
|
|
962
1006
|
* length is 0.
|
|
@@ -995,10 +1039,6 @@ declare class GTINCreator extends GTINCreator_base {
|
|
|
995
1039
|
* Validation parameters for required indicator digit.
|
|
996
1040
|
*/
|
|
997
1041
|
private static readonly REQUIRED_INDICATOR_DIGIT_VALIDATION;
|
|
998
|
-
/**
|
|
999
|
-
* Validation parameters for optional indicator digit.
|
|
1000
|
-
*/
|
|
1001
|
-
private static readonly OPTIONAL_INDICATOR_DIGIT_VALIDATION;
|
|
1002
1042
|
/**
|
|
1003
1043
|
* Constructor. Called internally by {@link PrefixManager.gtinCreator}; should not be called by other code.
|
|
1004
1044
|
*
|
|
@@ -1029,44 +1069,7 @@ declare class GTINCreator extends GTINCreator_base {
|
|
|
1029
1069
|
* @returns
|
|
1030
1070
|
* GTIN-14(s).
|
|
1031
1071
|
*/
|
|
1032
|
-
createGTIN14<
|
|
1033
|
-
/**
|
|
1034
|
-
* Zero suppress a GTIN-12.
|
|
1035
|
-
*
|
|
1036
|
-
* @param gtin12
|
|
1037
|
-
* GTIN-12.
|
|
1038
|
-
*
|
|
1039
|
-
* @returns
|
|
1040
|
-
* Zero-suppressed GTIN-12.
|
|
1041
|
-
*/
|
|
1042
|
-
static zeroSuppress(gtin12: string): string;
|
|
1043
|
-
/**
|
|
1044
|
-
* Convert a GTIN of any length to a GTIN-14 with an optional indicator digit.
|
|
1045
|
-
*
|
|
1046
|
-
* @param indicatorDigit
|
|
1047
|
-
* Indicator digit. If blank, assumes "0" if the GTIN is not already a GTIN-14.
|
|
1048
|
-
*
|
|
1049
|
-
* @param gtin
|
|
1050
|
-
* GTIN.
|
|
1051
|
-
*
|
|
1052
|
-
* @returns
|
|
1053
|
-
* GTIN-14.
|
|
1054
|
-
*/
|
|
1055
|
-
static convertToGTIN14(indicatorDigit: string, gtin: string): string;
|
|
1056
|
-
/**
|
|
1057
|
-
* Normalize a GTIN of any length.
|
|
1058
|
-
* - A GTIN-14 that starts with six zeros or a GTIN-13 that starts with five zeros is normalized to GTIN-8.
|
|
1059
|
-
* - A GTIN-14 that starts with two zeros or a GTIN-13 that starts with one zero is normalized to GTIN-12.
|
|
1060
|
-
* - A GTIN-14 that starts with one zero is normalized to GTIN-13.
|
|
1061
|
-
* - Otherwise, the GTIN is unchanged.
|
|
1062
|
-
*
|
|
1063
|
-
* @param gtin
|
|
1064
|
-
* GTIN.
|
|
1065
|
-
*
|
|
1066
|
-
* @returns
|
|
1067
|
-
* Normalized GTIN.
|
|
1068
|
-
*/
|
|
1069
|
-
static normalize(gtin: string): string;
|
|
1072
|
+
createGTIN14<TTransformerInput extends TransformerInput<number | bigint>>(indicatorDigit: string, valueOrValues: TTransformerInput, sparse?: boolean): TransformerOutput<TTransformerInput, string>;
|
|
1070
1073
|
}
|
|
1071
1074
|
declare const NonGTINNumericIdentificationKeyCreator_base: ts_mixer_dist_types_types_js.Class<any[], NonGTINNumericIdentificationKeyValidator & AbstractNumericIdentificationKeyCreator, typeof NonGTINNumericIdentificationKeyValidator & typeof AbstractNumericIdentificationKeyCreator>;
|
|
1072
1075
|
/**
|
|
@@ -1145,7 +1148,7 @@ declare class SerializableNumericIdentificationKeyCreator extends SerializableNu
|
|
|
1145
1148
|
* @returns
|
|
1146
1149
|
* Serialized identification keys.
|
|
1147
1150
|
*/
|
|
1148
|
-
createSerialized<
|
|
1151
|
+
createSerialized<TTransformerInput extends TransformerInput<string>>(value: number, serialComponentOrComponents: TTransformerInput, sparse?: boolean): TransformerOutput<TTransformerInput, string>;
|
|
1149
1152
|
/**
|
|
1150
1153
|
* Concatenate a base identification key with serial component(s).
|
|
1151
1154
|
*
|
|
@@ -1158,7 +1161,7 @@ declare class SerializableNumericIdentificationKeyCreator extends SerializableNu
|
|
|
1158
1161
|
* @returns
|
|
1159
1162
|
* Serialized identification key(s).
|
|
1160
1163
|
*/
|
|
1161
|
-
concatenate<
|
|
1164
|
+
concatenate<TTransformerInput extends TransformerInput<string>>(baseIdentificationKey: string, serialComponentOrComponents: TTransformerInput): TransformerOutput<TTransformerInput, string>;
|
|
1162
1165
|
}
|
|
1163
1166
|
declare const NonNumericIdentificationKeyCreator_base: ts_mixer_dist_types_types_js.Class<any[], NonNumericIdentificationKeyValidator & AbstractIdentificationKeyCreator, typeof NonNumericIdentificationKeyValidator & typeof AbstractIdentificationKeyCreator>;
|
|
1164
1167
|
/**
|
|
@@ -1202,7 +1205,7 @@ declare class NonNumericIdentificationKeyCreator extends NonNumericIdentificatio
|
|
|
1202
1205
|
* @returns
|
|
1203
1206
|
* Identification key(s).
|
|
1204
1207
|
*/
|
|
1205
|
-
create<
|
|
1208
|
+
create<TTransformerInput extends TransformerInput<string>>(referenceOrReferences: TTransformerInput): TransformerOutput<TTransformerInput, string>;
|
|
1206
1209
|
}
|
|
1207
1210
|
/**
|
|
1208
1211
|
* Prefix manager. This is the core class for identification key creation.
|
|
@@ -1299,6 +1302,10 @@ declare class PrefixManager {
|
|
|
1299
1302
|
* GS1-8 Prefix if prefix type is {@link PrefixType.GS18Prefix}.
|
|
1300
1303
|
*/
|
|
1301
1304
|
private readonly _gs18Prefix;
|
|
1305
|
+
/**
|
|
1306
|
+
* Default tweak factor.
|
|
1307
|
+
*/
|
|
1308
|
+
private readonly _defaultTweakFactor;
|
|
1302
1309
|
/**
|
|
1303
1310
|
* Tweak factor.
|
|
1304
1311
|
*/
|
package/dist/index.js
CHANGED
|
@@ -32,6 +32,7 @@ var localeStrings = {
|
|
|
32
32
|
gs1CompanyPrefix: "GS1 Company Prefix",
|
|
33
33
|
upcCompanyPrefix: "U.P.C. Company Prefix",
|
|
34
34
|
gs18Prefix: "GS1-8 Prefix",
|
|
35
|
+
invalidPrefixType: "Invalid prefix type",
|
|
35
36
|
gs1CompanyPrefixCantStartWith0: `GS1 Company Prefix can't start with "0"`,
|
|
36
37
|
gs1CompanyPrefixCantStartWith00000: `GS1 Company Prefix can't start with "00000"`,
|
|
37
38
|
gs1CompanyPrefixCantStartWith000000: `GS1 Company Prefix can't start with "000000"`,
|
|
@@ -70,6 +71,7 @@ var localeStrings2 = {
|
|
|
70
71
|
gs1CompanyPrefix: "Pr\xE9fixe de l'entreprise GS1",
|
|
71
72
|
upcCompanyPrefix: "Pr\xE9fixe de l'entreprise U.P.C.",
|
|
72
73
|
gs18Prefix: "Pr\xE9fixe GS1-8",
|
|
74
|
+
invalidPrefixType: "Type de pr\xE9fixe invalide",
|
|
73
75
|
gs1CompanyPrefixCantStartWith0: `Le pr\xE9fixe de l'entreprise GS1 ne peut pas commencer par "0"`,
|
|
74
76
|
gs1CompanyPrefixCantStartWith00000: `Le pr\xE9fixe de l'entreprise GS1 ne peut pas commencer par "00000"`,
|
|
75
77
|
gs1CompanyPrefixCantStartWith000000: `Le pr\xE9fixe de l'entreprise GS1 ne peut pas commencer par "000000"`,
|
|
@@ -653,7 +655,15 @@ var GTINLevel = /* @__PURE__ */ ((GTINLevel2) => {
|
|
|
653
655
|
})(GTINLevel || {});
|
|
654
656
|
var GTINValidator = class _GTINValidator extends AbstractNumericIdentificationKeyValidator {
|
|
655
657
|
/**
|
|
656
|
-
*
|
|
658
|
+
* Validation parameters for optional indicator digit.
|
|
659
|
+
*/
|
|
660
|
+
static OPTIONAL_INDICATOR_DIGIT_VALIDATION = {
|
|
661
|
+
minimumLength: 0,
|
|
662
|
+
maximumLength: 1,
|
|
663
|
+
component: () => i18nextGS1.t("IdentificationKey.indicatorDigit")
|
|
664
|
+
};
|
|
665
|
+
/**
|
|
666
|
+
* Validation parameters for zero-suppressed GTIN-12.
|
|
657
667
|
*/
|
|
658
668
|
static ZERO_SUPPRESSED_GTIN12_VALIDATION = {
|
|
659
669
|
minimumLength: 8,
|
|
@@ -694,6 +704,35 @@ var GTINValidator = class _GTINValidator extends AbstractNumericIdentificationKe
|
|
|
694
704
|
validatePrefix(partialIdentificationKey, positionOffset) {
|
|
695
705
|
PrefixManager.validatePrefix(this.prefixType, false, false, partialIdentificationKey, true, true, positionOffset);
|
|
696
706
|
}
|
|
707
|
+
/**
|
|
708
|
+
* Zero suppress a GTIN-12.
|
|
709
|
+
*
|
|
710
|
+
* @param gtin12
|
|
711
|
+
* GTIN-12.
|
|
712
|
+
*
|
|
713
|
+
* @returns
|
|
714
|
+
* Zero-suppressed GTIN-12.
|
|
715
|
+
*/
|
|
716
|
+
static zeroSuppress(gtin12) {
|
|
717
|
+
GTIN12_VALIDATOR.validate(gtin12);
|
|
718
|
+
const d = Array.from(gtin12);
|
|
719
|
+
let zeroSuppressedGTIN12;
|
|
720
|
+
if (d[0] === "0" && d[6] === "0" && d[7] === "0") {
|
|
721
|
+
if (d[10] >= "5" && d[8] === "0" && d[9] === "0" && d[5] !== "0") {
|
|
722
|
+
zeroSuppressedGTIN12 = `0${d[1]}${d[2]}${d[3]}${d[4]}${d[5]}${d[10]}${d[11]}`;
|
|
723
|
+
} else if (d[5] === "0" && d[8] === "0" && d[9] === "0" && d[4] !== "0") {
|
|
724
|
+
zeroSuppressedGTIN12 = `0${d[1]}${d[2]}${d[3]}${d[4]}${d[10]}4${d[11]}`;
|
|
725
|
+
} else if (d[3] <= "2" && d[4] === "0" && d[5] === "0") {
|
|
726
|
+
zeroSuppressedGTIN12 = `0${d[1]}${d[2]}${d[8]}${d[9]}${d[10]}${d[3]}${d[11]}`;
|
|
727
|
+
} else if (d[3] >= "3" && d[4] === "0" && d[5] === "0" && d[8] === "0") {
|
|
728
|
+
zeroSuppressedGTIN12 = `0${d[1]}${d[2]}${d[3]}${d[9]}${d[10]}3${d[11]}`;
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
if (zeroSuppressedGTIN12 === void 0) {
|
|
732
|
+
throw new RangeError(i18nextGS1.t("IdentificationKey.invalidZeroSuppressibleGTIN12"));
|
|
733
|
+
}
|
|
734
|
+
return zeroSuppressedGTIN12;
|
|
735
|
+
}
|
|
697
736
|
/**
|
|
698
737
|
* Zero expand a zero-suppressed GTIN-12.
|
|
699
738
|
*
|
|
@@ -724,6 +763,86 @@ var GTINValidator = class _GTINValidator extends AbstractNumericIdentificationKe
|
|
|
724
763
|
GTIN12_VALIDATOR.validate(gtin12);
|
|
725
764
|
return gtin12;
|
|
726
765
|
}
|
|
766
|
+
/**
|
|
767
|
+
* Convert a GTIN of any length to a GTIN-14 with an optional indicator digit.
|
|
768
|
+
*
|
|
769
|
+
* @param indicatorDigit
|
|
770
|
+
* Indicator digit. If blank, assumes "0" if the GTIN is not already a GTIN-14.
|
|
771
|
+
*
|
|
772
|
+
* @param gtin
|
|
773
|
+
* GTIN.
|
|
774
|
+
*
|
|
775
|
+
* @returns
|
|
776
|
+
* GTIN-14.
|
|
777
|
+
*/
|
|
778
|
+
static convertToGTIN14(indicatorDigit, gtin) {
|
|
779
|
+
GTINCreator.validateAny(gtin);
|
|
780
|
+
NUMERIC_CREATOR2.validate(indicatorDigit, _GTINValidator.OPTIONAL_INDICATOR_DIGIT_VALIDATION);
|
|
781
|
+
const gtinLength = gtin.length;
|
|
782
|
+
let gtin14 = "0".repeat(14 /* GTIN14 */ - gtinLength) + gtin;
|
|
783
|
+
if (indicatorDigit.length !== 0 && indicatorDigit !== gtin14.charAt(0)) {
|
|
784
|
+
const partialGTIN14 = indicatorDigit + gtin14.substring(1, 14 /* GTIN14 */ - 1);
|
|
785
|
+
gtin14 = partialGTIN14 + checkDigit(partialGTIN14);
|
|
786
|
+
}
|
|
787
|
+
return gtin14;
|
|
788
|
+
}
|
|
789
|
+
/**
|
|
790
|
+
* Normalize a GTIN of any length.
|
|
791
|
+
* - A GTIN-14 that starts with six zeros or a GTIN-13 that starts with five zeros is normalized to GTIN-8.
|
|
792
|
+
* - A GTIN-14 that starts with two zeros or a GTIN-13 that starts with one zero is normalized to GTIN-12.
|
|
793
|
+
* - A GTIN-14 that starts with one zero is normalized to GTIN-13.
|
|
794
|
+
* - Otherwise, the GTIN is unchanged.
|
|
795
|
+
*
|
|
796
|
+
* @param gtin
|
|
797
|
+
* GTIN.
|
|
798
|
+
*
|
|
799
|
+
* @returns
|
|
800
|
+
* Normalized GTIN.
|
|
801
|
+
*/
|
|
802
|
+
static normalize(gtin) {
|
|
803
|
+
const gtinLength = gtin.length;
|
|
804
|
+
let normalizedGTIN;
|
|
805
|
+
switch (gtinLength) {
|
|
806
|
+
case 13 /* GTIN13 */:
|
|
807
|
+
if (!gtin.startsWith("0")) {
|
|
808
|
+
normalizedGTIN = gtin;
|
|
809
|
+
} else if (!gtin.startsWith("00000")) {
|
|
810
|
+
normalizedGTIN = gtin.substring(1);
|
|
811
|
+
} else if (!gtin.startsWith("000000")) {
|
|
812
|
+
normalizedGTIN = gtin.substring(5);
|
|
813
|
+
} else {
|
|
814
|
+
throw new RangeError(i18nextGS1.t("IdentificationKey.invalidZeroSuppressedGTIN12AsGTIN13"));
|
|
815
|
+
}
|
|
816
|
+
break;
|
|
817
|
+
case 12 /* GTIN12 */:
|
|
818
|
+
normalizedGTIN = gtin;
|
|
819
|
+
break;
|
|
820
|
+
case 8 /* GTIN8 */:
|
|
821
|
+
if (!gtin.startsWith("0")) {
|
|
822
|
+
normalizedGTIN = gtin;
|
|
823
|
+
} else {
|
|
824
|
+
normalizedGTIN = _GTINValidator.zeroExpand(gtin);
|
|
825
|
+
}
|
|
826
|
+
break;
|
|
827
|
+
case 14 /* GTIN14 */:
|
|
828
|
+
if (!gtin.startsWith("0")) {
|
|
829
|
+
normalizedGTIN = gtin;
|
|
830
|
+
} else if (!gtin.startsWith("00")) {
|
|
831
|
+
normalizedGTIN = gtin.substring(1);
|
|
832
|
+
} else if (!gtin.startsWith("000000")) {
|
|
833
|
+
normalizedGTIN = gtin.substring(2);
|
|
834
|
+
} else if (!gtin.startsWith("0000000")) {
|
|
835
|
+
normalizedGTIN = gtin.substring(6);
|
|
836
|
+
} else {
|
|
837
|
+
throw new RangeError(i18nextGS1.t("IdentificationKey.invalidZeroSuppressedGTIN12AsGTIN14"));
|
|
838
|
+
}
|
|
839
|
+
break;
|
|
840
|
+
default:
|
|
841
|
+
throw new RangeError(i18nextGS1.t("IdentificationKey.invalidGTINLength"));
|
|
842
|
+
}
|
|
843
|
+
GTINCreator.validateAny(normalizedGTIN);
|
|
844
|
+
return normalizedGTIN;
|
|
845
|
+
}
|
|
727
846
|
/**
|
|
728
847
|
* Validate any GTIN, optionally against a level.
|
|
729
848
|
*
|
|
@@ -1133,14 +1252,6 @@ var GTINCreator = class _GTINCreator extends Mixin(GTINValidator, AbstractNumeri
|
|
|
1133
1252
|
maximumLength: 1,
|
|
1134
1253
|
component: () => i18nextGS1.t("IdentificationKey.indicatorDigit")
|
|
1135
1254
|
};
|
|
1136
|
-
/**
|
|
1137
|
-
* Validation parameters for optional indicator digit.
|
|
1138
|
-
*/
|
|
1139
|
-
static OPTIONAL_INDICATOR_DIGIT_VALIDATION = {
|
|
1140
|
-
minimumLength: 0,
|
|
1141
|
-
maximumLength: 1,
|
|
1142
|
-
component: () => i18nextGS1.t("IdentificationKey.indicatorDigit")
|
|
1143
|
-
};
|
|
1144
1255
|
/**
|
|
1145
1256
|
* Constructor. Called internally by {@link PrefixManager.gtinCreator}; should not be called by other code.
|
|
1146
1257
|
*
|
|
@@ -1183,115 +1294,6 @@ var GTINCreator = class _GTINCreator extends Mixin(GTINValidator, AbstractNumeri
|
|
|
1183
1294
|
return partialIdentificationKey + checkDigit(partialIdentificationKey);
|
|
1184
1295
|
});
|
|
1185
1296
|
}
|
|
1186
|
-
/**
|
|
1187
|
-
* Zero suppress a GTIN-12.
|
|
1188
|
-
*
|
|
1189
|
-
* @param gtin12
|
|
1190
|
-
* GTIN-12.
|
|
1191
|
-
*
|
|
1192
|
-
* @returns
|
|
1193
|
-
* Zero-suppressed GTIN-12.
|
|
1194
|
-
*/
|
|
1195
|
-
static zeroSuppress(gtin12) {
|
|
1196
|
-
GTIN12_VALIDATOR.validate(gtin12);
|
|
1197
|
-
const d = Array.from(gtin12);
|
|
1198
|
-
let zeroSuppressedGTIN12;
|
|
1199
|
-
if (d[0] === "0" && d[6] === "0" && d[7] === "0") {
|
|
1200
|
-
if (d[10] >= "5" && d[8] === "0" && d[9] === "0" && d[5] !== "0") {
|
|
1201
|
-
zeroSuppressedGTIN12 = `0${d[1]}${d[2]}${d[3]}${d[4]}${d[5]}${d[10]}${d[11]}`;
|
|
1202
|
-
} else if (d[5] === "0" && d[8] === "0" && d[9] === "0" && d[4] !== "0") {
|
|
1203
|
-
zeroSuppressedGTIN12 = `0${d[1]}${d[2]}${d[3]}${d[4]}${d[10]}4${d[11]}`;
|
|
1204
|
-
} else if (d[3] <= "2" && d[4] === "0" && d[5] === "0") {
|
|
1205
|
-
zeroSuppressedGTIN12 = `0${d[1]}${d[2]}${d[8]}${d[9]}${d[10]}${d[3]}${d[11]}`;
|
|
1206
|
-
} else if (d[3] >= "3" && d[4] === "0" && d[5] === "0" && d[8] === "0") {
|
|
1207
|
-
zeroSuppressedGTIN12 = `0${d[1]}${d[2]}${d[3]}${d[9]}${d[10]}3${d[11]}`;
|
|
1208
|
-
}
|
|
1209
|
-
}
|
|
1210
|
-
if (zeroSuppressedGTIN12 === void 0) {
|
|
1211
|
-
throw new RangeError(i18nextGS1.t("IdentificationKey.invalidZeroSuppressibleGTIN12"));
|
|
1212
|
-
}
|
|
1213
|
-
return zeroSuppressedGTIN12;
|
|
1214
|
-
}
|
|
1215
|
-
/**
|
|
1216
|
-
* Convert a GTIN of any length to a GTIN-14 with an optional indicator digit.
|
|
1217
|
-
*
|
|
1218
|
-
* @param indicatorDigit
|
|
1219
|
-
* Indicator digit. If blank, assumes "0" if the GTIN is not already a GTIN-14.
|
|
1220
|
-
*
|
|
1221
|
-
* @param gtin
|
|
1222
|
-
* GTIN.
|
|
1223
|
-
*
|
|
1224
|
-
* @returns
|
|
1225
|
-
* GTIN-14.
|
|
1226
|
-
*/
|
|
1227
|
-
static convertToGTIN14(indicatorDigit, gtin) {
|
|
1228
|
-
_GTINCreator.validateAny(gtin);
|
|
1229
|
-
NUMERIC_CREATOR2.validate(indicatorDigit, _GTINCreator.OPTIONAL_INDICATOR_DIGIT_VALIDATION);
|
|
1230
|
-
const gtinLength = gtin.length;
|
|
1231
|
-
let gtin14 = "0".repeat(14 /* GTIN14 */ - gtinLength) + gtin;
|
|
1232
|
-
if (indicatorDigit.length !== 0 && indicatorDigit !== gtin14.charAt(0)) {
|
|
1233
|
-
const partialGTIN14 = indicatorDigit + gtin14.substring(1, 14 /* GTIN14 */ - 1);
|
|
1234
|
-
gtin14 = partialGTIN14 + checkDigit(partialGTIN14);
|
|
1235
|
-
}
|
|
1236
|
-
return gtin14;
|
|
1237
|
-
}
|
|
1238
|
-
/**
|
|
1239
|
-
* Normalize a GTIN of any length.
|
|
1240
|
-
* - A GTIN-14 that starts with six zeros or a GTIN-13 that starts with five zeros is normalized to GTIN-8.
|
|
1241
|
-
* - A GTIN-14 that starts with two zeros or a GTIN-13 that starts with one zero is normalized to GTIN-12.
|
|
1242
|
-
* - A GTIN-14 that starts with one zero is normalized to GTIN-13.
|
|
1243
|
-
* - Otherwise, the GTIN is unchanged.
|
|
1244
|
-
*
|
|
1245
|
-
* @param gtin
|
|
1246
|
-
* GTIN.
|
|
1247
|
-
*
|
|
1248
|
-
* @returns
|
|
1249
|
-
* Normalized GTIN.
|
|
1250
|
-
*/
|
|
1251
|
-
static normalize(gtin) {
|
|
1252
|
-
const gtinLength = gtin.length;
|
|
1253
|
-
let normalizedGTIN;
|
|
1254
|
-
switch (gtinLength) {
|
|
1255
|
-
case 13 /* GTIN13 */:
|
|
1256
|
-
if (!gtin.startsWith("0")) {
|
|
1257
|
-
normalizedGTIN = gtin;
|
|
1258
|
-
} else if (!gtin.startsWith("00000")) {
|
|
1259
|
-
normalizedGTIN = gtin.substring(1);
|
|
1260
|
-
} else if (!gtin.startsWith("000000")) {
|
|
1261
|
-
normalizedGTIN = gtin.substring(5);
|
|
1262
|
-
} else {
|
|
1263
|
-
throw new RangeError(i18nextGS1.t("IdentificationKey.invalidZeroSuppressedGTIN12AsGTIN13"));
|
|
1264
|
-
}
|
|
1265
|
-
break;
|
|
1266
|
-
case 12 /* GTIN12 */:
|
|
1267
|
-
normalizedGTIN = gtin;
|
|
1268
|
-
break;
|
|
1269
|
-
case 8 /* GTIN8 */:
|
|
1270
|
-
if (!gtin.startsWith("0")) {
|
|
1271
|
-
normalizedGTIN = gtin;
|
|
1272
|
-
} else {
|
|
1273
|
-
normalizedGTIN = _GTINCreator.zeroExpand(gtin);
|
|
1274
|
-
}
|
|
1275
|
-
break;
|
|
1276
|
-
case 14 /* GTIN14 */:
|
|
1277
|
-
if (!gtin.startsWith("0")) {
|
|
1278
|
-
normalizedGTIN = gtin;
|
|
1279
|
-
} else if (!gtin.startsWith("00")) {
|
|
1280
|
-
normalizedGTIN = gtin.substring(1);
|
|
1281
|
-
} else if (!gtin.startsWith("000000")) {
|
|
1282
|
-
normalizedGTIN = gtin.substring(2);
|
|
1283
|
-
} else if (!gtin.startsWith("0000000")) {
|
|
1284
|
-
normalizedGTIN = gtin.substring(6);
|
|
1285
|
-
} else {
|
|
1286
|
-
throw new RangeError(i18nextGS1.t("IdentificationKey.invalidZeroSuppressedGTIN12AsGTIN14"));
|
|
1287
|
-
}
|
|
1288
|
-
break;
|
|
1289
|
-
default:
|
|
1290
|
-
throw new RangeError(i18nextGS1.t("IdentificationKey.invalidGTINLength"));
|
|
1291
|
-
}
|
|
1292
|
-
_GTINCreator.validateAny(normalizedGTIN);
|
|
1293
|
-
return normalizedGTIN;
|
|
1294
|
-
}
|
|
1295
1297
|
};
|
|
1296
1298
|
var NonGTINNumericIdentificationKeyCreator = class extends Mixin(NonGTINNumericIdentificationKeyValidator, AbstractNumericIdentificationKeyCreator) {
|
|
1297
1299
|
/**
|
|
@@ -1573,6 +1575,10 @@ var PrefixManager = class _PrefixManager {
|
|
|
1573
1575
|
* GS1-8 Prefix if prefix type is {@link PrefixType.GS18Prefix}.
|
|
1574
1576
|
*/
|
|
1575
1577
|
_gs18Prefix;
|
|
1578
|
+
/**
|
|
1579
|
+
* Default tweak factor.
|
|
1580
|
+
*/
|
|
1581
|
+
_defaultTweakFactor;
|
|
1576
1582
|
/**
|
|
1577
1583
|
* Tweak factor.
|
|
1578
1584
|
*/
|
|
@@ -1601,6 +1607,7 @@ var PrefixManager = class _PrefixManager {
|
|
|
1601
1607
|
this._gs18Prefix = gs1CompanyPrefix.substring(5);
|
|
1602
1608
|
this._prefix = this._gs18Prefix;
|
|
1603
1609
|
}
|
|
1610
|
+
this._defaultTweakFactor = BigInt(`1${this.gs1CompanyPrefix}`);
|
|
1604
1611
|
this.resetTweakFactor();
|
|
1605
1612
|
}
|
|
1606
1613
|
/**
|
|
@@ -1670,7 +1677,7 @@ var PrefixManager = class _PrefixManager {
|
|
|
1670
1677
|
* Reset the tweak factor to its default (numeric value of the GS1 Company Prefix preceded by '1').
|
|
1671
1678
|
*/
|
|
1672
1679
|
resetTweakFactor() {
|
|
1673
|
-
this.tweakFactor =
|
|
1680
|
+
this.tweakFactor = this._defaultTweakFactor;
|
|
1674
1681
|
}
|
|
1675
1682
|
/**
|
|
1676
1683
|
* Get a prefix manager.
|
|
@@ -1697,6 +1704,9 @@ var PrefixManager = class _PrefixManager {
|
|
|
1697
1704
|
case 2 /* GS18Prefix */:
|
|
1698
1705
|
gs1CompanyPrefix = "00000" + prefix;
|
|
1699
1706
|
break;
|
|
1707
|
+
// eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- Method may be called by unsafe means.
|
|
1708
|
+
default:
|
|
1709
|
+
throw new RangeError(i18nextGS1.t("Prefix.invalidPrefixType"));
|
|
1700
1710
|
}
|
|
1701
1711
|
let prefixManager = _PrefixManager.PREFIX_MANAGERS_MAP.get(gs1CompanyPrefix);
|
|
1702
1712
|
if (prefixManager === void 0) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aidc-toolkit/gs1",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.12-beta",
|
|
4
4
|
"description": "GS1 AIDC Toolkit",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -25,17 +25,18 @@
|
|
|
25
25
|
"test": "vitest run"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"@aidc-toolkit/dev": "^0.9.
|
|
29
|
-
"eslint": "^9.
|
|
28
|
+
"@aidc-toolkit/dev": "^0.9.12-beta",
|
|
29
|
+
"eslint": "^9.20.1",
|
|
30
|
+
"jiti": "^2.4.2",
|
|
30
31
|
"ts-node": "^10.9.2",
|
|
31
|
-
"tsup": "^8.3.
|
|
32
|
-
"typescript": "^5.7.
|
|
33
|
-
"vitest": "^
|
|
32
|
+
"tsup": "^8.3.6",
|
|
33
|
+
"typescript": "^5.7.3",
|
|
34
|
+
"vitest": "^3.0.5"
|
|
34
35
|
},
|
|
35
36
|
"dependencies": {
|
|
36
|
-
"@aidc-toolkit/core": "^0.9.
|
|
37
|
-
"@aidc-toolkit/utility": "^0.9.
|
|
38
|
-
"i18next": "^24.2.
|
|
37
|
+
"@aidc-toolkit/core": "^0.9.12-beta",
|
|
38
|
+
"@aidc-toolkit/utility": "^0.9.12-beta",
|
|
39
|
+
"i18next": "^24.2.2",
|
|
39
40
|
"ts-mixer": "^6.0.4"
|
|
40
41
|
}
|
|
41
42
|
}
|