@aidc-toolkit/gs1 1.0.24-beta → 1.0.26-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 (240) hide show
  1. package/dist/abstract-identifier-creator.d.ts +75 -0
  2. package/dist/abstract-identifier-creator.d.ts.map +1 -0
  3. package/dist/abstract-identifier-creator.js +56 -0
  4. package/dist/abstract-identifier-creator.js.map +1 -0
  5. package/dist/abstract-identifier-validator.d.ts +95 -0
  6. package/dist/abstract-identifier-validator.d.ts.map +1 -0
  7. package/dist/abstract-identifier-validator.js +123 -0
  8. package/dist/abstract-identifier-validator.js.map +1 -0
  9. package/dist/abstract-numeric-identifier-creator.d.ts +92 -0
  10. package/dist/abstract-numeric-identifier-creator.d.ts.map +1 -0
  11. package/dist/abstract-numeric-identifier-creator.js +137 -0
  12. package/dist/abstract-numeric-identifier-creator.js.map +1 -0
  13. package/dist/abstract-numeric-identifier-validator.d.ts +35 -0
  14. package/dist/abstract-numeric-identifier-validator.d.ts.map +1 -0
  15. package/dist/abstract-numeric-identifier-validator.js +61 -0
  16. package/dist/abstract-numeric-identifier-validator.js.map +1 -0
  17. package/dist/character-set.d.ts +27 -0
  18. package/dist/character-set.d.ts.map +1 -0
  19. package/dist/character-set.js +50 -0
  20. package/dist/character-set.js.map +1 -0
  21. package/dist/check.d.ts +81 -0
  22. package/dist/check.d.ts.map +1 -0
  23. package/dist/check.js +218 -0
  24. package/dist/check.js.map +1 -0
  25. package/dist/content-character-set.d.ts +26 -0
  26. package/dist/content-character-set.d.ts.map +1 -0
  27. package/dist/content-character-set.js +18 -0
  28. package/dist/content-character-set.js.map +1 -0
  29. package/dist/creators.d.ts +81 -0
  30. package/dist/creators.d.ts.map +1 -0
  31. package/dist/creators.js +62 -0
  32. package/dist/creators.js.map +1 -0
  33. package/dist/descriptors.d.ts +96 -0
  34. package/dist/descriptors.d.ts.map +1 -0
  35. package/dist/descriptors.js +266 -0
  36. package/dist/descriptors.js.map +1 -0
  37. package/dist/gtin-creator.d.ts +69 -0
  38. package/dist/gtin-creator.d.ts.map +1 -0
  39. package/dist/gtin-creator.js +159 -0
  40. package/dist/gtin-creator.js.map +1 -0
  41. package/dist/gtin-descriptor.d.ts +17 -0
  42. package/dist/gtin-descriptor.d.ts.map +1 -0
  43. package/dist/gtin-descriptor.js +2 -0
  44. package/dist/gtin-descriptor.js.map +1 -0
  45. package/dist/gtin-type.d.ts +39 -0
  46. package/dist/gtin-type.d.ts.map +1 -0
  47. package/dist/gtin-type.js +31 -0
  48. package/dist/gtin-type.js.map +1 -0
  49. package/dist/gtin-validator.d.ts +191 -0
  50. package/dist/gtin-validator.d.ts.map +1 -0
  51. package/dist/gtin-validator.js +450 -0
  52. package/dist/gtin-validator.js.map +1 -0
  53. package/dist/identifier-creator.d.ts +31 -0
  54. package/dist/identifier-creator.d.ts.map +1 -0
  55. package/dist/identifier-creator.js +2 -0
  56. package/dist/identifier-creator.js.map +1 -0
  57. package/dist/identifier-descriptor.d.ts +27 -0
  58. package/dist/identifier-descriptor.d.ts.map +1 -0
  59. package/dist/identifier-descriptor.js +2 -0
  60. package/dist/identifier-descriptor.js.map +1 -0
  61. package/dist/identifier-type.d.ts +62 -0
  62. package/dist/identifier-type.d.ts.map +1 -0
  63. package/dist/identifier-type.js +54 -0
  64. package/dist/identifier-type.js.map +1 -0
  65. package/dist/identifier-validator.d.ts +53 -0
  66. package/dist/identifier-validator.d.ts.map +1 -0
  67. package/dist/identifier-validator.js +2 -0
  68. package/dist/identifier-validator.js.map +1 -0
  69. package/dist/index.d.ts +52 -1588
  70. package/dist/index.d.ts.map +1 -0
  71. package/dist/index.js +21 -4541
  72. package/dist/index.js.map +1 -1
  73. package/dist/locale/en/locale-resources.d.ts +42 -0
  74. package/dist/locale/en/locale-resources.d.ts.map +1 -0
  75. package/dist/locale/en/locale-resources.js +41 -0
  76. package/dist/locale/en/locale-resources.js.map +1 -0
  77. package/dist/locale/fr/locale-resources.d.ts +42 -0
  78. package/dist/locale/fr/locale-resources.d.ts.map +1 -0
  79. package/dist/locale/fr/locale-resources.js +41 -0
  80. package/dist/locale/fr/locale-resources.js.map +1 -0
  81. package/dist/locale/i18n.d.ts +27 -0
  82. package/dist/locale/i18n.d.ts.map +1 -0
  83. package/dist/locale/i18n.js +36 -0
  84. package/dist/locale/i18n.js.map +1 -0
  85. package/dist/non-gtin-numeric-identifier-creator.d.ts +24 -0
  86. package/dist/non-gtin-numeric-identifier-creator.d.ts.map +1 -0
  87. package/dist/non-gtin-numeric-identifier-creator.js +23 -0
  88. package/dist/non-gtin-numeric-identifier-creator.js.map +1 -0
  89. package/dist/non-gtin-numeric-identifier-descriptor.d.ts +22 -0
  90. package/dist/non-gtin-numeric-identifier-descriptor.d.ts.map +1 -0
  91. package/dist/non-gtin-numeric-identifier-descriptor.js +2 -0
  92. package/dist/non-gtin-numeric-identifier-descriptor.js.map +1 -0
  93. package/dist/non-gtin-numeric-identifier-type.d.ts +7 -0
  94. package/dist/non-gtin-numeric-identifier-type.d.ts.map +1 -0
  95. package/dist/non-gtin-numeric-identifier-type.js +2 -0
  96. package/dist/non-gtin-numeric-identifier-type.js.map +1 -0
  97. package/dist/non-gtin-numeric-identifier-validator.d.ts +17 -0
  98. package/dist/non-gtin-numeric-identifier-validator.d.ts.map +1 -0
  99. package/dist/non-gtin-numeric-identifier-validator.js +17 -0
  100. package/dist/non-gtin-numeric-identifier-validator.js.map +1 -0
  101. package/dist/non-numeric-identifier-creator.d.ts +45 -0
  102. package/dist/non-numeric-identifier-creator.d.ts.map +1 -0
  103. package/dist/non-numeric-identifier-creator.js +84 -0
  104. package/dist/non-numeric-identifier-creator.js.map +1 -0
  105. package/dist/non-numeric-identifier-descriptor.d.ts +26 -0
  106. package/dist/non-numeric-identifier-descriptor.d.ts.map +1 -0
  107. package/dist/non-numeric-identifier-descriptor.js +2 -0
  108. package/dist/non-numeric-identifier-descriptor.js.map +1 -0
  109. package/dist/non-numeric-identifier-type.d.ts +7 -0
  110. package/dist/non-numeric-identifier-type.d.ts.map +1 -0
  111. package/dist/non-numeric-identifier-type.js +2 -0
  112. package/dist/non-numeric-identifier-type.js.map +1 -0
  113. package/dist/non-numeric-identifier-validator.d.ts +49 -0
  114. package/dist/non-numeric-identifier-validator.d.ts.map +1 -0
  115. package/dist/non-numeric-identifier-validator.js +69 -0
  116. package/dist/non-numeric-identifier-validator.js.map +1 -0
  117. package/dist/numeric-identifier-creator.d.ts +53 -0
  118. package/dist/numeric-identifier-creator.d.ts.map +1 -0
  119. package/dist/numeric-identifier-creator.js +2 -0
  120. package/dist/numeric-identifier-creator.js.map +1 -0
  121. package/dist/numeric-identifier-descriptor.d.ts +21 -0
  122. package/dist/numeric-identifier-descriptor.d.ts.map +1 -0
  123. package/dist/numeric-identifier-descriptor.js +2 -0
  124. package/dist/numeric-identifier-descriptor.js.map +1 -0
  125. package/dist/numeric-identifier-type.d.ts +31 -0
  126. package/dist/numeric-identifier-type.d.ts.map +1 -0
  127. package/dist/numeric-identifier-type.js +18 -0
  128. package/dist/numeric-identifier-type.js.map +1 -0
  129. package/dist/numeric-identifier-validator.d.ts +24 -0
  130. package/dist/numeric-identifier-validator.d.ts.map +1 -0
  131. package/dist/numeric-identifier-validator.js +2 -0
  132. package/dist/numeric-identifier-validator.js.map +1 -0
  133. package/dist/prefix-manager.d.ts +232 -0
  134. package/dist/prefix-manager.d.ts.map +1 -0
  135. package/dist/prefix-manager.js +334 -0
  136. package/dist/prefix-manager.js.map +1 -0
  137. package/dist/prefix-provider.d.ts +27 -0
  138. package/dist/prefix-provider.d.ts.map +1 -0
  139. package/dist/prefix-provider.js +2 -0
  140. package/dist/prefix-provider.js.map +1 -0
  141. package/dist/prefix-type.d.ts +26 -0
  142. package/dist/prefix-type.d.ts.map +1 -0
  143. package/dist/prefix-type.js +18 -0
  144. package/dist/prefix-type.js.map +1 -0
  145. package/dist/prefix-validator.d.ts +70 -0
  146. package/dist/prefix-validator.d.ts.map +1 -0
  147. package/dist/prefix-validator.js +208 -0
  148. package/dist/prefix-validator.js.map +1 -0
  149. package/dist/serializable-numeric-identifier-creator.d.ts +76 -0
  150. package/dist/serializable-numeric-identifier-creator.d.ts.map +1 -0
  151. package/dist/serializable-numeric-identifier-creator.js +107 -0
  152. package/dist/serializable-numeric-identifier-creator.js.map +1 -0
  153. package/dist/serializable-numeric-identifier-descriptor.d.ts +26 -0
  154. package/dist/serializable-numeric-identifier-descriptor.d.ts.map +1 -0
  155. package/dist/serializable-numeric-identifier-descriptor.js +2 -0
  156. package/dist/serializable-numeric-identifier-descriptor.js.map +1 -0
  157. package/dist/serializable-numeric-identifier-type.d.ts +6 -0
  158. package/dist/serializable-numeric-identifier-type.d.ts.map +1 -0
  159. package/dist/serializable-numeric-identifier-type.js +2 -0
  160. package/dist/serializable-numeric-identifier-type.js.map +1 -0
  161. package/dist/serializable-numeric-identifier-validator.d.ts +56 -0
  162. package/dist/serializable-numeric-identifier-validator.d.ts.map +1 -0
  163. package/dist/serializable-numeric-identifier-validator.js +77 -0
  164. package/dist/serializable-numeric-identifier-validator.js.map +1 -0
  165. package/dist/validators.d.ts +96 -0
  166. package/dist/validators.d.ts.map +1 -0
  167. package/dist/validators.js +140 -0
  168. package/dist/validators.js.map +1 -0
  169. package/package.json +12 -8
  170. package/src/abstract-identifier-creator.ts +97 -0
  171. package/src/abstract-identifier-validator.ts +140 -0
  172. package/src/abstract-numeric-identifier-creator.ts +168 -0
  173. package/src/abstract-numeric-identifier-validator.ts +69 -0
  174. package/src/character-set.ts +10 -10
  175. package/src/check.ts +9 -9
  176. package/src/content-character-set.ts +29 -0
  177. package/src/creators.ts +113 -0
  178. package/src/descriptors.ts +332 -0
  179. package/src/gtin-creator.ts +10 -9
  180. package/src/gtin-descriptor.ts +18 -0
  181. package/src/gtin-type.ts +50 -0
  182. package/src/gtin-validator.ts +40 -64
  183. package/src/identifier-creator.ts +12 -76
  184. package/src/identifier-descriptor.ts +30 -0
  185. package/src/identifier-type.ts +6 -1
  186. package/src/identifier-validator.ts +12 -188
  187. package/src/index.ts +44 -19
  188. package/src/locale/en/locale-resources.ts +0 -1
  189. package/src/locale/fr/locale-resources.ts +0 -1
  190. package/src/locale/i18n.ts +3 -3
  191. package/src/locale/i18next.d.ts +1 -1
  192. package/src/non-gtin-numeric-identifier-creator.ts +7 -13
  193. package/src/non-gtin-numeric-identifier-descriptor.ts +24 -0
  194. package/src/non-gtin-numeric-identifier-type.ts +7 -0
  195. package/src/non-gtin-numeric-identifier-validator.ts +8 -42
  196. package/src/non-numeric-identifier-creator.ts +9 -19
  197. package/src/non-numeric-identifier-descriptor.ts +29 -0
  198. package/src/non-numeric-identifier-type.ts +7 -0
  199. package/src/non-numeric-identifier-validator.ts +17 -55
  200. package/src/numeric-identifier-creator.ts +20 -163
  201. package/src/numeric-identifier-descriptor.ts +23 -0
  202. package/src/numeric-identifier-type.ts +44 -0
  203. package/src/numeric-identifier-validator.ts +13 -116
  204. package/src/prefix-manager.ts +94 -152
  205. package/src/prefix-provider.ts +3 -3
  206. package/src/prefix-type.ts +6 -1
  207. package/src/prefix-validator.ts +143 -81
  208. package/src/serializable-numeric-identifier-creator.ts +6 -16
  209. package/src/serializable-numeric-identifier-descriptor.ts +29 -0
  210. package/src/serializable-numeric-identifier-type.ts +9 -0
  211. package/src/serializable-numeric-identifier-validator.ts +18 -46
  212. package/src/validators.ts +203 -0
  213. package/test/check.test.ts +1 -1
  214. package/test/creator.test.ts +5 -7
  215. package/test/gtin-creator.ts +6 -2
  216. package/test/gtin-validator.test.ts +7 -10
  217. package/test/identifier-creator.ts +6 -5
  218. package/test/identifier-validator.ts +2 -2
  219. package/test/non-gtin-numeric-identifier-creator.ts +9 -93
  220. package/test/non-gtin-numeric-identifier-validator.ts +3 -3
  221. package/test/non-numeric-identifier-creator.ts +93 -0
  222. package/test/non-numeric-identifier-validator.ts +3 -3
  223. package/test/numeric-identifier-creator.ts +11 -5
  224. package/test/numeric-identifier-validator.ts +5 -9
  225. package/test/prefix-manager.test.ts +2 -2
  226. package/test/serializable-numeric-identifier-creator.ts +11 -3
  227. package/test/serializable-numeric-identifier-validator.ts +3 -3
  228. package/test/setup.ts +1 -1
  229. package/test/sparse.test.ts +1 -1
  230. package/test/utility.ts +1 -1
  231. package/test/validator.test.ts +64 -40
  232. package/test/variable-measure-rcn.test.ts +1 -1
  233. package/tsconfig-config.json +4 -0
  234. package/tsconfig-src.json +8 -0
  235. package/tsconfig-test.json +9 -0
  236. package/tsconfig.json +12 -1
  237. package/tsup.config.ts +3 -2
  238. package/dist/index.cjs +0 -4624
  239. package/dist/index.cjs.map +0 -1
  240. package/dist/index.d.cts +0 -1588
@@ -1,57 +1,39 @@
1
- import { GTINCreator } from "./gtin-creator";
2
- import { type GTINType, GTINTypes } from "./gtin-validator";
3
- import type { IdentifierCreator } from "./identifier-creator";
4
- import { type IdentifierType, IdentifierTypes } from "./identifier-type";
5
- import { i18nextGS1 } from "./locale/i18n";
6
- import { NonGTINNumericIdentifierCreator } from "./non-gtin-numeric-identifier-creator";
7
- import {
8
- GLN_VALIDATOR,
9
- GSIN_VALIDATOR,
10
- GSRN_VALIDATOR,
11
- type NonGTINNumericIdentifierValidator,
12
- SSCC_VALIDATOR
13
- } from "./non-gtin-numeric-identifier-validator";
14
- import { NonNumericIdentifierCreator } from "./non-numeric-identifier-creator";
15
- import {
16
- CPID_VALIDATOR,
17
- GIAI_VALIDATOR,
18
- GINC_VALIDATOR,
19
- GMN_VALIDATOR,
20
- type NonNumericIdentifierValidator
21
- } from "./non-numeric-identifier-validator";
22
- import type { AbstractNumericIdentifierCreator } from "./numeric-identifier-creator";
23
- import type { NumericIdentifierType } from "./numeric-identifier-validator";
24
- import type { PrefixProvider } from "./prefix-provider";
25
- import { type PrefixType, PrefixTypes } from "./prefix-type";
26
- import { PrefixValidator } from "./prefix-validator";
27
- import { SerializableNumericIdentifierCreator } from "./serializable-numeric-identifier-creator";
28
- import {
29
- GCN_VALIDATOR,
30
- GDTI_VALIDATOR,
31
- GRAI_VALIDATOR,
32
- type SerializableNumericIdentifierValidator
33
- } from "./serializable-numeric-identifier-validator";
1
+ import { type IdentifierCreatorsRecord, isNumericIdentifierCreator } from "./creators.js";
2
+ import { GTINCreator } from "./gtin-creator.js";
3
+ import { GTIN_BASE_TYPES } from "./gtin-type.js";
4
+ import type { IdentifierCreator } from "./identifier-creator.js";
5
+ import { type IdentifierType, IdentifierTypes } from "./identifier-type.js";
6
+ import { i18nextGS1 } from "./locale/i18n.js";
7
+ import { NonGTINNumericIdentifierCreator } from "./non-gtin-numeric-identifier-creator.js";
8
+ import type { NonGTINNumericIdentifierType } from "./non-gtin-numeric-identifier-type.js";
9
+ import { NonNumericIdentifierCreator } from "./non-numeric-identifier-creator.js";
10
+ import type { NonNumericIdentifierType } from "./non-numeric-identifier-type.js";
11
+ import type { NumericIdentifierType } from "./numeric-identifier-type.js";
12
+ import type { PrefixProvider } from "./prefix-provider.js";
13
+ import { type PrefixType, PrefixTypes } from "./prefix-type.js";
14
+ import { PrefixValidator } from "./prefix-validator.js";
15
+ import { SerializableNumericIdentifierCreator } from "./serializable-numeric-identifier-creator.js";
16
+ import type { SerializableNumericIdentifierType } from "./serializable-numeric-identifier-type.js";
34
17
 
35
18
  /**
36
19
  * Prefix manager. This is the core class for identifier creation.
37
20
  *
38
- * A prefix manager may be created for any {@link PrefixType | prefix type}. As most applications work with a limited
39
- * number of prefixes for creating identifiers, prefix managers are cached in memory and may be reused.
21
+ * A prefix manager may be constructed for any {@link PrefixType | prefix type}. Construction may be done directly or
22
+ * via the static {@linkcode PrefixManager.get | get()} method, which allows for caching and reuse. As most applications
23
+ * work with a limited number of prefixes for creating identifiers, caching and reuse may be a more efficient option.
40
24
  *
41
- * Prefix managers are keyed by GS1 Company Prefix, so the prefix type that is requested may not match the prefix type
42
- * of the returned prefix manager. For example, the prefix manager for GS1 Company Prefix 0614141 is identical to the
43
- * one for U.P.C. Company Prefix 614141, with the prefix type equal to {@link PrefixTypes.UPCCompanyPrefix} and the
44
- * prefix equal to "614141".
25
+ * The prefix type and prefix are normalized before the prefix manager is constructed, so they may not match the input
26
+ * values. For example, the GS1 Company Prefix 0614141 is equivalent to U.P.C. Company Prefix 614141; both result in a
27
+ * prefix manager with prefix type equal to {@linkcode PrefixTypes.UPCCompanyPrefix} and prefix equal to "614141".
45
28
  *
46
- * To support the creation of sparse identifiers, a prefix manager maintains a {@link tweakFactor | tweak
47
- * factor} which is used, along with a type-specific multiplier, as the tweak when creating numeric identifiers.
48
- * The default tweak factor is the numeric value of the GS1 Company Prefix representation of the prefix preceded by '1'
49
- * to ensure uniqueness (i.e., so that prefixes 0 N1 N2 N3... and N1 N2 N3... produce different tweak factors). This is
50
- * usually sufficient for obfuscation, but as the sparse creation algorithm is reversible and as the GS1 Company Prefix
51
- * is discoverable via {@link https://www.gs1.org/services/verified-by-gs1 | Verified by GS1}, a user-defined tweak
52
- * factor should be used if a higher degree of obfuscation is required. When using a tweak factor other than the
53
- * default, care should be taken to restore it when resuming the application. A tweak factor of 0 creates a straight
54
- * sequence.
29
+ * To support the creation of sparse identifiers, a prefix manager maintains a {@link tweakFactor | tweak factor} which
30
+ * is used, along with a type-specific multiplier, as the tweak when creating numeric identifiers. The default tweak
31
+ * factor is the numeric value of the GS1 Company Prefix representation of the prefix preceded by '1' to ensure
32
+ * uniqueness (i.e., so that prefixes 0 N1 N2 N3... and N1 N2 N3... produce different tweak factors). This is usually
33
+ * sufficient for obfuscation, but as the sparse creation algorithm is reversible and as the GS1 Company Prefix is
34
+ * discoverable via {@link https://www.gs1.org/services/verified-by-gs1 | Verified by GS1}, a user-defined tweak factor
35
+ * should be used if a higher degree of obfuscation is required. When using a tweak factor other than the default, care
36
+ * should be taken to restore it when resuming the application. A tweak factor of 0 creates a straight sequence.
55
37
  */
56
38
  export class PrefixManager implements PrefixProvider {
57
39
  /**
@@ -90,12 +72,12 @@ export class PrefixManager implements PrefixProvider {
90
72
  private readonly _gs1CompanyPrefix: string;
91
73
 
92
74
  /**
93
- * U.P.C. Company Prefix if prefix type is {@link PrefixTypes.UPCCompanyPrefix}.
75
+ * U.P.C. Company Prefix if prefix type is {@linkcode PrefixTypes.UPCCompanyPrefix}.
94
76
  */
95
77
  private readonly _upcCompanyPrefix: string | undefined;
96
78
 
97
79
  /**
98
- * GS1-8 Prefix if prefix type is {@link PrefixTypes.GS18Prefix}.
80
+ * GS1-8 Prefix if prefix type is {@linkcode PrefixTypes.GS18Prefix}.
99
81
  */
100
82
  private readonly _gs18Prefix: string | undefined;
101
83
 
@@ -112,30 +94,25 @@ export class PrefixManager implements PrefixProvider {
112
94
  /**
113
95
  * Cached identifier creators.
114
96
  */
115
- private readonly _identifierCreators: Partial<Record<IdentifierType, IdentifierCreator>> = {};
97
+ private readonly _identifierCreators: Partial<IdentifierCreatorsRecord> = {};
116
98
 
117
99
  /**
118
100
  * Constructor.
119
101
  *
120
- * @param gs1CompanyPrefix
121
- * GS1 Company Prefix.
122
- */
123
- private constructor(gs1CompanyPrefix: string) {
124
- this._gs1CompanyPrefix = gs1CompanyPrefix;
125
-
126
- // Determine the prefix type and populate the remaining fields.
127
- if (!gs1CompanyPrefix.startsWith("0")) {
128
- this._prefixType = PrefixTypes.GS1CompanyPrefix;
129
- this._prefix = this._gs1CompanyPrefix;
130
- } else if (!gs1CompanyPrefix.startsWith("00000")) {
131
- this._prefixType = PrefixTypes.UPCCompanyPrefix;
132
- this._upcCompanyPrefix = gs1CompanyPrefix.substring(1);
133
- this._prefix = this._upcCompanyPrefix;
134
- } else {
135
- this._prefixType = PrefixTypes.GS18Prefix;
136
- this._gs18Prefix = gs1CompanyPrefix.substring(5);
137
- this._prefix = this._gs18Prefix;
138
- }
102
+ * @param prefixType
103
+ * Prefix type.
104
+ *
105
+ * @param prefix
106
+ * Prefix.
107
+ */
108
+ constructor(prefixType: PrefixType, prefix: string) {
109
+ const normalizedPrefixProvider = PrefixValidator.normalize(prefixType, prefix);
110
+
111
+ this._prefixType = normalizedPrefixProvider.prefixType;
112
+ this._prefix = normalizedPrefixProvider.prefix;
113
+ this._gs1CompanyPrefix = normalizedPrefixProvider.gs1CompanyPrefix;
114
+ this._upcCompanyPrefix = normalizedPrefixProvider.upcCompanyPrefix;
115
+ this._gs18Prefix = normalizedPrefixProvider.gs18Prefix;
139
116
 
140
117
  // Default tweak factor is the numeric value of the GS1 Company Prefix preceded by '1'.
141
118
  this._defaultTweakFactor = BigInt(`1${this.gs1CompanyPrefix}`);
@@ -165,14 +142,14 @@ export class PrefixManager implements PrefixProvider {
165
142
  }
166
143
 
167
144
  /**
168
- * Get the U.P.C. Company Prefix if prefix type is {@link PrefixTypes.UPCCompanyPrefix} or undefined if not.
145
+ * Get the U.P.C. Company Prefix if prefix type is {@linkcode PrefixTypes.UPCCompanyPrefix} or undefined if not.
169
146
  */
170
147
  get upcCompanyPrefix(): string | undefined {
171
148
  return this._upcCompanyPrefix;
172
149
  }
173
150
 
174
151
  /**
175
- * Get the GS1-8 Prefix if prefix type is {@link PrefixTypes.GS18Prefix} or undefined if not.
152
+ * Get the GS1-8 Prefix if prefix type is {@linkcode PrefixTypes.GS18Prefix} or undefined if not.
176
153
  */
177
154
  get gs18Prefix(): string | undefined {
178
155
  return this._gs18Prefix;
@@ -181,16 +158,13 @@ export class PrefixManager implements PrefixProvider {
181
158
  /**
182
159
  * Set the tweak for an identifier creator if it's a numeric identifier creator.
183
160
  *
184
- * @param creator
161
+ * @param identifierCreator
185
162
  * Identifier creator.
186
163
  */
187
- private setCreatorTweak(creator: IdentifierCreator): void {
188
- if (creator.identifierType in PrefixManager.CREATOR_TWEAK_FACTORS) {
189
- // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- Type is known by testing for presence in record.
190
- const creatorTweakFactor = PrefixManager.CREATOR_TWEAK_FACTORS[creator.identifierType as NumericIdentifierType];
191
-
192
- // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion,no-param-reassign -- Type is known by testing identifier type. Method purpose is to set the tweak.
193
- (creator as AbstractNumericIdentifierCreator).tweak = this.tweakFactor * creatorTweakFactor;
164
+ private setCreatorTweak(identifierCreator: IdentifierCreator): void {
165
+ if (isNumericIdentifierCreator(identifierCreator)) {
166
+ // eslint-disable-next-line no-param-reassign -- Method purpose is to set the tweak.
167
+ identifierCreator.tweak = this.tweakFactor * PrefixManager.CREATOR_TWEAK_FACTORS[identifierCreator.identifierType];
194
168
  }
195
169
  }
196
170
 
@@ -239,33 +213,14 @@ export class PrefixManager implements PrefixProvider {
239
213
  * Prefix manager with normalized prefix type and prefix.
240
214
  */
241
215
  static get(prefixType: PrefixType, prefix: string): PrefixManager {
242
- PrefixValidator.validate(prefixType, true, true, prefix);
243
-
244
- let gs1CompanyPrefix: string;
216
+ // Normalization will occur in constructor as well, but it's necessary here for the map.
217
+ const normalizedPrefixProvider = PrefixValidator.normalize(prefixType, prefix);
245
218
 
246
- switch (prefixType) {
247
- case PrefixTypes.GS1CompanyPrefix:
248
- gs1CompanyPrefix = prefix;
249
- break;
250
-
251
- case PrefixTypes.UPCCompanyPrefix:
252
- gs1CompanyPrefix = "0" + prefix;
253
- break;
254
-
255
- case PrefixTypes.GS18Prefix:
256
- gs1CompanyPrefix = "00000" + prefix;
257
- break;
258
-
259
- // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- Method may be called by unsafe means.
260
- default:
261
- throw new RangeError(i18nextGS1.t("Prefix.invalidPrefixType"));
262
- }
263
-
264
- let prefixManager = PrefixManager.PREFIX_MANAGERS_MAP.get(gs1CompanyPrefix);
219
+ let prefixManager = PrefixManager.PREFIX_MANAGERS_MAP.get(normalizedPrefixProvider.gs1CompanyPrefix);
265
220
 
266
221
  if (prefixManager === undefined) {
267
- prefixManager = new PrefixManager(gs1CompanyPrefix);
268
- PrefixManager.PREFIX_MANAGERS_MAP.set(gs1CompanyPrefix, prefixManager);
222
+ prefixManager = new PrefixManager(normalizedPrefixProvider.prefixType, normalizedPrefixProvider.prefix);
223
+ PrefixManager.PREFIX_MANAGERS_MAP.set(normalizedPrefixProvider.gs1CompanyPrefix, prefixManager);
269
224
  }
270
225
 
271
226
  return prefixManager;
@@ -274,18 +229,23 @@ export class PrefixManager implements PrefixProvider {
274
229
  /**
275
230
  * Get an identifier creator.
276
231
  *
232
+ * @template TIdentifierType
233
+ * Identifier type type.
234
+ *
277
235
  * @param identifierType
278
- * Identifier type.
236
+ * Identifier type used to construct identifier creator.
279
237
  *
280
- * @param constructorCallback
238
+ * @param constructorParameter
239
+ * Second constructor parameter passed to constructor callback alongside this.
240
+ *
241
+ * @param ConstructorCallback
281
242
  * Constructor callback.
282
243
  *
283
244
  * @returns
284
245
  * Identifier creator.
285
246
  */
286
- private getIdentifierCreator<TIdentifierCreator extends IdentifierCreator>(identifierType: IdentifierType, constructorCallback: () => TIdentifierCreator): TIdentifierCreator {
287
- // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- Type is paired with constructor callback.
288
- let creator = this._identifierCreators[identifierType] as TIdentifierCreator | undefined;
247
+ private getIdentifierCreator<TIdentifierType extends IdentifierType, TConstructorParameter>(identifierType: TIdentifierType, constructorParameter: TConstructorParameter, ConstructorCallback: new (prefixProvider: PrefixProvider, constructorParameter: TConstructorParameter) => IdentifierCreatorsRecord[TIdentifierType]): IdentifierCreatorsRecord[TIdentifierType] {
248
+ let creator: IdentifierCreatorsRecord[TIdentifierType] | undefined = this._identifierCreators[identifierType];
289
249
 
290
250
  if (creator === undefined) {
291
251
  if (this.prefixType === PrefixTypes.GS18Prefix && identifierType !== IdentifierTypes.GTIN) {
@@ -294,7 +254,7 @@ export class PrefixManager implements PrefixProvider {
294
254
  }));
295
255
  }
296
256
 
297
- creator = constructorCallback();
257
+ creator = new ConstructorCallback(this, constructorParameter);
298
258
 
299
259
  this.setCreatorTweak(creator);
300
260
 
@@ -307,141 +267,123 @@ export class PrefixManager implements PrefixProvider {
307
267
  /**
308
268
  * Get non-GTIN numeric identifier creator.
309
269
  *
310
- * @param validator
311
- * Validator on which identifier creator is based.
270
+ * @param identifierType
271
+ * Identifier type used to construct identifier creator.
312
272
  *
313
273
  * @returns
314
274
  * Identifier creator.
315
275
  */
316
- private getNonGTINNumericIdentifierCreator(validator: NonGTINNumericIdentifierValidator): NonGTINNumericIdentifierCreator {
317
- return this.getIdentifierCreator(validator.identifierType, () => new NonGTINNumericIdentifierCreator(this, validator.identifierType, validator.length, validator.leaderType));
276
+ private getNonGTINNumericIdentifierCreator(identifierType: Exclude<NonGTINNumericIdentifierType, SerializableNumericIdentifierType>): NonGTINNumericIdentifierCreator {
277
+ return this.getIdentifierCreator(identifierType, identifierType, NonGTINNumericIdentifierCreator);
318
278
  }
319
279
 
320
280
  /**
321
281
  * Get serialized numeric identifier creator.
322
282
  *
323
- * @param validator
324
- * Validator on which identifier creator is based.
283
+ * @param identifierType
284
+ * Identifier type used to construct identifier creator.
325
285
  *
326
286
  * @returns
327
287
  * Identifier creator.
328
288
  */
329
- private getSerializableNumericIdentifierCreator(validator: SerializableNumericIdentifierValidator): SerializableNumericIdentifierCreator {
330
- return this.getIdentifierCreator(validator.identifierType, () => new SerializableNumericIdentifierCreator(this, validator.identifierType, validator.length, validator.serialComponentLength, validator.serialComponentCharacterSet));
289
+ private getSerializableNumericIdentifierCreator(identifierType: SerializableNumericIdentifierType): SerializableNumericIdentifierCreator {
290
+ return this.getIdentifierCreator(identifierType, identifierType, SerializableNumericIdentifierCreator);
331
291
  }
332
292
 
333
293
  /**
334
294
  * Get non-numeric identifier creator.
335
295
  *
336
- * @param validator
337
- * Validator on which identifier creator is based.
296
+ * @param identifierType
297
+ * Identifier type used to construct identifier creator.
338
298
  *
339
299
  * @returns
340
300
  * Identifier creator.
341
301
  */
342
- private getNonNumericIdentifierCreator(validator: NonNumericIdentifierValidator): NonNumericIdentifierCreator {
343
- return this.getIdentifierCreator(validator.identifierType, () => new NonNumericIdentifierCreator(this, validator.identifierType, validator.length, validator.referenceCharacterSet, validator.requiresCheckCharacterPair));
302
+ private getNonNumericIdentifierCreator(identifierType: NonNumericIdentifierType): NonNumericIdentifierCreator {
303
+ return this.getIdentifierCreator(identifierType, identifierType, NonNumericIdentifierCreator);
344
304
  }
345
305
 
346
306
  /**
347
307
  * Get GTIN creator.
348
308
  */
349
309
  get gtinCreator(): GTINCreator {
350
- return this.getIdentifierCreator(IdentifierTypes.GTIN, () => {
351
- let gtinType: GTINType;
352
-
353
- switch (this.prefixType) {
354
- case PrefixTypes.GS1CompanyPrefix:
355
- gtinType = GTINTypes.GTIN13;
356
- break;
357
-
358
- case PrefixTypes.UPCCompanyPrefix:
359
- gtinType = GTINTypes.GTIN12;
360
- break;
361
-
362
- case PrefixTypes.GS18Prefix:
363
- gtinType = GTINTypes.GTIN8;
364
- break;
365
- }
366
-
367
- return new GTINCreator(this, gtinType);
368
- });
310
+ return this.getIdentifierCreator(IdentifierTypes.GTIN, GTIN_BASE_TYPES[this.prefixType], GTINCreator);
369
311
  }
370
312
 
371
313
  /**
372
314
  * Get GLN creator.
373
315
  */
374
316
  get glnCreator(): NonGTINNumericIdentifierCreator {
375
- return this.getNonGTINNumericIdentifierCreator(GLN_VALIDATOR);
317
+ return this.getNonGTINNumericIdentifierCreator(IdentifierTypes.GLN);
376
318
  }
377
319
 
378
320
  /**
379
321
  * Get SSCC creator.
380
322
  */
381
323
  get ssccCreator(): NonGTINNumericIdentifierCreator {
382
- return this.getNonGTINNumericIdentifierCreator(SSCC_VALIDATOR);
324
+ return this.getNonGTINNumericIdentifierCreator(IdentifierTypes.SSCC);
383
325
  }
384
326
 
385
327
  /**
386
328
  * Get GRAI creator.
387
329
  */
388
330
  get graiCreator(): SerializableNumericIdentifierCreator {
389
- return this.getSerializableNumericIdentifierCreator(GRAI_VALIDATOR);
331
+ return this.getSerializableNumericIdentifierCreator(IdentifierTypes.GRAI);
390
332
  }
391
333
 
392
334
  /**
393
335
  * Get GIAI creator.
394
336
  */
395
337
  get giaiCreator(): NonNumericIdentifierCreator {
396
- return this.getNonNumericIdentifierCreator(GIAI_VALIDATOR);
338
+ return this.getNonNumericIdentifierCreator(IdentifierTypes.GIAI);
397
339
  }
398
340
 
399
341
  /**
400
342
  * Get GSRN creator.
401
343
  */
402
344
  get gsrnCreator(): NonGTINNumericIdentifierCreator {
403
- return this.getNonGTINNumericIdentifierCreator(GSRN_VALIDATOR);
345
+ return this.getNonGTINNumericIdentifierCreator(IdentifierTypes.GSRN);
404
346
  }
405
347
 
406
348
  /**
407
349
  * Get GDTI creator.
408
350
  */
409
351
  get gdtiCreator(): SerializableNumericIdentifierCreator {
410
- return this.getSerializableNumericIdentifierCreator(GDTI_VALIDATOR);
352
+ return this.getSerializableNumericIdentifierCreator(IdentifierTypes.GDTI);
411
353
  }
412
354
 
413
355
  /**
414
356
  * Get GINC creator.
415
357
  */
416
358
  get gincCreator(): NonNumericIdentifierCreator {
417
- return this.getNonNumericIdentifierCreator(GINC_VALIDATOR);
359
+ return this.getNonNumericIdentifierCreator(IdentifierTypes.GINC);
418
360
  }
419
361
 
420
362
  /**
421
363
  * Get GSIN creator.
422
364
  */
423
365
  get gsinCreator(): NonGTINNumericIdentifierCreator {
424
- return this.getNonGTINNumericIdentifierCreator(GSIN_VALIDATOR);
366
+ return this.getNonGTINNumericIdentifierCreator(IdentifierTypes.GSIN);
425
367
  }
426
368
 
427
369
  /**
428
370
  * Get GCN creator.
429
371
  */
430
372
  get gcnCreator(): SerializableNumericIdentifierCreator {
431
- return this.getSerializableNumericIdentifierCreator(GCN_VALIDATOR);
373
+ return this.getSerializableNumericIdentifierCreator(IdentifierTypes.GCN);
432
374
  }
433
375
 
434
376
  /**
435
377
  * Get CPID creator.
436
378
  */
437
379
  get cpidCreator(): NonNumericIdentifierCreator {
438
- return this.getNonNumericIdentifierCreator(CPID_VALIDATOR);
380
+ return this.getNonNumericIdentifierCreator(IdentifierTypes.CPID);
439
381
  }
440
382
 
441
383
  /**
442
384
  * Get GMN creator.
443
385
  */
444
386
  get gmnCreator(): NonNumericIdentifierCreator {
445
- return this.getNonNumericIdentifierCreator(GMN_VALIDATOR);
387
+ return this.getNonNumericIdentifierCreator(IdentifierTypes.GMN);
446
388
  }
447
389
  }
@@ -1,4 +1,4 @@
1
- import type { PrefixType } from "./prefix-type";
1
+ import type { PrefixType } from "./prefix-type.js";
2
2
 
3
3
  /**
4
4
  * Prefix provider.
@@ -20,12 +20,12 @@ export interface PrefixProvider {
20
20
  readonly gs1CompanyPrefix: string;
21
21
 
22
22
  /**
23
- * Prefix as U.P.C. Company Prefix if prefix type is {@link PrefixTypes.UPCCompanyPrefix} or undefined if not.
23
+ * Prefix as U.P.C. Company Prefix if prefix type is {@linkcode PrefixTypes.UPCCompanyPrefix} or undefined if not.
24
24
  */
25
25
  readonly upcCompanyPrefix: string | undefined;
26
26
 
27
27
  /**
28
- * Prefix as GS1-8 Prefix if prefix type is {@link PrefixTypes.GS18Prefix} or undefined if not.
28
+ * Prefix as GS1-8 Prefix if prefix type is {@linkcode PrefixTypes.GS18Prefix} or undefined if not.
29
29
  */
30
30
  readonly gs18Prefix: string | undefined;
31
31
  }
@@ -18,7 +18,12 @@ export const PrefixTypes = {
18
18
  GS18Prefix: "GS1-8 Prefix"
19
19
  } as const;
20
20
 
21
+ /**
22
+ * Prefix type key.
23
+ */
24
+ export type PrefixTypeKey = keyof typeof PrefixTypes;
25
+
21
26
  /**
22
27
  * Prefix type.
23
28
  */
24
- export type PrefixType = typeof PrefixTypes[keyof typeof PrefixTypes];
29
+ export type PrefixType = typeof PrefixTypes[PrefixTypeKey];