@aidc-toolkit/gs1 0.9.10-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.js CHANGED
@@ -653,7 +653,15 @@ var GTINLevel = /* @__PURE__ */ ((GTINLevel2) => {
653
653
  })(GTINLevel || {});
654
654
  var GTINValidator = class _GTINValidator extends AbstractNumericIdentificationKeyValidator {
655
655
  /**
656
- * Zero-suppressed GTIN-12 validation parameters.
656
+ * Validation parameters for optional indicator digit.
657
+ */
658
+ static OPTIONAL_INDICATOR_DIGIT_VALIDATION = {
659
+ minimumLength: 0,
660
+ maximumLength: 1,
661
+ component: () => i18nextGS1.t("IdentificationKey.indicatorDigit")
662
+ };
663
+ /**
664
+ * Validation parameters for zero-suppressed GTIN-12.
657
665
  */
658
666
  static ZERO_SUPPRESSED_GTIN12_VALIDATION = {
659
667
  minimumLength: 8,
@@ -694,6 +702,35 @@ var GTINValidator = class _GTINValidator extends AbstractNumericIdentificationKe
694
702
  validatePrefix(partialIdentificationKey, positionOffset) {
695
703
  PrefixManager.validatePrefix(this.prefixType, false, false, partialIdentificationKey, true, true, positionOffset);
696
704
  }
705
+ /**
706
+ * Zero suppress a GTIN-12.
707
+ *
708
+ * @param gtin12
709
+ * GTIN-12.
710
+ *
711
+ * @returns
712
+ * Zero-suppressed GTIN-12.
713
+ */
714
+ static zeroSuppress(gtin12) {
715
+ GTIN12_VALIDATOR.validate(gtin12);
716
+ const d = Array.from(gtin12);
717
+ let zeroSuppressedGTIN12;
718
+ if (d[0] === "0" && d[6] === "0" && d[7] === "0") {
719
+ if (d[10] >= "5" && d[8] === "0" && d[9] === "0" && d[5] !== "0") {
720
+ zeroSuppressedGTIN12 = `0${d[1]}${d[2]}${d[3]}${d[4]}${d[5]}${d[10]}${d[11]}`;
721
+ } else if (d[5] === "0" && d[8] === "0" && d[9] === "0" && d[4] !== "0") {
722
+ zeroSuppressedGTIN12 = `0${d[1]}${d[2]}${d[3]}${d[4]}${d[10]}4${d[11]}`;
723
+ } else if (d[3] <= "2" && d[4] === "0" && d[5] === "0") {
724
+ zeroSuppressedGTIN12 = `0${d[1]}${d[2]}${d[8]}${d[9]}${d[10]}${d[3]}${d[11]}`;
725
+ } else if (d[3] >= "3" && d[4] === "0" && d[5] === "0" && d[8] === "0") {
726
+ zeroSuppressedGTIN12 = `0${d[1]}${d[2]}${d[3]}${d[9]}${d[10]}3${d[11]}`;
727
+ }
728
+ }
729
+ if (zeroSuppressedGTIN12 === void 0) {
730
+ throw new RangeError(i18nextGS1.t("IdentificationKey.invalidZeroSuppressibleGTIN12"));
731
+ }
732
+ return zeroSuppressedGTIN12;
733
+ }
697
734
  /**
698
735
  * Zero expand a zero-suppressed GTIN-12.
699
736
  *
@@ -724,6 +761,86 @@ var GTINValidator = class _GTINValidator extends AbstractNumericIdentificationKe
724
761
  GTIN12_VALIDATOR.validate(gtin12);
725
762
  return gtin12;
726
763
  }
764
+ /**
765
+ * Convert a GTIN of any length to a GTIN-14 with an optional indicator digit.
766
+ *
767
+ * @param indicatorDigit
768
+ * Indicator digit. If blank, assumes "0" if the GTIN is not already a GTIN-14.
769
+ *
770
+ * @param gtin
771
+ * GTIN.
772
+ *
773
+ * @returns
774
+ * GTIN-14.
775
+ */
776
+ static convertToGTIN14(indicatorDigit, gtin) {
777
+ GTINCreator.validateAny(gtin);
778
+ NUMERIC_CREATOR2.validate(indicatorDigit, _GTINValidator.OPTIONAL_INDICATOR_DIGIT_VALIDATION);
779
+ const gtinLength = gtin.length;
780
+ let gtin14 = "0".repeat(14 /* GTIN14 */ - gtinLength) + gtin;
781
+ if (indicatorDigit.length !== 0 && indicatorDigit !== gtin14.charAt(0)) {
782
+ const partialGTIN14 = indicatorDigit + gtin14.substring(1, 14 /* GTIN14 */ - 1);
783
+ gtin14 = partialGTIN14 + checkDigit(partialGTIN14);
784
+ }
785
+ return gtin14;
786
+ }
787
+ /**
788
+ * Normalize a GTIN of any length.
789
+ * - A GTIN-14 that starts with six zeros or a GTIN-13 that starts with five zeros is normalized to GTIN-8.
790
+ * - A GTIN-14 that starts with two zeros or a GTIN-13 that starts with one zero is normalized to GTIN-12.
791
+ * - A GTIN-14 that starts with one zero is normalized to GTIN-13.
792
+ * - Otherwise, the GTIN is unchanged.
793
+ *
794
+ * @param gtin
795
+ * GTIN.
796
+ *
797
+ * @returns
798
+ * Normalized GTIN.
799
+ */
800
+ static normalize(gtin) {
801
+ const gtinLength = gtin.length;
802
+ let normalizedGTIN;
803
+ switch (gtinLength) {
804
+ case 13 /* GTIN13 */:
805
+ if (!gtin.startsWith("0")) {
806
+ normalizedGTIN = gtin;
807
+ } else if (!gtin.startsWith("00000")) {
808
+ normalizedGTIN = gtin.substring(1);
809
+ } else if (!gtin.startsWith("000000")) {
810
+ normalizedGTIN = gtin.substring(5);
811
+ } else {
812
+ throw new RangeError(i18nextGS1.t("IdentificationKey.invalidZeroSuppressedGTIN12AsGTIN13"));
813
+ }
814
+ break;
815
+ case 12 /* GTIN12 */:
816
+ normalizedGTIN = gtin;
817
+ break;
818
+ case 8 /* GTIN8 */:
819
+ if (!gtin.startsWith("0")) {
820
+ normalizedGTIN = gtin;
821
+ } else {
822
+ normalizedGTIN = _GTINValidator.zeroExpand(gtin);
823
+ }
824
+ break;
825
+ case 14 /* GTIN14 */:
826
+ if (!gtin.startsWith("0")) {
827
+ normalizedGTIN = gtin;
828
+ } else if (!gtin.startsWith("00")) {
829
+ normalizedGTIN = gtin.substring(1);
830
+ } else if (!gtin.startsWith("000000")) {
831
+ normalizedGTIN = gtin.substring(2);
832
+ } else if (!gtin.startsWith("0000000")) {
833
+ normalizedGTIN = gtin.substring(6);
834
+ } else {
835
+ throw new RangeError(i18nextGS1.t("IdentificationKey.invalidZeroSuppressedGTIN12AsGTIN14"));
836
+ }
837
+ break;
838
+ default:
839
+ throw new RangeError(i18nextGS1.t("IdentificationKey.invalidGTINLength"));
840
+ }
841
+ GTINCreator.validateAny(normalizedGTIN);
842
+ return normalizedGTIN;
843
+ }
727
844
  /**
728
845
  * Validate any GTIN, optionally against a level.
729
846
  *
@@ -1133,14 +1250,6 @@ var GTINCreator = class _GTINCreator extends Mixin(GTINValidator, AbstractNumeri
1133
1250
  maximumLength: 1,
1134
1251
  component: () => i18nextGS1.t("IdentificationKey.indicatorDigit")
1135
1252
  };
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
1253
  /**
1145
1254
  * Constructor. Called internally by {@link PrefixManager.gtinCreator}; should not be called by other code.
1146
1255
  *
@@ -1183,115 +1292,6 @@ var GTINCreator = class _GTINCreator extends Mixin(GTINValidator, AbstractNumeri
1183
1292
  return partialIdentificationKey + checkDigit(partialIdentificationKey);
1184
1293
  });
1185
1294
  }
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
1295
  };
1296
1296
  var NonGTINNumericIdentificationKeyCreator = class extends Mixin(NonGTINNumericIdentificationKeyValidator, AbstractNumericIdentificationKeyCreator) {
1297
1297
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aidc-toolkit/gs1",
3
- "version": "0.9.10-beta",
3
+ "version": "0.9.11-beta",
4
4
  "description": "GS1 AIDC Toolkit",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -25,16 +25,16 @@
25
25
  "test": "vitest run"
26
26
  },
27
27
  "devDependencies": {
28
- "@aidc-toolkit/dev": "^0.9.10-beta",
29
- "eslint": "^9.17.0",
28
+ "@aidc-toolkit/dev": "^0.9.11-beta",
29
+ "eslint": "^9.18.0",
30
30
  "ts-node": "^10.9.2",
31
31
  "tsup": "^8.3.5",
32
- "typescript": "^5.7.2",
32
+ "typescript": "^5.7.3",
33
33
  "vitest": "^2.1.8"
34
34
  },
35
35
  "dependencies": {
36
- "@aidc-toolkit/core": "^0.9.10-beta",
37
- "@aidc-toolkit/utility": "^0.9.10-beta",
36
+ "@aidc-toolkit/core": "^0.9.11-beta",
37
+ "@aidc-toolkit/utility": "^0.9.11-beta",
38
38
  "i18next": "^24.2.1",
39
39
  "ts-mixer": "^6.0.4"
40
40
  }