@aidc-toolkit/gs1 0.9.20-beta → 1.0.22-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.
Files changed (131) hide show
  1. package/dist/character-set.d.ts +4 -4
  2. package/dist/character-set.d.ts.map +1 -1
  3. package/dist/character-set.js +7 -7
  4. package/dist/character-set.js.map +1 -1
  5. package/dist/gtin-creator.d.ts +68 -0
  6. package/dist/gtin-creator.d.ts.map +1 -0
  7. package/dist/gtin-creator.js +158 -0
  8. package/dist/gtin-creator.js.map +1 -0
  9. package/dist/gtin-validator.d.ts +202 -0
  10. package/dist/gtin-validator.d.ts.map +1 -0
  11. package/dist/gtin-validator.js +470 -0
  12. package/dist/gtin-validator.js.map +1 -0
  13. package/dist/identifier-creator.d.ts +72 -0
  14. package/dist/identifier-creator.d.ts.map +1 -0
  15. package/dist/identifier-creator.js +50 -0
  16. package/dist/identifier-creator.js.map +1 -0
  17. package/dist/identifier-type.d.ts +58 -0
  18. package/dist/identifier-type.d.ts.map +1 -0
  19. package/dist/identifier-type.js +54 -0
  20. package/dist/identifier-type.js.map +1 -0
  21. package/dist/identifier-validator.d.ts +174 -0
  22. package/dist/identifier-validator.d.ts.map +1 -0
  23. package/dist/identifier-validator.js +145 -0
  24. package/dist/identifier-validator.js.map +1 -0
  25. package/dist/index.d.ts +16 -1
  26. package/dist/index.d.ts.map +1 -1
  27. package/dist/index.js +16 -1
  28. package/dist/index.js.map +1 -1
  29. package/dist/locale/en/locale-strings.d.ts +3 -3
  30. package/dist/locale/en/locale-strings.js +3 -3
  31. package/dist/locale/en/locale-strings.js.map +1 -1
  32. package/dist/locale/fr/locale-strings.d.ts +3 -3
  33. package/dist/locale/fr/locale-strings.js +3 -3
  34. package/dist/locale/fr/locale-strings.js.map +1 -1
  35. package/dist/non-gtin-numeric-identifier-creator.d.ts +30 -0
  36. package/dist/non-gtin-numeric-identifier-creator.d.ts.map +1 -0
  37. package/dist/non-gtin-numeric-identifier-creator.js +30 -0
  38. package/dist/non-gtin-numeric-identifier-creator.js.map +1 -0
  39. package/dist/non-gtin-numeric-identifier-validator.d.ts +41 -0
  40. package/dist/non-gtin-numeric-identifier-validator.d.ts.map +1 -0
  41. package/dist/non-gtin-numeric-identifier-validator.js +40 -0
  42. package/dist/non-gtin-numeric-identifier-validator.js.map +1 -0
  43. package/dist/non-numeric-identifier-creator.d.ts +55 -0
  44. package/dist/non-numeric-identifier-creator.d.ts.map +1 -0
  45. package/dist/non-numeric-identifier-creator.js +93 -0
  46. package/dist/non-numeric-identifier-creator.js.map +1 -0
  47. package/dist/non-numeric-identifier-validator.d.ts +78 -0
  48. package/dist/non-numeric-identifier-validator.d.ts.map +1 -0
  49. package/dist/non-numeric-identifier-validator.js +95 -0
  50. package/dist/non-numeric-identifier-validator.js.map +1 -0
  51. package/dist/numeric-identifier-creator.d.ts +121 -0
  52. package/dist/numeric-identifier-creator.d.ts.map +1 -0
  53. package/dist/numeric-identifier-creator.js +135 -0
  54. package/dist/numeric-identifier-creator.js.map +1 -0
  55. package/dist/numeric-identifier-validator.d.ts +76 -0
  56. package/dist/numeric-identifier-validator.d.ts.map +1 -0
  57. package/dist/numeric-identifier-validator.js +84 -0
  58. package/dist/numeric-identifier-validator.js.map +1 -0
  59. package/dist/prefix-manager.d.ts +224 -0
  60. package/dist/prefix-manager.d.ts.map +1 -0
  61. package/dist/prefix-manager.js +369 -0
  62. package/dist/prefix-manager.js.map +1 -0
  63. package/dist/prefix-provider.d.ts +27 -0
  64. package/dist/prefix-provider.d.ts.map +1 -0
  65. package/dist/prefix-provider.js +2 -0
  66. package/dist/prefix-provider.js.map +1 -0
  67. package/dist/prefix-type.d.ts +22 -0
  68. package/dist/prefix-type.d.ts.map +1 -0
  69. package/dist/prefix-type.js +18 -0
  70. package/dist/prefix-type.js.map +1 -0
  71. package/dist/prefix-validator.d.ts +58 -0
  72. package/dist/prefix-validator.d.ts.map +1 -0
  73. package/dist/prefix-validator.js +154 -0
  74. package/dist/prefix-validator.js.map +1 -0
  75. package/dist/serializable-numeric-identifier-creator.d.ts +86 -0
  76. package/dist/serializable-numeric-identifier-creator.d.ts.map +1 -0
  77. package/dist/serializable-numeric-identifier-creator.js +116 -0
  78. package/dist/serializable-numeric-identifier-creator.js.map +1 -0
  79. package/dist/serializable-numeric-identifier-validator.d.ts +79 -0
  80. package/dist/serializable-numeric-identifier-validator.d.ts.map +1 -0
  81. package/dist/serializable-numeric-identifier-validator.js +99 -0
  82. package/dist/serializable-numeric-identifier-validator.js.map +1 -0
  83. package/gs1.iml +4 -1
  84. package/package.json +2 -3
  85. package/src/character-set.ts +7 -7
  86. package/src/gtin-creator.ts +195 -0
  87. package/src/gtin-validator.ts +564 -0
  88. package/src/identifier-creator.ts +97 -0
  89. package/src/identifier-type.ts +69 -0
  90. package/src/identifier-validator.ts +235 -0
  91. package/src/index.ts +16 -1
  92. package/src/locale/en/locale-strings.ts +3 -3
  93. package/src/locale/fr/locale-strings.ts +3 -3
  94. package/src/non-gtin-numeric-identifier-creator.ts +33 -0
  95. package/src/non-gtin-numeric-identifier-validator.ts +54 -0
  96. package/src/non-numeric-identifier-creator.ts +111 -0
  97. package/src/non-numeric-identifier-validator.ts +128 -0
  98. package/src/numeric-identifier-creator.ts +200 -0
  99. package/src/numeric-identifier-validator.ts +128 -0
  100. package/src/prefix-manager.ts +446 -0
  101. package/src/prefix-provider.ts +31 -0
  102. package/src/prefix-type.ts +24 -0
  103. package/src/prefix-validator.ts +191 -0
  104. package/src/serializable-numeric-identifier-creator.ts +128 -0
  105. package/src/serializable-numeric-identifier-validator.ts +124 -0
  106. package/test/check.test.ts +0 -4
  107. package/test/creator.test.ts +30 -0
  108. package/test/gtin-creator.ts +239 -0
  109. package/test/gtin-validator.test.ts +149 -0
  110. package/test/identifier-creator.ts +84 -0
  111. package/test/identifier-validator.ts +8 -0
  112. package/test/non-gtin-numeric-identifier-creator.ts +98 -0
  113. package/test/non-gtin-numeric-identifier-validator.ts +6 -0
  114. package/test/non-numeric-identifier-validator.ts +24 -0
  115. package/test/numeric-identifier-creator.ts +132 -0
  116. package/test/numeric-identifier-validator.ts +23 -0
  117. package/test/prefix-manager.test.ts +112 -0
  118. package/test/serializable-numeric-identifier-creator.ts +56 -0
  119. package/test/serializable-numeric-identifier-validator.ts +24 -0
  120. package/test/setup.ts +4 -0
  121. package/test/sparse.test.ts +56 -0
  122. package/test/utility.ts +22 -0
  123. package/test/validator.test.ts +52 -0
  124. package/test/variable-measure-rcn.test.ts +201 -0
  125. package/vitest.config.ts +7 -0
  126. package/dist/idkey.d.ts +0 -1346
  127. package/dist/idkey.d.ts.map +0 -1
  128. package/dist/idkey.js +0 -2024
  129. package/dist/idkey.js.map +0 -1
  130. package/src/idkey.ts +0 -2532
  131. package/test/idkey.test.ts +0 -1247
@@ -0,0 +1,564 @@
1
+ import { type CharacterSetValidation, NUMERIC_CREATOR } from "@aidc-toolkit/utility";
2
+ import { checkDigit, hasValidCheckDigit, isValidPriceOrWeightCheckDigit } from "./check.js";
3
+ import { IdentifierTypes } from "./identifier-type.js";
4
+ import { i18nextGS1 } from "./locale/i18n.js";
5
+ import { AbstractNumericIdentifierValidator, LeaderTypes } from "./numeric-identifier-validator.js";
6
+ import { type PrefixType, PrefixTypes } from "./prefix-type.js";
7
+ import { PrefixValidator } from "./prefix-validator.js";
8
+
9
+ /**
10
+ * GTIN types. The numeric values are equal to the lengths of the GTIN types.
11
+ */
12
+ export const GTINTypes = {
13
+ /**
14
+ * GTIN-13.
15
+ */
16
+ GTIN13: 13,
17
+
18
+ /**
19
+ * GTIN-12.
20
+ */
21
+ GTIN12: 12,
22
+
23
+ /**
24
+ * GTIN-8.
25
+ */
26
+ GTIN8: 8,
27
+
28
+ /**
29
+ * GTIN-14.
30
+ */
31
+ GTIN14: 14
32
+ } as const;
33
+
34
+ /**
35
+ * GTIN type.
36
+ */
37
+ export type GTINType = typeof GTINTypes[keyof typeof GTINTypes];
38
+
39
+ /**
40
+ * Levels at which GTIN is to be validated.
41
+ */
42
+ export const GTINLevels = {
43
+ /**
44
+ * Any level (level is ignored).
45
+ */
46
+ Any: 0,
47
+
48
+ /**
49
+ * Retail consumer trade item level, supporting GTIN-13, GTIN-12 (optionally zero-suppressed), and GTIN-8.
50
+ */
51
+ RetailConsumer: 1,
52
+
53
+ /**
54
+ * Other than retail consumer trade item level, supporting GTIN-13, GTIN-12 (not zero-suppressed), and GTIN-14.
55
+ */
56
+ OtherThanRetailConsumer: 2
57
+ } as const;
58
+
59
+ /**
60
+ * GTIN level.
61
+ */
62
+ export type GTINLevel = typeof GTINLevels[keyof typeof GTINLevels];
63
+
64
+ /**
65
+ * Restricted Circulation Number reference.
66
+ */
67
+ export interface RCNReference {
68
+ /**
69
+ * Item reference.
70
+ */
71
+ itemReference: number;
72
+
73
+ /**
74
+ * Price or weight (whole number only).
75
+ */
76
+ priceOrWeight: number;
77
+ }
78
+
79
+ /**
80
+ * GTIN validator.
81
+ */
82
+ export class GTINValidator extends AbstractNumericIdentifierValidator {
83
+ /**
84
+ * Validation parameters for optional indicator digit.
85
+ */
86
+ private static readonly OPTIONAL_INDICATOR_DIGIT_VALIDATION: CharacterSetValidation = {
87
+ minimumLength: 0,
88
+ maximumLength: 1,
89
+ component: () => i18nextGS1.t("Identifier.indicatorDigit")
90
+ };
91
+
92
+ /**
93
+ * Validation parameters for zero-suppressed GTIN-12.
94
+ */
95
+ private static readonly ZERO_SUPPRESSED_GTIN12_VALIDATION: CharacterSetValidation = {
96
+ minimumLength: 8,
97
+ maximumLength: 8
98
+ };
99
+
100
+ /**
101
+ * Constructor.
102
+ *
103
+ * @param gtinType
104
+ * GTIN type.
105
+ */
106
+ constructor(gtinType: GTINType) {
107
+ let prefixType: PrefixType;
108
+
109
+ // Determine the prefix type based on the GTIN type.
110
+ switch (gtinType) {
111
+ case GTINTypes.GTIN13:
112
+ prefixType = PrefixTypes.GS1CompanyPrefix;
113
+ break;
114
+
115
+ case GTINTypes.GTIN12:
116
+ prefixType = PrefixTypes.UPCCompanyPrefix;
117
+ break;
118
+
119
+ case GTINTypes.GTIN8:
120
+ prefixType = PrefixTypes.GS18Prefix;
121
+ break;
122
+
123
+ default:
124
+ // Should never get here.
125
+ throw new Error("Not supported");
126
+ }
127
+
128
+ super(IdentifierTypes.GTIN, prefixType, gtinType, LeaderTypes.IndicatorDigit);
129
+ }
130
+
131
+ /**
132
+ * @inheritDoc
133
+ */
134
+ get gtinType(): GTINType {
135
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- Length maps to GTIN type.
136
+ return this.length as GTINType;
137
+ }
138
+
139
+ /**
140
+ * @inheritDoc
141
+ */
142
+ protected override validatePrefix(partialIdentifier: string, positionOffset?: number): void {
143
+ // Delegate to prefix validator requiring exact match for prefix type.
144
+ PrefixValidator.validate(this.prefixType, false, false, partialIdentifier, true, true, positionOffset);
145
+ }
146
+
147
+ /**
148
+ * Zero suppress a GTIN-12.
149
+ *
150
+ * @param gtin12
151
+ * GTIN-12.
152
+ *
153
+ * @returns
154
+ * Zero-suppressed GTIN-12.
155
+ */
156
+ static zeroSuppress(gtin12: string): string {
157
+ GTIN12_VALIDATOR.validate(gtin12);
158
+
159
+ // Convert to individual digits.
160
+ const d = Array.from(gtin12);
161
+
162
+ let zeroSuppressedGTIN12: string | undefined;
163
+
164
+ // All rules require that digits in positions 1, 5, and 6 be zero.
165
+ if (d[0] === "0" && d[6] === "0" && d[7] === "0") {
166
+ if (d[10] >= "5" && d[8] === "0" && d[9] === "0" && d[5] !== "0") {
167
+ zeroSuppressedGTIN12 = `0${d[1]}${d[2]}${d[3]}${d[4]}${d[5]}${d[10]}${d[11]}`;
168
+ } else if (d[5] === "0" && d[8] === "0" && d[9] === "0" && d[4] !== "0") {
169
+ zeroSuppressedGTIN12 = `0${d[1]}${d[2]}${d[3]}${d[4]}${d[10]}4${d[11]}`;
170
+ } else if (d[3] <= "2" && d[4] === "0" && d[5] === "0") {
171
+ zeroSuppressedGTIN12 = `0${d[1]}${d[2]}${d[8]}${d[9]}${d[10]}${d[3]}${d[11]}`;
172
+ } else if (d[3] >= "3" && d[4] === "0" && d[5] === "0" && d[8] === "0") {
173
+ zeroSuppressedGTIN12 = `0${d[1]}${d[2]}${d[3]}${d[9]}${d[10]}3${d[11]}`;
174
+ }
175
+ }
176
+
177
+ if (zeroSuppressedGTIN12 === undefined) {
178
+ throw new RangeError(i18nextGS1.t("Identifier.invalidZeroSuppressibleGTIN12"));
179
+ }
180
+
181
+ return zeroSuppressedGTIN12;
182
+ }
183
+
184
+ /**
185
+ * Zero expand a zero-suppressed GTIN-12.
186
+ *
187
+ * @param zeroSuppressedGTIN12
188
+ * Zero-suppressed GTIN-12.
189
+ *
190
+ * @returns
191
+ * GTIN-12.
192
+ */
193
+ static zeroExpand(zeroSuppressedGTIN12: string): string {
194
+ NUMERIC_CREATOR.validate(zeroSuppressedGTIN12, GTINValidator.ZERO_SUPPRESSED_GTIN12_VALIDATION);
195
+
196
+ // Convert to individual digits.
197
+ const d = Array.from(zeroSuppressedGTIN12);
198
+
199
+ let gtin12: string | undefined;
200
+
201
+ // Zero-suppressed GTIN-12 always starts with 0.
202
+ if (d[0] === "0") {
203
+ if (d[6] >= "5" && d[5] !== "0") {
204
+ gtin12 = `0${d[1]}${d[2]}${d[3]}${d[4]}${d[5]}0000${d[6]}${d[7]}`;
205
+ } else if (d[6] === "4" && d[4] !== "0") {
206
+ gtin12 = `0${d[1]}${d[2]}${d[3]}${d[4]}00000${d[5]}${d[7]}`;
207
+ } else if (d[6] <= "2") {
208
+ gtin12 = `0${d[1]}${d[2]}${d[6]}0000${d[3]}${d[4]}${d[5]}${d[7]}`;
209
+ } else if (d[6] === "3" && d[3] >= "3") {
210
+ gtin12 = `0${d[1]}${d[2]}${d[3]}00000${d[4]}${d[5]}${d[7]}`;
211
+ }
212
+ }
213
+
214
+ if (gtin12 === undefined) {
215
+ throw new RangeError(i18nextGS1.t("Identifier.invalidZeroSuppressedGTIN12"));
216
+ }
217
+
218
+ // Make sure that resulting GTIN-12 is valid.
219
+ GTIN12_VALIDATOR.validate(gtin12);
220
+
221
+ return gtin12;
222
+ }
223
+
224
+ /**
225
+ * Convert a GTIN of any length to a GTIN-14 with an optional indicator digit.
226
+ *
227
+ * @param indicatorDigit
228
+ * Indicator digit. If blank, assumes "0" if the GTIN is not already a GTIN-14.
229
+ *
230
+ * @param gtin
231
+ * GTIN.
232
+ *
233
+ * @returns
234
+ * GTIN-14.
235
+ */
236
+ static convertToGTIN14(indicatorDigit: string, gtin: string): string {
237
+ GTINValidator.validateAny(gtin);
238
+
239
+ NUMERIC_CREATOR.validate(indicatorDigit, GTINValidator.OPTIONAL_INDICATOR_DIGIT_VALIDATION);
240
+
241
+ // Check digit doesn't change by prepending zeros.
242
+ let gtin14 = gtin.padStart(GTINTypes.GTIN14, "0");
243
+
244
+ // If indicator digit provided and is different, recalculate the check digit.
245
+ if (indicatorDigit.length !== 0 && indicatorDigit !== gtin14.charAt(0)) {
246
+ const partialGTIN14 = indicatorDigit + gtin14.substring(1, GTINTypes.GTIN14 - 1);
247
+
248
+ gtin14 = partialGTIN14 + checkDigit(partialGTIN14);
249
+ }
250
+
251
+ return gtin14;
252
+ }
253
+
254
+ /**
255
+ * Normalize a GTIN of any length.
256
+ * - A GTIN-14 that starts with six zeros or a GTIN-13 that starts with five zeros is normalized to GTIN-8.
257
+ * - A GTIN-14 that starts with two zeros or a GTIN-13 that starts with one zero is normalized to GTIN-12.
258
+ * - A GTIN-14 that starts with one zero is normalized to GTIN-13.
259
+ * - Otherwise, the GTIN is unchanged.
260
+ *
261
+ * @param gtin
262
+ * GTIN.
263
+ *
264
+ * @returns
265
+ * Normalized GTIN.
266
+ */
267
+ static normalize(gtin: string): string {
268
+ const gtinLength = gtin.length;
269
+
270
+ let normalizedGTIN: string;
271
+
272
+ switch (gtinLength) {
273
+ case GTINTypes.GTIN13 as number:
274
+ if (!gtin.startsWith("0")) {
275
+ // GTIN is GTIN-13.
276
+ normalizedGTIN = gtin;
277
+ } else if (!gtin.startsWith("00000")) {
278
+ // GTIN is GTIN-12.
279
+ normalizedGTIN = gtin.substring(1);
280
+ } else if (!gtin.startsWith("000000")) {
281
+ // GTIN is GTIN-8.
282
+ normalizedGTIN = gtin.substring(5);
283
+ } else {
284
+ throw new RangeError(i18nextGS1.t("Identifier.invalidZeroSuppressedGTIN12AsGTIN13"));
285
+ }
286
+ break;
287
+
288
+ case GTINTypes.GTIN12 as number:
289
+ // GTIN is GTIN-12.
290
+ normalizedGTIN = gtin;
291
+ break;
292
+
293
+ case GTINTypes.GTIN8 as number:
294
+ if (!gtin.startsWith("0")) {
295
+ // GTIN is GTIN-8.
296
+ normalizedGTIN = gtin;
297
+ } else {
298
+ // GTIN is zero-suppressed GTIN-12.
299
+ normalizedGTIN = GTINValidator.zeroExpand(gtin);
300
+ }
301
+ break;
302
+
303
+ case GTINTypes.GTIN14 as number:
304
+ if (!gtin.startsWith("0")) {
305
+ // GTIN is GTIN-14.
306
+ normalizedGTIN = gtin;
307
+ } else if (!gtin.startsWith("00")) {
308
+ // GTIN is GTIN-13.
309
+ normalizedGTIN = gtin.substring(1);
310
+ } else if (!gtin.startsWith("000000")) {
311
+ // GTIN is GTIN-12.
312
+ normalizedGTIN = gtin.substring(2);
313
+ } else if (!gtin.startsWith("0000000")) {
314
+ // GTIN is GTIN-8.
315
+ normalizedGTIN = gtin.substring(6);
316
+ } else {
317
+ throw new RangeError(i18nextGS1.t("Identifier.invalidZeroSuppressedGTIN12AsGTIN14"));
318
+ }
319
+ break;
320
+
321
+ default:
322
+ throw new RangeError(i18nextGS1.t("Identifier.invalidGTINLength"));
323
+ }
324
+
325
+ // Validation applies to the normalized GTIN.
326
+ GTINValidator.validateAny(normalizedGTIN);
327
+
328
+ return normalizedGTIN;
329
+ }
330
+
331
+ /**
332
+ * Validate any GTIN, optionally against a level.
333
+ *
334
+ * @param gtin
335
+ * GTIN.
336
+ *
337
+ * @param gtinLevel
338
+ * Level at which GTIN is to be validated.
339
+ */
340
+ static validateAny(gtin: string, gtinLevel: GTINLevel = GTINLevels.Any): void {
341
+ // Assume length-validated GTIN is the GTIN (true for all except zero-suppressed GTIN-12).
342
+ let lengthValidatedGTIN = gtin;
343
+
344
+ let gtinLevelRestriction: GTINLevel;
345
+
346
+ switch (gtin.length) {
347
+ case GTINTypes.GTIN13 as number:
348
+ if (gtin.startsWith("0")) {
349
+ throw new RangeError(i18nextGS1.t("Identifier.invalidGTIN13AtRetail"));
350
+ }
351
+
352
+ // Validate prefix requiring exact match for prefix type.
353
+ PrefixValidator.validate(PrefixTypes.GS1CompanyPrefix, false, false, gtin, true, true);
354
+
355
+ gtinLevelRestriction = GTINLevels.Any;
356
+ break;
357
+
358
+ case GTINTypes.GTIN12 as number:
359
+ // Validate prefix requiring exact match for prefix type.
360
+ PrefixValidator.validate(PrefixTypes.UPCCompanyPrefix, false, false, gtin, true, true);
361
+
362
+ gtinLevelRestriction = GTINLevels.Any;
363
+ break;
364
+
365
+ case GTINTypes.GTIN8 as number:
366
+ // Zero-suppressed GTIN-12 always starts with 0.
367
+ if (!gtin.startsWith("0")) {
368
+ // Validate prefix requiring exact match for prefix type.
369
+ PrefixValidator.validate(PrefixTypes.GS18Prefix, false, false, gtin, true, true);
370
+ } else {
371
+ lengthValidatedGTIN = GTINValidator.zeroExpand(gtin);
372
+ }
373
+
374
+ gtinLevelRestriction = GTINLevels.RetailConsumer;
375
+ break;
376
+
377
+ case GTINTypes.GTIN14 as number:
378
+ // Validate prefix supporting any prefix type.
379
+ PrefixValidator.validate(PrefixTypes.GS1CompanyPrefix, true, true, gtin.substring(1), true, true);
380
+
381
+ gtinLevelRestriction = GTINLevels.OtherThanRetailConsumer;
382
+ break;
383
+
384
+ default:
385
+ throw new RangeError(i18nextGS1.t("Identifier.invalidGTINLength"));
386
+ }
387
+
388
+ // Validating the check digit will also validate the characters.
389
+ if (!hasValidCheckDigit(lengthValidatedGTIN)) {
390
+ throw new RangeError(i18nextGS1.t("Identifier.invalidCheckDigit"));
391
+ }
392
+
393
+ // Validate against level if required.
394
+ if (gtinLevel !== GTINLevels.Any && gtinLevelRestriction !== GTINLevels.Any && gtinLevelRestriction !== gtinLevel) {
395
+ throw new RangeError(i18nextGS1.t(gtinLevel === GTINLevels.RetailConsumer ? "Identifier.invalidGTINAtRetail" : "Identifier.invalidGTINAtOtherThanRetail"));
396
+ }
397
+ }
398
+
399
+ /**
400
+ * Validate a GTIN-14.
401
+ *
402
+ * @param gtin14
403
+ * GTIN-14.
404
+ */
405
+ static validateGTIN14(gtin14: string): void {
406
+ if (gtin14.length !== GTINTypes.GTIN14) {
407
+ throw new RangeError(i18nextGS1.t("Identifier.invalidGTIN14Length"));
408
+ }
409
+
410
+ GTINValidator.validateAny(gtin14);
411
+ }
412
+
413
+ /**
414
+ * Parse a Restricted Circulation Number (RCN) using a variable measure trade item format. The format is a 12- or
415
+ * 13-character string (for RCN-12 or RCN-13 respectively), containing the following:
416
+ *
417
+ * - '2' - The first character of the RCN.
418
+ * - '0'-'9' - The second character of the RCN (RCN-13 only).
419
+ * - 'I' - One or more, in sequence, for the item reference.
420
+ * - 'P' - One or more, in sequence, for the price or weight.
421
+ * - 'V' - Zero or one, for the price or weight check digit.
422
+ * - 'C' - The check digit of the entire RCN.
423
+ *
424
+ * The 'I', 'P', and 'V' formats may be in any order.
425
+ *
426
+ * Some examples:
427
+ *
428
+ * - 2IIIIIVPPPPC - RCN-12 with a five-digit item reference, a price or weight check digit, and a four-digit price
429
+ * or weight.
430
+ * - 23IIIIVPPPPPC - RCN-13 with a four-digit item reference, a price or weight check digit, and a five-digit price
431
+ * or weight.
432
+ * - 2IIIIIIPPPPC - RCN-12 with a six-digit item reference and a four-digit price or eight.
433
+ * - 29IIIIIPPPPPC - RCN-13 with a five-digit item reference and a five-digit price or weight.
434
+ *
435
+ * @param format
436
+ * Format.
437
+ *
438
+ * @param rcn
439
+ * RCN.
440
+ *
441
+ * @returns
442
+ * RCN reference.
443
+ */
444
+ static parseVariableMeasureRCN(format: string, rcn: string): RCNReference {
445
+ const formatLength = format.length;
446
+
447
+ if (rcn.length !== formatLength) {
448
+ throw new RangeError(i18nextGS1.t("Identifier.invalidRCNLength"));
449
+ }
450
+
451
+ let validFormat = formatLength === 12 || formatLength === 13;
452
+ let validRCNPrefix = true;
453
+
454
+ let buildingItemReference = false;
455
+ let itemReference = "";
456
+
457
+ let buildingPriceOrWeight = false;
458
+ let priceOrWeight = "";
459
+
460
+ let priceOrWeightCheckDigit = "";
461
+
462
+ for (let index = 0; validFormat && index < formatLength; index++) {
463
+ const formatChar = format.charAt(index);
464
+ const rcnChar = rcn.charAt(index);
465
+
466
+ if (index === 0) {
467
+ validFormat = formatChar === "2";
468
+ validRCNPrefix = rcnChar === "2";
469
+ } else if (formatLength === 13 && index === 1) {
470
+ validFormat = NUMERIC_CREATOR.characterIndex(formatChar) !== undefined;
471
+ validRCNPrefix = rcnChar === formatChar;
472
+ } else if (index === formatLength - 1) {
473
+ validFormat = formatChar === "C";
474
+ } else {
475
+ switch (formatChar) {
476
+ case "I":
477
+ if (!buildingItemReference) {
478
+ // Item reference can't appear more than once.
479
+ validFormat = itemReference === "";
480
+
481
+ buildingItemReference = true;
482
+ buildingPriceOrWeight = false;
483
+ }
484
+
485
+ itemReference += rcnChar;
486
+ break;
487
+
488
+ case "P":
489
+ if (!buildingPriceOrWeight) {
490
+ // Price or weight can't appear more than once.
491
+ validFormat = priceOrWeight === "";
492
+
493
+ buildingPriceOrWeight = true;
494
+ buildingItemReference = false;
495
+ }
496
+
497
+ priceOrWeight += rcnChar;
498
+ break;
499
+
500
+ case "V":
501
+ // Price or weight check digit can't appear more than once.
502
+ validFormat = priceOrWeightCheckDigit === "";
503
+
504
+ buildingItemReference = false;
505
+ buildingPriceOrWeight = false;
506
+
507
+ priceOrWeightCheckDigit = rcnChar;
508
+ break;
509
+
510
+ default:
511
+ validFormat = false;
512
+ break;
513
+ }
514
+ }
515
+ }
516
+
517
+ validFormat &&= itemReference !== "" && priceOrWeight !== "";
518
+
519
+ if (!validFormat) {
520
+ throw new RangeError(i18nextGS1.t("Identifier.invalidVariableMeasureRCNFormat"));
521
+ }
522
+
523
+ if (!validRCNPrefix) {
524
+ throw new RangeError(i18nextGS1.t("Identifier.invalidVariableMeasureRCNPrefix"));
525
+ }
526
+
527
+ if (priceOrWeightCheckDigit !== "" && !isValidPriceOrWeightCheckDigit(priceOrWeight, priceOrWeightCheckDigit)) {
528
+ throw new RangeError(i18nextGS1.t("Identifier.invalidVariableMeasurePriceOrWeight"));
529
+ }
530
+
531
+ if (!hasValidCheckDigit(rcn)) {
532
+ throw new RangeError(i18nextGS1.t("Identifier.invalidCheckDigit"));
533
+ }
534
+
535
+ return {
536
+ itemReference: Number(itemReference),
537
+ priceOrWeight: Number(priceOrWeight)
538
+ };
539
+ }
540
+ }
541
+
542
+ /**
543
+ * GTIN-13 validator.
544
+ */
545
+ export const GTIN13_VALIDATOR = new GTINValidator(GTINTypes.GTIN13);
546
+
547
+ /**
548
+ * GTIN-12 validator.
549
+ */
550
+ export const GTIN12_VALIDATOR = new GTINValidator(GTINTypes.GTIN12);
551
+
552
+ /**
553
+ * GTIN-8 validator.
554
+ */
555
+ export const GTIN8_VALIDATOR = new GTINValidator(GTINTypes.GTIN8);
556
+
557
+ /**
558
+ * GTIN validators indexed by prefix type.
559
+ */
560
+ export const GTIN_VALIDATORS: Record<PrefixType, GTINValidator> = {
561
+ [PrefixTypes.GS1CompanyPrefix]: GTIN13_VALIDATOR,
562
+ [PrefixTypes.UPCCompanyPrefix]: GTIN12_VALIDATOR,
563
+ [PrefixTypes.GS18Prefix]: GTIN8_VALIDATOR
564
+ };
@@ -0,0 +1,97 @@
1
+ import type { CharacterSetCreator } from "@aidc-toolkit/utility";
2
+ import type { IdentifierType } from "./identifier-type.js";
3
+ import type { ContentCharacterSet, IdentifierValidation, IdentifierValidator } from "./identifier-validator.js";
4
+ import type { PrefixProvider } from "./prefix-provider";
5
+ import type { PrefixType } from "./prefix-type.js";
6
+
7
+ /**
8
+ * Identifier creator. Creates an identifier based on its definition in section 3 of the {@link
9
+ * https://www.gs1.org/genspecs | GS1 General Specifications}.
10
+ *
11
+ * Keys are created based on a prefix defined in a prefix manager to which the identifier creator is bound.
12
+ */
13
+ export interface IdentifierCreator extends IdentifierValidator {
14
+ /**
15
+ * Get the prefix provider to which this identifier creator is bound.
16
+ */
17
+ get prefixProvider(): PrefixProvider;
18
+
19
+ /**
20
+ * Get the prefix, equivalent to calling {@linkcode PrefixProvider.prefix | prefixProvider.prefix} for a GTIN or
21
+ * {@linkcode PrefixProvider.gs1CompanyPrefix | prefixProvider.gs1CompanyPrefix} for all other identifier types.
22
+ */
23
+ get prefix(): string;
24
+
25
+ /**
26
+ * Get the reference length.
27
+ */
28
+ get referenceLength(): number;
29
+ }
30
+
31
+ /**
32
+ * Abstract identifier creator. Implements common functionality for an identifier creator, bound to a
33
+ * {@link PrefixProvider}.
34
+ */
35
+ export abstract class AbstractIdentifierCreator implements IdentifierCreator {
36
+ /**
37
+ * Prefix provider.
38
+ */
39
+ private _prefixProvider!: PrefixProvider;
40
+
41
+ /**
42
+ * Reference length.
43
+ */
44
+ private _referenceLength!: number;
45
+
46
+ /**
47
+ * Initialize the prefix manager. This method is in lieu of a constructor due to the mixin architecture.
48
+ *
49
+ * @param prefixProvider
50
+ * Prefix provider.
51
+ *
52
+ * @param prefix
53
+ * Prefix within prefix manager to use to calculate reference length.
54
+ *
55
+ * @param checkAllowance
56
+ * Number of characters to allow for check digit or check character pair.
57
+ */
58
+ protected init(prefixProvider: PrefixProvider, prefix: string, checkAllowance: number): void {
59
+ this._prefixProvider = prefixProvider;
60
+
61
+ // Reference length allows for prefix and optionally check digit or check character pair.
62
+ this._referenceLength = this.length - prefix.length - checkAllowance;
63
+ }
64
+
65
+ abstract get identifierType(): IdentifierType;
66
+
67
+ abstract get prefixType(): PrefixType;
68
+
69
+ abstract get length(): number;
70
+
71
+ abstract get referenceCharacterSet(): ContentCharacterSet;
72
+
73
+ abstract get referenceCreator(): CharacterSetCreator;
74
+
75
+ /**
76
+ * @inheritDoc
77
+ */
78
+ get prefixProvider(): PrefixProvider {
79
+ return this._prefixProvider;
80
+ }
81
+
82
+ /**
83
+ * @inheritDoc
84
+ */
85
+ get prefix(): string {
86
+ return this.prefixProvider.gs1CompanyPrefix;
87
+ }
88
+
89
+ /**
90
+ * @inheritDoc
91
+ */
92
+ get referenceLength(): number {
93
+ return this._referenceLength;
94
+ }
95
+
96
+ abstract validate(identifier: string, validation?: IdentifierValidation): void;
97
+ }