@aidc-toolkit/gs1 0.9.9-beta → 0.9.11-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 +118 -118
- package/dist/index.d.cts +52 -52
- package/dist/index.d.ts +52 -52
- package/dist/index.js +118 -118
- package/package.json +7 -8
- package/src/idkey.ts +171 -171
- package/test/idkey.test.ts +114 -114
package/test/idkey.test.ts
CHANGED
|
@@ -19,10 +19,10 @@ import {
|
|
|
19
19
|
GTIN13_VALIDATOR,
|
|
20
20
|
GTIN8_VALIDATOR,
|
|
21
21
|
GTIN_VALIDATORS,
|
|
22
|
-
GTINCreator,
|
|
22
|
+
type GTINCreator,
|
|
23
23
|
GTINLevel,
|
|
24
24
|
GTINType,
|
|
25
|
-
|
|
25
|
+
GTINValidator,
|
|
26
26
|
hasValidCheckCharacterPair,
|
|
27
27
|
hasValidCheckDigit,
|
|
28
28
|
i18nGS1Init,
|
|
@@ -550,7 +550,7 @@ function testGTINCreator(creator: GTINCreator): void {
|
|
|
550
550
|
|
|
551
551
|
function validate(gtin: string, index: number, sparse: boolean): void {
|
|
552
552
|
expect(() => {
|
|
553
|
-
|
|
553
|
+
GTINValidator.validateGTIN14(gtin);
|
|
554
554
|
}).not.toThrow(RangeError);
|
|
555
555
|
expect(gtin).toBe(creator.createGTIN14("5", index, sparse));
|
|
556
556
|
|
|
@@ -626,109 +626,109 @@ function testGTINCreator(creator: GTINCreator): void {
|
|
|
626
626
|
|
|
627
627
|
if (creator.gtinType === GTINType.GTIN12) {
|
|
628
628
|
test("Zero-suppress GTIN-12 rule 1", () => {
|
|
629
|
-
expect(
|
|
630
|
-
expect(
|
|
631
|
-
expect(
|
|
632
|
-
expect(
|
|
633
|
-
expect(
|
|
629
|
+
expect(GTINValidator.zeroSuppress("012345000058")).toBe("01234558");
|
|
630
|
+
expect(GTINValidator.zeroSuppress("012345000065")).toBe("01234565");
|
|
631
|
+
expect(GTINValidator.zeroSuppress("012345000072")).toBe("01234572");
|
|
632
|
+
expect(GTINValidator.zeroSuppress("012345000089")).toBe("01234589");
|
|
633
|
+
expect(GTINValidator.zeroSuppress("012345000096")).toBe("01234596");
|
|
634
634
|
});
|
|
635
635
|
|
|
636
636
|
test("Zero-suppress GTIN-12 rule 2", () => {
|
|
637
|
-
expect(
|
|
637
|
+
expect(GTINValidator.zeroSuppress("045670000080")).toBe("04567840");
|
|
638
638
|
});
|
|
639
639
|
|
|
640
640
|
test("Zero-suppress GTIN-12 rule 3", () => {
|
|
641
|
-
expect(
|
|
642
|
-
expect(
|
|
643
|
-
expect(
|
|
641
|
+
expect(GTINValidator.zeroSuppress("034000005673")).toBe("03456703");
|
|
642
|
+
expect(GTINValidator.zeroSuppress("034100005672")).toBe("03456712");
|
|
643
|
+
expect(GTINValidator.zeroSuppress("034200005671")).toBe("03456721");
|
|
644
644
|
});
|
|
645
645
|
|
|
646
646
|
test("Zero-suppress GTIN-12 rule 4", () => {
|
|
647
|
-
expect(
|
|
648
|
-
expect(
|
|
649
|
-
expect(
|
|
650
|
-
expect(
|
|
651
|
-
expect(
|
|
652
|
-
expect(
|
|
653
|
-
expect(
|
|
647
|
+
expect(GTINValidator.zeroSuppress("098300000752")).toBe("09837532");
|
|
648
|
+
expect(GTINValidator.zeroSuppress("098400000751")).toBe("09847531");
|
|
649
|
+
expect(GTINValidator.zeroSuppress("098500000750")).toBe("09857530");
|
|
650
|
+
expect(GTINValidator.zeroSuppress("098600000759")).toBe("09867539");
|
|
651
|
+
expect(GTINValidator.zeroSuppress("098700000758")).toBe("09877538");
|
|
652
|
+
expect(GTINValidator.zeroSuppress("098800000757")).toBe("09887537");
|
|
653
|
+
expect(GTINValidator.zeroSuppress("098900000756")).toBe("09897536");
|
|
654
654
|
});
|
|
655
655
|
|
|
656
656
|
test("Non-zero-suppressible GTIN-12 rule 1", () => {
|
|
657
|
-
expect(() =>
|
|
658
|
-
expect(() =>
|
|
659
|
-
expect(() =>
|
|
660
|
-
expect(() =>
|
|
661
|
-
expect(() =>
|
|
662
|
-
expect(() =>
|
|
657
|
+
expect(() => GTINValidator.zeroSuppress("012345100055")).toThrow("GTIN-12 not zero-suppressible");
|
|
658
|
+
expect(() => GTINValidator.zeroSuppress("012345010057")).toThrow("GTIN-12 not zero-suppressible");
|
|
659
|
+
expect(() => GTINValidator.zeroSuppress("012345001055")).toThrow("GTIN-12 not zero-suppressible");
|
|
660
|
+
expect(() => GTINValidator.zeroSuppress("012345000157")).toThrow("GTIN-12 not zero-suppressible");
|
|
661
|
+
expect(() => GTINValidator.zeroSuppress("012345000041")).toThrow("GTIN-12 not zero-suppressible");
|
|
662
|
+
expect(() => GTINValidator.zeroSuppress("012345000003")).toThrow("GTIN-12 not zero-suppressible");
|
|
663
663
|
});
|
|
664
664
|
|
|
665
665
|
test("Non-zero-suppressible GTIN-12 rule 2", () => {
|
|
666
|
-
expect(() =>
|
|
667
|
-
expect(() =>
|
|
668
|
-
expect(() =>
|
|
669
|
-
expect(() =>
|
|
666
|
+
expect(() => GTINValidator.zeroSuppress("045670100087")).toThrow("GTIN-12 not zero-suppressible");
|
|
667
|
+
expect(() => GTINValidator.zeroSuppress("045670010089")).toThrow("GTIN-12 not zero-suppressible");
|
|
668
|
+
expect(() => GTINValidator.zeroSuppress("045670001087")).toThrow("GTIN-12 not zero-suppressible");
|
|
669
|
+
expect(() => GTINValidator.zeroSuppress("045670000189")).toThrow("GTIN-12 not zero-suppressible");
|
|
670
670
|
});
|
|
671
671
|
|
|
672
672
|
test("Non-zero-suppressible GTIN-12 rule 3", () => {
|
|
673
|
-
expect(() =>
|
|
674
|
-
expect(() =>
|
|
675
|
-
expect(() =>
|
|
676
|
-
expect(() =>
|
|
673
|
+
expect(() => GTINValidator.zeroSuppress("034010005670")).toThrow("GTIN-12 not zero-suppressible");
|
|
674
|
+
expect(() => GTINValidator.zeroSuppress("034001005672")).toThrow("GTIN-12 not zero-suppressible");
|
|
675
|
+
expect(() => GTINValidator.zeroSuppress("034000105670")).toThrow("GTIN-12 not zero-suppressible");
|
|
676
|
+
expect(() => GTINValidator.zeroSuppress("034000015672")).toThrow("GTIN-12 not zero-suppressible");
|
|
677
677
|
});
|
|
678
678
|
|
|
679
679
|
test("Non-zero-suppressible GTIN-12 rule 4", () => {
|
|
680
|
-
expect(() =>
|
|
681
|
-
expect(() =>
|
|
682
|
-
expect(() =>
|
|
683
|
-
expect(() =>
|
|
684
|
-
expect(() =>
|
|
680
|
+
expect(() => GTINValidator.zeroSuppress("098310000759")).toThrow("GTIN-12 not zero-suppressible");
|
|
681
|
+
expect(() => GTINValidator.zeroSuppress("098301000751")).toThrow("GTIN-12 not zero-suppressible");
|
|
682
|
+
expect(() => GTINValidator.zeroSuppress("098300100759")).toThrow("GTIN-12 not zero-suppressible");
|
|
683
|
+
expect(() => GTINValidator.zeroSuppress("098300010751")).toThrow("GTIN-12 not zero-suppressible");
|
|
684
|
+
expect(() => GTINValidator.zeroSuppress("098300001759")).toThrow("GTIN-12 not zero-suppressible");
|
|
685
685
|
});
|
|
686
686
|
|
|
687
687
|
test("Zero-suppress other error", () => {
|
|
688
|
-
expect(() =>
|
|
689
|
-
expect(() =>
|
|
690
|
-
expect(() =>
|
|
688
|
+
expect(() => GTINValidator.zeroSuppress("0012345000059")).toThrow("GTIN must be 12 digits long");
|
|
689
|
+
expect(() => GTINValidator.zeroSuppress("012345000059")).toThrow("Invalid check digit");
|
|
690
|
+
expect(() => GTINValidator.zeroSuppress("112345000055")).toThrow("GTIN-12 not zero-suppressible");
|
|
691
691
|
});
|
|
692
692
|
|
|
693
693
|
test("Zero-expand GTIN-12 rule 1", () => {
|
|
694
|
-
expect(
|
|
695
|
-
expect(
|
|
696
|
-
expect(
|
|
697
|
-
expect(
|
|
698
|
-
expect(
|
|
699
|
-
expect(() =>
|
|
694
|
+
expect(GTINValidator.zeroExpand("01234558")).toBe("012345000058");
|
|
695
|
+
expect(GTINValidator.zeroExpand("01234565")).toBe("012345000065");
|
|
696
|
+
expect(GTINValidator.zeroExpand("01234572")).toBe("012345000072");
|
|
697
|
+
expect(GTINValidator.zeroExpand("01234589")).toBe("012345000089");
|
|
698
|
+
expect(GTINValidator.zeroExpand("01234596")).toBe("012345000096");
|
|
699
|
+
expect(() => GTINValidator.zeroExpand("00000154")).toThrow("U.P.C. Company Prefix can't start with \"0000\"");
|
|
700
700
|
});
|
|
701
701
|
|
|
702
702
|
test("Zero-expand GTIN-12 rule 2", () => {
|
|
703
|
-
expect(
|
|
704
|
-
expect(() =>
|
|
703
|
+
expect(GTINValidator.zeroExpand("04567840")).toBe("045670000080");
|
|
704
|
+
expect(() => GTINValidator.zeroExpand("00001047")).toThrow("U.P.C. Company Prefix can't start with \"0000\"");
|
|
705
705
|
});
|
|
706
706
|
|
|
707
707
|
test("Zero-expand GTIN-12 rule 3", () => {
|
|
708
|
-
expect(
|
|
709
|
-
expect(
|
|
710
|
-
expect(
|
|
711
|
-
expect(() =>
|
|
708
|
+
expect(GTINValidator.zeroExpand("03456703")).toBe("034000005673");
|
|
709
|
+
expect(GTINValidator.zeroExpand("03456712")).toBe("034100005672");
|
|
710
|
+
expect(GTINValidator.zeroExpand("03456721")).toBe("034200005671");
|
|
711
|
+
expect(() => GTINValidator.zeroExpand("00000028")).not.toThrow(RangeError);
|
|
712
712
|
});
|
|
713
713
|
|
|
714
714
|
test("Zero-expand GTIN-12 rule 4", () => {
|
|
715
|
-
expect(
|
|
716
|
-
expect(
|
|
717
|
-
expect(
|
|
718
|
-
expect(
|
|
719
|
-
expect(
|
|
720
|
-
expect(
|
|
721
|
-
expect(
|
|
722
|
-
expect(() =>
|
|
715
|
+
expect(GTINValidator.zeroExpand("09837532")).toBe("098300000752");
|
|
716
|
+
expect(GTINValidator.zeroExpand("09847531")).toBe("098400000751");
|
|
717
|
+
expect(GTINValidator.zeroExpand("09857530")).toBe("098500000750");
|
|
718
|
+
expect(GTINValidator.zeroExpand("09867539")).toBe("098600000759");
|
|
719
|
+
expect(GTINValidator.zeroExpand("09877538")).toBe("098700000758");
|
|
720
|
+
expect(GTINValidator.zeroExpand("09887537")).toBe("098800000757");
|
|
721
|
+
expect(GTINValidator.zeroExpand("09897536")).toBe("098900000756");
|
|
722
|
+
expect(() => GTINValidator.zeroExpand("00030037")).not.toThrow(RangeError);
|
|
723
723
|
});
|
|
724
724
|
|
|
725
725
|
test("Zero-expand error", () => {
|
|
726
|
-
expect(() =>
|
|
727
|
-
expect(() =>
|
|
728
|
-
expect(() =>
|
|
729
|
-
expect(() =>
|
|
730
|
-
expect(() =>
|
|
731
|
-
expect(() =>
|
|
726
|
+
expect(() => GTINValidator.zeroExpand("001234505")).toThrow("Length 9 must be less than or equal to 8");
|
|
727
|
+
expect(() => GTINValidator.zeroExpand("01234506")).toThrow("Invalid check digit");
|
|
728
|
+
expect(() => GTINValidator.zeroExpand("11234506")).toThrow("Invalid zero-suppressed GTIN-12");
|
|
729
|
+
expect(() => GTINValidator.zeroExpand("09800037")).toThrow("Invalid zero-suppressed GTIN-12");
|
|
730
|
+
expect(() => GTINValidator.zeroExpand("09800047")).toThrow("Invalid zero-suppressed GTIN-12");
|
|
731
|
+
expect(() => GTINValidator.zeroExpand("09800052")).toThrow("Invalid zero-suppressed GTIN-12");
|
|
732
732
|
});
|
|
733
733
|
}
|
|
734
734
|
|
|
@@ -737,20 +737,20 @@ function testGTINCreator(creator: GTINCreator): void {
|
|
|
737
737
|
|
|
738
738
|
expect(gtin.length).toBe(creator.length);
|
|
739
739
|
|
|
740
|
-
let gtin14 =
|
|
740
|
+
let gtin14 = GTINValidator.convertToGTIN14("0", gtin);
|
|
741
741
|
|
|
742
742
|
expect(gtin14.length).toBe(14);
|
|
743
|
-
expect(
|
|
743
|
+
expect(GTINValidator.normalize(gtin14)).toBe(gtin);
|
|
744
744
|
|
|
745
|
-
gtin14 =
|
|
745
|
+
gtin14 = GTINValidator.convertToGTIN14("1", gtin);
|
|
746
746
|
|
|
747
747
|
expect(gtin14.length).toBe(14);
|
|
748
|
-
expect(
|
|
748
|
+
expect(GTINValidator.normalize(gtin14)).not.toBe(gtin);
|
|
749
749
|
|
|
750
|
-
gtin14 =
|
|
750
|
+
gtin14 = GTINValidator.convertToGTIN14("2", gtin14);
|
|
751
751
|
|
|
752
752
|
expect(gtin14.length).toBe(14);
|
|
753
|
-
expect(
|
|
753
|
+
expect(GTINValidator.normalize(gtin14)).not.toBe(gtin);
|
|
754
754
|
});
|
|
755
755
|
});
|
|
756
756
|
}
|
|
@@ -759,112 +759,112 @@ function testGTINValidationAndNormalization(): void {
|
|
|
759
759
|
describe("GTIN validation and normalization", () => {
|
|
760
760
|
test("Validation", () => {
|
|
761
761
|
expect(() => {
|
|
762
|
-
|
|
762
|
+
GTINValidator.validateAny("9521873000122", GTINLevel.Any);
|
|
763
763
|
}).not.toThrow(RangeError);
|
|
764
764
|
expect(() => {
|
|
765
|
-
|
|
765
|
+
GTINValidator.validateAny("19521873000129", GTINLevel.Any);
|
|
766
766
|
}).not.toThrow(RangeError);
|
|
767
767
|
expect(() => {
|
|
768
|
-
|
|
768
|
+
GTINValidator.validateAny("9521873000160", GTINLevel.Any);
|
|
769
769
|
}).not.toThrow(RangeError);
|
|
770
770
|
expect(() => {
|
|
771
|
-
|
|
771
|
+
GTINValidator.validateAny("95216843", GTINLevel.Any);
|
|
772
772
|
}).not.toThrow(RangeError);
|
|
773
773
|
expect(() => {
|
|
774
|
-
|
|
774
|
+
GTINValidator.validateAny("95217800031", GTINLevel.Any);
|
|
775
775
|
}).toThrow("GTIN must be 13, 12, 8, or 14 digits long");
|
|
776
776
|
expect(() => {
|
|
777
|
-
|
|
777
|
+
GTINValidator.validateAny("614141773985", GTINLevel.Any);
|
|
778
778
|
}).not.toThrow(RangeError);
|
|
779
779
|
expect(() => {
|
|
780
|
-
|
|
780
|
+
GTINValidator.validateAny("614141773991", GTINLevel.Any);
|
|
781
781
|
}).toThrow("Invalid check digit");
|
|
782
782
|
expect(() => {
|
|
783
|
-
|
|
783
|
+
GTINValidator.validateAny("09867539", GTINLevel.Any);
|
|
784
784
|
}).not.toThrow(RangeError);
|
|
785
785
|
expect(() => {
|
|
786
|
-
|
|
786
|
+
GTINValidator.validateAny("09800037", GTINLevel.Any);
|
|
787
787
|
}).toThrow("Invalid zero-suppressed GTIN-12");
|
|
788
788
|
expect(() => {
|
|
789
|
-
|
|
789
|
+
GTINValidator.validateAny("9521873000122", GTINLevel.RetailConsumer);
|
|
790
790
|
}).not.toThrow(RangeError);
|
|
791
791
|
expect(() => {
|
|
792
|
-
|
|
792
|
+
GTINValidator.validateAny("19521873000129", GTINLevel.RetailConsumer);
|
|
793
793
|
}).toThrow("GTIN not supported at retail consumer trade item level");
|
|
794
794
|
expect(() => {
|
|
795
|
-
|
|
795
|
+
GTINValidator.validateAny("9521873000160", GTINLevel.RetailConsumer);
|
|
796
796
|
}).not.toThrow(RangeError);
|
|
797
797
|
expect(() => {
|
|
798
|
-
|
|
798
|
+
GTINValidator.validateAny("95216843", GTINLevel.RetailConsumer);
|
|
799
799
|
}).not.toThrow(RangeError);
|
|
800
800
|
expect(() => {
|
|
801
|
-
|
|
801
|
+
GTINValidator.validateAny("95217800031", GTINLevel.RetailConsumer);
|
|
802
802
|
}).toThrow("GTIN must be 13, 12, 8, or 14 digits long");
|
|
803
803
|
expect(() => {
|
|
804
|
-
|
|
804
|
+
GTINValidator.validateAny("614141773985", GTINLevel.RetailConsumer);
|
|
805
805
|
}).not.toThrow(RangeError);
|
|
806
806
|
expect(() => {
|
|
807
|
-
|
|
807
|
+
GTINValidator.validateAny("0614141773985", GTINLevel.RetailConsumer);
|
|
808
808
|
}).toThrow("GTIN-13 at retail consumer trade item level can't start with zero");
|
|
809
809
|
expect(() => {
|
|
810
|
-
|
|
810
|
+
GTINValidator.validateAny("614141773991", GTINLevel.RetailConsumer);
|
|
811
811
|
}).toThrow("Invalid check digit");
|
|
812
812
|
expect(() => {
|
|
813
|
-
|
|
813
|
+
GTINValidator.validateAny("09867539", GTINLevel.RetailConsumer);
|
|
814
814
|
}).not.toThrow(RangeError);
|
|
815
815
|
expect(() => {
|
|
816
|
-
|
|
816
|
+
GTINValidator.validateAny("09800037", GTINLevel.RetailConsumer);
|
|
817
817
|
}).toThrow("Invalid zero-suppressed GTIN-12");
|
|
818
818
|
expect(() => {
|
|
819
|
-
|
|
819
|
+
GTINValidator.validateAny("9521873000122", GTINLevel.OtherThanRetailConsumer);
|
|
820
820
|
}).not.toThrow(RangeError);
|
|
821
821
|
expect(() => {
|
|
822
|
-
|
|
822
|
+
GTINValidator.validateAny("19521873000129", GTINLevel.OtherThanRetailConsumer);
|
|
823
823
|
}).not.toThrow(RangeError);
|
|
824
824
|
expect(() => {
|
|
825
|
-
|
|
825
|
+
GTINValidator.validateAny("9521873000160", GTINLevel.OtherThanRetailConsumer);
|
|
826
826
|
}).not.toThrow(RangeError);
|
|
827
827
|
expect(() => {
|
|
828
|
-
|
|
828
|
+
GTINValidator.validateAny("95216843", GTINLevel.OtherThanRetailConsumer);
|
|
829
829
|
}).toThrow("GTIN not supported at other than retail consumer trade item level");
|
|
830
830
|
expect(() => {
|
|
831
|
-
|
|
831
|
+
GTINValidator.validateAny("95217800031", GTINLevel.OtherThanRetailConsumer);
|
|
832
832
|
}).toThrow("GTIN must be 13, 12, 8, or 14 digits long");
|
|
833
833
|
expect(() => {
|
|
834
|
-
|
|
834
|
+
GTINValidator.validateAny("614141773985", GTINLevel.OtherThanRetailConsumer);
|
|
835
835
|
}).not.toThrow(RangeError);
|
|
836
836
|
expect(() => {
|
|
837
|
-
|
|
837
|
+
GTINValidator.validateAny("614141773991", GTINLevel.OtherThanRetailConsumer);
|
|
838
838
|
}).toThrow("Invalid check digit");
|
|
839
839
|
expect(() => {
|
|
840
|
-
|
|
840
|
+
GTINValidator.validateAny("09867539", GTINLevel.OtherThanRetailConsumer);
|
|
841
841
|
}).toThrow("GTIN not supported at other than retail consumer trade item level");
|
|
842
842
|
expect(() => {
|
|
843
|
-
|
|
843
|
+
GTINValidator.validateAny("09800037", GTINLevel.OtherThanRetailConsumer);
|
|
844
844
|
}).toThrow("Invalid zero-suppressed GTIN-12");
|
|
845
845
|
});
|
|
846
846
|
|
|
847
847
|
test("Normalization", () => {
|
|
848
848
|
// GTIN-14.
|
|
849
|
-
expect(
|
|
850
|
-
expect(
|
|
851
|
-
expect(() =>
|
|
852
|
-
expect(
|
|
853
|
-
expect(
|
|
849
|
+
expect(GTINValidator.normalize("09526543219996")).toBe("9526543219996");
|
|
850
|
+
expect(GTINValidator.normalize("00614141009992")).toBe("614141009992");
|
|
851
|
+
expect(() => GTINValidator.normalize("00000001234505")).toThrow("Invalid zero-suppressed GTIN-12 as GTIN-14");
|
|
852
|
+
expect(GTINValidator.normalize("00000095209999")).toBe("95209999");
|
|
853
|
+
expect(GTINValidator.normalize("49526543219994")).toBe("49526543219994");
|
|
854
854
|
|
|
855
855
|
// GTIN-13.
|
|
856
|
-
expect(
|
|
857
|
-
expect(
|
|
858
|
-
expect(() =>
|
|
859
|
-
expect(
|
|
856
|
+
expect(GTINValidator.normalize("9526543219996")).toBe("9526543219996");
|
|
857
|
+
expect(GTINValidator.normalize("0614141009992")).toBe("614141009992");
|
|
858
|
+
expect(() => GTINValidator.normalize("0000001234505")).toThrow("Invalid zero-suppressed GTIN-12 as GTIN-13");
|
|
859
|
+
expect(GTINValidator.normalize("0000095209999")).toBe("95209999");
|
|
860
860
|
|
|
861
861
|
// GTIN-12.
|
|
862
|
-
expect(
|
|
863
|
-
expect(
|
|
864
|
-
expect(() =>
|
|
862
|
+
expect(GTINValidator.normalize("614141009992")).toBe("614141009992");
|
|
863
|
+
expect(GTINValidator.normalize("01234505")).toBe("012000003455");
|
|
864
|
+
expect(() => GTINValidator.normalize("09800037")).toThrow("Invalid zero-suppressed GTIN-12");
|
|
865
865
|
|
|
866
866
|
// GTIN-8.
|
|
867
|
-
expect(
|
|
867
|
+
expect(GTINValidator.normalize("95209999")).toBe("95209999");
|
|
868
868
|
});
|
|
869
869
|
});
|
|
870
870
|
}
|