@aidc-toolkit/gs1 0.9.7-beta → 0.9.9-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/LICENSE +0 -27
- package/README.md +14 -0
- package/dist/index.cjs +143 -131
- package/dist/index.d.cts +145 -6
- package/dist/index.d.ts +145 -6
- package/dist/index.js +104 -105
- package/package.json +9 -8
- package/src/check.ts +24 -9
- package/src/idkey.ts +53 -99
- package/src/index.ts +18 -2
- 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 +40 -6
- package/src/locale/i18next.d.ts +5 -3
- package/test/check.test.ts +9 -8
- package/test/idkey.test.ts +17 -16
- /package/src/{character_set.ts → character-set.ts} +0 -0
package/dist/index.js
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
// src/locale/i18n.ts
|
|
2
|
-
import {
|
|
2
|
+
import { i18nAssertValidResources, i18nCoreInit } from "@aidc-toolkit/core";
|
|
3
|
+
import { i18nUtilityInit, utilityResources } from "@aidc-toolkit/utility";
|
|
4
|
+
import i18next from "i18next";
|
|
3
5
|
|
|
4
|
-
// src/locale/en/
|
|
6
|
+
// src/locale/en/locale-strings.ts
|
|
5
7
|
var localeStrings = {
|
|
6
8
|
Check: {
|
|
9
|
+
lengthOfStringForPriceOrWeightMustBeExactly: "Length {{length}} of string for price or weight sum must be exactly {{exactLength}}",
|
|
10
|
+
priceOrWeightComponent: "price or weight",
|
|
7
11
|
lengthOfStringForCheckCharacterPairMustBeLessThanOrEqualTo: "Length {{length}} of string for check character pair must be less than or equal to {{maximumLength}}"
|
|
8
12
|
},
|
|
9
13
|
IdentificationKey: {
|
|
@@ -37,9 +41,11 @@ var localeStrings = {
|
|
|
37
41
|
}
|
|
38
42
|
};
|
|
39
43
|
|
|
40
|
-
// src/locale/fr/
|
|
44
|
+
// src/locale/fr/locale-strings.ts
|
|
41
45
|
var localeStrings2 = {
|
|
42
46
|
Check: {
|
|
47
|
+
lengthOfStringForPriceOrWeightMustBeExactly: "La longueur {{longueur}} de la cha\xEEne pour le prix ou la somme du poids doit \xEAtre exactement {{exactLength}}",
|
|
48
|
+
priceOrWeightComponent: "prix ou poids",
|
|
43
49
|
lengthOfStringForCheckCharacterPairMustBeLessThanOrEqualTo: "La longueur {{length}} de la cha\xEEne pour la paire de caract\xE8res de v\xE9rification doit \xEAtre inf\xE9rieure ou \xE9gale \xE0 {{maximum Length}}"
|
|
44
50
|
},
|
|
45
51
|
IdentificationKey: {
|
|
@@ -76,11 +82,21 @@ var localeStrings2 = {
|
|
|
76
82
|
// src/locale/i18n.ts
|
|
77
83
|
var gs1NS = "aidct_gs1";
|
|
78
84
|
i18nAssertValidResources(localeStrings, "fr", localeStrings2);
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
85
|
+
var gs1Resources = {
|
|
86
|
+
en: {
|
|
87
|
+
aidct_gs1: localeStrings
|
|
88
|
+
},
|
|
89
|
+
fr: {
|
|
90
|
+
aidct_gs1: localeStrings2
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
var i18nextGS1 = i18next.createInstance();
|
|
94
|
+
async function i18nGS1Init(environment, debug = false) {
|
|
95
|
+
await i18nUtilityInit(environment, debug);
|
|
96
|
+
await i18nCoreInit(i18nextGS1, environment, debug, gs1NS, utilityResources, gs1Resources);
|
|
97
|
+
}
|
|
82
98
|
|
|
83
|
-
// src/
|
|
99
|
+
// src/character-set.ts
|
|
84
100
|
import { CharacterSetCreator, Exclusion } from "@aidc-toolkit/utility";
|
|
85
101
|
var AI82_CREATOR = new CharacterSetCreator([
|
|
86
102
|
"!",
|
|
@@ -209,7 +225,7 @@ var AI39_CREATOR = new CharacterSetCreator([
|
|
|
209
225
|
], Exclusion.AllNumeric);
|
|
210
226
|
|
|
211
227
|
// src/check.ts
|
|
212
|
-
import { NUMERIC_CREATOR } from "@aidc-toolkit/utility";
|
|
228
|
+
import { NUMERIC_CREATOR, utilityNS } from "@aidc-toolkit/utility";
|
|
213
229
|
var THREE_WEIGHT_RESULTS = [
|
|
214
230
|
0,
|
|
215
231
|
3,
|
|
@@ -274,7 +290,11 @@ function checkDigitSum(exchangeWeights, s) {
|
|
|
274
290
|
let weight3 = (s.length + Number(exchangeWeights)) % 2 === 0;
|
|
275
291
|
return NUMERIC_CREATOR.characterIndexes(s).reduce((accumulator, characterIndex, index) => {
|
|
276
292
|
if (characterIndex === void 0) {
|
|
277
|
-
throw new RangeError(
|
|
293
|
+
throw new RangeError(i18nextGS1.t("CharacterSetValidator.invalidCharacterAtPosition", {
|
|
294
|
+
ns: utilityNS,
|
|
295
|
+
c: s.charAt(index),
|
|
296
|
+
position: index + 1
|
|
297
|
+
}));
|
|
278
298
|
}
|
|
279
299
|
weight3 = !weight3;
|
|
280
300
|
return accumulator + (weight3 ? THREE_WEIGHT_RESULTS[characterIndex] : characterIndex);
|
|
@@ -288,12 +308,20 @@ function hasValidCheckDigit(s) {
|
|
|
288
308
|
}
|
|
289
309
|
function priceWeightSum(weightsResults, s) {
|
|
290
310
|
if (s.length !== weightsResults.length) {
|
|
291
|
-
throw new RangeError(
|
|
311
|
+
throw new RangeError(i18nextGS1.t("Check.lengthOfStringForPriceOrWeightMustBeExactly", {
|
|
312
|
+
length: s.length,
|
|
313
|
+
exactLength: weightsResults.length
|
|
314
|
+
}));
|
|
292
315
|
}
|
|
293
316
|
const characterIndexes = NUMERIC_CREATOR.characterIndexes(s);
|
|
294
317
|
return characterIndexes.reduce((accumulator, characterIndex, index) => {
|
|
295
318
|
if (characterIndex === void 0) {
|
|
296
|
-
throw new RangeError(
|
|
319
|
+
throw new RangeError(i18nextGS1.t("CharacterSetValidator.invalidCharacterAtPositionOfComponent", {
|
|
320
|
+
ns: utilityNS,
|
|
321
|
+
c: s.charAt(index),
|
|
322
|
+
position: index + 1,
|
|
323
|
+
component: i18nextGS1.t("Check.priceOrWeightComponent")
|
|
324
|
+
}));
|
|
297
325
|
}
|
|
298
326
|
return accumulator + weightsResults[index][characterIndex];
|
|
299
327
|
}, 0);
|
|
@@ -371,15 +399,18 @@ var CHECK_CHARACTERS = [
|
|
|
371
399
|
function checkCharacterPair(s) {
|
|
372
400
|
const weightIndexStart = CHECK_CHARACTER_WEIGHTS.length - s.length;
|
|
373
401
|
if (weightIndexStart < 0) {
|
|
374
|
-
throw new RangeError(
|
|
375
|
-
ns: gs1NS,
|
|
402
|
+
throw new RangeError(i18nextGS1.t("Check.lengthOfStringForCheckCharacterPairMustBeLessThanOrEqualTo", {
|
|
376
403
|
length: s.length,
|
|
377
404
|
maximumLength: CHECK_CHARACTER_WEIGHTS.length
|
|
378
405
|
}));
|
|
379
406
|
}
|
|
380
407
|
const checkCharacterPairSum = AI82_CREATOR.characterIndexes(s).reduce((accumulator, characterIndex, index) => {
|
|
381
408
|
if (characterIndex === void 0) {
|
|
382
|
-
throw new RangeError(
|
|
409
|
+
throw new RangeError(i18nextGS1.t("CharacterSetValidator.invalidCharacterAtPosition", {
|
|
410
|
+
ns: utilityNS,
|
|
411
|
+
c: s.charAt(index),
|
|
412
|
+
position: index + 1
|
|
413
|
+
}));
|
|
383
414
|
}
|
|
384
415
|
return accumulator + characterIndex * CHECK_CHARACTER_WEIGHTS[weightIndexStart + index];
|
|
385
416
|
}, 0) % 1021;
|
|
@@ -395,9 +426,9 @@ function hasValidCheckCharacterPair(s) {
|
|
|
395
426
|
import {
|
|
396
427
|
CharacterSetCreator as CharacterSetCreator2,
|
|
397
428
|
Exclusion as Exclusion2,
|
|
398
|
-
IteratorProxy,
|
|
399
429
|
NUMERIC_CREATOR as NUMERIC_CREATOR2,
|
|
400
|
-
RegExpValidator
|
|
430
|
+
RegExpValidator,
|
|
431
|
+
transformIterable
|
|
401
432
|
} from "@aidc-toolkit/utility";
|
|
402
433
|
import { Mixin } from "ts-mixer";
|
|
403
434
|
var IdentificationKeyType = /* @__PURE__ */ ((IdentificationKeyType2) => {
|
|
@@ -597,16 +628,13 @@ var AbstractNumericIdentificationKeyValidator = class extends AbstractIdentifica
|
|
|
597
628
|
super.validatePrefix(identificationKey.substring(this._prefixPosition), validation?.positionOffset === void 0 ? this._prefixPosition : validation.positionOffset + this._prefixPosition);
|
|
598
629
|
}
|
|
599
630
|
if (identificationKey.length !== this.length) {
|
|
600
|
-
throw new RangeError(
|
|
601
|
-
ns: gs1NS,
|
|
631
|
+
throw new RangeError(i18nextGS1.t("IdentificationKey.identificationKeyTypeLength", {
|
|
602
632
|
identificationKeyType: this.identificationKeyType,
|
|
603
633
|
length: this.length
|
|
604
634
|
}));
|
|
605
635
|
}
|
|
606
636
|
if (!hasValidCheckDigit(this.padIdentificationKey(identificationKey, validation))) {
|
|
607
|
-
throw new RangeError(
|
|
608
|
-
ns: gs1NS
|
|
609
|
-
}));
|
|
637
|
+
throw new RangeError(i18nextGS1.t("IdentificationKey.invalidCheckDigit"));
|
|
610
638
|
}
|
|
611
639
|
}
|
|
612
640
|
};
|
|
@@ -691,9 +719,7 @@ var GTINValidator = class _GTINValidator extends AbstractNumericIdentificationKe
|
|
|
691
719
|
}
|
|
692
720
|
}
|
|
693
721
|
if (gtin12 === void 0) {
|
|
694
|
-
throw new RangeError(
|
|
695
|
-
ns: gs1NS
|
|
696
|
-
}));
|
|
722
|
+
throw new RangeError(i18nextGS1.t("IdentificationKey.invalidZeroSuppressedGTIN12"));
|
|
697
723
|
}
|
|
698
724
|
GTIN12_VALIDATOR.validate(gtin12);
|
|
699
725
|
return gtin12;
|
|
@@ -713,9 +739,7 @@ var GTINValidator = class _GTINValidator extends AbstractNumericIdentificationKe
|
|
|
713
739
|
switch (gtin.length) {
|
|
714
740
|
case 13 /* GTIN13 */:
|
|
715
741
|
if (gtin.startsWith("0")) {
|
|
716
|
-
throw new RangeError(
|
|
717
|
-
ns: gs1NS
|
|
718
|
-
}));
|
|
742
|
+
throw new RangeError(i18nextGS1.t("IdentificationKey.invalidGTIN13AtRetail"));
|
|
719
743
|
}
|
|
720
744
|
PrefixManager.validatePrefix(0 /* GS1CompanyPrefix */, false, false, gtin, true, true);
|
|
721
745
|
gtinLevelRestriction = 0 /* Any */;
|
|
@@ -737,19 +761,13 @@ var GTINValidator = class _GTINValidator extends AbstractNumericIdentificationKe
|
|
|
737
761
|
gtinLevelRestriction = 2 /* OtherThanRetailConsumer */;
|
|
738
762
|
break;
|
|
739
763
|
default:
|
|
740
|
-
throw new RangeError(
|
|
741
|
-
ns: gs1NS
|
|
742
|
-
}));
|
|
764
|
+
throw new RangeError(i18nextGS1.t("IdentificationKey.invalidGTINLength"));
|
|
743
765
|
}
|
|
744
766
|
if (!hasValidCheckDigit(lengthValidatedGTIN)) {
|
|
745
|
-
throw new RangeError(
|
|
746
|
-
ns: gs1NS
|
|
747
|
-
}));
|
|
767
|
+
throw new RangeError(i18nextGS1.t("IdentificationKey.invalidCheckDigit"));
|
|
748
768
|
}
|
|
749
769
|
if (gtinLevel !== 0 /* Any */ && gtinLevelRestriction !== 0 /* Any */ && gtinLevelRestriction !== gtinLevel) {
|
|
750
|
-
throw new RangeError(
|
|
751
|
-
ns: gs1NS
|
|
752
|
-
}));
|
|
770
|
+
throw new RangeError(i18nextGS1.t(gtinLevel === 1 /* RetailConsumer */ ? "IdentificationKey.invalidGTINAtRetail" : "IdentificationKey.invalidGTINAtOtherThanRetail"));
|
|
753
771
|
}
|
|
754
772
|
}
|
|
755
773
|
/**
|
|
@@ -760,9 +778,7 @@ var GTINValidator = class _GTINValidator extends AbstractNumericIdentificationKe
|
|
|
760
778
|
*/
|
|
761
779
|
static validateGTIN14(gtin14) {
|
|
762
780
|
if (gtin14.length !== 14 /* GTIN14 */) {
|
|
763
|
-
throw new RangeError(
|
|
764
|
-
ns: gs1NS
|
|
765
|
-
}));
|
|
781
|
+
throw new RangeError(i18nextGS1.t("IdentificationKey.invalidGTIN14Length"));
|
|
766
782
|
}
|
|
767
783
|
GTINCreator.validateAny(gtin14);
|
|
768
784
|
}
|
|
@@ -823,9 +839,7 @@ var SerializableNumericIdentificationKeyValidator = class _SerializableNumericId
|
|
|
823
839
|
this._serialComponentValidation = {
|
|
824
840
|
minimumLength: 1,
|
|
825
841
|
maximumLength: serialComponentLength,
|
|
826
|
-
component: () =>
|
|
827
|
-
ns: gs1NS
|
|
828
|
-
})
|
|
842
|
+
component: () => i18nextGS1.t("IdentificationKey.serialComponent")
|
|
829
843
|
};
|
|
830
844
|
this._serialComponentCreator = _SerializableNumericIdentificationKeyValidator.creatorFor(serialComponentCharacterSet);
|
|
831
845
|
}
|
|
@@ -872,9 +886,7 @@ var NonNumericIdentificationKeyValidator = class _NonNumericIdentificationKeyVal
|
|
|
872
886
|
* @inheritDoc
|
|
873
887
|
*/
|
|
874
888
|
createErrorMessage(_s) {
|
|
875
|
-
return
|
|
876
|
-
ns: gs1NS
|
|
877
|
-
});
|
|
889
|
+
return i18nextGS1.t("IdentificationKey.referenceCantBeAllNumeric");
|
|
878
890
|
}
|
|
879
891
|
}(/\D/);
|
|
880
892
|
/**
|
|
@@ -924,9 +936,7 @@ var NonNumericIdentificationKeyValidator = class _NonNumericIdentificationKeyVal
|
|
|
924
936
|
positionOffset: validation?.positionOffset
|
|
925
937
|
});
|
|
926
938
|
} else if (!hasValidCheckCharacterPair(this.padIdentificationKey(identificationKey, validation))) {
|
|
927
|
-
throw new RangeError(
|
|
928
|
-
ns: gs1NS
|
|
929
|
-
}));
|
|
939
|
+
throw new RangeError(i18nextGS1.t("IdentificationKey.invalidCheckCharacterPair"));
|
|
930
940
|
}
|
|
931
941
|
if (validation?.exclusion === Exclusion2.AllNumeric) {
|
|
932
942
|
_NonNumericIdentificationKeyValidator.NOT_ALL_NUMERIC_VALIDATOR.validate(partialIdentificationKey);
|
|
@@ -1104,9 +1114,14 @@ var AbstractNumericIdentificationKeyCreator = class _AbstractNumericIdentificati
|
|
|
1104
1114
|
createAll() {
|
|
1105
1115
|
const hasExtensionDigit = this.leaderType === 2 /* ExtensionDigit */;
|
|
1106
1116
|
const prefix = this.prefix;
|
|
1117
|
+
const length = this.length;
|
|
1107
1118
|
const referenceLength = this.referenceLength;
|
|
1108
1119
|
const startWeight = 3 - 2 * ((referenceLength + 1 - Number(hasExtensionDigit)) % 2);
|
|
1109
|
-
return
|
|
1120
|
+
return {
|
|
1121
|
+
[Symbol.iterator]() {
|
|
1122
|
+
return _AbstractNumericIdentificationKeyCreator.createAllPartial(prefix, referenceLength, hasExtensionDigit ? 3 - 2 * length % 2 : 0, startWeight, checkDigitSum(startWeight === 3, prefix));
|
|
1123
|
+
}
|
|
1124
|
+
};
|
|
1110
1125
|
}
|
|
1111
1126
|
};
|
|
1112
1127
|
var GTINCreator = class _GTINCreator extends Mixin(GTINValidator, AbstractNumericIdentificationKeyCreator) {
|
|
@@ -1116,9 +1131,7 @@ var GTINCreator = class _GTINCreator extends Mixin(GTINValidator, AbstractNumeri
|
|
|
1116
1131
|
static REQUIRED_INDICATOR_DIGIT_VALIDATION = {
|
|
1117
1132
|
minimumLength: 1,
|
|
1118
1133
|
maximumLength: 1,
|
|
1119
|
-
component: () =>
|
|
1120
|
-
ns: gs1NS
|
|
1121
|
-
})
|
|
1134
|
+
component: () => i18nextGS1.t("IdentificationKey.indicatorDigit")
|
|
1122
1135
|
};
|
|
1123
1136
|
/**
|
|
1124
1137
|
* Validation parameters for optional indicator digit.
|
|
@@ -1126,9 +1139,7 @@ var GTINCreator = class _GTINCreator extends Mixin(GTINValidator, AbstractNumeri
|
|
|
1126
1139
|
static OPTIONAL_INDICATOR_DIGIT_VALIDATION = {
|
|
1127
1140
|
minimumLength: 0,
|
|
1128
1141
|
maximumLength: 1,
|
|
1129
|
-
component: () =>
|
|
1130
|
-
ns: gs1NS
|
|
1131
|
-
})
|
|
1142
|
+
component: () => i18nextGS1.t("IdentificationKey.indicatorDigit")
|
|
1132
1143
|
};
|
|
1133
1144
|
/**
|
|
1134
1145
|
* Constructor. Called internally by {@link PrefixManager.gtinCreator}; should not be called by other code.
|
|
@@ -1197,9 +1208,7 @@ var GTINCreator = class _GTINCreator extends Mixin(GTINValidator, AbstractNumeri
|
|
|
1197
1208
|
}
|
|
1198
1209
|
}
|
|
1199
1210
|
if (zeroSuppressedGTIN12 === void 0) {
|
|
1200
|
-
throw new RangeError(
|
|
1201
|
-
ns: gs1NS
|
|
1202
|
-
}));
|
|
1211
|
+
throw new RangeError(i18nextGS1.t("IdentificationKey.invalidZeroSuppressibleGTIN12"));
|
|
1203
1212
|
}
|
|
1204
1213
|
return zeroSuppressedGTIN12;
|
|
1205
1214
|
}
|
|
@@ -1251,9 +1260,7 @@ var GTINCreator = class _GTINCreator extends Mixin(GTINValidator, AbstractNumeri
|
|
|
1251
1260
|
} else if (!gtin.startsWith("000000")) {
|
|
1252
1261
|
normalizedGTIN = gtin.substring(5);
|
|
1253
1262
|
} else {
|
|
1254
|
-
throw new RangeError(
|
|
1255
|
-
ns: gs1NS
|
|
1256
|
-
}));
|
|
1263
|
+
throw new RangeError(i18nextGS1.t("IdentificationKey.invalidZeroSuppressedGTIN12AsGTIN13"));
|
|
1257
1264
|
}
|
|
1258
1265
|
break;
|
|
1259
1266
|
case 12 /* GTIN12 */:
|
|
@@ -1276,15 +1283,11 @@ var GTINCreator = class _GTINCreator extends Mixin(GTINValidator, AbstractNumeri
|
|
|
1276
1283
|
} else if (!gtin.startsWith("0000000")) {
|
|
1277
1284
|
normalizedGTIN = gtin.substring(6);
|
|
1278
1285
|
} else {
|
|
1279
|
-
throw new RangeError(
|
|
1280
|
-
ns: gs1NS
|
|
1281
|
-
}));
|
|
1286
|
+
throw new RangeError(i18nextGS1.t("IdentificationKey.invalidZeroSuppressedGTIN12AsGTIN14"));
|
|
1282
1287
|
}
|
|
1283
1288
|
break;
|
|
1284
1289
|
default:
|
|
1285
|
-
throw new RangeError(
|
|
1286
|
-
ns: gs1NS
|
|
1287
|
-
}));
|
|
1290
|
+
throw new RangeError(i18nextGS1.t("IdentificationKey.invalidGTINLength"));
|
|
1288
1291
|
}
|
|
1289
1292
|
_GTINCreator.validateAny(normalizedGTIN);
|
|
1290
1293
|
return normalizedGTIN;
|
|
@@ -1359,7 +1362,7 @@ var SerializableNumericIdentificationKeyCreator = class extends Mixin(Serializab
|
|
|
1359
1362
|
if (typeof serialComponentOrComponents !== "object") {
|
|
1360
1363
|
result = validateAndConcatenate(serialComponentOrComponents);
|
|
1361
1364
|
} else {
|
|
1362
|
-
result =
|
|
1365
|
+
result = transformIterable(serialComponentOrComponents, validateAndConcatenate);
|
|
1363
1366
|
}
|
|
1364
1367
|
return result;
|
|
1365
1368
|
}
|
|
@@ -1430,9 +1433,7 @@ var NonNumericIdentificationKeyCreator = class extends Mixin(NonNumericIdentific
|
|
|
1430
1433
|
minimumLength: 1,
|
|
1431
1434
|
// Maximum reference length has to account for prefix and check character pair.
|
|
1432
1435
|
maximumLength: this.referenceLength,
|
|
1433
|
-
component: () =>
|
|
1434
|
-
ns: gs1NS
|
|
1435
|
-
})
|
|
1436
|
+
component: () => i18nextGS1.t("IdentificationKey.reference")
|
|
1436
1437
|
};
|
|
1437
1438
|
}
|
|
1438
1439
|
/**
|
|
@@ -1464,7 +1465,7 @@ var NonNumericIdentificationKeyCreator = class extends Mixin(NonNumericIdentific
|
|
|
1464
1465
|
if (typeof referenceOrReferences !== "object") {
|
|
1465
1466
|
result = validateAndCreate(referenceOrReferences);
|
|
1466
1467
|
} else {
|
|
1467
|
-
result =
|
|
1468
|
+
result = transformIterable(referenceOrReferences, validateAndCreate);
|
|
1468
1469
|
}
|
|
1469
1470
|
return result;
|
|
1470
1471
|
}
|
|
@@ -1504,9 +1505,7 @@ var PrefixManager = class _PrefixManager {
|
|
|
1504
1505
|
static GS1_COMPANY_PREFIX_VALIDATION = {
|
|
1505
1506
|
minimumLength: _PrefixManager.GS1_COMPANY_PREFIX_MINIMUM_LENGTH,
|
|
1506
1507
|
maximumLength: _PrefixManager.GS1_COMPANY_PREFIX_MAXIMUM_LENGTH,
|
|
1507
|
-
component: () =>
|
|
1508
|
-
ns: gs1NS
|
|
1509
|
-
})
|
|
1508
|
+
component: () => i18nextGS1.t("Prefix.gs1CompanyPrefix")
|
|
1510
1509
|
};
|
|
1511
1510
|
/**
|
|
1512
1511
|
* Validation parameters for U.P.C. Company Prefix expressed as GS1 Company Prefix.
|
|
@@ -1514,9 +1513,7 @@ var PrefixManager = class _PrefixManager {
|
|
|
1514
1513
|
static UPC_COMPANY_PREFIX_AS_GS1_COMPANY_PREFIX_VALIDATION = {
|
|
1515
1514
|
minimumLength: _PrefixManager.UPC_COMPANY_PREFIX_MINIMUM_LENGTH + 1,
|
|
1516
1515
|
maximumLength: _PrefixManager.UPC_COMPANY_PREFIX_MAXIMUM_LENGTH + 1,
|
|
1517
|
-
component: () =>
|
|
1518
|
-
ns: gs1NS
|
|
1519
|
-
})
|
|
1516
|
+
component: () => i18nextGS1.t("Prefix.gs1CompanyPrefix")
|
|
1520
1517
|
};
|
|
1521
1518
|
/**
|
|
1522
1519
|
* Validation parameters for GS1-8 Prefix expressed as GS1 Company Prefix.
|
|
@@ -1524,9 +1521,7 @@ var PrefixManager = class _PrefixManager {
|
|
|
1524
1521
|
static GS1_8_PREFIX_AS_GS1_COMPANY_PREFIX_VALIDATION = {
|
|
1525
1522
|
minimumLength: _PrefixManager.GS1_8_PREFIX_MINIMUM_LENGTH + 5,
|
|
1526
1523
|
maximumLength: _PrefixManager.GS1_8_PREFIX_MAXIMUM_LENGTH + 5,
|
|
1527
|
-
component: () =>
|
|
1528
|
-
ns: gs1NS
|
|
1529
|
-
})
|
|
1524
|
+
component: () => i18nextGS1.t("Prefix.gs1CompanyPrefix")
|
|
1530
1525
|
};
|
|
1531
1526
|
/**
|
|
1532
1527
|
* Validation parameters for U.P.C. Company Prefix.
|
|
@@ -1534,9 +1529,7 @@ var PrefixManager = class _PrefixManager {
|
|
|
1534
1529
|
static UPC_COMPANY_PREFIX_VALIDATION = {
|
|
1535
1530
|
minimumLength: _PrefixManager.UPC_COMPANY_PREFIX_MINIMUM_LENGTH,
|
|
1536
1531
|
maximumLength: _PrefixManager.UPC_COMPANY_PREFIX_MAXIMUM_LENGTH,
|
|
1537
|
-
component: () =>
|
|
1538
|
-
ns: gs1NS
|
|
1539
|
-
})
|
|
1532
|
+
component: () => i18nextGS1.t("Prefix.upcCompanyPrefix")
|
|
1540
1533
|
};
|
|
1541
1534
|
/**
|
|
1542
1535
|
* Validation parameters for GS1-8 Prefix.
|
|
@@ -1544,9 +1537,7 @@ var PrefixManager = class _PrefixManager {
|
|
|
1544
1537
|
static GS1_8_PREFIX_VALIDATION = {
|
|
1545
1538
|
minimumLength: _PrefixManager.GS1_8_PREFIX_MINIMUM_LENGTH,
|
|
1546
1539
|
maximumLength: _PrefixManager.GS1_8_PREFIX_MAXIMUM_LENGTH,
|
|
1547
|
-
component: () =>
|
|
1548
|
-
ns: gs1NS
|
|
1549
|
-
})
|
|
1540
|
+
component: () => i18nextGS1.t("Prefix.gs18Prefix")
|
|
1550
1541
|
};
|
|
1551
1542
|
/**
|
|
1552
1543
|
* Creator tweak factors. Different numeric identification key types have different tweak factors so that sparse
|
|
@@ -1746,37 +1737,27 @@ var PrefixManager = class _PrefixManager {
|
|
|
1746
1737
|
baseValidation = _PrefixManager.GS1_COMPANY_PREFIX_VALIDATION;
|
|
1747
1738
|
} else if (!prefix.startsWith("00000")) {
|
|
1748
1739
|
if (!allowUPCCompanyPrefix) {
|
|
1749
|
-
throw new RangeError(
|
|
1750
|
-
ns: gs1NS
|
|
1751
|
-
}));
|
|
1740
|
+
throw new RangeError(i18nextGS1.t("Prefix.gs1CompanyPrefixCantStartWith0"));
|
|
1752
1741
|
}
|
|
1753
1742
|
baseValidation = _PrefixManager.UPC_COMPANY_PREFIX_AS_GS1_COMPANY_PREFIX_VALIDATION;
|
|
1754
1743
|
} else if (!prefix.startsWith("000000")) {
|
|
1755
1744
|
if (!allowGS18Prefix) {
|
|
1756
|
-
throw new RangeError(
|
|
1757
|
-
ns: gs1NS
|
|
1758
|
-
}));
|
|
1745
|
+
throw new RangeError(i18nextGS1.t("Prefix.gs1CompanyPrefixCantStartWith00000"));
|
|
1759
1746
|
}
|
|
1760
1747
|
baseValidation = _PrefixManager.GS1_8_PREFIX_AS_GS1_COMPANY_PREFIX_VALIDATION;
|
|
1761
1748
|
} else {
|
|
1762
|
-
throw new RangeError(
|
|
1763
|
-
ns: gs1NS
|
|
1764
|
-
}));
|
|
1749
|
+
throw new RangeError(i18nextGS1.t("Prefix.gs1CompanyPrefixCantStartWith000000"));
|
|
1765
1750
|
}
|
|
1766
1751
|
break;
|
|
1767
1752
|
case 1 /* UPCCompanyPrefix */:
|
|
1768
1753
|
if (prefix.startsWith("0000")) {
|
|
1769
|
-
throw new RangeError(
|
|
1770
|
-
ns: gs1NS
|
|
1771
|
-
}));
|
|
1754
|
+
throw new RangeError(i18nextGS1.t("Prefix.upcCompanyPrefixCantStartWith0000"));
|
|
1772
1755
|
}
|
|
1773
1756
|
baseValidation = _PrefixManager.UPC_COMPANY_PREFIX_VALIDATION;
|
|
1774
1757
|
break;
|
|
1775
1758
|
case 2 /* GS18Prefix */:
|
|
1776
1759
|
if (prefix.startsWith("0")) {
|
|
1777
|
-
throw new RangeError(
|
|
1778
|
-
ns: gs1NS
|
|
1779
|
-
}));
|
|
1760
|
+
throw new RangeError(i18nextGS1.t("Prefix.gs18PrefixCantStartWith0"));
|
|
1780
1761
|
}
|
|
1781
1762
|
baseValidation = _PrefixManager.GS1_8_PREFIX_VALIDATION;
|
|
1782
1763
|
break;
|
|
@@ -1807,8 +1788,7 @@ var PrefixManager = class _PrefixManager {
|
|
|
1807
1788
|
let creator = this._identificationKeyCreatorsMap.get(identificationKeyType);
|
|
1808
1789
|
if (creator === void 0) {
|
|
1809
1790
|
if (this.prefixType === 2 /* GS18Prefix */ && identificationKeyType !== "GTIN" /* GTIN */) {
|
|
1810
|
-
throw new RangeError(
|
|
1811
|
-
ns: gs1NS,
|
|
1791
|
+
throw new RangeError(i18nextGS1.t("Prefix.identificationKeyTypeNotSupportedByGS18Prefix", {
|
|
1812
1792
|
identificationKeyType
|
|
1813
1793
|
}));
|
|
1814
1794
|
}
|
|
@@ -1980,6 +1960,25 @@ export {
|
|
|
1980
1960
|
fiveDigitPriceWeightCheckDigit,
|
|
1981
1961
|
fourDigitPriceWeightCheckDigit,
|
|
1982
1962
|
gs1NS,
|
|
1963
|
+
gs1Resources,
|
|
1983
1964
|
hasValidCheckCharacterPair,
|
|
1984
|
-
hasValidCheckDigit
|
|
1965
|
+
hasValidCheckDigit,
|
|
1966
|
+
i18nGS1Init,
|
|
1967
|
+
i18nextGS1
|
|
1985
1968
|
};
|
|
1969
|
+
/*!
|
|
1970
|
+
* Copyright © 2024-2025 Dolphin Data Development Ltd. and AIDC Toolkit
|
|
1971
|
+
* contributors
|
|
1972
|
+
*
|
|
1973
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
1974
|
+
* you may not use this file except in compliance with the License.
|
|
1975
|
+
* You may obtain a copy of the License at
|
|
1976
|
+
*
|
|
1977
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
1978
|
+
*
|
|
1979
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
1980
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
1981
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
1982
|
+
* See the License for the specific language governing permissions and
|
|
1983
|
+
* limitations under the License.
|
|
1984
|
+
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aidc-toolkit/gs1",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.9-beta",
|
|
4
4
|
"description": "GS1 AIDC Toolkit",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -20,22 +20,23 @@
|
|
|
20
20
|
},
|
|
21
21
|
"scripts": {
|
|
22
22
|
"lint": "eslint .",
|
|
23
|
-
"build": "tsup src/index.ts --clean --format cjs,esm --dts",
|
|
24
|
-
"build-doc": "npm run build && tsc src/index.ts --outDir dist --target esnext --moduleResolution nodenext --module nodenext --emitDeclarationOnly --declaration --declarationMap",
|
|
23
|
+
"build-dist": "tsup src/index.ts --clean --format cjs,esm --dts",
|
|
24
|
+
"build-doc": "npm run build-dist && tsc src/index.ts --outDir dist --target esnext --moduleResolution nodenext --module nodenext --emitDeclarationOnly --declaration --declarationMap",
|
|
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.9-beta",
|
|
29
|
+
"eslint": "^9.17.0",
|
|
30
30
|
"ts-node": "^10.9.2",
|
|
31
31
|
"tsup": "^8.3.5",
|
|
32
32
|
"typescript": "^5.7.2",
|
|
33
33
|
"vitest": "^2.1.8"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@aidc-toolkit/core": "^0.9.
|
|
37
|
-
"@aidc-toolkit/utility": "^0.9.
|
|
38
|
-
"@rollup/rollup-linux-x64-gnu": "^4.
|
|
36
|
+
"@aidc-toolkit/core": "^0.9.9-beta",
|
|
37
|
+
"@aidc-toolkit/utility": "^0.9.9-beta",
|
|
38
|
+
"@rollup/rollup-linux-x64-gnu": "^4.29.1",
|
|
39
|
+
"i18next": "^24.2.0",
|
|
39
40
|
"ts-mixer": "^6.0.4"
|
|
40
41
|
}
|
|
41
42
|
}
|
package/src/check.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { NUMERIC_CREATOR } from "@aidc-toolkit/utility";
|
|
2
|
-
import { AI82_CREATOR } from "./
|
|
3
|
-
import
|
|
1
|
+
import { NUMERIC_CREATOR, utilityNS } from "@aidc-toolkit/utility";
|
|
2
|
+
import { AI82_CREATOR } from "./character-set.js";
|
|
3
|
+
import { i18nextGS1 } from "./locale/i18n.js";
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Results of multiplying digits by 3.
|
|
@@ -57,7 +57,11 @@ export function checkDigitSum(exchangeWeights: boolean, s: string): number {
|
|
|
57
57
|
// Calculate sum of each character value multiplied by the weight at its position.
|
|
58
58
|
return NUMERIC_CREATOR.characterIndexes(s).reduce<number>((accumulator, characterIndex, index) => {
|
|
59
59
|
if (characterIndex === undefined) {
|
|
60
|
-
throw new RangeError(
|
|
60
|
+
throw new RangeError(i18nextGS1.t("CharacterSetValidator.invalidCharacterAtPosition", {
|
|
61
|
+
ns: utilityNS,
|
|
62
|
+
c: s.charAt(index),
|
|
63
|
+
position: index + 1
|
|
64
|
+
}));
|
|
61
65
|
}
|
|
62
66
|
|
|
63
67
|
weight3 = !weight3;
|
|
@@ -109,7 +113,10 @@ export function hasValidCheckDigit(s: string): boolean {
|
|
|
109
113
|
*/
|
|
110
114
|
function priceWeightSum(weightsResults: ReadonlyArray<readonly number[]>, s: string): number {
|
|
111
115
|
if (s.length !== weightsResults.length) {
|
|
112
|
-
throw new RangeError(
|
|
116
|
+
throw new RangeError(i18nextGS1.t("Check.lengthOfStringForPriceOrWeightMustBeExactly", {
|
|
117
|
+
length: s.length,
|
|
118
|
+
exactLength: weightsResults.length
|
|
119
|
+
}));
|
|
113
120
|
}
|
|
114
121
|
|
|
115
122
|
// The value of each character is its index in the character set.
|
|
@@ -118,7 +125,12 @@ function priceWeightSum(weightsResults: ReadonlyArray<readonly number[]>, s: str
|
|
|
118
125
|
// Calculate sum of each weight result for each digit at its position.
|
|
119
126
|
return characterIndexes.reduce<number>((accumulator, characterIndex, index) => {
|
|
120
127
|
if (characterIndex === undefined) {
|
|
121
|
-
throw new RangeError(
|
|
128
|
+
throw new RangeError(i18nextGS1.t("CharacterSetValidator.invalidCharacterAtPositionOfComponent", {
|
|
129
|
+
ns: utilityNS,
|
|
130
|
+
c: s.charAt(index),
|
|
131
|
+
position: index + 1,
|
|
132
|
+
component: i18nextGS1.t("Check.priceOrWeightComponent")
|
|
133
|
+
}));
|
|
122
134
|
}
|
|
123
135
|
|
|
124
136
|
// Add the weight result of the character index to the accumulator.
|
|
@@ -184,8 +196,7 @@ export function checkCharacterPair(s: string): string {
|
|
|
184
196
|
const weightIndexStart = CHECK_CHARACTER_WEIGHTS.length - s.length;
|
|
185
197
|
|
|
186
198
|
if (weightIndexStart < 0) {
|
|
187
|
-
throw new RangeError(
|
|
188
|
-
ns: gs1NS,
|
|
199
|
+
throw new RangeError(i18nextGS1.t("Check.lengthOfStringForCheckCharacterPairMustBeLessThanOrEqualTo", {
|
|
189
200
|
length: s.length,
|
|
190
201
|
maximumLength: CHECK_CHARACTER_WEIGHTS.length
|
|
191
202
|
}));
|
|
@@ -194,7 +205,11 @@ export function checkCharacterPair(s: string): string {
|
|
|
194
205
|
// Calculate sum of each character value multiplied by the weight at its position, mod 1021.
|
|
195
206
|
const checkCharacterPairSum = AI82_CREATOR.characterIndexes(s).reduce<number>((accumulator, characterIndex, index) => {
|
|
196
207
|
if (characterIndex === undefined) {
|
|
197
|
-
throw new RangeError(
|
|
208
|
+
throw new RangeError(i18nextGS1.t("CharacterSetValidator.invalidCharacterAtPosition", {
|
|
209
|
+
ns: utilityNS,
|
|
210
|
+
c: s.charAt(index),
|
|
211
|
+
position: index + 1
|
|
212
|
+
}));
|
|
198
213
|
}
|
|
199
214
|
|
|
200
215
|
return accumulator + characterIndex * CHECK_CHARACTER_WEIGHTS[weightIndexStart + index];
|