@aidc-toolkit/gs1 1.0.24-beta → 1.0.25-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 (60) hide show
  1. package/dist/index.cjs +562 -373
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/index.d.cts +776 -415
  4. package/dist/index.d.ts +776 -415
  5. package/dist/index.js +549 -363
  6. package/dist/index.js.map +1 -1
  7. package/package.json +5 -5
  8. package/src/abstract-identifier-creator.ts +97 -0
  9. package/src/abstract-identifier-validator.ts +140 -0
  10. package/src/abstract-numeric-identifier-creator.ts +168 -0
  11. package/src/abstract-numeric-identifier-validator.ts +69 -0
  12. package/src/character-set.ts +10 -10
  13. package/src/check.ts +7 -7
  14. package/src/content-character-set.ts +29 -0
  15. package/src/creators.ts +113 -0
  16. package/src/descriptors.ts +332 -0
  17. package/src/gtin-creator.ts +7 -6
  18. package/src/gtin-descriptor.ts +18 -0
  19. package/src/gtin-type.ts +50 -0
  20. package/src/gtin-validator.ts +36 -60
  21. package/src/identifier-creator.ts +11 -75
  22. package/src/identifier-descriptor.ts +30 -0
  23. package/src/identifier-type.ts +6 -1
  24. package/src/identifier-validator.ts +12 -188
  25. package/src/index.ts +29 -5
  26. package/src/locale/en/locale-resources.ts +0 -1
  27. package/src/locale/fr/locale-resources.ts +0 -1
  28. package/src/non-gtin-numeric-identifier-creator.ts +5 -11
  29. package/src/non-gtin-numeric-identifier-descriptor.ts +24 -0
  30. package/src/non-gtin-numeric-identifier-type.ts +7 -0
  31. package/src/non-gtin-numeric-identifier-validator.ts +8 -42
  32. package/src/non-numeric-identifier-creator.ts +5 -15
  33. package/src/non-numeric-identifier-descriptor.ts +29 -0
  34. package/src/non-numeric-identifier-type.ts +7 -0
  35. package/src/non-numeric-identifier-validator.ts +15 -53
  36. package/src/numeric-identifier-creator.ts +20 -163
  37. package/src/numeric-identifier-descriptor.ts +23 -0
  38. package/src/numeric-identifier-type.ts +44 -0
  39. package/src/numeric-identifier-validator.ts +13 -116
  40. package/src/prefix-manager.ts +84 -142
  41. package/src/prefix-provider.ts +2 -2
  42. package/src/prefix-type.ts +6 -1
  43. package/src/prefix-validator.ts +141 -79
  44. package/src/serializable-numeric-identifier-creator.ts +4 -14
  45. package/src/serializable-numeric-identifier-descriptor.ts +29 -0
  46. package/src/serializable-numeric-identifier-type.ts +9 -0
  47. package/src/serializable-numeric-identifier-validator.ts +17 -45
  48. package/src/validators.ts +203 -0
  49. package/test/creator.test.ts +2 -4
  50. package/test/gtin-creator.ts +5 -1
  51. package/test/gtin-validator.test.ts +5 -8
  52. package/test/identifier-creator.ts +1 -0
  53. package/test/identifier-validator.ts +2 -2
  54. package/test/non-gtin-numeric-identifier-creator.ts +8 -92
  55. package/test/non-gtin-numeric-identifier-validator.ts +1 -1
  56. package/test/non-numeric-identifier-creator.ts +93 -0
  57. package/test/numeric-identifier-creator.ts +9 -3
  58. package/test/numeric-identifier-validator.ts +3 -7
  59. package/test/serializable-numeric-identifier-creator.ts +10 -2
  60. package/test/validator.test.ts +59 -35
@@ -0,0 +1,113 @@
1
+ import {
2
+ isGTINDescriptor,
3
+ isNonGTINNumericIdentifierDescriptor,
4
+ isNonNumericIdentifierDescriptor,
5
+ isNumericIdentifierDescriptor,
6
+ isSerializableNumericIdentifierDescriptor
7
+ } from "./descriptors";
8
+ import type { GTINCreator } from "./gtin-creator";
9
+ import type { IdentifierCreator } from "./identifier-creator";
10
+ import type { IdentifierType, IdentifierTypes } from "./identifier-type";
11
+ import type { NonGTINNumericIdentifierCreator } from "./non-gtin-numeric-identifier-creator";
12
+ import type { NonGTINNumericIdentifierType } from "./non-gtin-numeric-identifier-type";
13
+ import type { NonNumericIdentifierCreator } from "./non-numeric-identifier-creator";
14
+ import type { NonNumericIdentifierType } from "./non-numeric-identifier-type";
15
+ import type { NumericIdentifierCreator } from "./numeric-identifier-creator";
16
+ import type { SerializableNumericIdentifierCreator } from "./serializable-numeric-identifier-creator";
17
+ import type { SerializableNumericIdentifierType } from "./serializable-numeric-identifier-type";
18
+
19
+ /**
20
+ * Determine the identifier creator type for an identifier type.
21
+ *
22
+ * @template TIdentifierType
23
+ * Identifier type type.
24
+ */
25
+ export type IdentifierTypeCreator<TIdentifierType extends IdentifierType> = TIdentifierType extends NonNumericIdentifierType ?
26
+ NonNumericIdentifierCreator :
27
+ TIdentifierType extends SerializableNumericIdentifierType ?
28
+ SerializableNumericIdentifierCreator :
29
+ TIdentifierType extends NonGTINNumericIdentifierType ?
30
+ NonGTINNumericIdentifierCreator :
31
+ TIdentifierType extends typeof IdentifierTypes.GTIN ?
32
+ GTINCreator :
33
+ IdentifierCreator;
34
+
35
+ /**
36
+ * Identifier creators entry type based on identifier type type.
37
+ *
38
+ * @template TIdentifierType
39
+ * Identifier type type.
40
+ */
41
+ export type IdentifierCreatorsEntry<TIdentifierType extends IdentifierType> = IdentifierTypeCreator<TIdentifierType>;
42
+
43
+ /**
44
+ * Identifier creators record type.
45
+ */
46
+ export type IdentifierCreatorsRecord = {
47
+ [TIdentifierType in IdentifierType]: IdentifierCreatorsEntry<TIdentifierType>;
48
+ };
49
+
50
+ /**
51
+ * Determine if identifier creator is a numeric identifier creator.
52
+ *
53
+ * @param identifierCreator
54
+ * Identifier creator.
55
+ *
56
+ * @returns
57
+ * True if identifier creator is a numeric identifier creator.
58
+ */
59
+ export function isNumericIdentifierCreator(identifierCreator: IdentifierCreator): identifierCreator is NumericIdentifierCreator {
60
+ return isNumericIdentifierDescriptor(identifierCreator);
61
+ }
62
+
63
+ /**
64
+ * Determine if identifier creator is a GTIN creator.
65
+ *
66
+ * @param identifierCreator
67
+ * Identifier creator.
68
+ *
69
+ * @returns
70
+ * True if identifier creator is a GTIN creator.
71
+ */
72
+ export function isGTINCreator(identifierCreator: IdentifierCreator): identifierCreator is GTINCreator {
73
+ return isGTINDescriptor(identifierCreator);
74
+ }
75
+
76
+ /**
77
+ * Determine if identifier creator is a non-GTIN numeric identifier creator.
78
+ *
79
+ * @param identifierCreator
80
+ * Identifier creator.
81
+ *
82
+ * @returns
83
+ * True if identifier creator is a non-GTIN numeric identifier creator.
84
+ */
85
+ export function isNonGTINNumericIdentifierCreator(identifierCreator: IdentifierCreator): identifierCreator is NonGTINNumericIdentifierCreator {
86
+ return isNonGTINNumericIdentifierDescriptor(identifierCreator);
87
+ }
88
+
89
+ /**
90
+ * Determine if identifier creator is a serializable numeric identifier creator.
91
+ *
92
+ * @param identifierCreator
93
+ * Identifier creator.
94
+ *
95
+ * @returns
96
+ * True if identifier creator is a serializable numeric identifier creator.
97
+ */
98
+ export function isSerializableNumericIdentifierCreator(identifierCreator: IdentifierCreator): identifierCreator is SerializableNumericIdentifierCreator {
99
+ return isSerializableNumericIdentifierDescriptor(identifierCreator);
100
+ }
101
+
102
+ /**
103
+ * Determine if identifier creator is a non-numeric identifier creator.
104
+ *
105
+ * @param identifierCreator
106
+ * Identifier creator.
107
+ *
108
+ * @returns
109
+ * True if identifier creator is a non-numeric identifier creator.
110
+ */
111
+ export function isNonNumericIdentifierCreator(identifierCreator: IdentifierCreator): identifierCreator is NonNumericIdentifierCreator {
112
+ return isNonNumericIdentifierDescriptor(identifierCreator);
113
+ }
@@ -0,0 +1,332 @@
1
+ import { ContentCharacterSets } from "./content-character-set";
2
+ import type { GTINDescriptor } from "./gtin-descriptor";
3
+ import { type GTINBaseType, GTINTypes } from "./gtin-type";
4
+ import type { IdentifierDescriptor } from "./identifier-descriptor";
5
+ import { type IdentifierType, IdentifierTypes } from "./identifier-type";
6
+ import type { NonGTINNumericIdentifierDescriptor } from "./non-gtin-numeric-identifier-descriptor";
7
+ import type { NonGTINNumericIdentifierType } from "./non-gtin-numeric-identifier-type";
8
+ import type { NonNumericIdentifierDescriptor } from "./non-numeric-identifier-descriptor";
9
+ import type { NonNumericIdentifierType } from "./non-numeric-identifier-type";
10
+ import type { NumericIdentifierDescriptor } from "./numeric-identifier-descriptor";
11
+ import { LeaderTypes, type NumericIdentifierType } from "./numeric-identifier-type";
12
+ import { PrefixTypes } from "./prefix-type";
13
+ import type { SerializableNumericIdentifierDescriptor } from "./serializable-numeric-identifier-descriptor";
14
+ import type { SerializableNumericIdentifierType } from "./serializable-numeric-identifier-type";
15
+
16
+ /**
17
+ * GTIN-13 descriptor.
18
+ */
19
+ const GTIN13_DESCRIPTOR: GTINDescriptor = {
20
+ identifierType: IdentifierTypes.GTIN,
21
+ prefixType: PrefixTypes.GS1CompanyPrefix,
22
+ length: GTINTypes.GTIN13,
23
+ referenceCharacterSet: ContentCharacterSets.Numeric,
24
+ leaderType: LeaderTypes.IndicatorDigit
25
+ };
26
+
27
+ /**
28
+ * GTIN-12 descriptor.
29
+ */
30
+ const GTIN12_DESCRIPTOR: GTINDescriptor = {
31
+ identifierType: IdentifierTypes.GTIN,
32
+ prefixType: PrefixTypes.UPCCompanyPrefix,
33
+ length: GTINTypes.GTIN12,
34
+ referenceCharacterSet: ContentCharacterSets.Numeric,
35
+ leaderType: LeaderTypes.IndicatorDigit
36
+ };
37
+
38
+ /**
39
+ * GTIN-8 descriptor.
40
+ */
41
+ const GTIN8_DESCRIPTOR: GTINDescriptor = {
42
+ identifierType: IdentifierTypes.GTIN,
43
+ prefixType: PrefixTypes.GS18Prefix,
44
+ length: GTINTypes.GTIN8,
45
+ referenceCharacterSet: ContentCharacterSets.Numeric,
46
+ leaderType: LeaderTypes.IndicatorDigit
47
+ };
48
+
49
+ /**
50
+ * GTIN descriptors indexed by prefix type.
51
+ */
52
+ const GTIN_DESCRIPTORS: Readonly<Record<GTINBaseType, GTINDescriptor>> = {
53
+ [GTINTypes.GTIN13]: GTIN13_DESCRIPTOR,
54
+ [GTINTypes.GTIN12]: GTIN12_DESCRIPTOR,
55
+ [GTINTypes.GTIN8]: GTIN8_DESCRIPTOR
56
+ };
57
+
58
+ /**
59
+ * GLN descriptor.
60
+ */
61
+ const GLN_DESCRIPTOR: NonGTINNumericIdentifierDescriptor = {
62
+ identifierType: IdentifierTypes.GLN,
63
+ prefixType: PrefixTypes.GS1CompanyPrefix,
64
+ length: 13,
65
+ referenceCharacterSet: ContentCharacterSets.Numeric,
66
+ leaderType: LeaderTypes.None
67
+ };
68
+
69
+ /**
70
+ * SSCC descriptor.
71
+ */
72
+ const SSCC_DESCRIPTOR: NonGTINNumericIdentifierDescriptor = {
73
+ identifierType: IdentifierTypes.SSCC,
74
+ prefixType: PrefixTypes.GS1CompanyPrefix,
75
+ length: 18,
76
+ referenceCharacterSet: ContentCharacterSets.Numeric,
77
+ leaderType: LeaderTypes.ExtensionDigit
78
+ };
79
+
80
+ /**
81
+ * GRAI descriptor.
82
+ */
83
+ const GRAI_DESCRIPTOR: SerializableNumericIdentifierDescriptor = {
84
+ identifierType: IdentifierTypes.GRAI,
85
+ prefixType: PrefixTypes.GS1CompanyPrefix,
86
+ length: 13,
87
+ referenceCharacterSet: ContentCharacterSets.Numeric,
88
+ leaderType: LeaderTypes.None,
89
+ serialComponentLength: 16,
90
+ serialComponentCharacterSet: ContentCharacterSets.AI82
91
+ };
92
+
93
+ /**
94
+ * GIAI descriptor.
95
+ */
96
+ const GIAI_DESCRIPTOR: NonNumericIdentifierDescriptor = {
97
+ identifierType: IdentifierTypes.GIAI,
98
+ prefixType: PrefixTypes.GS1CompanyPrefix,
99
+ length: 30,
100
+ referenceCharacterSet: ContentCharacterSets.AI82,
101
+ requiresCheckCharacterPair: false
102
+ };
103
+
104
+ /**
105
+ * GSRN descriptor.
106
+ */
107
+ const GSRN_DESCRIPTOR: NonGTINNumericIdentifierDescriptor = {
108
+ identifierType: IdentifierTypes.GSRN,
109
+ prefixType: PrefixTypes.GS1CompanyPrefix,
110
+ length: 18,
111
+ referenceCharacterSet: ContentCharacterSets.Numeric,
112
+ leaderType: LeaderTypes.None
113
+ };
114
+
115
+ /**
116
+ * GDTI descriptor.
117
+ */
118
+ const GDTI_DESCRIPTOR: SerializableNumericIdentifierDescriptor = {
119
+ identifierType: IdentifierTypes.GDTI,
120
+ prefixType: PrefixTypes.GS1CompanyPrefix,
121
+ length: 13,
122
+ referenceCharacterSet: ContentCharacterSets.Numeric,
123
+ leaderType: LeaderTypes.None,
124
+ serialComponentLength: 17,
125
+ serialComponentCharacterSet: ContentCharacterSets.AI82
126
+ };
127
+
128
+ /**
129
+ * GINC descriptor.
130
+ */
131
+ const GINC_DESCRIPTOR: NonNumericIdentifierDescriptor = {
132
+ identifierType: IdentifierTypes.GINC,
133
+ prefixType: PrefixTypes.GS1CompanyPrefix,
134
+ length: 30,
135
+ referenceCharacterSet: ContentCharacterSets.AI82,
136
+ requiresCheckCharacterPair: false
137
+ };
138
+
139
+ /**
140
+ * GSIN descriptor.
141
+ */
142
+ const GSIN_DESCRIPTOR: NonGTINNumericIdentifierDescriptor = {
143
+ identifierType: IdentifierTypes.GSIN,
144
+ prefixType: PrefixTypes.GS1CompanyPrefix,
145
+ length: 17,
146
+ referenceCharacterSet: ContentCharacterSets.Numeric,
147
+ leaderType: LeaderTypes.None
148
+ };
149
+
150
+ /**
151
+ * GCN descriptor.
152
+ */
153
+ const GCN_DESCRIPTOR: SerializableNumericIdentifierDescriptor = {
154
+ identifierType: IdentifierTypes.GCN,
155
+ prefixType: PrefixTypes.GS1CompanyPrefix,
156
+ length: 13,
157
+ referenceCharacterSet: ContentCharacterSets.Numeric,
158
+ leaderType: LeaderTypes.None,
159
+ serialComponentLength: 12,
160
+ serialComponentCharacterSet: ContentCharacterSets.Numeric
161
+ };
162
+
163
+ /**
164
+ * CPID descriptor.
165
+ */
166
+ const CPID_DESCRIPTOR: NonNumericIdentifierDescriptor = {
167
+ identifierType: IdentifierTypes.CPID,
168
+ prefixType: PrefixTypes.GS1CompanyPrefix,
169
+ length: 30,
170
+ referenceCharacterSet: ContentCharacterSets.AI39,
171
+ requiresCheckCharacterPair: false
172
+ };
173
+
174
+ /**
175
+ * GMN descriptor.
176
+ */
177
+ const GMN_DESCRIPTOR: NonNumericIdentifierDescriptor = {
178
+ identifierType: IdentifierTypes.GMN,
179
+ prefixType: PrefixTypes.GS1CompanyPrefix,
180
+ length: 25,
181
+ referenceCharacterSet: ContentCharacterSets.AI82,
182
+ requiresCheckCharacterPair: true
183
+ };
184
+
185
+ /**
186
+ * Identifier descriptor type based on identifier type type.
187
+ *
188
+ * @template TIdentifierType
189
+ * Identifier type type.
190
+ */
191
+ export type IdentifierTypeDescriptor<TIdentifierType extends IdentifierType> = TIdentifierType extends NonNumericIdentifierType ?
192
+ NonNumericIdentifierDescriptor :
193
+ TIdentifierType extends SerializableNumericIdentifierType ?
194
+ SerializableNumericIdentifierDescriptor :
195
+ TIdentifierType extends NonGTINNumericIdentifierType ?
196
+ NonGTINNumericIdentifierDescriptor :
197
+ TIdentifierType extends typeof IdentifierTypes.GTIN ?
198
+ GTINDescriptor :
199
+ IdentifierDescriptor;
200
+
201
+ /**
202
+ * Identifier descriptors entry type based on identifier type type.
203
+ *
204
+ * @template TIdentifierType
205
+ * Identifier type type.
206
+ */
207
+ export type IdentifierDescriptorsEntry<TIdentifierType extends IdentifierType> = TIdentifierType extends typeof IdentifierTypes.GTIN ?
208
+ Readonly<Record<GTINBaseType, GTINDescriptor>> :
209
+ IdentifierTypeDescriptor<TIdentifierType>;
210
+
211
+ /**
212
+ * Identifier descriptors record type.
213
+ */
214
+ export type IdentifierDescriptorsRecord = {
215
+ [TIdentifierType in IdentifierType]: IdentifierDescriptorsEntry<TIdentifierType>;
216
+ };
217
+
218
+ /**
219
+ * Identifier descriptors for all identifier types.
220
+ */
221
+ export const IdentifierDescriptors: IdentifierDescriptorsRecord = {
222
+ [IdentifierTypes.GTIN]: GTIN_DESCRIPTORS,
223
+ [IdentifierTypes.GLN]: GLN_DESCRIPTOR,
224
+ [IdentifierTypes.SSCC]: SSCC_DESCRIPTOR,
225
+ [IdentifierTypes.GRAI]: GRAI_DESCRIPTOR,
226
+ [IdentifierTypes.GIAI]: GIAI_DESCRIPTOR,
227
+ [IdentifierTypes.GSRN]: GSRN_DESCRIPTOR,
228
+ [IdentifierTypes.GDTI]: GDTI_DESCRIPTOR,
229
+ [IdentifierTypes.GINC]: GINC_DESCRIPTOR,
230
+ [IdentifierTypes.GSIN]: GSIN_DESCRIPTOR,
231
+ [IdentifierTypes.GCN]: GCN_DESCRIPTOR,
232
+ [IdentifierTypes.CPID]: CPID_DESCRIPTOR,
233
+ [IdentifierTypes.GMN]: GMN_DESCRIPTOR
234
+ } as const;
235
+
236
+ /**
237
+ * Determine if identifier descriptors or descriptor is GTIN descriptors.
238
+ *
239
+ * @param identifierDescriptorsOrDescriptor
240
+ * Identifier descriptors or descriptor.
241
+ *
242
+ * @returns
243
+ * True if GTIN descriptors.
244
+ */
245
+ export function isGTINDescriptors(identifierDescriptorsOrDescriptor: IdentifierDescriptorsEntry<IdentifierType>): identifierDescriptorsOrDescriptor is Readonly<Record<GTINBaseType, GTINDescriptor>> {
246
+ return !("identifierType" in identifierDescriptorsOrDescriptor);
247
+ }
248
+
249
+ /**
250
+ * Determine if an array of identifier types includes a given identifier type.
251
+ *
252
+ * @param identifierTypes
253
+ * Identifier types.
254
+ *
255
+ * @param identifierType
256
+ * Identifier type.
257
+ *
258
+ * @returns
259
+ * True if element is included in the array.
260
+ */
261
+ function identifierTypesIncludes(identifierTypes: readonly IdentifierType[], identifierType: IdentifierType): boolean {
262
+ return identifierTypes.includes(identifierType);
263
+ }
264
+
265
+ const NUMERIC_IDENTIFIER_TYPES: readonly NumericIdentifierType[] = [IdentifierTypes.GTIN, IdentifierTypes.GLN, IdentifierTypes.SSCC, IdentifierTypes.GRAI, IdentifierTypes.GSRN, IdentifierTypes.GDTI, IdentifierTypes.GSIN, IdentifierTypes.GCN];
266
+
267
+ /**
268
+ * Determine if identifier descriptor is a numeric identifier descriptor.
269
+ *
270
+ * @param identifierDescriptor
271
+ * Identifier descriptor.
272
+ *
273
+ * @returns
274
+ * True if identifier descriptor is a numeric identifier descriptor.
275
+ */
276
+ export function isNumericIdentifierDescriptor(identifierDescriptor: IdentifierDescriptor): identifierDescriptor is NumericIdentifierDescriptor {
277
+ return identifierTypesIncludes(NUMERIC_IDENTIFIER_TYPES, identifierDescriptor.identifierType);
278
+ }
279
+
280
+ /**
281
+ * Determine if identifier descriptor is a GTIN descriptor.
282
+ *
283
+ * @param identifierDescriptor
284
+ * Identifier descriptor.
285
+ *
286
+ * @returns
287
+ * True if identifier descriptor is a GTIN descriptor.
288
+ */
289
+ export function isGTINDescriptor(identifierDescriptor: IdentifierDescriptor): identifierDescriptor is GTINDescriptor {
290
+ return identifierDescriptor.identifierType === IdentifierTypes.GTIN;
291
+ }
292
+
293
+ /**
294
+ * Determine if identifier descriptor is a non-GTIN numeric identifier descriptor.
295
+ *
296
+ * @param identifierDescriptor
297
+ * Identifier descriptor.
298
+ *
299
+ * @returns
300
+ * True if identifier descriptor is a non-GTIN numeric identifier descriptor.
301
+ */
302
+ export function isNonGTINNumericIdentifierDescriptor(identifierDescriptor: IdentifierDescriptor): identifierDescriptor is NonGTINNumericIdentifierDescriptor {
303
+ return isNumericIdentifierDescriptor(identifierDescriptor) && !isGTINDescriptor(identifierDescriptor);
304
+ }
305
+
306
+ const SERIALIZABLE_NUMERIC_IDENTIFIER_TYPES: readonly SerializableNumericIdentifierType[] = [IdentifierTypes.GRAI, IdentifierTypes.GDTI, IdentifierTypes.GCN];
307
+
308
+ /**
309
+ * Determine if identifier descriptor is a serializable numeric identifier descriptor.
310
+ *
311
+ * @param identifierDescriptor
312
+ * Identifier descriptor.
313
+ *
314
+ * @returns
315
+ * True if identifier descriptor is a serializable numeric identifier descriptor.
316
+ */
317
+ export function isSerializableNumericIdentifierDescriptor(identifierDescriptor: IdentifierDescriptor): identifierDescriptor is SerializableNumericIdentifierDescriptor {
318
+ return identifierTypesIncludes(SERIALIZABLE_NUMERIC_IDENTIFIER_TYPES, identifierDescriptor.identifierType);
319
+ }
320
+
321
+ /**
322
+ * Determine if identifier descriptor is a non-numeric identifier descriptor.
323
+ *
324
+ * @param identifierDescriptor
325
+ * Identifier descriptor.
326
+ *
327
+ * @returns
328
+ * True if identifier descriptor is a non-numeric identifier descriptor.
329
+ */
330
+ export function isNonNumericIdentifierDescriptor(identifierDescriptor: IdentifierDescriptor): identifierDescriptor is NonNumericIdentifierDescriptor {
331
+ return !isNumericIdentifierDescriptor(identifierDescriptor);
332
+ }
@@ -6,10 +6,11 @@ import {
6
6
  type TransformerOutput
7
7
  } from "@aidc-toolkit/utility";
8
8
  import { Mixin } from "ts-mixer";
9
+ import { AbstractNumericIdentifierCreator } from "./abstract-numeric-identifier-creator";
9
10
  import { checkDigit, priceOrWeightCheckDigit } from "./check";
10
- import { type GTINType, GTINTypes, GTINValidator } from "./gtin-validator";
11
+ import { type GTINBaseType, GTINTypes } from "./gtin-type";
12
+ import { GTINValidator } from "./gtin-validator";
11
13
  import { i18nextGS1 } from "./locale/i18n";
12
- import { AbstractNumericIdentifierCreator } from "./numeric-identifier-creator";
13
14
  import type { PrefixProvider } from "./prefix-provider";
14
15
 
15
16
  /**
@@ -32,11 +33,11 @@ export class GTINCreator extends Mixin(GTINValidator, AbstractNumericIdentifierC
32
33
  * @param prefixProvider
33
34
  * Prefix provider.
34
35
  *
35
- * @param gtinType
36
- * GTIN type.
36
+ * @param gtinBaseType
37
+ * GTIN base type (all except GTIN-14).
37
38
  */
38
- constructor(prefixProvider: PrefixProvider, gtinType: GTINType) {
39
- super(gtinType);
39
+ constructor(prefixProvider: PrefixProvider, gtinBaseType: GTINBaseType) {
40
+ super(gtinBaseType);
40
41
 
41
42
  this.init(prefixProvider, prefixProvider.prefix);
42
43
  }
@@ -0,0 +1,18 @@
1
+ import type { IdentifierTypes } from "./identifier-type";
2
+ import type { NumericIdentifierDescriptor } from "./numeric-identifier-descriptor";
3
+ import type { LeaderTypes } from "./numeric-identifier-type";
4
+
5
+ /**
6
+ * GTIN descriptor.
7
+ */
8
+ export interface GTINDescriptor extends NumericIdentifierDescriptor {
9
+ /**
10
+ * @inheritDoc
11
+ */
12
+ readonly identifierType: typeof IdentifierTypes.GTIN;
13
+
14
+ /**
15
+ * @inheritDoc
16
+ */
17
+ readonly leaderType: typeof LeaderTypes.IndicatorDigit;
18
+ }
@@ -0,0 +1,50 @@
1
+ import { type PrefixType, PrefixTypes } from "./prefix-type";
2
+
3
+ /**
4
+ * GTIN types. The numeric values are equal to the lengths of the GTIN types.
5
+ */
6
+ export const GTINTypes = {
7
+ /**
8
+ * GTIN-13.
9
+ */
10
+ GTIN13: 13,
11
+
12
+ /**
13
+ * GTIN-12.
14
+ */
15
+ GTIN12: 12,
16
+
17
+ /**
18
+ * GTIN-8.
19
+ */
20
+ GTIN8: 8,
21
+
22
+ /**
23
+ * GTIN-14.
24
+ */
25
+ GTIN14: 14
26
+ } as const;
27
+
28
+ /**
29
+ * GTIN type key.
30
+ */
31
+ export type GTINTypeKey = keyof typeof GTINTypes;
32
+
33
+ /**
34
+ * GTIN type.
35
+ */
36
+ export type GTINType = typeof GTINTypes[GTINTypeKey];
37
+
38
+ /**
39
+ * GTIN base type (all except GTIN-14).
40
+ */
41
+ export type GTINBaseType = Exclude<GTINType, typeof GTINTypes.GTIN14>;
42
+
43
+ /**
44
+ * GTIN base types by prefix type.
45
+ */
46
+ export const GTIN_BASE_TYPES: Readonly<Record<PrefixType, GTINBaseType>> = {
47
+ [PrefixTypes.GS1CompanyPrefix]: GTINTypes.GTIN13,
48
+ [PrefixTypes.UPCCompanyPrefix]: GTINTypes.GTIN12,
49
+ [PrefixTypes.GS18Prefix]: GTINTypes.GTIN8
50
+ };
@@ -1,41 +1,13 @@
1
1
  import { type CharacterSetValidation, NUMERIC_CREATOR } from "@aidc-toolkit/utility";
2
+ import { AbstractNumericIdentifierValidator } from "./abstract-numeric-identifier-validator";
2
3
  import { checkDigit, hasValidCheckDigit, isValidPriceOrWeightCheckDigit } from "./check";
3
- import { IdentifierTypes } from "./identifier-type";
4
+ import { IdentifierDescriptors } from "./descriptors";
5
+ import type { GTINDescriptor } from "./gtin-descriptor";
6
+ import { type GTINBaseType, type GTINType, GTINTypes } from "./gtin-type";
4
7
  import { i18nextGS1 } from "./locale/i18n";
5
- import { AbstractNumericIdentifierValidator, LeaderTypes } from "./numeric-identifier-validator";
6
8
  import { type PrefixType, PrefixTypes } from "./prefix-type";
7
9
  import { PrefixValidator } from "./prefix-validator";
8
10
 
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
11
  /**
40
12
  * Levels at which GTIN is to be validated.
41
13
  */
@@ -56,10 +28,15 @@ export const GTINLevels = {
56
28
  OtherThanRetailConsumer: 2
57
29
  } as const;
58
30
 
31
+ /**
32
+ * GTIN level key.
33
+ */
34
+ export type GTINLevelKey = keyof typeof GTINLevels;
35
+
59
36
  /**
60
37
  * GTIN level.
61
38
  */
62
- export type GTINLevel = typeof GTINLevels[keyof typeof GTINLevels];
39
+ export type GTINLevel = typeof GTINLevels[GTINLevelKey];
63
40
 
64
41
  /**
65
42
  * Restricted Circulation Number reference.
@@ -79,7 +56,7 @@ export interface RCNReference {
79
56
  /**
80
57
  * GTIN validator.
81
58
  */
82
- export class GTINValidator extends AbstractNumericIdentifierValidator {
59
+ export class GTINValidator extends AbstractNumericIdentifierValidator<GTINDescriptor> {
83
60
  /**
84
61
  * Validation parameters for optional indicator digit.
85
62
  */
@@ -97,35 +74,30 @@ export class GTINValidator extends AbstractNumericIdentifierValidator {
97
74
  maximumLength: 8
98
75
  };
99
76
 
77
+ /**
78
+ * Prefix type.
79
+ */
80
+ private readonly _prefixType: PrefixType;
81
+
100
82
  /**
101
83
  * Constructor.
102
84
  *
103
- * @param gtinType
104
- * GTIN type.
85
+ * @param gtinBaseType
86
+ * GTIN base type (all except GTIN-14).
105
87
  */
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;
88
+ constructor(gtinBaseType: GTINBaseType) {
89
+ const identifierDescriptor = IdentifierDescriptors.GTIN[gtinBaseType];
114
90
 
115
- case GTINTypes.GTIN12:
116
- prefixType = PrefixTypes.UPCCompanyPrefix;
117
- break;
118
-
119
- case GTINTypes.GTIN8:
120
- prefixType = PrefixTypes.GS18Prefix;
121
- break;
91
+ super(identifierDescriptor);
122
92
 
123
- default:
124
- // Should never get here.
125
- throw new Error("Not supported");
126
- }
93
+ this._prefixType = identifierDescriptor.prefixType;
94
+ }
127
95
 
128
- super(IdentifierTypes.GTIN, prefixType, gtinType, LeaderTypes.IndicatorDigit);
96
+ /**
97
+ * @inheritDoc
98
+ */
99
+ override get prefixType(): PrefixType {
100
+ return this._prefixType;
129
101
  }
130
102
 
131
103
  /**
@@ -542,23 +514,27 @@ export class GTINValidator extends AbstractNumericIdentifierValidator {
542
514
  /**
543
515
  * GTIN-13 validator.
544
516
  */
517
+ // Defined here because of circular reference.
545
518
  export const GTIN13_VALIDATOR = new GTINValidator(GTINTypes.GTIN13);
546
519
 
547
520
  /**
548
521
  * GTIN-12 validator.
549
522
  */
523
+ // Defined here because of circular reference.
550
524
  export const GTIN12_VALIDATOR = new GTINValidator(GTINTypes.GTIN12);
551
525
 
552
526
  /**
553
527
  * GTIN-8 validator.
554
528
  */
529
+ // Defined here because of circular reference.
555
530
  export const GTIN8_VALIDATOR = new GTINValidator(GTINTypes.GTIN8);
556
531
 
557
532
  /**
558
533
  * GTIN validators indexed by prefix type.
559
534
  */
560
- export const GTIN_VALIDATORS: Record<PrefixType, GTINValidator> = {
561
- [PrefixTypes.GS1CompanyPrefix]: GTIN13_VALIDATOR,
562
- [PrefixTypes.UPCCompanyPrefix]: GTIN12_VALIDATOR,
563
- [PrefixTypes.GS18Prefix]: GTIN8_VALIDATOR
535
+ // Defined here because of circular reference.
536
+ export const GTIN_VALIDATORS: Readonly<Record<GTINBaseType, GTINValidator>> = {
537
+ [GTINTypes.GTIN13]: GTIN13_VALIDATOR,
538
+ [GTINTypes.GTIN12]: GTIN12_VALIDATOR,
539
+ [GTINTypes.GTIN8]: GTIN8_VALIDATOR
564
540
  };