@aidc-toolkit/gs1 0.9.4 → 0.9.6-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 +2045 -0
- package/dist/index.d.cts +1349 -0
- package/dist/index.d.ts +1349 -0
- package/dist/index.js +1986 -0
- package/package.json +9 -9
- package/src/idkey.ts +120 -211
- package/test/check.test.ts +1 -1
- package/test/idkey.test.ts +4 -4
- package/.github/ISSUE_TEMPLATE/bug_report.md +0 -32
- package/.github/ISSUE_TEMPLATE/feature_request.md +0 -20
- package/.github/workflows/npm-publish.yml +0 -38
- package/.idea/gs1.iml +0 -9
- package/.idea/inspectionProfiles/Project_Default.xml +0 -7
- package/.idea/misc.xml +0 -6
- package/.idea/modules.xml +0 -8
- package/.idea/runConfigurations/Test_all.xml +0 -12
- package/.idea/runConfigurations/Test_check.xml +0 -12
- package/.idea/runConfigurations/Test_identification_key.xml +0 -12
- package/.idea/runConfigurations/build.xml +0 -12
- package/.idea/runConfigurations/build_dev.xml +0 -12
- package/.idea/runConfigurations/eslint.xml +0 -12
- package/.idea/vcs.xml +0 -6
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,2045 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
AI39_CREATOR: () => AI39_CREATOR,
|
|
24
|
+
AI82_CREATOR: () => AI82_CREATOR,
|
|
25
|
+
CPID_VALIDATOR: () => CPID_VALIDATOR,
|
|
26
|
+
CharacterSet: () => CharacterSet,
|
|
27
|
+
GCN_VALIDATOR: () => GCN_VALIDATOR,
|
|
28
|
+
GDTI_VALIDATOR: () => GDTI_VALIDATOR,
|
|
29
|
+
GIAI_VALIDATOR: () => GIAI_VALIDATOR,
|
|
30
|
+
GINC_VALIDATOR: () => GINC_VALIDATOR,
|
|
31
|
+
GLN_VALIDATOR: () => GLN_VALIDATOR,
|
|
32
|
+
GMN_VALIDATOR: () => GMN_VALIDATOR,
|
|
33
|
+
GRAI_VALIDATOR: () => GRAI_VALIDATOR,
|
|
34
|
+
GSIN_VALIDATOR: () => GSIN_VALIDATOR,
|
|
35
|
+
GSRN_VALIDATOR: () => GSRN_VALIDATOR,
|
|
36
|
+
GTIN12_VALIDATOR: () => GTIN12_VALIDATOR,
|
|
37
|
+
GTIN13_VALIDATOR: () => GTIN13_VALIDATOR,
|
|
38
|
+
GTIN8_VALIDATOR: () => GTIN8_VALIDATOR,
|
|
39
|
+
GTINCreator: () => GTINCreator,
|
|
40
|
+
GTINLevel: () => GTINLevel,
|
|
41
|
+
GTINType: () => GTINType,
|
|
42
|
+
GTINValidator: () => GTINValidator,
|
|
43
|
+
GTIN_VALIDATORS: () => GTIN_VALIDATORS,
|
|
44
|
+
IdentificationKeyType: () => IdentificationKeyType,
|
|
45
|
+
LeaderType: () => LeaderType,
|
|
46
|
+
NonGTINNumericIdentificationKeyCreator: () => NonGTINNumericIdentificationKeyCreator,
|
|
47
|
+
NonGTINNumericIdentificationKeyValidator: () => NonGTINNumericIdentificationKeyValidator,
|
|
48
|
+
NonNumericIdentificationKeyCreator: () => NonNumericIdentificationKeyCreator,
|
|
49
|
+
NonNumericIdentificationKeyValidator: () => NonNumericIdentificationKeyValidator,
|
|
50
|
+
PrefixManager: () => PrefixManager,
|
|
51
|
+
PrefixType: () => PrefixType,
|
|
52
|
+
SSCC_VALIDATOR: () => SSCC_VALIDATOR,
|
|
53
|
+
SerializableNumericIdentificationKeyCreator: () => SerializableNumericIdentificationKeyCreator,
|
|
54
|
+
SerializableNumericIdentificationKeyValidator: () => SerializableNumericIdentificationKeyValidator,
|
|
55
|
+
checkCharacterPair: () => checkCharacterPair,
|
|
56
|
+
checkDigit: () => checkDigit,
|
|
57
|
+
checkDigitSum: () => checkDigitSum,
|
|
58
|
+
fiveDigitPriceWeightCheckDigit: () => fiveDigitPriceWeightCheckDigit,
|
|
59
|
+
fourDigitPriceWeightCheckDigit: () => fourDigitPriceWeightCheckDigit,
|
|
60
|
+
hasValidCheckCharacterPair: () => hasValidCheckCharacterPair,
|
|
61
|
+
hasValidCheckDigit: () => hasValidCheckDigit
|
|
62
|
+
});
|
|
63
|
+
module.exports = __toCommonJS(src_exports);
|
|
64
|
+
|
|
65
|
+
// src/character_set.ts
|
|
66
|
+
var import_utility = require("@aidc-toolkit/utility");
|
|
67
|
+
var AI82_CREATOR = new import_utility.CharacterSetCreator([
|
|
68
|
+
"!",
|
|
69
|
+
'"',
|
|
70
|
+
"%",
|
|
71
|
+
"&",
|
|
72
|
+
"'",
|
|
73
|
+
"(",
|
|
74
|
+
")",
|
|
75
|
+
"*",
|
|
76
|
+
"+",
|
|
77
|
+
",",
|
|
78
|
+
"-",
|
|
79
|
+
".",
|
|
80
|
+
"/",
|
|
81
|
+
"0",
|
|
82
|
+
"1",
|
|
83
|
+
"2",
|
|
84
|
+
"3",
|
|
85
|
+
"4",
|
|
86
|
+
"5",
|
|
87
|
+
"6",
|
|
88
|
+
"7",
|
|
89
|
+
"8",
|
|
90
|
+
"9",
|
|
91
|
+
":",
|
|
92
|
+
";",
|
|
93
|
+
"<",
|
|
94
|
+
"=",
|
|
95
|
+
">",
|
|
96
|
+
"?",
|
|
97
|
+
"A",
|
|
98
|
+
"B",
|
|
99
|
+
"C",
|
|
100
|
+
"D",
|
|
101
|
+
"E",
|
|
102
|
+
"F",
|
|
103
|
+
"G",
|
|
104
|
+
"H",
|
|
105
|
+
"I",
|
|
106
|
+
"J",
|
|
107
|
+
"K",
|
|
108
|
+
"L",
|
|
109
|
+
"M",
|
|
110
|
+
"N",
|
|
111
|
+
"O",
|
|
112
|
+
"P",
|
|
113
|
+
"Q",
|
|
114
|
+
"R",
|
|
115
|
+
"S",
|
|
116
|
+
"T",
|
|
117
|
+
"U",
|
|
118
|
+
"V",
|
|
119
|
+
"W",
|
|
120
|
+
"X",
|
|
121
|
+
"Y",
|
|
122
|
+
"Z",
|
|
123
|
+
"_",
|
|
124
|
+
"a",
|
|
125
|
+
"b",
|
|
126
|
+
"c",
|
|
127
|
+
"d",
|
|
128
|
+
"e",
|
|
129
|
+
"f",
|
|
130
|
+
"g",
|
|
131
|
+
"h",
|
|
132
|
+
"i",
|
|
133
|
+
"j",
|
|
134
|
+
"k",
|
|
135
|
+
"l",
|
|
136
|
+
"m",
|
|
137
|
+
"n",
|
|
138
|
+
"o",
|
|
139
|
+
"p",
|
|
140
|
+
"q",
|
|
141
|
+
"r",
|
|
142
|
+
"s",
|
|
143
|
+
"t",
|
|
144
|
+
"u",
|
|
145
|
+
"v",
|
|
146
|
+
"w",
|
|
147
|
+
"x",
|
|
148
|
+
"y",
|
|
149
|
+
"z"
|
|
150
|
+
], import_utility.Exclusion.AllNumeric);
|
|
151
|
+
var AI39_CREATOR = new import_utility.CharacterSetCreator([
|
|
152
|
+
"#",
|
|
153
|
+
"-",
|
|
154
|
+
"/",
|
|
155
|
+
"0",
|
|
156
|
+
"1",
|
|
157
|
+
"2",
|
|
158
|
+
"3",
|
|
159
|
+
"4",
|
|
160
|
+
"5",
|
|
161
|
+
"6",
|
|
162
|
+
"7",
|
|
163
|
+
"8",
|
|
164
|
+
"9",
|
|
165
|
+
"A",
|
|
166
|
+
"B",
|
|
167
|
+
"C",
|
|
168
|
+
"D",
|
|
169
|
+
"E",
|
|
170
|
+
"F",
|
|
171
|
+
"G",
|
|
172
|
+
"H",
|
|
173
|
+
"I",
|
|
174
|
+
"J",
|
|
175
|
+
"K",
|
|
176
|
+
"L",
|
|
177
|
+
"M",
|
|
178
|
+
"N",
|
|
179
|
+
"O",
|
|
180
|
+
"P",
|
|
181
|
+
"Q",
|
|
182
|
+
"R",
|
|
183
|
+
"S",
|
|
184
|
+
"T",
|
|
185
|
+
"U",
|
|
186
|
+
"V",
|
|
187
|
+
"W",
|
|
188
|
+
"X",
|
|
189
|
+
"Y",
|
|
190
|
+
"Z"
|
|
191
|
+
], import_utility.Exclusion.AllNumeric);
|
|
192
|
+
|
|
193
|
+
// src/check.ts
|
|
194
|
+
var import_utility2 = require("@aidc-toolkit/utility");
|
|
195
|
+
|
|
196
|
+
// src/locale/i18n.ts
|
|
197
|
+
var import_core = require("@aidc-toolkit/core");
|
|
198
|
+
|
|
199
|
+
// src/locale/en/locale_strings.ts
|
|
200
|
+
var localeStrings = {
|
|
201
|
+
Check: {
|
|
202
|
+
lengthOfStringForCheckCharacterPairMustBeLessThanOrEqualTo: "Length {{length}} of string for check character pair must be less than or equal to {{maximumLength}}"
|
|
203
|
+
},
|
|
204
|
+
IdentificationKey: {
|
|
205
|
+
identificationKeyTypeLength: "{{identificationKeyType}} must be {{length}} digits long",
|
|
206
|
+
invalidCheckDigit: "Invalid check digit",
|
|
207
|
+
invalidGTINLength: "GTIN must be 13, 12, 8, or 14 digits long",
|
|
208
|
+
invalidGTIN14Length: "GTIN must be 14 digits long",
|
|
209
|
+
invalidZeroSuppressedGTIN12: "Invalid zero-suppressed GTIN-12",
|
|
210
|
+
invalidZeroSuppressibleGTIN12: "GTIN-12 not zero-suppressible",
|
|
211
|
+
invalidZeroSuppressedGTIN12AsGTIN13: "Invalid zero-suppressed GTIN-12 as GTIN-13",
|
|
212
|
+
invalidZeroSuppressedGTIN12AsGTIN14: "Invalid zero-suppressed GTIN-12 as GTIN-14",
|
|
213
|
+
invalidGTIN13AtRetail: "GTIN-13 at retail consumer trade item level can't start with zero",
|
|
214
|
+
invalidGTINAtRetail: "GTIN not supported at retail consumer trade item level",
|
|
215
|
+
invalidGTINAtOtherThanRetail: "GTIN not supported at other than retail consumer trade item level",
|
|
216
|
+
indicatorDigit: "indicator digit",
|
|
217
|
+
serialComponent: "serial component",
|
|
218
|
+
reference: "reference",
|
|
219
|
+
referenceCantBeAllNumeric: "Reference can't be all-numeric",
|
|
220
|
+
invalidCheckCharacterPair: "Invalid check character pair"
|
|
221
|
+
},
|
|
222
|
+
Prefix: {
|
|
223
|
+
gs1CompanyPrefix: "GS1 Company Prefix",
|
|
224
|
+
upcCompanyPrefix: "U.P.C. Company Prefix",
|
|
225
|
+
gs18Prefix: "GS1-8 Prefix",
|
|
226
|
+
gs1CompanyPrefixCantStartWith0: `GS1 Company Prefix can't start with "0"`,
|
|
227
|
+
gs1CompanyPrefixCantStartWith00000: `GS1 Company Prefix can't start with "00000"`,
|
|
228
|
+
gs1CompanyPrefixCantStartWith000000: `GS1 Company Prefix can't start with "000000"`,
|
|
229
|
+
upcCompanyPrefixCantStartWith0000: `U.P.C. Company Prefix can't start with "0000"`,
|
|
230
|
+
gs18PrefixCantStartWith0: `GS1-8 Prefix can't start with "0"`,
|
|
231
|
+
identificationKeyTypeNotSupportedByGS18Prefix: "{{identificationKeyType}} not supported by GS1-8 Prefix"
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
// src/locale/fr/locale_strings.ts
|
|
236
|
+
var localeStrings2 = {
|
|
237
|
+
Check: {
|
|
238
|
+
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}}"
|
|
239
|
+
},
|
|
240
|
+
IdentificationKey: {
|
|
241
|
+
identificationKeyTypeLength: "{{identificationKeyType}} doit comporter {{length}} chiffres",
|
|
242
|
+
invalidCheckDigit: "Chiffre de contr\xF4le non valide",
|
|
243
|
+
invalidGTINLength: "Le GTIN doit comporter 13, 12, 8 ou 14 chiffres",
|
|
244
|
+
invalidGTIN14Length: "Le GTIN doit comporter 14 chiffres",
|
|
245
|
+
invalidZeroSuppressedGTIN12: "Code GTIN-12 non valide avec z\xE9ro supprim\xE9",
|
|
246
|
+
invalidZeroSuppressibleGTIN12: "Le GTIN-12 ne peut pas \xEAtre supprim\xE9 par z\xE9ro",
|
|
247
|
+
invalidZeroSuppressedGTIN12AsGTIN13: "GTIN-12 non valide avec z\xE9ro supprim\xE9 en tant que GTIN-13",
|
|
248
|
+
invalidZeroSuppressedGTIN12AsGTIN14: "GTIN-12 non valide avec z\xE9ro supprim\xE9 en tant que GTIN-14",
|
|
249
|
+
invalidGTIN13AtRetail: "Le GTIN-13 au niveau des articles de consommation au d\xE9tail ne peut pas commencer par z\xE9ro",
|
|
250
|
+
invalidGTINAtRetail: "Le GTIN n'est pas pris en charge au niveau des articles de consommation au d\xE9tail",
|
|
251
|
+
invalidGTINAtOtherThanRetail: "Le GTIN n'est pas pris en charge \xE0 d'autres niveaux que ceux des articles de consommation au d\xE9tail",
|
|
252
|
+
indicatorDigit: "chiffre indicateur",
|
|
253
|
+
serialComponent: "composant s\xE9rie",
|
|
254
|
+
reference: "r\xE9f\xE9rence",
|
|
255
|
+
referenceCantBeAllNumeric: "La r\xE9f\xE9rence ne peut pas \xEAtre enti\xE8rement num\xE9rique",
|
|
256
|
+
invalidCheckCharacterPair: "Paire de caract\xE8res de contr\xF4le non valide"
|
|
257
|
+
},
|
|
258
|
+
Prefix: {
|
|
259
|
+
gs1CompanyPrefix: "Pr\xE9fixe de l'entreprise GS1",
|
|
260
|
+
upcCompanyPrefix: "Pr\xE9fixe de l'entreprise U.P.C.",
|
|
261
|
+
gs18Prefix: "Pr\xE9fixe GS1-8",
|
|
262
|
+
gs1CompanyPrefixCantStartWith0: `Le pr\xE9fixe de l'entreprise GS1 ne peut pas commencer par "0"`,
|
|
263
|
+
gs1CompanyPrefixCantStartWith00000: `Le pr\xE9fixe de l'entreprise GS1 ne peut pas commencer par "00000"`,
|
|
264
|
+
gs1CompanyPrefixCantStartWith000000: `Le pr\xE9fixe de l'entreprise GS1 ne peut pas commencer par "000000"`,
|
|
265
|
+
upcCompanyPrefixCantStartWith0000: `Le pr\xE9fixe de l'entreprise U.P.C. ne peut pas commencer par "0000"`,
|
|
266
|
+
gs18PrefixCantStartWith0: 'Le pr\xE9fixe GS1-8 ne peut pas commencer par "0"',
|
|
267
|
+
identificationKeyTypeNotSupportedByGS18Prefix: "{{identificationKeyType}} non pris en charge par le pr\xE9fixe GS1-8"
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
// src/locale/i18n.ts
|
|
272
|
+
var gs1NS = "aidct_gs1";
|
|
273
|
+
(0, import_core.i18nAssertValidResources)(localeStrings, "fr", localeStrings2);
|
|
274
|
+
(0, import_core.i18nAddResourceBundle)("en", gs1NS, localeStrings);
|
|
275
|
+
(0, import_core.i18nAddResourceBundle)("fr", gs1NS, localeStrings2);
|
|
276
|
+
var i18n_default = import_core.i18next;
|
|
277
|
+
|
|
278
|
+
// src/check.ts
|
|
279
|
+
var THREE_WEIGHT_RESULTS = [
|
|
280
|
+
0,
|
|
281
|
+
3,
|
|
282
|
+
6,
|
|
283
|
+
9,
|
|
284
|
+
12,
|
|
285
|
+
15,
|
|
286
|
+
18,
|
|
287
|
+
21,
|
|
288
|
+
24,
|
|
289
|
+
27
|
|
290
|
+
];
|
|
291
|
+
var TWO_MINUS_WEIGHT_RESULTS = [
|
|
292
|
+
0,
|
|
293
|
+
2,
|
|
294
|
+
4,
|
|
295
|
+
6,
|
|
296
|
+
8,
|
|
297
|
+
9,
|
|
298
|
+
1,
|
|
299
|
+
3,
|
|
300
|
+
5,
|
|
301
|
+
7
|
|
302
|
+
];
|
|
303
|
+
var FIVE_PLUS_WEIGHT_RESULTS = [
|
|
304
|
+
0,
|
|
305
|
+
5,
|
|
306
|
+
1,
|
|
307
|
+
6,
|
|
308
|
+
2,
|
|
309
|
+
7,
|
|
310
|
+
3,
|
|
311
|
+
8,
|
|
312
|
+
4,
|
|
313
|
+
9
|
|
314
|
+
];
|
|
315
|
+
var FIVE_MINUS_WEIGHT_RESULTS = [
|
|
316
|
+
0,
|
|
317
|
+
5,
|
|
318
|
+
9,
|
|
319
|
+
4,
|
|
320
|
+
8,
|
|
321
|
+
3,
|
|
322
|
+
7,
|
|
323
|
+
2,
|
|
324
|
+
6,
|
|
325
|
+
1
|
|
326
|
+
];
|
|
327
|
+
var INVERSE_FIVE_MINUS_WEIGHT_RESULTS = [
|
|
328
|
+
0,
|
|
329
|
+
9,
|
|
330
|
+
7,
|
|
331
|
+
5,
|
|
332
|
+
3,
|
|
333
|
+
1,
|
|
334
|
+
8,
|
|
335
|
+
6,
|
|
336
|
+
4,
|
|
337
|
+
2
|
|
338
|
+
];
|
|
339
|
+
function checkDigitSum(exchangeWeights, s) {
|
|
340
|
+
let weight3 = (s.length + Number(exchangeWeights)) % 2 === 0;
|
|
341
|
+
return import_utility2.NUMERIC_CREATOR.characterIndexes(s).reduce((accumulator, characterIndex, index) => {
|
|
342
|
+
if (characterIndex === void 0) {
|
|
343
|
+
throw new RangeError(`Invalid character '${s.charAt(index)}' at position ${index + 1}`);
|
|
344
|
+
}
|
|
345
|
+
weight3 = !weight3;
|
|
346
|
+
return accumulator + (weight3 ? THREE_WEIGHT_RESULTS[characterIndex] : characterIndex);
|
|
347
|
+
}, 0);
|
|
348
|
+
}
|
|
349
|
+
function checkDigit(s) {
|
|
350
|
+
return import_utility2.NUMERIC_CREATOR.character(9 - (checkDigitSum(false, s) + 9) % 10);
|
|
351
|
+
}
|
|
352
|
+
function hasValidCheckDigit(s) {
|
|
353
|
+
return checkDigitSum(true, s) % 10 === 0;
|
|
354
|
+
}
|
|
355
|
+
function priceWeightSum(weightsResults, s) {
|
|
356
|
+
if (s.length !== weightsResults.length) {
|
|
357
|
+
throw new RangeError(`String for price or weight sum must be exactly ${weightsResults.length} characters`);
|
|
358
|
+
}
|
|
359
|
+
const characterIndexes = import_utility2.NUMERIC_CREATOR.characterIndexes(s);
|
|
360
|
+
return characterIndexes.reduce((accumulator, characterIndex, index) => {
|
|
361
|
+
if (characterIndex === void 0) {
|
|
362
|
+
throw new RangeError(`Invalid character '${s.charAt(index)}' at position ${index + 1}`);
|
|
363
|
+
}
|
|
364
|
+
return accumulator + weightsResults[index][characterIndex];
|
|
365
|
+
}, 0);
|
|
366
|
+
}
|
|
367
|
+
function fourDigitPriceWeightCheckDigit(s) {
|
|
368
|
+
return import_utility2.NUMERIC_CREATOR.character(priceWeightSum([TWO_MINUS_WEIGHT_RESULTS, TWO_MINUS_WEIGHT_RESULTS, THREE_WEIGHT_RESULTS, FIVE_MINUS_WEIGHT_RESULTS], s) * 3 % 10);
|
|
369
|
+
}
|
|
370
|
+
function fiveDigitPriceWeightCheckDigit(s) {
|
|
371
|
+
return import_utility2.NUMERIC_CREATOR.character(INVERSE_FIVE_MINUS_WEIGHT_RESULTS[9 - (priceWeightSum([FIVE_PLUS_WEIGHT_RESULTS, TWO_MINUS_WEIGHT_RESULTS, FIVE_MINUS_WEIGHT_RESULTS, FIVE_PLUS_WEIGHT_RESULTS, TWO_MINUS_WEIGHT_RESULTS], s) + 9) % 10]);
|
|
372
|
+
}
|
|
373
|
+
var CHECK_CHARACTER_WEIGHTS = [
|
|
374
|
+
107,
|
|
375
|
+
103,
|
|
376
|
+
101,
|
|
377
|
+
97,
|
|
378
|
+
89,
|
|
379
|
+
83,
|
|
380
|
+
79,
|
|
381
|
+
73,
|
|
382
|
+
71,
|
|
383
|
+
67,
|
|
384
|
+
61,
|
|
385
|
+
59,
|
|
386
|
+
53,
|
|
387
|
+
47,
|
|
388
|
+
43,
|
|
389
|
+
41,
|
|
390
|
+
37,
|
|
391
|
+
31,
|
|
392
|
+
29,
|
|
393
|
+
23,
|
|
394
|
+
19,
|
|
395
|
+
17,
|
|
396
|
+
13,
|
|
397
|
+
11,
|
|
398
|
+
7,
|
|
399
|
+
5,
|
|
400
|
+
3,
|
|
401
|
+
2
|
|
402
|
+
];
|
|
403
|
+
var CHECK_CHARACTERS = [
|
|
404
|
+
"2",
|
|
405
|
+
"3",
|
|
406
|
+
"4",
|
|
407
|
+
"5",
|
|
408
|
+
"6",
|
|
409
|
+
"7",
|
|
410
|
+
"8",
|
|
411
|
+
"9",
|
|
412
|
+
"A",
|
|
413
|
+
"B",
|
|
414
|
+
"C",
|
|
415
|
+
"D",
|
|
416
|
+
"E",
|
|
417
|
+
"F",
|
|
418
|
+
"G",
|
|
419
|
+
"H",
|
|
420
|
+
"J",
|
|
421
|
+
"K",
|
|
422
|
+
"L",
|
|
423
|
+
"M",
|
|
424
|
+
"N",
|
|
425
|
+
"P",
|
|
426
|
+
"Q",
|
|
427
|
+
"R",
|
|
428
|
+
"S",
|
|
429
|
+
"T",
|
|
430
|
+
"U",
|
|
431
|
+
"V",
|
|
432
|
+
"W",
|
|
433
|
+
"X",
|
|
434
|
+
"Y",
|
|
435
|
+
"Z"
|
|
436
|
+
];
|
|
437
|
+
function checkCharacterPair(s) {
|
|
438
|
+
const weightIndexStart = CHECK_CHARACTER_WEIGHTS.length - s.length;
|
|
439
|
+
if (weightIndexStart < 0) {
|
|
440
|
+
throw new RangeError(i18n_default.t("Check.lengthOfStringForCheckCharacterPairMustBeLessThanOrEqualTo", {
|
|
441
|
+
ns: gs1NS,
|
|
442
|
+
length: s.length,
|
|
443
|
+
maximumLength: CHECK_CHARACTER_WEIGHTS.length
|
|
444
|
+
}));
|
|
445
|
+
}
|
|
446
|
+
const checkCharacterPairSum = AI82_CREATOR.characterIndexes(s).reduce((accumulator, characterIndex, index) => {
|
|
447
|
+
if (characterIndex === void 0) {
|
|
448
|
+
throw new RangeError(`Invalid character '${s.charAt(index)}' at position ${index + 1}`);
|
|
449
|
+
}
|
|
450
|
+
return accumulator + characterIndex * CHECK_CHARACTER_WEIGHTS[weightIndexStart + index];
|
|
451
|
+
}, 0) % 1021;
|
|
452
|
+
const checkCharacterPairSumMod32 = checkCharacterPairSum % 32;
|
|
453
|
+
return CHECK_CHARACTERS[(checkCharacterPairSum - checkCharacterPairSumMod32) / 32] + CHECK_CHARACTERS[checkCharacterPairSumMod32];
|
|
454
|
+
}
|
|
455
|
+
function hasValidCheckCharacterPair(s) {
|
|
456
|
+
const checkCharacterPairIndex = s.length - 2;
|
|
457
|
+
return checkCharacterPair(s.substring(0, checkCharacterPairIndex)) === s.substring(checkCharacterPairIndex);
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
// src/idkey.ts
|
|
461
|
+
var import_utility3 = require("@aidc-toolkit/utility");
|
|
462
|
+
var import_ts_mixer = require("ts-mixer");
|
|
463
|
+
var IdentificationKeyType = /* @__PURE__ */ ((IdentificationKeyType2) => {
|
|
464
|
+
IdentificationKeyType2["GTIN"] = "GTIN";
|
|
465
|
+
IdentificationKeyType2["GLN"] = "GLN";
|
|
466
|
+
IdentificationKeyType2["SSCC"] = "SSCC";
|
|
467
|
+
IdentificationKeyType2["GRAI"] = "GRAI";
|
|
468
|
+
IdentificationKeyType2["GIAI"] = "GIAI";
|
|
469
|
+
IdentificationKeyType2["GSRN"] = "GSRN";
|
|
470
|
+
IdentificationKeyType2["GDTI"] = "GDTI";
|
|
471
|
+
IdentificationKeyType2["GINC"] = "GINC";
|
|
472
|
+
IdentificationKeyType2["GSIN"] = "GSIN";
|
|
473
|
+
IdentificationKeyType2["GCN"] = "GCN";
|
|
474
|
+
IdentificationKeyType2["CPID"] = "CPID";
|
|
475
|
+
IdentificationKeyType2["GMN"] = "GMN";
|
|
476
|
+
return IdentificationKeyType2;
|
|
477
|
+
})(IdentificationKeyType || {});
|
|
478
|
+
var PrefixType = /* @__PURE__ */ ((PrefixType2) => {
|
|
479
|
+
PrefixType2[PrefixType2["GS1CompanyPrefix"] = 0] = "GS1CompanyPrefix";
|
|
480
|
+
PrefixType2[PrefixType2["UPCCompanyPrefix"] = 1] = "UPCCompanyPrefix";
|
|
481
|
+
PrefixType2[PrefixType2["GS18Prefix"] = 2] = "GS18Prefix";
|
|
482
|
+
return PrefixType2;
|
|
483
|
+
})(PrefixType || {});
|
|
484
|
+
var CharacterSet = /* @__PURE__ */ ((CharacterSet2) => {
|
|
485
|
+
CharacterSet2[CharacterSet2["Numeric"] = 0] = "Numeric";
|
|
486
|
+
CharacterSet2[CharacterSet2["AI82"] = 1] = "AI82";
|
|
487
|
+
CharacterSet2[CharacterSet2["AI39"] = 2] = "AI39";
|
|
488
|
+
return CharacterSet2;
|
|
489
|
+
})(CharacterSet || {});
|
|
490
|
+
var AbstractIdentificationKeyValidator = class _AbstractIdentificationKeyValidator {
|
|
491
|
+
static CHARACTER_SET_CREATORS = [
|
|
492
|
+
import_utility3.NUMERIC_CREATOR,
|
|
493
|
+
AI82_CREATOR,
|
|
494
|
+
AI39_CREATOR
|
|
495
|
+
];
|
|
496
|
+
/**
|
|
497
|
+
* Identification key type.
|
|
498
|
+
*/
|
|
499
|
+
_identificationKeyType;
|
|
500
|
+
/**
|
|
501
|
+
* Prefix type.
|
|
502
|
+
*/
|
|
503
|
+
_prefixType;
|
|
504
|
+
/**
|
|
505
|
+
* Length.
|
|
506
|
+
*/
|
|
507
|
+
_length;
|
|
508
|
+
/**
|
|
509
|
+
* Reference character set.
|
|
510
|
+
*/
|
|
511
|
+
_referenceCharacterSet;
|
|
512
|
+
/**
|
|
513
|
+
* Reference creator.
|
|
514
|
+
*/
|
|
515
|
+
_referenceCreator;
|
|
516
|
+
/**
|
|
517
|
+
* Get the character set creator for a character set.
|
|
518
|
+
*
|
|
519
|
+
* @param characterSet
|
|
520
|
+
* Character set.
|
|
521
|
+
*
|
|
522
|
+
* @returns
|
|
523
|
+
* Character set creator.
|
|
524
|
+
*/
|
|
525
|
+
static creatorFor(characterSet) {
|
|
526
|
+
return _AbstractIdentificationKeyValidator.CHARACTER_SET_CREATORS[characterSet];
|
|
527
|
+
}
|
|
528
|
+
/**
|
|
529
|
+
* Constructor.
|
|
530
|
+
*
|
|
531
|
+
* @param identificationKeyType
|
|
532
|
+
* Identification key type.
|
|
533
|
+
*
|
|
534
|
+
* @param prefixType
|
|
535
|
+
* Prefix type.
|
|
536
|
+
*
|
|
537
|
+
* @param length
|
|
538
|
+
* Length.
|
|
539
|
+
*
|
|
540
|
+
* @param referenceCharacterSet
|
|
541
|
+
* Reference character set.
|
|
542
|
+
*/
|
|
543
|
+
constructor(identificationKeyType, prefixType, length, referenceCharacterSet) {
|
|
544
|
+
this._identificationKeyType = identificationKeyType;
|
|
545
|
+
this._prefixType = prefixType;
|
|
546
|
+
this._length = length;
|
|
547
|
+
this._referenceCharacterSet = referenceCharacterSet;
|
|
548
|
+
this._referenceCreator = _AbstractIdentificationKeyValidator.creatorFor(referenceCharacterSet);
|
|
549
|
+
}
|
|
550
|
+
/**
|
|
551
|
+
* @inheritDoc
|
|
552
|
+
*/
|
|
553
|
+
get identificationKeyType() {
|
|
554
|
+
return this._identificationKeyType;
|
|
555
|
+
}
|
|
556
|
+
/**
|
|
557
|
+
* @inheritDoc
|
|
558
|
+
*/
|
|
559
|
+
get prefixType() {
|
|
560
|
+
return this._prefixType;
|
|
561
|
+
}
|
|
562
|
+
/**
|
|
563
|
+
* @inheritDoc
|
|
564
|
+
*/
|
|
565
|
+
get length() {
|
|
566
|
+
return this._length;
|
|
567
|
+
}
|
|
568
|
+
/**
|
|
569
|
+
* @inheritDoc
|
|
570
|
+
*/
|
|
571
|
+
get referenceCharacterSet() {
|
|
572
|
+
return this._referenceCharacterSet;
|
|
573
|
+
}
|
|
574
|
+
/**
|
|
575
|
+
* @inheritDoc
|
|
576
|
+
*/
|
|
577
|
+
get referenceCreator() {
|
|
578
|
+
return this._referenceCreator;
|
|
579
|
+
}
|
|
580
|
+
/**
|
|
581
|
+
* Pad an identification key on the left with zero-value character for validation purposes. This is done to align an
|
|
582
|
+
* identification key with a position offset for any error message that may be thrown by the reference validator.
|
|
583
|
+
*
|
|
584
|
+
* @param identificationKey
|
|
585
|
+
* Identification key.
|
|
586
|
+
*
|
|
587
|
+
* @param validation
|
|
588
|
+
* Identification key validation parameters.
|
|
589
|
+
*
|
|
590
|
+
* @returns
|
|
591
|
+
* Padded identification key.
|
|
592
|
+
*/
|
|
593
|
+
padIdentificationKey(identificationKey, validation) {
|
|
594
|
+
return validation?.positionOffset === void 0 ? identificationKey : this.referenceCreator.character(0).repeat(validation.positionOffset).concat(identificationKey);
|
|
595
|
+
}
|
|
596
|
+
/**
|
|
597
|
+
* Validate the prefix within an identification key.
|
|
598
|
+
*
|
|
599
|
+
* @param partialIdentificationKey
|
|
600
|
+
* Partial identification key.
|
|
601
|
+
*
|
|
602
|
+
* @param positionOffset
|
|
603
|
+
* Position offset within a larger string.
|
|
604
|
+
*/
|
|
605
|
+
validatePrefix(partialIdentificationKey, positionOffset) {
|
|
606
|
+
PrefixManager.validatePrefix(this.prefixType, true, false, partialIdentificationKey, true, this.referenceCharacterSet === 0 /* Numeric */, positionOffset);
|
|
607
|
+
}
|
|
608
|
+
};
|
|
609
|
+
var LeaderType = /* @__PURE__ */ ((LeaderType2) => {
|
|
610
|
+
LeaderType2[LeaderType2["None"] = 0] = "None";
|
|
611
|
+
LeaderType2[LeaderType2["IndicatorDigit"] = 1] = "IndicatorDigit";
|
|
612
|
+
LeaderType2[LeaderType2["ExtensionDigit"] = 2] = "ExtensionDigit";
|
|
613
|
+
return LeaderType2;
|
|
614
|
+
})(LeaderType || {});
|
|
615
|
+
var AbstractNumericIdentificationKeyValidator = class extends AbstractIdentificationKeyValidator {
|
|
616
|
+
/**
|
|
617
|
+
* Leader type.
|
|
618
|
+
*/
|
|
619
|
+
_leaderType;
|
|
620
|
+
/**
|
|
621
|
+
* Prefix position, determined by the leader type.
|
|
622
|
+
*/
|
|
623
|
+
_prefixPosition;
|
|
624
|
+
/**
|
|
625
|
+
* Constructor.
|
|
626
|
+
*
|
|
627
|
+
* @param identificationKeyType
|
|
628
|
+
* Identification key type.
|
|
629
|
+
*
|
|
630
|
+
* @param prefixType
|
|
631
|
+
* Prefix type.
|
|
632
|
+
*
|
|
633
|
+
* @param length
|
|
634
|
+
* Length.
|
|
635
|
+
*
|
|
636
|
+
* @param leaderType
|
|
637
|
+
* Leader type.
|
|
638
|
+
*/
|
|
639
|
+
constructor(identificationKeyType, prefixType, length, leaderType) {
|
|
640
|
+
super(identificationKeyType, prefixType, length, 0 /* Numeric */);
|
|
641
|
+
this._leaderType = leaderType;
|
|
642
|
+
this._prefixPosition = Number(this.leaderType === 2 /* ExtensionDigit */);
|
|
643
|
+
}
|
|
644
|
+
/**
|
|
645
|
+
* @inheritDoc
|
|
646
|
+
*/
|
|
647
|
+
get leaderType() {
|
|
648
|
+
return this._leaderType;
|
|
649
|
+
}
|
|
650
|
+
/**
|
|
651
|
+
* @inheritDoc
|
|
652
|
+
*/
|
|
653
|
+
validate(identificationKey, validation) {
|
|
654
|
+
if (this._prefixPosition === 0) {
|
|
655
|
+
super.validatePrefix(identificationKey, validation?.positionOffset);
|
|
656
|
+
} else {
|
|
657
|
+
super.validatePrefix(identificationKey.substring(this._prefixPosition), validation?.positionOffset === void 0 ? this._prefixPosition : validation.positionOffset + this._prefixPosition);
|
|
658
|
+
}
|
|
659
|
+
if (identificationKey.length !== this.length) {
|
|
660
|
+
throw new RangeError(i18n_default.t("IdentificationKey.identificationKeyTypeLength", {
|
|
661
|
+
ns: gs1NS,
|
|
662
|
+
identificationKeyType: this.identificationKeyType,
|
|
663
|
+
length: this.length
|
|
664
|
+
}));
|
|
665
|
+
}
|
|
666
|
+
if (!hasValidCheckDigit(this.padIdentificationKey(identificationKey, validation))) {
|
|
667
|
+
throw new RangeError(i18n_default.t("IdentificationKey.invalidCheckDigit", {
|
|
668
|
+
ns: gs1NS
|
|
669
|
+
}));
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
};
|
|
673
|
+
var GTINType = /* @__PURE__ */ ((GTINType2) => {
|
|
674
|
+
GTINType2[GTINType2["GTIN13"] = 13] = "GTIN13";
|
|
675
|
+
GTINType2[GTINType2["GTIN12"] = 12] = "GTIN12";
|
|
676
|
+
GTINType2[GTINType2["GTIN8"] = 8] = "GTIN8";
|
|
677
|
+
GTINType2[GTINType2["GTIN14"] = 14] = "GTIN14";
|
|
678
|
+
return GTINType2;
|
|
679
|
+
})(GTINType || {});
|
|
680
|
+
var GTINLevel = /* @__PURE__ */ ((GTINLevel2) => {
|
|
681
|
+
GTINLevel2[GTINLevel2["Any"] = 0] = "Any";
|
|
682
|
+
GTINLevel2[GTINLevel2["RetailConsumer"] = 1] = "RetailConsumer";
|
|
683
|
+
GTINLevel2[GTINLevel2["OtherThanRetailConsumer"] = 2] = "OtherThanRetailConsumer";
|
|
684
|
+
return GTINLevel2;
|
|
685
|
+
})(GTINLevel || {});
|
|
686
|
+
var GTINValidator = class _GTINValidator extends AbstractNumericIdentificationKeyValidator {
|
|
687
|
+
/**
|
|
688
|
+
* Zero-suppressed GTIN-12 validation parameters.
|
|
689
|
+
*/
|
|
690
|
+
static ZERO_SUPPRESSED_GTIN12_VALIDATION = {
|
|
691
|
+
minimumLength: 8,
|
|
692
|
+
maximumLength: 8
|
|
693
|
+
};
|
|
694
|
+
/**
|
|
695
|
+
* Constructor.
|
|
696
|
+
*
|
|
697
|
+
* @param gtinType
|
|
698
|
+
* GTIN type.
|
|
699
|
+
*/
|
|
700
|
+
constructor(gtinType) {
|
|
701
|
+
let prefixType;
|
|
702
|
+
switch (gtinType) {
|
|
703
|
+
case 13 /* GTIN13 */:
|
|
704
|
+
prefixType = 0 /* GS1CompanyPrefix */;
|
|
705
|
+
break;
|
|
706
|
+
case 12 /* GTIN12 */:
|
|
707
|
+
prefixType = 1 /* UPCCompanyPrefix */;
|
|
708
|
+
break;
|
|
709
|
+
case 8 /* GTIN8 */:
|
|
710
|
+
prefixType = 2 /* GS18Prefix */;
|
|
711
|
+
break;
|
|
712
|
+
default:
|
|
713
|
+
throw new Error("Not supported");
|
|
714
|
+
}
|
|
715
|
+
super("GTIN" /* GTIN */, prefixType, gtinType, 1 /* IndicatorDigit */);
|
|
716
|
+
}
|
|
717
|
+
/**
|
|
718
|
+
* @inheritDoc
|
|
719
|
+
*/
|
|
720
|
+
get gtinType() {
|
|
721
|
+
return this.length;
|
|
722
|
+
}
|
|
723
|
+
/**
|
|
724
|
+
* @inheritDoc
|
|
725
|
+
*/
|
|
726
|
+
validatePrefix(partialIdentificationKey, positionOffset) {
|
|
727
|
+
PrefixManager.validatePrefix(this.prefixType, false, false, partialIdentificationKey, true, true, positionOffset);
|
|
728
|
+
}
|
|
729
|
+
/**
|
|
730
|
+
* Zero expand a zero-suppressed GTIN-12.
|
|
731
|
+
*
|
|
732
|
+
* @param zeroSuppressedGTIN12
|
|
733
|
+
* Zero-suppressed GTIN-12.
|
|
734
|
+
*
|
|
735
|
+
* @returns
|
|
736
|
+
* GTIN-12.
|
|
737
|
+
*/
|
|
738
|
+
static zeroExpand(zeroSuppressedGTIN12) {
|
|
739
|
+
import_utility3.NUMERIC_CREATOR.validate(zeroSuppressedGTIN12, _GTINValidator.ZERO_SUPPRESSED_GTIN12_VALIDATION);
|
|
740
|
+
const d = Array.from(zeroSuppressedGTIN12);
|
|
741
|
+
let gtin12;
|
|
742
|
+
if (d[0] === "0") {
|
|
743
|
+
if (d[6] >= "5" && d[5] !== "0") {
|
|
744
|
+
gtin12 = `0${d[1]}${d[2]}${d[3]}${d[4]}${d[5]}0000${d[6]}${d[7]}`;
|
|
745
|
+
} else if (d[6] === "4" && d[4] !== "0") {
|
|
746
|
+
gtin12 = `0${d[1]}${d[2]}${d[3]}${d[4]}00000${d[5]}${d[7]}`;
|
|
747
|
+
} else if (d[6] <= "2") {
|
|
748
|
+
gtin12 = `0${d[1]}${d[2]}${d[6]}0000${d[3]}${d[4]}${d[5]}${d[7]}`;
|
|
749
|
+
} else if (d[6] === "3" && d[3] >= "3") {
|
|
750
|
+
gtin12 = `0${d[1]}${d[2]}${d[3]}00000${d[4]}${d[5]}${d[7]}`;
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
if (gtin12 === void 0) {
|
|
754
|
+
throw new RangeError(i18n_default.t("IdentificationKey.invalidZeroSuppressedGTIN12", {
|
|
755
|
+
ns: gs1NS
|
|
756
|
+
}));
|
|
757
|
+
}
|
|
758
|
+
GTIN12_VALIDATOR.validate(gtin12);
|
|
759
|
+
return gtin12;
|
|
760
|
+
}
|
|
761
|
+
/**
|
|
762
|
+
* Validate any GTIN, optionally against a level.
|
|
763
|
+
*
|
|
764
|
+
* @param gtin
|
|
765
|
+
* GTIN.
|
|
766
|
+
*
|
|
767
|
+
* @param gtinLevel
|
|
768
|
+
* Level at which GTIN is to be validated.
|
|
769
|
+
*/
|
|
770
|
+
static validateAny(gtin, gtinLevel = 0 /* Any */) {
|
|
771
|
+
let lengthValidatedGTIN = gtin;
|
|
772
|
+
let gtinLevelRestriction;
|
|
773
|
+
switch (gtin.length) {
|
|
774
|
+
case 13 /* GTIN13 */:
|
|
775
|
+
if (gtin.startsWith("0")) {
|
|
776
|
+
throw new RangeError(i18n_default.t("IdentificationKey.invalidGTIN13AtRetail", {
|
|
777
|
+
ns: gs1NS
|
|
778
|
+
}));
|
|
779
|
+
}
|
|
780
|
+
PrefixManager.validatePrefix(0 /* GS1CompanyPrefix */, false, false, gtin, true, true);
|
|
781
|
+
gtinLevelRestriction = 0 /* Any */;
|
|
782
|
+
break;
|
|
783
|
+
case 12 /* GTIN12 */:
|
|
784
|
+
PrefixManager.validatePrefix(1 /* UPCCompanyPrefix */, false, false, gtin, true, true);
|
|
785
|
+
gtinLevelRestriction = 0 /* Any */;
|
|
786
|
+
break;
|
|
787
|
+
case 8 /* GTIN8 */:
|
|
788
|
+
if (!gtin.startsWith("0")) {
|
|
789
|
+
PrefixManager.validatePrefix(2 /* GS18Prefix */, false, false, gtin, true, true);
|
|
790
|
+
} else {
|
|
791
|
+
lengthValidatedGTIN = _GTINValidator.zeroExpand(gtin);
|
|
792
|
+
}
|
|
793
|
+
gtinLevelRestriction = 1 /* RetailConsumer */;
|
|
794
|
+
break;
|
|
795
|
+
case 14 /* GTIN14 */:
|
|
796
|
+
PrefixManager.validatePrefix(0 /* GS1CompanyPrefix */, true, true, gtin.substring(1), true, true);
|
|
797
|
+
gtinLevelRestriction = 2 /* OtherThanRetailConsumer */;
|
|
798
|
+
break;
|
|
799
|
+
default:
|
|
800
|
+
throw new RangeError(i18n_default.t("IdentificationKey.invalidGTINLength", {
|
|
801
|
+
ns: gs1NS
|
|
802
|
+
}));
|
|
803
|
+
}
|
|
804
|
+
if (!hasValidCheckDigit(lengthValidatedGTIN)) {
|
|
805
|
+
throw new RangeError(i18n_default.t("IdentificationKey.invalidCheckDigit", {
|
|
806
|
+
ns: gs1NS
|
|
807
|
+
}));
|
|
808
|
+
}
|
|
809
|
+
if (gtinLevel !== 0 /* Any */ && gtinLevelRestriction !== 0 /* Any */ && gtinLevelRestriction !== gtinLevel) {
|
|
810
|
+
throw new RangeError(i18n_default.t(gtinLevel === 1 /* RetailConsumer */ ? "IdentificationKey.invalidGTINAtRetail" : "IdentificationKey.invalidGTINAtOtherThanRetail", {
|
|
811
|
+
ns: gs1NS
|
|
812
|
+
}));
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
/**
|
|
816
|
+
* Validate a GTIN-14.
|
|
817
|
+
*
|
|
818
|
+
* @param gtin14
|
|
819
|
+
* GTIN-14.
|
|
820
|
+
*/
|
|
821
|
+
static validateGTIN14(gtin14) {
|
|
822
|
+
if (gtin14.length !== 14 /* GTIN14 */) {
|
|
823
|
+
throw new RangeError(i18n_default.t("IdentificationKey.invalidGTIN14Length", {
|
|
824
|
+
ns: gs1NS
|
|
825
|
+
}));
|
|
826
|
+
}
|
|
827
|
+
GTINCreator.validateAny(gtin14);
|
|
828
|
+
}
|
|
829
|
+
};
|
|
830
|
+
var NonGTINNumericIdentificationKeyValidator = class extends AbstractNumericIdentificationKeyValidator {
|
|
831
|
+
/**
|
|
832
|
+
* Constructor.
|
|
833
|
+
*
|
|
834
|
+
* @param identificationKeyType
|
|
835
|
+
* Identification key type.
|
|
836
|
+
*
|
|
837
|
+
* @param length
|
|
838
|
+
* Length.
|
|
839
|
+
*
|
|
840
|
+
* @param leaderType
|
|
841
|
+
* Leader type.
|
|
842
|
+
*/
|
|
843
|
+
constructor(identificationKeyType, length, leaderType = 0 /* None */) {
|
|
844
|
+
super(identificationKeyType, 0 /* GS1CompanyPrefix */, length, leaderType);
|
|
845
|
+
}
|
|
846
|
+
};
|
|
847
|
+
var SerializableNumericIdentificationKeyValidator = class _SerializableNumericIdentificationKeyValidator extends NonGTINNumericIdentificationKeyValidator {
|
|
848
|
+
/**
|
|
849
|
+
* Serial component length.
|
|
850
|
+
*/
|
|
851
|
+
_serialComponentLength;
|
|
852
|
+
/**
|
|
853
|
+
* Serial component character set.
|
|
854
|
+
*/
|
|
855
|
+
_serialComponentCharacterSet;
|
|
856
|
+
/**
|
|
857
|
+
* Serial component validation parameters.
|
|
858
|
+
*/
|
|
859
|
+
_serialComponentValidation;
|
|
860
|
+
/**
|
|
861
|
+
* Serial component creator.
|
|
862
|
+
*/
|
|
863
|
+
_serialComponentCreator;
|
|
864
|
+
/**
|
|
865
|
+
* Constructor.
|
|
866
|
+
*
|
|
867
|
+
* @param identificationKeyType
|
|
868
|
+
* Identification key type.
|
|
869
|
+
*
|
|
870
|
+
* @param length
|
|
871
|
+
* Length.
|
|
872
|
+
*
|
|
873
|
+
* @param serialComponentLength
|
|
874
|
+
* Serial component length.
|
|
875
|
+
*
|
|
876
|
+
* @param serialComponentCharacterSet
|
|
877
|
+
* Serial component character set.
|
|
878
|
+
*/
|
|
879
|
+
constructor(identificationKeyType, length, serialComponentLength, serialComponentCharacterSet) {
|
|
880
|
+
super(identificationKeyType, length, 0 /* None */);
|
|
881
|
+
this._serialComponentLength = serialComponentLength;
|
|
882
|
+
this._serialComponentCharacterSet = serialComponentCharacterSet;
|
|
883
|
+
this._serialComponentValidation = {
|
|
884
|
+
minimumLength: 1,
|
|
885
|
+
maximumLength: serialComponentLength,
|
|
886
|
+
component: () => i18n_default.t("IdentificationKey.serialComponent", {
|
|
887
|
+
ns: gs1NS
|
|
888
|
+
})
|
|
889
|
+
};
|
|
890
|
+
this._serialComponentCreator = _SerializableNumericIdentificationKeyValidator.creatorFor(serialComponentCharacterSet);
|
|
891
|
+
}
|
|
892
|
+
/**
|
|
893
|
+
* Get the serial component length.
|
|
894
|
+
*/
|
|
895
|
+
get serialComponentLength() {
|
|
896
|
+
return this._serialComponentLength;
|
|
897
|
+
}
|
|
898
|
+
/**
|
|
899
|
+
* Get the serial component character set.
|
|
900
|
+
*/
|
|
901
|
+
get serialComponentCharacterSet() {
|
|
902
|
+
return this._serialComponentCharacterSet;
|
|
903
|
+
}
|
|
904
|
+
/**
|
|
905
|
+
* Get the serial component validation parameters.
|
|
906
|
+
*/
|
|
907
|
+
get serialComponentValidation() {
|
|
908
|
+
return this._serialComponentValidation;
|
|
909
|
+
}
|
|
910
|
+
/**
|
|
911
|
+
* Get the serial component creator.
|
|
912
|
+
*/
|
|
913
|
+
get serialComponentCreator() {
|
|
914
|
+
return this._serialComponentCreator;
|
|
915
|
+
}
|
|
916
|
+
/**
|
|
917
|
+
* @inheritDoc
|
|
918
|
+
*/
|
|
919
|
+
validate(identificationKey, validation) {
|
|
920
|
+
super.validate(identificationKey.substring(0, this.length), validation);
|
|
921
|
+
if (identificationKey.length > this.length) {
|
|
922
|
+
this.serialComponentCreator.validate(identificationKey.substring(this.length), this._serialComponentValidation);
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
};
|
|
926
|
+
var NonNumericIdentificationKeyValidator = class _NonNumericIdentificationKeyValidator extends AbstractIdentificationKeyValidator {
|
|
927
|
+
/**
|
|
928
|
+
* Validator to ensure that an identification key (minus check character pair) is not all numeric.
|
|
929
|
+
*/
|
|
930
|
+
static NOT_ALL_NUMERIC_VALIDATOR = new class extends import_utility3.RegExpValidator {
|
|
931
|
+
/**
|
|
932
|
+
* @inheritDoc
|
|
933
|
+
*/
|
|
934
|
+
createErrorMessage(_s) {
|
|
935
|
+
return i18n_default.t("IdentificationKey.referenceCantBeAllNumeric", {
|
|
936
|
+
ns: gs1NS
|
|
937
|
+
});
|
|
938
|
+
}
|
|
939
|
+
}(/\D/);
|
|
940
|
+
/**
|
|
941
|
+
* True if the identification key requires a check character pair.
|
|
942
|
+
*/
|
|
943
|
+
_requiresCheckCharacterPair;
|
|
944
|
+
/**
|
|
945
|
+
* Constructor.
|
|
946
|
+
*
|
|
947
|
+
* @param identificationKeyType
|
|
948
|
+
* Identification key type.
|
|
949
|
+
*
|
|
950
|
+
* @param length
|
|
951
|
+
* Length.
|
|
952
|
+
*
|
|
953
|
+
* @param referenceCharacterSet
|
|
954
|
+
* Reference character set.
|
|
955
|
+
*
|
|
956
|
+
* @param requiresCheckCharacterPair
|
|
957
|
+
* True if the identification key requires a check character pair.
|
|
958
|
+
*/
|
|
959
|
+
constructor(identificationKeyType, length, referenceCharacterSet, requiresCheckCharacterPair = false) {
|
|
960
|
+
super(identificationKeyType, 0 /* GS1CompanyPrefix */, length, referenceCharacterSet);
|
|
961
|
+
this._requiresCheckCharacterPair = requiresCheckCharacterPair;
|
|
962
|
+
}
|
|
963
|
+
/**
|
|
964
|
+
* Determine if the identification key requires a check character pair.
|
|
965
|
+
*/
|
|
966
|
+
get requiresCheckCharacterPair() {
|
|
967
|
+
return this._requiresCheckCharacterPair;
|
|
968
|
+
}
|
|
969
|
+
/**
|
|
970
|
+
* Validate a non-numeric identification key and throw an error if validation fails.
|
|
971
|
+
*
|
|
972
|
+
* @param identificationKey
|
|
973
|
+
* Identification key.
|
|
974
|
+
*
|
|
975
|
+
* @param validation
|
|
976
|
+
* Validation parameters.
|
|
977
|
+
*/
|
|
978
|
+
validate(identificationKey, validation) {
|
|
979
|
+
const partialIdentificationKey = this.requiresCheckCharacterPair ? identificationKey.substring(0, identificationKey.length - 2) : identificationKey;
|
|
980
|
+
super.validatePrefix(partialIdentificationKey, validation?.positionOffset);
|
|
981
|
+
if (!this.requiresCheckCharacterPair) {
|
|
982
|
+
this.referenceCreator.validate(identificationKey, {
|
|
983
|
+
maximumLength: this.length,
|
|
984
|
+
positionOffset: validation?.positionOffset
|
|
985
|
+
});
|
|
986
|
+
} else if (!hasValidCheckCharacterPair(this.padIdentificationKey(identificationKey, validation))) {
|
|
987
|
+
throw new RangeError(i18n_default.t("IdentificationKey.invalidCheckCharacterPair", {
|
|
988
|
+
ns: gs1NS
|
|
989
|
+
}));
|
|
990
|
+
}
|
|
991
|
+
if (validation?.exclusion === import_utility3.Exclusion.AllNumeric) {
|
|
992
|
+
_NonNumericIdentificationKeyValidator.NOT_ALL_NUMERIC_VALIDATOR.validate(partialIdentificationKey);
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
};
|
|
996
|
+
var GTIN13_VALIDATOR = new GTINValidator(13 /* GTIN13 */);
|
|
997
|
+
var GTIN12_VALIDATOR = new GTINValidator(12 /* GTIN12 */);
|
|
998
|
+
var GTIN8_VALIDATOR = new GTINValidator(8 /* GTIN8 */);
|
|
999
|
+
var GTIN_VALIDATORS = [
|
|
1000
|
+
GTIN13_VALIDATOR,
|
|
1001
|
+
GTIN12_VALIDATOR,
|
|
1002
|
+
GTIN8_VALIDATOR
|
|
1003
|
+
];
|
|
1004
|
+
var GLN_VALIDATOR = new NonGTINNumericIdentificationKeyValidator("GLN" /* GLN */, 13);
|
|
1005
|
+
var SSCC_VALIDATOR = new NonGTINNumericIdentificationKeyValidator("SSCC" /* SSCC */, 18, 2 /* ExtensionDigit */);
|
|
1006
|
+
var GRAI_VALIDATOR = new SerializableNumericIdentificationKeyValidator("GRAI" /* GRAI */, 13, 16, 1 /* AI82 */);
|
|
1007
|
+
var GIAI_VALIDATOR = new NonNumericIdentificationKeyValidator("GIAI" /* GIAI */, 30, 1 /* AI82 */);
|
|
1008
|
+
var GSRN_VALIDATOR = new NonGTINNumericIdentificationKeyValidator("GSRN" /* GSRN */, 18);
|
|
1009
|
+
var GDTI_VALIDATOR = new SerializableNumericIdentificationKeyValidator("GDTI" /* GDTI */, 13, 17, 1 /* AI82 */);
|
|
1010
|
+
var GINC_VALIDATOR = new NonNumericIdentificationKeyValidator("GINC" /* GINC */, 30, 1 /* AI82 */);
|
|
1011
|
+
var GSIN_VALIDATOR = new NonGTINNumericIdentificationKeyValidator("GSIN" /* GSIN */, 17);
|
|
1012
|
+
var GCN_VALIDATOR = new SerializableNumericIdentificationKeyValidator("GCN" /* GCN */, 13, 12, 0 /* Numeric */);
|
|
1013
|
+
var CPID_VALIDATOR = new NonNumericIdentificationKeyValidator("CPID" /* CPID */, 30, 2 /* AI39 */);
|
|
1014
|
+
var GMN_VALIDATOR = new NonNumericIdentificationKeyValidator("GMN" /* GMN */, 25, 1 /* AI82 */, true);
|
|
1015
|
+
var AbstractIdentificationKeyCreator = class {
|
|
1016
|
+
/**
|
|
1017
|
+
* Prefix manager.
|
|
1018
|
+
*/
|
|
1019
|
+
_prefixManager;
|
|
1020
|
+
/**
|
|
1021
|
+
* Reference length.
|
|
1022
|
+
*/
|
|
1023
|
+
_referenceLength;
|
|
1024
|
+
/**
|
|
1025
|
+
* Initialize the prefix manager. This method is in lieu of a constructor due to the mixin architecture.
|
|
1026
|
+
*
|
|
1027
|
+
* @param prefixManager
|
|
1028
|
+
* Prefix manager.
|
|
1029
|
+
*
|
|
1030
|
+
* @param prefix
|
|
1031
|
+
* Prefix within prefix manager to use to calculate reference length.
|
|
1032
|
+
*
|
|
1033
|
+
* @param checkAllowance
|
|
1034
|
+
* Number of characters to allow for check digit or check character pair.
|
|
1035
|
+
*/
|
|
1036
|
+
init(prefixManager, prefix, checkAllowance) {
|
|
1037
|
+
this._prefixManager = prefixManager;
|
|
1038
|
+
this._referenceLength = this.length - prefix.length - checkAllowance;
|
|
1039
|
+
}
|
|
1040
|
+
/**
|
|
1041
|
+
* @inheritDoc
|
|
1042
|
+
*/
|
|
1043
|
+
get prefixManager() {
|
|
1044
|
+
return this._prefixManager;
|
|
1045
|
+
}
|
|
1046
|
+
/**
|
|
1047
|
+
* @inheritDoc
|
|
1048
|
+
*/
|
|
1049
|
+
get prefix() {
|
|
1050
|
+
return this.prefixManager.gs1CompanyPrefix;
|
|
1051
|
+
}
|
|
1052
|
+
/**
|
|
1053
|
+
* @inheritDoc
|
|
1054
|
+
*/
|
|
1055
|
+
get referenceLength() {
|
|
1056
|
+
return this._referenceLength;
|
|
1057
|
+
}
|
|
1058
|
+
};
|
|
1059
|
+
var AbstractNumericIdentificationKeyCreator = class _AbstractNumericIdentificationKeyCreator extends AbstractIdentificationKeyCreator {
|
|
1060
|
+
/**
|
|
1061
|
+
* Capacity.
|
|
1062
|
+
*/
|
|
1063
|
+
_capacity;
|
|
1064
|
+
/**
|
|
1065
|
+
* Tweak for sparse creation.
|
|
1066
|
+
*/
|
|
1067
|
+
_tweak = 0n;
|
|
1068
|
+
/**
|
|
1069
|
+
* Initialize the prefix manager. This method is in lieu of a constructor due to the mixin architecture.
|
|
1070
|
+
*
|
|
1071
|
+
* @param prefixManager
|
|
1072
|
+
* Prefix manager.
|
|
1073
|
+
*
|
|
1074
|
+
* @param prefix
|
|
1075
|
+
* Prefix within prefix manager to use to calculate reference length.
|
|
1076
|
+
*/
|
|
1077
|
+
init(prefixManager, prefix) {
|
|
1078
|
+
super.init(prefixManager, prefix, 1);
|
|
1079
|
+
this._capacity = Number(import_utility3.CharacterSetCreator.powerOf10(this.referenceLength));
|
|
1080
|
+
}
|
|
1081
|
+
/**
|
|
1082
|
+
* @inheritDoc
|
|
1083
|
+
*/
|
|
1084
|
+
get capacity() {
|
|
1085
|
+
return this._capacity;
|
|
1086
|
+
}
|
|
1087
|
+
/**
|
|
1088
|
+
* Get the tweak for sparse creation.
|
|
1089
|
+
*/
|
|
1090
|
+
get tweak() {
|
|
1091
|
+
return this._tweak;
|
|
1092
|
+
}
|
|
1093
|
+
/**
|
|
1094
|
+
* Set the tweak for sparse creation.
|
|
1095
|
+
*/
|
|
1096
|
+
set tweak(value) {
|
|
1097
|
+
this._tweak = value;
|
|
1098
|
+
}
|
|
1099
|
+
/**
|
|
1100
|
+
* Build an identification key from a reference by merging it with the prefix and adding the check digit.
|
|
1101
|
+
*
|
|
1102
|
+
* @param reference
|
|
1103
|
+
* Identification key reference.
|
|
1104
|
+
*
|
|
1105
|
+
* @returns
|
|
1106
|
+
* Identification key.
|
|
1107
|
+
*/
|
|
1108
|
+
buildIdentificationKey(reference) {
|
|
1109
|
+
const partialIdentificationKey = this.leaderType === 2 /* ExtensionDigit */ ? reference.substring(0, 1) + this.prefix + reference.substring(1) : this.prefix + reference;
|
|
1110
|
+
return partialIdentificationKey + checkDigit(partialIdentificationKey);
|
|
1111
|
+
}
|
|
1112
|
+
/**
|
|
1113
|
+
* @inheritDoc
|
|
1114
|
+
*/
|
|
1115
|
+
create(valueOrValues, sparse = false) {
|
|
1116
|
+
return import_utility3.NUMERIC_CREATOR.create(this.referenceLength, valueOrValues, import_utility3.Exclusion.None, sparse ? this.tweak : void 0, (reference) => this.buildIdentificationKey(reference));
|
|
1117
|
+
}
|
|
1118
|
+
/**
|
|
1119
|
+
* Create all identification keys from a partial identification key. Call is recursive until remaining reference
|
|
1120
|
+
* length is 0.
|
|
1121
|
+
*
|
|
1122
|
+
* @param partialIdentificationKey
|
|
1123
|
+
* Partial identification key. Initial value is `this.prefix`.
|
|
1124
|
+
*
|
|
1125
|
+
* @param remainingReferenceLength
|
|
1126
|
+
* Remaining reference length. Initial value is `this.referenceLength`.
|
|
1127
|
+
*
|
|
1128
|
+
* @param extensionWeight
|
|
1129
|
+
* If this value is not zero, the identification key has an extension digit, this call is setting it, and this value
|
|
1130
|
+
* is applied to the calculation of the check digit.
|
|
1131
|
+
*
|
|
1132
|
+
* @param weight
|
|
1133
|
+
* If the extension weight is zero, this value is applied to the calculation of the check digit.
|
|
1134
|
+
*
|
|
1135
|
+
* @param partialCheckDigitSum
|
|
1136
|
+
* Partial check digit sum for the partial identification key.
|
|
1137
|
+
*
|
|
1138
|
+
* @yields
|
|
1139
|
+
* Identification key.
|
|
1140
|
+
*/
|
|
1141
|
+
static *createAllPartial(partialIdentificationKey, remainingReferenceLength, extensionWeight, weight, partialCheckDigitSum) {
|
|
1142
|
+
if (remainingReferenceLength === 0) {
|
|
1143
|
+
yield partialIdentificationKey + import_utility3.NUMERIC_CREATOR.character(9 - (partialCheckDigitSum + 9) % 10);
|
|
1144
|
+
} else {
|
|
1145
|
+
const nextRemainingReferenceLength = remainingReferenceLength - 1;
|
|
1146
|
+
let nextPartialCheckDigitSum = partialCheckDigitSum;
|
|
1147
|
+
if (extensionWeight !== 0) {
|
|
1148
|
+
for (const c of import_utility3.NUMERIC_CREATOR.characterSet) {
|
|
1149
|
+
yield* _AbstractNumericIdentificationKeyCreator.createAllPartial(c + partialIdentificationKey, nextRemainingReferenceLength, 0, weight, nextPartialCheckDigitSum);
|
|
1150
|
+
nextPartialCheckDigitSum += extensionWeight;
|
|
1151
|
+
}
|
|
1152
|
+
} else {
|
|
1153
|
+
const nextWeight = 4 - weight;
|
|
1154
|
+
for (const c of import_utility3.NUMERIC_CREATOR.characterSet) {
|
|
1155
|
+
yield* _AbstractNumericIdentificationKeyCreator.createAllPartial(partialIdentificationKey + c, nextRemainingReferenceLength, 0, nextWeight, nextPartialCheckDigitSum);
|
|
1156
|
+
nextPartialCheckDigitSum += weight;
|
|
1157
|
+
}
|
|
1158
|
+
}
|
|
1159
|
+
}
|
|
1160
|
+
}
|
|
1161
|
+
/**
|
|
1162
|
+
* @inheritDoc
|
|
1163
|
+
*/
|
|
1164
|
+
createAll() {
|
|
1165
|
+
const hasExtensionDigit = this.leaderType === 2 /* ExtensionDigit */;
|
|
1166
|
+
const prefix = this.prefix;
|
|
1167
|
+
const referenceLength = this.referenceLength;
|
|
1168
|
+
const startWeight = 3 - 2 * ((referenceLength + 1 - Number(hasExtensionDigit)) % 2);
|
|
1169
|
+
return _AbstractNumericIdentificationKeyCreator.createAllPartial(prefix, referenceLength, hasExtensionDigit ? 3 - 2 * this.length % 2 : 0, startWeight, checkDigitSum(startWeight === 3, prefix));
|
|
1170
|
+
}
|
|
1171
|
+
};
|
|
1172
|
+
var GTINCreator = class _GTINCreator extends (0, import_ts_mixer.Mixin)(GTINValidator, AbstractNumericIdentificationKeyCreator) {
|
|
1173
|
+
/**
|
|
1174
|
+
* Validation parameters for required indicator digit.
|
|
1175
|
+
*/
|
|
1176
|
+
static REQUIRED_INDICATOR_DIGIT_VALIDATION = {
|
|
1177
|
+
minimumLength: 1,
|
|
1178
|
+
maximumLength: 1,
|
|
1179
|
+
component: () => i18n_default.t("IdentificationKey.indicatorDigit", {
|
|
1180
|
+
ns: gs1NS
|
|
1181
|
+
})
|
|
1182
|
+
};
|
|
1183
|
+
/**
|
|
1184
|
+
* Validation parameters for optional indicator digit.
|
|
1185
|
+
*/
|
|
1186
|
+
static OPTIONAL_INDICATOR_DIGIT_VALIDATION = {
|
|
1187
|
+
minimumLength: 0,
|
|
1188
|
+
maximumLength: 1,
|
|
1189
|
+
component: () => i18n_default.t("IdentificationKey.indicatorDigit", {
|
|
1190
|
+
ns: gs1NS
|
|
1191
|
+
})
|
|
1192
|
+
};
|
|
1193
|
+
/**
|
|
1194
|
+
* Constructor. Called internally by {@link PrefixManager.gtinCreator}; should not be called by other code.
|
|
1195
|
+
*
|
|
1196
|
+
* @param prefixManager
|
|
1197
|
+
* Prefix manager.
|
|
1198
|
+
*
|
|
1199
|
+
* @param gtinType
|
|
1200
|
+
* GTIN type.
|
|
1201
|
+
*/
|
|
1202
|
+
constructor(prefixManager, gtinType) {
|
|
1203
|
+
super(gtinType);
|
|
1204
|
+
this.init(prefixManager, prefixManager.prefix);
|
|
1205
|
+
}
|
|
1206
|
+
/**
|
|
1207
|
+
* @inheritDoc
|
|
1208
|
+
*/
|
|
1209
|
+
get prefix() {
|
|
1210
|
+
return this.prefixManager.prefix;
|
|
1211
|
+
}
|
|
1212
|
+
/**
|
|
1213
|
+
* Create GTIN-14(s) with an indicator digit and reference(s) based on numeric value(s). The value(s) is/are
|
|
1214
|
+
* converted to reference(s) of the appropriate length using {@linkcode NUMERIC_CREATOR}.
|
|
1215
|
+
*
|
|
1216
|
+
* @param indicatorDigit
|
|
1217
|
+
* Indicator digit.
|
|
1218
|
+
*
|
|
1219
|
+
* @param valueOrValues
|
|
1220
|
+
* Numeric value(s).
|
|
1221
|
+
*
|
|
1222
|
+
* @param sparse
|
|
1223
|
+
* If true, the value(s) is/are mapped to a sparse sequence resistant to discovery. Default is false.
|
|
1224
|
+
*
|
|
1225
|
+
* @returns
|
|
1226
|
+
* GTIN-14(s).
|
|
1227
|
+
*/
|
|
1228
|
+
createGTIN14(indicatorDigit, valueOrValues, sparse = false) {
|
|
1229
|
+
import_utility3.NUMERIC_CREATOR.validate(indicatorDigit, _GTINCreator.REQUIRED_INDICATOR_DIGIT_VALIDATION);
|
|
1230
|
+
return import_utility3.NUMERIC_CREATOR.create(13 /* GTIN13 */ - this.prefixManager.gs1CompanyPrefix.length - 1, valueOrValues, import_utility3.Exclusion.None, sparse ? this.tweak : void 0, (reference) => {
|
|
1231
|
+
const partialIdentificationKey = indicatorDigit + this.prefixManager.gs1CompanyPrefix + reference;
|
|
1232
|
+
return partialIdentificationKey + checkDigit(partialIdentificationKey);
|
|
1233
|
+
});
|
|
1234
|
+
}
|
|
1235
|
+
/**
|
|
1236
|
+
* Zero suppress a GTIN-12.
|
|
1237
|
+
*
|
|
1238
|
+
* @param gtin12
|
|
1239
|
+
* GTIN-12.
|
|
1240
|
+
*
|
|
1241
|
+
* @returns
|
|
1242
|
+
* Zero-suppressed GTIN-12.
|
|
1243
|
+
*/
|
|
1244
|
+
static zeroSuppress(gtin12) {
|
|
1245
|
+
GTIN12_VALIDATOR.validate(gtin12);
|
|
1246
|
+
const d = Array.from(gtin12);
|
|
1247
|
+
let zeroSuppressedGTIN12;
|
|
1248
|
+
if (d[0] === "0" && d[6] === "0" && d[7] === "0") {
|
|
1249
|
+
if (d[10] >= "5" && d[8] === "0" && d[9] === "0" && d[5] !== "0") {
|
|
1250
|
+
zeroSuppressedGTIN12 = `0${d[1]}${d[2]}${d[3]}${d[4]}${d[5]}${d[10]}${d[11]}`;
|
|
1251
|
+
} else if (d[5] === "0" && d[8] === "0" && d[9] === "0" && d[4] !== "0") {
|
|
1252
|
+
zeroSuppressedGTIN12 = `0${d[1]}${d[2]}${d[3]}${d[4]}${d[10]}4${d[11]}`;
|
|
1253
|
+
} else if (d[3] <= "2" && d[4] === "0" && d[5] === "0") {
|
|
1254
|
+
zeroSuppressedGTIN12 = `0${d[1]}${d[2]}${d[8]}${d[9]}${d[10]}${d[3]}${d[11]}`;
|
|
1255
|
+
} else if (d[3] >= "3" && d[4] === "0" && d[5] === "0" && d[8] === "0") {
|
|
1256
|
+
zeroSuppressedGTIN12 = `0${d[1]}${d[2]}${d[3]}${d[9]}${d[10]}3${d[11]}`;
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
1259
|
+
if (zeroSuppressedGTIN12 === void 0) {
|
|
1260
|
+
throw new RangeError(i18n_default.t("IdentificationKey.invalidZeroSuppressibleGTIN12", {
|
|
1261
|
+
ns: gs1NS
|
|
1262
|
+
}));
|
|
1263
|
+
}
|
|
1264
|
+
return zeroSuppressedGTIN12;
|
|
1265
|
+
}
|
|
1266
|
+
/**
|
|
1267
|
+
* Convert a GTIN of any length to a GTIN-14 with an optional indicator digit.
|
|
1268
|
+
*
|
|
1269
|
+
* @param indicatorDigit
|
|
1270
|
+
* Indicator digit. If blank, assumes "0" if the GTIN is not already a GTIN-14.
|
|
1271
|
+
*
|
|
1272
|
+
* @param gtin
|
|
1273
|
+
* GTIN.
|
|
1274
|
+
*
|
|
1275
|
+
* @returns
|
|
1276
|
+
* GTIN-14.
|
|
1277
|
+
*/
|
|
1278
|
+
static convertToGTIN14(indicatorDigit, gtin) {
|
|
1279
|
+
_GTINCreator.validateAny(gtin);
|
|
1280
|
+
import_utility3.NUMERIC_CREATOR.validate(indicatorDigit, _GTINCreator.OPTIONAL_INDICATOR_DIGIT_VALIDATION);
|
|
1281
|
+
const gtinLength = gtin.length;
|
|
1282
|
+
let gtin14 = "0".repeat(14 /* GTIN14 */ - gtinLength) + gtin;
|
|
1283
|
+
if (indicatorDigit.length !== 0 && indicatorDigit !== gtin14.charAt(0)) {
|
|
1284
|
+
const partialGTIN14 = indicatorDigit + gtin14.substring(1, 14 /* GTIN14 */ - 1);
|
|
1285
|
+
gtin14 = partialGTIN14 + checkDigit(partialGTIN14);
|
|
1286
|
+
}
|
|
1287
|
+
return gtin14;
|
|
1288
|
+
}
|
|
1289
|
+
/**
|
|
1290
|
+
* Normalize a GTIN of any length.
|
|
1291
|
+
* - A GTIN-14 that starts with six zeros or a GTIN-13 that starts with five zeros is normalized to GTIN-8.
|
|
1292
|
+
* - A GTIN-14 that starts with two zeros or a GTIN-13 that starts with one zero is normalized to GTIN-12.
|
|
1293
|
+
* - A GTIN-14 that starts with one zero is normalized to GTIN-13.
|
|
1294
|
+
* - Otherwise, the GTIN is unchanged.
|
|
1295
|
+
*
|
|
1296
|
+
* @param gtin
|
|
1297
|
+
* GTIN.
|
|
1298
|
+
*
|
|
1299
|
+
* @returns
|
|
1300
|
+
* Normalized GTIN.
|
|
1301
|
+
*/
|
|
1302
|
+
static normalize(gtin) {
|
|
1303
|
+
const gtinLength = gtin.length;
|
|
1304
|
+
let normalizedGTIN;
|
|
1305
|
+
switch (gtinLength) {
|
|
1306
|
+
case 13 /* GTIN13 */:
|
|
1307
|
+
if (!gtin.startsWith("0")) {
|
|
1308
|
+
normalizedGTIN = gtin;
|
|
1309
|
+
} else if (!gtin.startsWith("00000")) {
|
|
1310
|
+
normalizedGTIN = gtin.substring(1);
|
|
1311
|
+
} else if (!gtin.startsWith("000000")) {
|
|
1312
|
+
normalizedGTIN = gtin.substring(5);
|
|
1313
|
+
} else {
|
|
1314
|
+
throw new RangeError(i18n_default.t("IdentificationKey.invalidZeroSuppressedGTIN12AsGTIN13", {
|
|
1315
|
+
ns: gs1NS
|
|
1316
|
+
}));
|
|
1317
|
+
}
|
|
1318
|
+
break;
|
|
1319
|
+
case 12 /* GTIN12 */:
|
|
1320
|
+
normalizedGTIN = gtin;
|
|
1321
|
+
break;
|
|
1322
|
+
case 8 /* GTIN8 */:
|
|
1323
|
+
if (!gtin.startsWith("0")) {
|
|
1324
|
+
normalizedGTIN = gtin;
|
|
1325
|
+
} else {
|
|
1326
|
+
normalizedGTIN = _GTINCreator.zeroExpand(gtin);
|
|
1327
|
+
}
|
|
1328
|
+
break;
|
|
1329
|
+
case 14 /* GTIN14 */:
|
|
1330
|
+
if (!gtin.startsWith("0")) {
|
|
1331
|
+
normalizedGTIN = gtin;
|
|
1332
|
+
} else if (!gtin.startsWith("00")) {
|
|
1333
|
+
normalizedGTIN = gtin.substring(1);
|
|
1334
|
+
} else if (!gtin.startsWith("000000")) {
|
|
1335
|
+
normalizedGTIN = gtin.substring(2);
|
|
1336
|
+
} else if (!gtin.startsWith("0000000")) {
|
|
1337
|
+
normalizedGTIN = gtin.substring(6);
|
|
1338
|
+
} else {
|
|
1339
|
+
throw new RangeError(i18n_default.t("IdentificationKey.invalidZeroSuppressedGTIN12AsGTIN14", {
|
|
1340
|
+
ns: gs1NS
|
|
1341
|
+
}));
|
|
1342
|
+
}
|
|
1343
|
+
break;
|
|
1344
|
+
default:
|
|
1345
|
+
throw new RangeError(i18n_default.t("IdentificationKey.invalidGTINLength", {
|
|
1346
|
+
ns: gs1NS
|
|
1347
|
+
}));
|
|
1348
|
+
}
|
|
1349
|
+
_GTINCreator.validateAny(normalizedGTIN);
|
|
1350
|
+
return normalizedGTIN;
|
|
1351
|
+
}
|
|
1352
|
+
};
|
|
1353
|
+
var NonGTINNumericIdentificationKeyCreator = class extends (0, import_ts_mixer.Mixin)(NonGTINNumericIdentificationKeyValidator, AbstractNumericIdentificationKeyCreator) {
|
|
1354
|
+
/**
|
|
1355
|
+
* Constructor. Called internally by {@link PrefixManager} non-GTIN numeric identification key creator getters;
|
|
1356
|
+
* should not be called by other code.
|
|
1357
|
+
*
|
|
1358
|
+
* @param prefixManager
|
|
1359
|
+
* Prefix manager.
|
|
1360
|
+
*
|
|
1361
|
+
* @param identificationKeyType
|
|
1362
|
+
* Identification key type.
|
|
1363
|
+
*
|
|
1364
|
+
* @param length
|
|
1365
|
+
* Length.
|
|
1366
|
+
*
|
|
1367
|
+
* @param leaderType
|
|
1368
|
+
* Leader type.
|
|
1369
|
+
*/
|
|
1370
|
+
constructor(prefixManager, identificationKeyType, length, leaderType = 0 /* None */) {
|
|
1371
|
+
super(identificationKeyType, length, leaderType);
|
|
1372
|
+
this.init(prefixManager, prefixManager.gs1CompanyPrefix);
|
|
1373
|
+
}
|
|
1374
|
+
};
|
|
1375
|
+
var SerializableNumericIdentificationKeyCreator = class extends (0, import_ts_mixer.Mixin)(SerializableNumericIdentificationKeyValidator, AbstractNumericIdentificationKeyCreator) {
|
|
1376
|
+
/**
|
|
1377
|
+
* Constructor. Called internally by {@link PrefixManager} serialized numeric identification key creator getters;
|
|
1378
|
+
* should not be called by other code.
|
|
1379
|
+
*
|
|
1380
|
+
* @param prefixManager
|
|
1381
|
+
* Prefix manager.
|
|
1382
|
+
*
|
|
1383
|
+
* @param identificationKeyType
|
|
1384
|
+
* Identification key type.
|
|
1385
|
+
*
|
|
1386
|
+
* @param length
|
|
1387
|
+
* Length.
|
|
1388
|
+
*
|
|
1389
|
+
* @param serialComponentLength
|
|
1390
|
+
* Serial component length.
|
|
1391
|
+
*
|
|
1392
|
+
* @param serialComponentCharacterSet
|
|
1393
|
+
* Serial component character set.
|
|
1394
|
+
*/
|
|
1395
|
+
constructor(prefixManager, identificationKeyType, length, serialComponentLength, serialComponentCharacterSet) {
|
|
1396
|
+
super(identificationKeyType, length, serialComponentLength, serialComponentCharacterSet);
|
|
1397
|
+
this.init(prefixManager, prefixManager.gs1CompanyPrefix);
|
|
1398
|
+
}
|
|
1399
|
+
/**
|
|
1400
|
+
* Concatenate a validated base identification key with serial component(s).
|
|
1401
|
+
*
|
|
1402
|
+
* @param baseIdentificationKey
|
|
1403
|
+
* Base identification key.
|
|
1404
|
+
*
|
|
1405
|
+
* @param serialComponentOrComponents
|
|
1406
|
+
* Serial component(s).
|
|
1407
|
+
*
|
|
1408
|
+
* @returns
|
|
1409
|
+
* Serialized identification key(s).
|
|
1410
|
+
*/
|
|
1411
|
+
concatenateValidated(baseIdentificationKey, serialComponentOrComponents) {
|
|
1412
|
+
let result;
|
|
1413
|
+
const serialComponentCreator = this.serialComponentCreator;
|
|
1414
|
+
const serialComponentValidation = this.serialComponentValidation;
|
|
1415
|
+
function validateAndConcatenate(serialComponent) {
|
|
1416
|
+
serialComponentCreator.validate(serialComponent, serialComponentValidation);
|
|
1417
|
+
return baseIdentificationKey + serialComponent;
|
|
1418
|
+
}
|
|
1419
|
+
if (typeof serialComponentOrComponents !== "object") {
|
|
1420
|
+
result = validateAndConcatenate(serialComponentOrComponents);
|
|
1421
|
+
} else {
|
|
1422
|
+
result = import_utility3.IteratorProxy.from(serialComponentOrComponents).map(validateAndConcatenate);
|
|
1423
|
+
}
|
|
1424
|
+
return result;
|
|
1425
|
+
}
|
|
1426
|
+
/**
|
|
1427
|
+
* Create serialized identification key(s) with a reference based on a numeric value concatenated with serial
|
|
1428
|
+
* component(s). The value is converted to a reference of the appropriate length using {@linkcode NUMERIC_CREATOR}.
|
|
1429
|
+
*
|
|
1430
|
+
* @param value
|
|
1431
|
+
* Numeric value of the reference.
|
|
1432
|
+
*
|
|
1433
|
+
* @param serialComponentOrComponents
|
|
1434
|
+
* Serial component(s).
|
|
1435
|
+
*
|
|
1436
|
+
* @param sparse
|
|
1437
|
+
* If true, the value is mapped to a sparse sequence resistant to discovery. Default is false.
|
|
1438
|
+
*
|
|
1439
|
+
* @returns
|
|
1440
|
+
* Serialized identification keys.
|
|
1441
|
+
*/
|
|
1442
|
+
createSerialized(value, serialComponentOrComponents, sparse) {
|
|
1443
|
+
return this.concatenateValidated(this.create(value, sparse), serialComponentOrComponents);
|
|
1444
|
+
}
|
|
1445
|
+
/**
|
|
1446
|
+
* Concatenate a base identification key with serial component(s).
|
|
1447
|
+
*
|
|
1448
|
+
* @param baseIdentificationKey
|
|
1449
|
+
* Base identification key.
|
|
1450
|
+
*
|
|
1451
|
+
* @param serialComponentOrComponents
|
|
1452
|
+
* Serial component(s).
|
|
1453
|
+
*
|
|
1454
|
+
* @returns
|
|
1455
|
+
* Serialized identification key(s).
|
|
1456
|
+
*/
|
|
1457
|
+
concatenate(baseIdentificationKey, serialComponentOrComponents) {
|
|
1458
|
+
this.validate(baseIdentificationKey);
|
|
1459
|
+
return this.concatenateValidated(baseIdentificationKey, serialComponentOrComponents);
|
|
1460
|
+
}
|
|
1461
|
+
};
|
|
1462
|
+
var NonNumericIdentificationKeyCreator = class extends (0, import_ts_mixer.Mixin)(NonNumericIdentificationKeyValidator, AbstractIdentificationKeyCreator) {
|
|
1463
|
+
/**
|
|
1464
|
+
* Reference validation parameters.
|
|
1465
|
+
*/
|
|
1466
|
+
_referenceValidation;
|
|
1467
|
+
/**
|
|
1468
|
+
* Constructor. Called internally by {@link PrefixManager} non-numeric identification key creator getters; should
|
|
1469
|
+
* not be called by other code.
|
|
1470
|
+
*
|
|
1471
|
+
* @param prefixManager
|
|
1472
|
+
* Prefix manager.
|
|
1473
|
+
*
|
|
1474
|
+
* @param identificationKeyType
|
|
1475
|
+
* Identification key type.
|
|
1476
|
+
*
|
|
1477
|
+
* @param length
|
|
1478
|
+
* Length.
|
|
1479
|
+
*
|
|
1480
|
+
* @param referenceCharacterSet
|
|
1481
|
+
* Reference character set.
|
|
1482
|
+
*
|
|
1483
|
+
* @param requiresCheckCharacterPair
|
|
1484
|
+
* True if the identification key requires a check character pair.
|
|
1485
|
+
*/
|
|
1486
|
+
constructor(prefixManager, identificationKeyType, length, referenceCharacterSet, requiresCheckCharacterPair = false) {
|
|
1487
|
+
super(identificationKeyType, length, referenceCharacterSet, requiresCheckCharacterPair);
|
|
1488
|
+
this.init(prefixManager, prefixManager.gs1CompanyPrefix, 2 * Number(requiresCheckCharacterPair));
|
|
1489
|
+
this._referenceValidation = {
|
|
1490
|
+
minimumLength: 1,
|
|
1491
|
+
// Maximum reference length has to account for prefix and check character pair.
|
|
1492
|
+
maximumLength: this.referenceLength,
|
|
1493
|
+
component: () => i18n_default.t("IdentificationKey.reference", {
|
|
1494
|
+
ns: gs1NS
|
|
1495
|
+
})
|
|
1496
|
+
};
|
|
1497
|
+
}
|
|
1498
|
+
/**
|
|
1499
|
+
* Get the reference validation parameters.
|
|
1500
|
+
*/
|
|
1501
|
+
get referenceValidation() {
|
|
1502
|
+
return this._referenceValidation;
|
|
1503
|
+
}
|
|
1504
|
+
/**
|
|
1505
|
+
* Create identification key(s) with reference(s).
|
|
1506
|
+
*
|
|
1507
|
+
* @param referenceOrReferences
|
|
1508
|
+
* Reference(s).
|
|
1509
|
+
*
|
|
1510
|
+
* @returns
|
|
1511
|
+
* Identification key(s).
|
|
1512
|
+
*/
|
|
1513
|
+
create(referenceOrReferences) {
|
|
1514
|
+
let result;
|
|
1515
|
+
const referenceCreator = this.referenceCreator;
|
|
1516
|
+
const referenceValidation = this.referenceValidation;
|
|
1517
|
+
const prefix = this.prefix;
|
|
1518
|
+
const requiresCheckCharacterPair = this.requiresCheckCharacterPair;
|
|
1519
|
+
function validateAndCreate(reference) {
|
|
1520
|
+
referenceCreator.validate(reference, referenceValidation);
|
|
1521
|
+
const partialIdentificationKey = prefix + reference;
|
|
1522
|
+
return requiresCheckCharacterPair ? partialIdentificationKey + checkCharacterPair(partialIdentificationKey) : partialIdentificationKey;
|
|
1523
|
+
}
|
|
1524
|
+
if (typeof referenceOrReferences !== "object") {
|
|
1525
|
+
result = validateAndCreate(referenceOrReferences);
|
|
1526
|
+
} else {
|
|
1527
|
+
result = import_utility3.IteratorProxy.from(referenceOrReferences).map(validateAndCreate);
|
|
1528
|
+
}
|
|
1529
|
+
return result;
|
|
1530
|
+
}
|
|
1531
|
+
};
|
|
1532
|
+
var PrefixManager = class _PrefixManager {
|
|
1533
|
+
/**
|
|
1534
|
+
* Cached prefix managers, keyed by GS1 Company Prefix.
|
|
1535
|
+
*/
|
|
1536
|
+
static PREFIX_MANAGERS_MAP = /* @__PURE__ */ new Map();
|
|
1537
|
+
/**
|
|
1538
|
+
* GS1 Company Prefix minimum length.
|
|
1539
|
+
*/
|
|
1540
|
+
static GS1_COMPANY_PREFIX_MINIMUM_LENGTH = 4;
|
|
1541
|
+
/**
|
|
1542
|
+
* GS1 Company Prefix maximum length.
|
|
1543
|
+
*/
|
|
1544
|
+
static GS1_COMPANY_PREFIX_MAXIMUM_LENGTH = 12;
|
|
1545
|
+
/**
|
|
1546
|
+
* U.P.C. Company Prefix minimum length.
|
|
1547
|
+
*/
|
|
1548
|
+
static UPC_COMPANY_PREFIX_MINIMUM_LENGTH = 6;
|
|
1549
|
+
/**
|
|
1550
|
+
* U.P.C. Company Prefix maximum length.
|
|
1551
|
+
*/
|
|
1552
|
+
static UPC_COMPANY_PREFIX_MAXIMUM_LENGTH = 11;
|
|
1553
|
+
/**
|
|
1554
|
+
* GS1-8 Prefix minimum length.
|
|
1555
|
+
*/
|
|
1556
|
+
static GS1_8_PREFIX_MINIMUM_LENGTH = 2;
|
|
1557
|
+
/**
|
|
1558
|
+
* GS1-8 Prefix maximum length.
|
|
1559
|
+
*/
|
|
1560
|
+
static GS1_8_PREFIX_MAXIMUM_LENGTH = 7;
|
|
1561
|
+
/**
|
|
1562
|
+
* Validation parameters for GS1 Company Prefix.
|
|
1563
|
+
*/
|
|
1564
|
+
static GS1_COMPANY_PREFIX_VALIDATION = {
|
|
1565
|
+
minimumLength: _PrefixManager.GS1_COMPANY_PREFIX_MINIMUM_LENGTH,
|
|
1566
|
+
maximumLength: _PrefixManager.GS1_COMPANY_PREFIX_MAXIMUM_LENGTH,
|
|
1567
|
+
component: () => i18n_default.t("Prefix.gs1CompanyPrefix", {
|
|
1568
|
+
ns: gs1NS
|
|
1569
|
+
})
|
|
1570
|
+
};
|
|
1571
|
+
/**
|
|
1572
|
+
* Validation parameters for U.P.C. Company Prefix expressed as GS1 Company Prefix.
|
|
1573
|
+
*/
|
|
1574
|
+
static UPC_COMPANY_PREFIX_AS_GS1_COMPANY_PREFIX_VALIDATION = {
|
|
1575
|
+
minimumLength: _PrefixManager.UPC_COMPANY_PREFIX_MINIMUM_LENGTH + 1,
|
|
1576
|
+
maximumLength: _PrefixManager.UPC_COMPANY_PREFIX_MAXIMUM_LENGTH + 1,
|
|
1577
|
+
component: () => i18n_default.t("Prefix.gs1CompanyPrefix", {
|
|
1578
|
+
ns: gs1NS
|
|
1579
|
+
})
|
|
1580
|
+
};
|
|
1581
|
+
/**
|
|
1582
|
+
* Validation parameters for GS1-8 Prefix expressed as GS1 Company Prefix.
|
|
1583
|
+
*/
|
|
1584
|
+
static GS1_8_PREFIX_AS_GS1_COMPANY_PREFIX_VALIDATION = {
|
|
1585
|
+
minimumLength: _PrefixManager.GS1_8_PREFIX_MINIMUM_LENGTH + 5,
|
|
1586
|
+
maximumLength: _PrefixManager.GS1_8_PREFIX_MAXIMUM_LENGTH + 5,
|
|
1587
|
+
component: () => i18n_default.t("Prefix.gs1CompanyPrefix", {
|
|
1588
|
+
ns: gs1NS
|
|
1589
|
+
})
|
|
1590
|
+
};
|
|
1591
|
+
/**
|
|
1592
|
+
* Validation parameters for U.P.C. Company Prefix.
|
|
1593
|
+
*/
|
|
1594
|
+
static UPC_COMPANY_PREFIX_VALIDATION = {
|
|
1595
|
+
minimumLength: _PrefixManager.UPC_COMPANY_PREFIX_MINIMUM_LENGTH,
|
|
1596
|
+
maximumLength: _PrefixManager.UPC_COMPANY_PREFIX_MAXIMUM_LENGTH,
|
|
1597
|
+
component: () => i18n_default.t("Prefix.upcCompanyPrefix", {
|
|
1598
|
+
ns: gs1NS
|
|
1599
|
+
})
|
|
1600
|
+
};
|
|
1601
|
+
/**
|
|
1602
|
+
* Validation parameters for GS1-8 Prefix.
|
|
1603
|
+
*/
|
|
1604
|
+
static GS1_8_PREFIX_VALIDATION = {
|
|
1605
|
+
minimumLength: _PrefixManager.GS1_8_PREFIX_MINIMUM_LENGTH,
|
|
1606
|
+
maximumLength: _PrefixManager.GS1_8_PREFIX_MAXIMUM_LENGTH,
|
|
1607
|
+
component: () => i18n_default.t("Prefix.gs18Prefix", {
|
|
1608
|
+
ns: gs1NS
|
|
1609
|
+
})
|
|
1610
|
+
};
|
|
1611
|
+
/**
|
|
1612
|
+
* Creator tweak factors. Different numeric identification key types have different tweak factors so that sparse
|
|
1613
|
+
* creation generates different sequences for each.
|
|
1614
|
+
*/
|
|
1615
|
+
static CREATOR_TWEAK_FACTORS_MAP = /* @__PURE__ */ new Map([
|
|
1616
|
+
["GTIN" /* GTIN */, 1987n],
|
|
1617
|
+
["GLN" /* GLN */, 4241n],
|
|
1618
|
+
["SSCC" /* SSCC */, 8087n],
|
|
1619
|
+
["GRAI" /* GRAI */, 3221n],
|
|
1620
|
+
["GSRN" /* GSRN */, 2341n],
|
|
1621
|
+
["GDTI" /* GDTI */, 7333n],
|
|
1622
|
+
["GSIN" /* GSIN */, 5623n],
|
|
1623
|
+
["GCN" /* GCN */, 6869n]
|
|
1624
|
+
]);
|
|
1625
|
+
/**
|
|
1626
|
+
* Normalized prefix type.
|
|
1627
|
+
*/
|
|
1628
|
+
_prefixType;
|
|
1629
|
+
/**
|
|
1630
|
+
* Normalized prefix.
|
|
1631
|
+
*/
|
|
1632
|
+
_prefix;
|
|
1633
|
+
/**
|
|
1634
|
+
* Prefix as GS1 Company Prefix.
|
|
1635
|
+
*/
|
|
1636
|
+
_gs1CompanyPrefix;
|
|
1637
|
+
/**
|
|
1638
|
+
* U.P.C. Company Prefix if prefix type is {@link PrefixType.UPCCompanyPrefix}.
|
|
1639
|
+
*/
|
|
1640
|
+
_upcCompanyPrefix;
|
|
1641
|
+
/**
|
|
1642
|
+
* GS1-8 Prefix if prefix type is {@link PrefixType.GS18Prefix}.
|
|
1643
|
+
*/
|
|
1644
|
+
_gs18Prefix;
|
|
1645
|
+
/**
|
|
1646
|
+
* Tweak factor.
|
|
1647
|
+
*/
|
|
1648
|
+
_tweakFactor = 0n;
|
|
1649
|
+
/**
|
|
1650
|
+
* Cached identification key creators.
|
|
1651
|
+
*/
|
|
1652
|
+
_identificationKeyCreatorsMap = /* @__PURE__ */ new Map();
|
|
1653
|
+
/**
|
|
1654
|
+
* Constructor.
|
|
1655
|
+
*
|
|
1656
|
+
* @param gs1CompanyPrefix
|
|
1657
|
+
* GS1 Company Prefix.
|
|
1658
|
+
*/
|
|
1659
|
+
constructor(gs1CompanyPrefix) {
|
|
1660
|
+
this._gs1CompanyPrefix = gs1CompanyPrefix;
|
|
1661
|
+
if (!gs1CompanyPrefix.startsWith("0")) {
|
|
1662
|
+
this._prefixType = 0 /* GS1CompanyPrefix */;
|
|
1663
|
+
this._prefix = this._gs1CompanyPrefix;
|
|
1664
|
+
} else if (!gs1CompanyPrefix.startsWith("00000")) {
|
|
1665
|
+
this._prefixType = 1 /* UPCCompanyPrefix */;
|
|
1666
|
+
this._upcCompanyPrefix = gs1CompanyPrefix.substring(1);
|
|
1667
|
+
this._prefix = this._upcCompanyPrefix;
|
|
1668
|
+
} else {
|
|
1669
|
+
this._prefixType = 2 /* GS18Prefix */;
|
|
1670
|
+
this._gs18Prefix = gs1CompanyPrefix.substring(5);
|
|
1671
|
+
this._prefix = this._gs18Prefix;
|
|
1672
|
+
}
|
|
1673
|
+
this.resetTweakFactor();
|
|
1674
|
+
}
|
|
1675
|
+
/**
|
|
1676
|
+
* Get the prefix type.
|
|
1677
|
+
*/
|
|
1678
|
+
get prefixType() {
|
|
1679
|
+
return this._prefixType;
|
|
1680
|
+
}
|
|
1681
|
+
/**
|
|
1682
|
+
* Get the prefix.
|
|
1683
|
+
*/
|
|
1684
|
+
get prefix() {
|
|
1685
|
+
return this._prefix;
|
|
1686
|
+
}
|
|
1687
|
+
/**
|
|
1688
|
+
* Get the GS1 Company Prefix.
|
|
1689
|
+
*/
|
|
1690
|
+
get gs1CompanyPrefix() {
|
|
1691
|
+
return this._gs1CompanyPrefix;
|
|
1692
|
+
}
|
|
1693
|
+
/**
|
|
1694
|
+
* Get the U.P.C. Company Prefix if prefix type is {@link PrefixType.UPCCompanyPrefix} or undefined if not.
|
|
1695
|
+
*/
|
|
1696
|
+
get upcCompanyPrefix() {
|
|
1697
|
+
return this._upcCompanyPrefix;
|
|
1698
|
+
}
|
|
1699
|
+
/**
|
|
1700
|
+
* Get the GS1-8 Prefix if prefix type is {@link PrefixType.GS18Prefix} or undefined if not.
|
|
1701
|
+
*/
|
|
1702
|
+
get gs18Prefix() {
|
|
1703
|
+
return this._gs18Prefix;
|
|
1704
|
+
}
|
|
1705
|
+
/**
|
|
1706
|
+
* Set the tweak for an identification key creator if it's a numeric identification key creator.
|
|
1707
|
+
*
|
|
1708
|
+
* @param creator
|
|
1709
|
+
* Identification key creator.
|
|
1710
|
+
*/
|
|
1711
|
+
setCreatorTweak(creator) {
|
|
1712
|
+
const creatorTweakFactor = _PrefixManager.CREATOR_TWEAK_FACTORS_MAP.get(creator.identificationKeyType);
|
|
1713
|
+
if (creatorTweakFactor !== void 0) {
|
|
1714
|
+
creator.tweak = this.tweakFactor * creatorTweakFactor;
|
|
1715
|
+
}
|
|
1716
|
+
}
|
|
1717
|
+
/**
|
|
1718
|
+
* Get the tweak factor.
|
|
1719
|
+
*/
|
|
1720
|
+
get tweakFactor() {
|
|
1721
|
+
return this._tweakFactor;
|
|
1722
|
+
}
|
|
1723
|
+
/**
|
|
1724
|
+
* Set the tweak factor.
|
|
1725
|
+
*
|
|
1726
|
+
* @param value
|
|
1727
|
+
* Tweak factor.
|
|
1728
|
+
*/
|
|
1729
|
+
set tweakFactor(value) {
|
|
1730
|
+
const tweakFactor = BigInt(value);
|
|
1731
|
+
if (this._tweakFactor !== tweakFactor) {
|
|
1732
|
+
this._tweakFactor = tweakFactor;
|
|
1733
|
+
for (const creator of this._identificationKeyCreatorsMap.values()) {
|
|
1734
|
+
this.setCreatorTweak(creator);
|
|
1735
|
+
}
|
|
1736
|
+
}
|
|
1737
|
+
}
|
|
1738
|
+
/**
|
|
1739
|
+
* Reset the tweak factor to its default (numeric value of the GS1 Company Prefix preceded by '1').
|
|
1740
|
+
*/
|
|
1741
|
+
resetTweakFactor() {
|
|
1742
|
+
this.tweakFactor = BigInt("1" + this.gs1CompanyPrefix);
|
|
1743
|
+
}
|
|
1744
|
+
/**
|
|
1745
|
+
* Get a prefix manager.
|
|
1746
|
+
*
|
|
1747
|
+
* @param prefixType
|
|
1748
|
+
* Prefix type.
|
|
1749
|
+
*
|
|
1750
|
+
* @param prefix
|
|
1751
|
+
* Prefix.
|
|
1752
|
+
*
|
|
1753
|
+
* @returns
|
|
1754
|
+
* Prefix manager with normalized prefix type and prefix.
|
|
1755
|
+
*/
|
|
1756
|
+
static get(prefixType, prefix) {
|
|
1757
|
+
_PrefixManager.validatePrefix(prefixType, true, true, prefix);
|
|
1758
|
+
let gs1CompanyPrefix;
|
|
1759
|
+
switch (prefixType) {
|
|
1760
|
+
case 0 /* GS1CompanyPrefix */:
|
|
1761
|
+
gs1CompanyPrefix = prefix;
|
|
1762
|
+
break;
|
|
1763
|
+
case 1 /* UPCCompanyPrefix */:
|
|
1764
|
+
gs1CompanyPrefix = "0" + prefix;
|
|
1765
|
+
break;
|
|
1766
|
+
case 2 /* GS18Prefix */:
|
|
1767
|
+
gs1CompanyPrefix = "00000" + prefix;
|
|
1768
|
+
break;
|
|
1769
|
+
}
|
|
1770
|
+
let prefixManager = _PrefixManager.PREFIX_MANAGERS_MAP.get(gs1CompanyPrefix);
|
|
1771
|
+
if (prefixManager === void 0) {
|
|
1772
|
+
prefixManager = new _PrefixManager(gs1CompanyPrefix);
|
|
1773
|
+
_PrefixManager.PREFIX_MANAGERS_MAP.set(gs1CompanyPrefix, prefixManager);
|
|
1774
|
+
}
|
|
1775
|
+
return prefixManager;
|
|
1776
|
+
}
|
|
1777
|
+
/**
|
|
1778
|
+
* Validate a prefix.
|
|
1779
|
+
*
|
|
1780
|
+
* @param prefixType
|
|
1781
|
+
* Prefix type.
|
|
1782
|
+
*
|
|
1783
|
+
* @param allowUPCCompanyPrefix
|
|
1784
|
+
* If true, a U.P.C. Company Prefix expressed as a GS1 Company Prefix is permitted.
|
|
1785
|
+
*
|
|
1786
|
+
* @param allowGS18Prefix
|
|
1787
|
+
* If true, a GS1-8 Prefix expressed as a GS1 Company Prefix is permitted.
|
|
1788
|
+
*
|
|
1789
|
+
* @param prefix
|
|
1790
|
+
* Prefix.
|
|
1791
|
+
*
|
|
1792
|
+
* @param isFromIdentificationKey
|
|
1793
|
+
* If true, the prefix is from an identification key and should be trimmed before its character set is validated.
|
|
1794
|
+
*
|
|
1795
|
+
* @param isNumericIdentificationKey
|
|
1796
|
+
* If true, the prefix is from a numeric identification key and its character set will be validated by the caller.
|
|
1797
|
+
*
|
|
1798
|
+
* @param positionOffset
|
|
1799
|
+
* Position offset within a larger string.
|
|
1800
|
+
*/
|
|
1801
|
+
static validatePrefix(prefixType, allowUPCCompanyPrefix, allowGS18Prefix, prefix, isFromIdentificationKey = false, isNumericIdentificationKey = false, positionOffset) {
|
|
1802
|
+
let baseValidation;
|
|
1803
|
+
switch (prefixType) {
|
|
1804
|
+
case 0 /* GS1CompanyPrefix */:
|
|
1805
|
+
if (!prefix.startsWith("0")) {
|
|
1806
|
+
baseValidation = _PrefixManager.GS1_COMPANY_PREFIX_VALIDATION;
|
|
1807
|
+
} else if (!prefix.startsWith("00000")) {
|
|
1808
|
+
if (!allowUPCCompanyPrefix) {
|
|
1809
|
+
throw new RangeError(i18n_default.t("Prefix.gs1CompanyPrefixCantStartWith0", {
|
|
1810
|
+
ns: gs1NS
|
|
1811
|
+
}));
|
|
1812
|
+
}
|
|
1813
|
+
baseValidation = _PrefixManager.UPC_COMPANY_PREFIX_AS_GS1_COMPANY_PREFIX_VALIDATION;
|
|
1814
|
+
} else if (!prefix.startsWith("000000")) {
|
|
1815
|
+
if (!allowGS18Prefix) {
|
|
1816
|
+
throw new RangeError(i18n_default.t("Prefix.gs1CompanyPrefixCantStartWith00000", {
|
|
1817
|
+
ns: gs1NS
|
|
1818
|
+
}));
|
|
1819
|
+
}
|
|
1820
|
+
baseValidation = _PrefixManager.GS1_8_PREFIX_AS_GS1_COMPANY_PREFIX_VALIDATION;
|
|
1821
|
+
} else {
|
|
1822
|
+
throw new RangeError(i18n_default.t("Prefix.gs1CompanyPrefixCantStartWith000000", {
|
|
1823
|
+
ns: gs1NS
|
|
1824
|
+
}));
|
|
1825
|
+
}
|
|
1826
|
+
break;
|
|
1827
|
+
case 1 /* UPCCompanyPrefix */:
|
|
1828
|
+
if (prefix.startsWith("0000")) {
|
|
1829
|
+
throw new RangeError(i18n_default.t("Prefix.upcCompanyPrefixCantStartWith0000", {
|
|
1830
|
+
ns: gs1NS
|
|
1831
|
+
}));
|
|
1832
|
+
}
|
|
1833
|
+
baseValidation = _PrefixManager.UPC_COMPANY_PREFIX_VALIDATION;
|
|
1834
|
+
break;
|
|
1835
|
+
case 2 /* GS18Prefix */:
|
|
1836
|
+
if (prefix.startsWith("0")) {
|
|
1837
|
+
throw new RangeError(i18n_default.t("Prefix.gs18PrefixCantStartWith0", {
|
|
1838
|
+
ns: gs1NS
|
|
1839
|
+
}));
|
|
1840
|
+
}
|
|
1841
|
+
baseValidation = _PrefixManager.GS1_8_PREFIX_VALIDATION;
|
|
1842
|
+
break;
|
|
1843
|
+
}
|
|
1844
|
+
const mergedValidation = {
|
|
1845
|
+
...baseValidation,
|
|
1846
|
+
positionOffset
|
|
1847
|
+
};
|
|
1848
|
+
if (!isFromIdentificationKey) {
|
|
1849
|
+
import_utility3.NUMERIC_CREATOR.validate(prefix, mergedValidation);
|
|
1850
|
+
} else if (!isNumericIdentificationKey) {
|
|
1851
|
+
import_utility3.NUMERIC_CREATOR.validate(prefix.substring(0, Math.min(mergedValidation.minimumLength, prefix.length - 1)), mergedValidation);
|
|
1852
|
+
}
|
|
1853
|
+
}
|
|
1854
|
+
/**
|
|
1855
|
+
* Get an identification key creator.
|
|
1856
|
+
*
|
|
1857
|
+
* @param identificationKeyType
|
|
1858
|
+
* Identification key type.
|
|
1859
|
+
*
|
|
1860
|
+
* @param constructorCallback
|
|
1861
|
+
* Constructor callback.
|
|
1862
|
+
*
|
|
1863
|
+
* @returns
|
|
1864
|
+
* Identification key creator.
|
|
1865
|
+
*/
|
|
1866
|
+
getIdentificationKeyCreator(identificationKeyType, constructorCallback) {
|
|
1867
|
+
let creator = this._identificationKeyCreatorsMap.get(identificationKeyType);
|
|
1868
|
+
if (creator === void 0) {
|
|
1869
|
+
if (this.prefixType === 2 /* GS18Prefix */ && identificationKeyType !== "GTIN" /* GTIN */) {
|
|
1870
|
+
throw new RangeError(i18n_default.t("Prefix.identificationKeyTypeNotSupportedByGS18Prefix", {
|
|
1871
|
+
ns: gs1NS,
|
|
1872
|
+
identificationKeyType
|
|
1873
|
+
}));
|
|
1874
|
+
}
|
|
1875
|
+
creator = constructorCallback();
|
|
1876
|
+
this.setCreatorTweak(creator);
|
|
1877
|
+
this._identificationKeyCreatorsMap.set(identificationKeyType, creator);
|
|
1878
|
+
}
|
|
1879
|
+
return creator;
|
|
1880
|
+
}
|
|
1881
|
+
/**
|
|
1882
|
+
* Get non-GTIN numeric identification key creator.
|
|
1883
|
+
*
|
|
1884
|
+
* @param validator
|
|
1885
|
+
* Validator on which identification key creator is based.
|
|
1886
|
+
*
|
|
1887
|
+
* @returns
|
|
1888
|
+
* Identification key creator.
|
|
1889
|
+
*/
|
|
1890
|
+
getNonGTINNumericIdentificationKeyCreator(validator) {
|
|
1891
|
+
return this.getIdentificationKeyCreator(validator.identificationKeyType, () => new NonGTINNumericIdentificationKeyCreator(this, validator.identificationKeyType, validator.length, validator.leaderType));
|
|
1892
|
+
}
|
|
1893
|
+
/**
|
|
1894
|
+
* Get serialized numeric identification key creator.
|
|
1895
|
+
*
|
|
1896
|
+
* @param validator
|
|
1897
|
+
* Validator on which identification key creator is based.
|
|
1898
|
+
*
|
|
1899
|
+
* @returns
|
|
1900
|
+
* Identification key creator.
|
|
1901
|
+
*/
|
|
1902
|
+
getSerializableNumericIdentificationKeyCreator(validator) {
|
|
1903
|
+
return this.getIdentificationKeyCreator(validator.identificationKeyType, () => new SerializableNumericIdentificationKeyCreator(this, validator.identificationKeyType, validator.length, validator.serialComponentLength, validator.serialComponentCharacterSet));
|
|
1904
|
+
}
|
|
1905
|
+
/**
|
|
1906
|
+
* Get non-numeric identification key creator.
|
|
1907
|
+
*
|
|
1908
|
+
* @param validator
|
|
1909
|
+
* Validator on which identification key creator is based.
|
|
1910
|
+
*
|
|
1911
|
+
* @returns
|
|
1912
|
+
* Identification key creator.
|
|
1913
|
+
*/
|
|
1914
|
+
getNonNumericIdentificationKeyCreator(validator) {
|
|
1915
|
+
return this.getIdentificationKeyCreator(validator.identificationKeyType, () => new NonNumericIdentificationKeyCreator(this, validator.identificationKeyType, validator.length, validator.referenceCharacterSet, validator.requiresCheckCharacterPair));
|
|
1916
|
+
}
|
|
1917
|
+
/**
|
|
1918
|
+
* Get GTIN creator.
|
|
1919
|
+
*/
|
|
1920
|
+
get gtinCreator() {
|
|
1921
|
+
return this.getIdentificationKeyCreator("GTIN" /* GTIN */, () => {
|
|
1922
|
+
let gtinType;
|
|
1923
|
+
switch (this.prefixType) {
|
|
1924
|
+
case 0 /* GS1CompanyPrefix */:
|
|
1925
|
+
gtinType = 13 /* GTIN13 */;
|
|
1926
|
+
break;
|
|
1927
|
+
case 1 /* UPCCompanyPrefix */:
|
|
1928
|
+
gtinType = 12 /* GTIN12 */;
|
|
1929
|
+
break;
|
|
1930
|
+
case 2 /* GS18Prefix */:
|
|
1931
|
+
gtinType = 8 /* GTIN8 */;
|
|
1932
|
+
break;
|
|
1933
|
+
}
|
|
1934
|
+
return new GTINCreator(this, gtinType);
|
|
1935
|
+
});
|
|
1936
|
+
}
|
|
1937
|
+
/**
|
|
1938
|
+
* Get GLN creator.
|
|
1939
|
+
*/
|
|
1940
|
+
get glnCreator() {
|
|
1941
|
+
return this.getNonGTINNumericIdentificationKeyCreator(GLN_VALIDATOR);
|
|
1942
|
+
}
|
|
1943
|
+
/**
|
|
1944
|
+
* Get SSCC creator.
|
|
1945
|
+
*/
|
|
1946
|
+
get ssccCreator() {
|
|
1947
|
+
return this.getNonGTINNumericIdentificationKeyCreator(SSCC_VALIDATOR);
|
|
1948
|
+
}
|
|
1949
|
+
/**
|
|
1950
|
+
* Get GRAI creator.
|
|
1951
|
+
*/
|
|
1952
|
+
get graiCreator() {
|
|
1953
|
+
return this.getSerializableNumericIdentificationKeyCreator(GRAI_VALIDATOR);
|
|
1954
|
+
}
|
|
1955
|
+
/**
|
|
1956
|
+
* Get GIAI creator.
|
|
1957
|
+
*/
|
|
1958
|
+
get giaiCreator() {
|
|
1959
|
+
return this.getNonNumericIdentificationKeyCreator(GIAI_VALIDATOR);
|
|
1960
|
+
}
|
|
1961
|
+
/**
|
|
1962
|
+
* Get GSRN creator.
|
|
1963
|
+
*/
|
|
1964
|
+
get gsrnCreator() {
|
|
1965
|
+
return this.getNonGTINNumericIdentificationKeyCreator(GSRN_VALIDATOR);
|
|
1966
|
+
}
|
|
1967
|
+
/**
|
|
1968
|
+
* Get GDTI creator.
|
|
1969
|
+
*/
|
|
1970
|
+
get gdtiCreator() {
|
|
1971
|
+
return this.getSerializableNumericIdentificationKeyCreator(GDTI_VALIDATOR);
|
|
1972
|
+
}
|
|
1973
|
+
/**
|
|
1974
|
+
* Get GINC creator.
|
|
1975
|
+
*/
|
|
1976
|
+
get gincCreator() {
|
|
1977
|
+
return this.getNonNumericIdentificationKeyCreator(GINC_VALIDATOR);
|
|
1978
|
+
}
|
|
1979
|
+
/**
|
|
1980
|
+
* Get GSIN creator.
|
|
1981
|
+
*/
|
|
1982
|
+
get gsinCreator() {
|
|
1983
|
+
return this.getNonGTINNumericIdentificationKeyCreator(GSIN_VALIDATOR);
|
|
1984
|
+
}
|
|
1985
|
+
/**
|
|
1986
|
+
* Get GCN creator.
|
|
1987
|
+
*/
|
|
1988
|
+
get gcnCreator() {
|
|
1989
|
+
return this.getSerializableNumericIdentificationKeyCreator(GCN_VALIDATOR);
|
|
1990
|
+
}
|
|
1991
|
+
/**
|
|
1992
|
+
* Get CPID creator.
|
|
1993
|
+
*/
|
|
1994
|
+
get cpidCreator() {
|
|
1995
|
+
return this.getNonNumericIdentificationKeyCreator(CPID_VALIDATOR);
|
|
1996
|
+
}
|
|
1997
|
+
/**
|
|
1998
|
+
* Get GMN creator.
|
|
1999
|
+
*/
|
|
2000
|
+
get gmnCreator() {
|
|
2001
|
+
return this.getNonNumericIdentificationKeyCreator(GMN_VALIDATOR);
|
|
2002
|
+
}
|
|
2003
|
+
};
|
|
2004
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
2005
|
+
0 && (module.exports = {
|
|
2006
|
+
AI39_CREATOR,
|
|
2007
|
+
AI82_CREATOR,
|
|
2008
|
+
CPID_VALIDATOR,
|
|
2009
|
+
CharacterSet,
|
|
2010
|
+
GCN_VALIDATOR,
|
|
2011
|
+
GDTI_VALIDATOR,
|
|
2012
|
+
GIAI_VALIDATOR,
|
|
2013
|
+
GINC_VALIDATOR,
|
|
2014
|
+
GLN_VALIDATOR,
|
|
2015
|
+
GMN_VALIDATOR,
|
|
2016
|
+
GRAI_VALIDATOR,
|
|
2017
|
+
GSIN_VALIDATOR,
|
|
2018
|
+
GSRN_VALIDATOR,
|
|
2019
|
+
GTIN12_VALIDATOR,
|
|
2020
|
+
GTIN13_VALIDATOR,
|
|
2021
|
+
GTIN8_VALIDATOR,
|
|
2022
|
+
GTINCreator,
|
|
2023
|
+
GTINLevel,
|
|
2024
|
+
GTINType,
|
|
2025
|
+
GTINValidator,
|
|
2026
|
+
GTIN_VALIDATORS,
|
|
2027
|
+
IdentificationKeyType,
|
|
2028
|
+
LeaderType,
|
|
2029
|
+
NonGTINNumericIdentificationKeyCreator,
|
|
2030
|
+
NonGTINNumericIdentificationKeyValidator,
|
|
2031
|
+
NonNumericIdentificationKeyCreator,
|
|
2032
|
+
NonNumericIdentificationKeyValidator,
|
|
2033
|
+
PrefixManager,
|
|
2034
|
+
PrefixType,
|
|
2035
|
+
SSCC_VALIDATOR,
|
|
2036
|
+
SerializableNumericIdentificationKeyCreator,
|
|
2037
|
+
SerializableNumericIdentificationKeyValidator,
|
|
2038
|
+
checkCharacterPair,
|
|
2039
|
+
checkDigit,
|
|
2040
|
+
checkDigitSum,
|
|
2041
|
+
fiveDigitPriceWeightCheckDigit,
|
|
2042
|
+
fourDigitPriceWeightCheckDigit,
|
|
2043
|
+
hasValidCheckCharacterPair,
|
|
2044
|
+
hasValidCheckDigit
|
|
2045
|
+
});
|