@aidc-toolkit/gs1 1.0.26-beta → 1.0.28-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 (205) hide show
  1. package/dist/abstract-identifier-creator.d.ts +32 -66
  2. package/dist/abstract-identifier-creator.d.ts.map +1 -1
  3. package/dist/abstract-identifier-creator.js +62 -45
  4. package/dist/abstract-identifier-creator.js.map +1 -1
  5. package/dist/abstract-non-gtin-numeric-identifier-creator.d.ts +36 -0
  6. package/dist/abstract-non-gtin-numeric-identifier-creator.d.ts.map +1 -0
  7. package/dist/abstract-non-gtin-numeric-identifier-creator.js +37 -0
  8. package/dist/abstract-non-gtin-numeric-identifier-creator.js.map +1 -0
  9. package/dist/abstract-numeric-identifier-creator.d.ts +28 -86
  10. package/dist/abstract-numeric-identifier-creator.d.ts.map +1 -1
  11. package/dist/abstract-numeric-identifier-creator.js +138 -121
  12. package/dist/abstract-numeric-identifier-creator.js.map +1 -1
  13. package/dist/content-character-set-creators.d.ts +4 -0
  14. package/dist/content-character-set-creators.d.ts.map +1 -0
  15. package/dist/content-character-set-creators.js +9 -0
  16. package/dist/content-character-set-creators.js.map +1 -0
  17. package/dist/gtin-creator.d.ts +8 -13
  18. package/dist/gtin-creator.d.ts.map +1 -1
  19. package/dist/gtin-creator.js +12 -15
  20. package/dist/gtin-creator.js.map +1 -1
  21. package/dist/gtin-descriptor.d.ts +8 -3
  22. package/dist/gtin-descriptor.d.ts.map +1 -1
  23. package/dist/gtin-length.d.ts +64 -0
  24. package/dist/gtin-length.d.ts.map +1 -0
  25. package/dist/gtin-length.js +36 -0
  26. package/dist/gtin-length.js.map +1 -0
  27. package/dist/gtin-type.d.ts +15 -28
  28. package/dist/gtin-type.d.ts.map +1 -1
  29. package/dist/gtin-type.js +4 -28
  30. package/dist/gtin-type.js.map +1 -1
  31. package/dist/gtin-validator.d.ts +9 -23
  32. package/dist/gtin-validator.d.ts.map +1 -1
  33. package/dist/gtin-validator.js +32 -39
  34. package/dist/gtin-validator.js.map +1 -1
  35. package/dist/identifier-creator.d.ts +4 -4
  36. package/dist/identifier-creator.d.ts.map +1 -1
  37. package/dist/{creators.d.ts → identifier-creators.d.ts} +47 -10
  38. package/dist/identifier-creators.d.ts.map +1 -0
  39. package/dist/identifier-creators.js +92 -0
  40. package/dist/identifier-creators.js.map +1 -0
  41. package/dist/{descriptors.d.ts → identifier-descriptors.d.ts} +20 -10
  42. package/dist/identifier-descriptors.d.ts.map +1 -0
  43. package/dist/{descriptors.js → identifier-descriptors.js} +27 -31
  44. package/dist/identifier-descriptors.js.map +1 -0
  45. package/dist/identifier-extension.d.ts +82 -0
  46. package/dist/identifier-extension.d.ts.map +1 -0
  47. package/dist/identifier-extension.js +79 -0
  48. package/dist/identifier-extension.js.map +1 -0
  49. package/dist/identifier-validator.d.ts +44 -9
  50. package/dist/identifier-validator.d.ts.map +1 -1
  51. package/dist/identifier-validator.js +109 -1
  52. package/dist/identifier-validator.js.map +1 -1
  53. package/dist/{validators.d.ts → identifier-validators.d.ts} +47 -12
  54. package/dist/identifier-validators.d.ts.map +1 -0
  55. package/dist/{validators.js → identifier-validators.js} +40 -13
  56. package/dist/identifier-validators.js.map +1 -0
  57. package/dist/index.d.ts +15 -9
  58. package/dist/index.d.ts.map +1 -1
  59. package/dist/index.js +13 -4
  60. package/dist/index.js.map +1 -1
  61. package/dist/leader-type.d.ts +26 -0
  62. package/dist/leader-type.d.ts.map +1 -0
  63. package/dist/leader-type.js +18 -0
  64. package/dist/leader-type.js.map +1 -0
  65. package/dist/non-gtin-numeric-identifier-creator.d.ts +7 -19
  66. package/dist/non-gtin-numeric-identifier-creator.d.ts.map +1 -1
  67. package/dist/non-gtin-numeric-identifier-creator.js +1 -22
  68. package/dist/non-gtin-numeric-identifier-creator.js.map +1 -1
  69. package/dist/non-gtin-numeric-identifier-descriptor.d.ts +1 -1
  70. package/dist/non-gtin-numeric-identifier-descriptor.d.ts.map +1 -1
  71. package/dist/non-gtin-numeric-identifier-type.d.ts +22 -3
  72. package/dist/non-gtin-numeric-identifier-type.d.ts.map +1 -1
  73. package/dist/non-gtin-numeric-identifier-type.js +7 -1
  74. package/dist/non-gtin-numeric-identifier-type.js.map +1 -1
  75. package/dist/non-gtin-numeric-identifier-validator.d.ts +5 -10
  76. package/dist/non-gtin-numeric-identifier-validator.d.ts.map +1 -1
  77. package/dist/non-gtin-numeric-identifier-validator.js +5 -12
  78. package/dist/non-gtin-numeric-identifier-validator.js.map +1 -1
  79. package/dist/non-numeric-identifier-creator.d.ts +4 -9
  80. package/dist/non-numeric-identifier-creator.d.ts.map +1 -1
  81. package/dist/non-numeric-identifier-creator.js +8 -10
  82. package/dist/non-numeric-identifier-creator.js.map +1 -1
  83. package/dist/non-numeric-identifier-type.d.ts +22 -3
  84. package/dist/non-numeric-identifier-type.d.ts.map +1 -1
  85. package/dist/non-numeric-identifier-type.js +7 -1
  86. package/dist/non-numeric-identifier-type.js.map +1 -1
  87. package/dist/non-numeric-identifier-validator.d.ts +3 -11
  88. package/dist/non-numeric-identifier-validator.d.ts.map +1 -1
  89. package/dist/non-numeric-identifier-validator.js +8 -8
  90. package/dist/non-numeric-identifier-validator.js.map +1 -1
  91. package/dist/non-serializable-numeric-identifier-creator.d.ts +10 -0
  92. package/dist/non-serializable-numeric-identifier-creator.d.ts.map +1 -0
  93. package/dist/non-serializable-numeric-identifier-creator.js +8 -0
  94. package/dist/non-serializable-numeric-identifier-creator.js.map +1 -0
  95. package/dist/non-serializable-numeric-identifier-descriptor.d.ts +12 -0
  96. package/dist/non-serializable-numeric-identifier-descriptor.d.ts.map +1 -0
  97. package/dist/non-serializable-numeric-identifier-descriptor.js +2 -0
  98. package/dist/non-serializable-numeric-identifier-descriptor.js.map +1 -0
  99. package/dist/non-serializable-numeric-identifier-type.d.ts +26 -0
  100. package/dist/non-serializable-numeric-identifier-type.d.ts.map +1 -0
  101. package/dist/non-serializable-numeric-identifier-type.js +7 -0
  102. package/dist/non-serializable-numeric-identifier-type.js.map +1 -0
  103. package/dist/non-serializable-numeric-identifier-validator.d.ts +16 -0
  104. package/dist/non-serializable-numeric-identifier-validator.d.ts.map +1 -0
  105. package/dist/non-serializable-numeric-identifier-validator.js +17 -0
  106. package/dist/non-serializable-numeric-identifier-validator.js.map +1 -0
  107. package/dist/numeric-identifier-creator.d.ts +6 -6
  108. package/dist/numeric-identifier-creator.d.ts.map +1 -1
  109. package/dist/numeric-identifier-descriptor.d.ts +2 -1
  110. package/dist/numeric-identifier-descriptor.d.ts.map +1 -1
  111. package/dist/numeric-identifier-type.d.ts +19 -24
  112. package/dist/numeric-identifier-type.d.ts.map +1 -1
  113. package/dist/numeric-identifier-type.js +4 -15
  114. package/dist/numeric-identifier-type.js.map +1 -1
  115. package/dist/numeric-identifier-validator.d.ts +20 -6
  116. package/dist/numeric-identifier-validator.d.ts.map +1 -1
  117. package/dist/numeric-identifier-validator.js +60 -1
  118. package/dist/numeric-identifier-validator.js.map +1 -1
  119. package/dist/prefix-manager.d.ts +13 -94
  120. package/dist/prefix-manager.d.ts.map +1 -1
  121. package/dist/prefix-manager.js +65 -102
  122. package/dist/prefix-manager.js.map +1 -1
  123. package/dist/serializable-numeric-identifier-creator.d.ts +3 -31
  124. package/dist/serializable-numeric-identifier-creator.d.ts.map +1 -1
  125. package/dist/serializable-numeric-identifier-creator.js +7 -21
  126. package/dist/serializable-numeric-identifier-creator.js.map +1 -1
  127. package/dist/serializable-numeric-identifier-descriptor.d.ts +1 -1
  128. package/dist/serializable-numeric-identifier-descriptor.d.ts.map +1 -1
  129. package/dist/serializable-numeric-identifier-type.d.ts +22 -2
  130. package/dist/serializable-numeric-identifier-type.d.ts.map +1 -1
  131. package/dist/serializable-numeric-identifier-type.js +7 -1
  132. package/dist/serializable-numeric-identifier-type.js.map +1 -1
  133. package/dist/serializable-numeric-identifier-validator.d.ts +3 -18
  134. package/dist/serializable-numeric-identifier-validator.d.ts.map +1 -1
  135. package/dist/serializable-numeric-identifier-validator.js +17 -16
  136. package/dist/serializable-numeric-identifier-validator.js.map +1 -1
  137. package/package.json +4 -5
  138. package/src/abstract-identifier-creator.ts +104 -75
  139. package/src/abstract-non-gtin-numeric-identifier-creator.ts +81 -0
  140. package/src/abstract-numeric-identifier-creator.ts +185 -133
  141. package/src/content-character-set-creators.ts +9 -0
  142. package/src/gtin-creator.ts +15 -16
  143. package/src/gtin-descriptor.ts +9 -3
  144. package/src/gtin-length.ts +61 -0
  145. package/src/gtin-type.ts +6 -37
  146. package/src/gtin-validator.ts +34 -41
  147. package/src/identifier-creator.ts +4 -4
  148. package/src/identifier-creators.ts +177 -0
  149. package/src/{descriptors.ts → identifier-descriptors.ts} +56 -54
  150. package/src/identifier-extension.ts +155 -0
  151. package/src/identifier-validator.ts +93 -10
  152. package/src/{validators.ts → identifier-validators.ts} +98 -36
  153. package/src/index.ts +15 -9
  154. package/src/leader-type.ts +29 -0
  155. package/src/non-gtin-numeric-identifier-creator.ts +7 -22
  156. package/src/non-gtin-numeric-identifier-descriptor.ts +1 -1
  157. package/src/non-gtin-numeric-identifier-type.ts +14 -3
  158. package/src/non-gtin-numeric-identifier-validator.ts +5 -13
  159. package/src/non-numeric-identifier-creator.ts +15 -12
  160. package/src/non-numeric-identifier-type.ts +14 -3
  161. package/src/non-numeric-identifier-validator.ts +8 -9
  162. package/src/non-serializable-numeric-identifier-creator.ts +11 -0
  163. package/src/non-serializable-numeric-identifier-descriptor.ts +12 -0
  164. package/src/non-serializable-numeric-identifier-type.ts +22 -0
  165. package/src/non-serializable-numeric-identifier-validator.ts +19 -0
  166. package/src/numeric-identifier-creator.ts +7 -6
  167. package/src/numeric-identifier-descriptor.ts +2 -1
  168. package/src/numeric-identifier-type.ts +17 -35
  169. package/src/numeric-identifier-validator.ts +61 -6
  170. package/src/prefix-manager.ts +76 -112
  171. package/src/serializable-numeric-identifier-creator.ts +10 -25
  172. package/src/serializable-numeric-identifier-descriptor.ts +1 -1
  173. package/src/serializable-numeric-identifier-type.ts +14 -5
  174. package/src/serializable-numeric-identifier-validator.ts +17 -16
  175. package/test/gtin-creator.ts +9 -2
  176. package/test/gtin-validator.test.ts +8 -10
  177. package/test/identifier-creator.ts +7 -7
  178. package/test/mixin.test.ts +35 -0
  179. package/test/non-gtin-numeric-identifier-validator.ts +7 -2
  180. package/test/non-numeric-identifier-validator.ts +2 -2
  181. package/test/non-serializable-numeric-identifier-creator.ts +64 -0
  182. package/test/non-serializable-numeric-identifier-validator.ts +6 -0
  183. package/test/numeric-identifier-creator.ts +7 -2
  184. package/test/serializable-numeric-identifier-validator.ts +2 -2
  185. package/test/utility.ts +6 -6
  186. package/test/validator.test.ts +11 -10
  187. package/test/variable-measure-rcn.test.ts +23 -23
  188. package/dist/abstract-identifier-validator.d.ts +0 -95
  189. package/dist/abstract-identifier-validator.d.ts.map +0 -1
  190. package/dist/abstract-identifier-validator.js +0 -123
  191. package/dist/abstract-identifier-validator.js.map +0 -1
  192. package/dist/abstract-numeric-identifier-validator.d.ts +0 -35
  193. package/dist/abstract-numeric-identifier-validator.d.ts.map +0 -1
  194. package/dist/abstract-numeric-identifier-validator.js +0 -61
  195. package/dist/abstract-numeric-identifier-validator.js.map +0 -1
  196. package/dist/creators.d.ts.map +0 -1
  197. package/dist/creators.js +0 -62
  198. package/dist/creators.js.map +0 -1
  199. package/dist/descriptors.d.ts.map +0 -1
  200. package/dist/descriptors.js.map +0 -1
  201. package/dist/validators.d.ts.map +0 -1
  202. package/dist/validators.js.map +0 -1
  203. package/src/abstract-identifier-validator.ts +0 -140
  204. package/src/abstract-numeric-identifier-validator.ts +0 -69
  205. package/src/creators.ts +0 -113
@@ -1,3 +1,4 @@
1
+ import type { AbstractConstructor } from "@aidc-toolkit/core";
1
2
  import {
2
3
  CharacterSetCreator,
3
4
  Exclusions,
@@ -5,164 +6,215 @@ import {
5
6
  type TransformerInput,
6
7
  type TransformerOutput
7
8
  } from "@aidc-toolkit/utility";
8
- import { AbstractIdentifierCreator } from "./abstract-identifier-creator.js";
9
+ import { type IdentifierCreatorConstructor, MixinAbstractIdentifierCreator } from "./abstract-identifier-creator.js";
9
10
  import { checkDigit, checkDigitSum } from "./check.js";
11
+ import type { IdentifierTypeValidator, IdentifierValidatorConstructorsEntry } from "./identifier-validators.js";
12
+ import { LeaderTypes } from "./leader-type.js";
10
13
  import type { NumericIdentifierCreator } from "./numeric-identifier-creator.js";
11
- import type { NumericIdentifierDescriptor } from "./numeric-identifier-descriptor.js";
12
- import { type LeaderType, LeaderTypes } from "./numeric-identifier-type.js";
13
- import type { NumericIdentifierValidation } from "./numeric-identifier-validator.js";
14
+ import type { NumericIdentifierType } from "./numeric-identifier-type.js";
15
+ import type { NumericIdentifierValidation, NumericIdentifierValidator } from "./numeric-identifier-validator.js";
14
16
  import type { PrefixProvider } from "./prefix-provider.js";
15
17
 
16
18
  /**
17
- * Abstract numeric identifier creator. Implements common functionality for a numeric identifier creator.
19
+ * Numeric identifier creator constructor type, which delegates to a numeric identifier validator constructor.
18
20
  *
19
- * @template TNumericIdentifierDescriptor
20
- * Numeric identifier descriptor type.
21
+ * @template TNumericIdentifierType
22
+ * Numeric identifier type type.
23
+ *
24
+ * @template TNumericIdentifierValidator
25
+ * Numeric identifier validator type.
21
26
  */
22
- export abstract class AbstractNumericIdentifierCreator<TNumericIdentifierDescriptor extends NumericIdentifierDescriptor> extends AbstractIdentifierCreator<TNumericIdentifierDescriptor, NumericIdentifierValidation> implements NumericIdentifierCreator<TNumericIdentifierDescriptor> {
23
- /**
24
- * Capacity.
25
- */
26
- private _capacity!: number;
27
-
28
- /**
29
- * Tweak for sparse creation.
30
- */
31
- private _tweak = 0n;
27
+ export type NumericIdentifierCreatorConstructor<
28
+ TNumericIdentifierType extends NumericIdentifierType,
29
+ TNumericIdentifierValidator extends NumericIdentifierValidator<TNumericIdentifierType>
30
+ > = AbstractConstructor<
31
+ [prefixProvider: PrefixProvider, prefix: string, ...args: ConstructorParameters<IdentifierValidatorConstructorsEntry<TNumericIdentifierType>>],
32
+ TNumericIdentifierValidator & NumericIdentifierCreator<TNumericIdentifierType>
33
+ >;
32
34
 
35
+ /**
36
+ * Mixin implementation of {@linkcode NumericIdentifierCreator} with an appropriate numeric identifier validator base.
37
+ *
38
+ * @template TNumericIdentifierType
39
+ * Numeric identifier type type.
40
+ *
41
+ * @param NumericIdentifierValidatorConstructor
42
+ * Numeric identifier validator base.
43
+ *
44
+ * @returns
45
+ * Numeric identifier creator class.
46
+ */
47
+ export function MixinAbstractNumericIdentifierCreator<
48
+ TNumericIdentifierType extends NumericIdentifierType
49
+ >(NumericIdentifierValidatorConstructor: AbstractConstructor<
50
+ ConstructorParameters<IdentifierValidatorConstructorsEntry<TNumericIdentifierType>>,
51
+ IdentifierTypeValidator<TNumericIdentifierType>
52
+ >): NumericIdentifierCreatorConstructor<
53
+ TNumericIdentifierType,
54
+ IdentifierTypeValidator<TNumericIdentifierType>
55
+ > {
33
56
  /**
34
- * Initialize the prefix provider. This method is in lieu of a constructor due to the mixin architecture.
35
- *
36
- * @param prefixProvider
37
- * Prefix provider.
38
- *
39
- * @param prefix
40
- * Prefix within prefix manager to use to calculate reference length.
57
+ * Abstract numeric identifier creator. Implements common functionality for a numeric identifier creator, mixed in
58
+ * with a matching numeric identifier validator.
41
59
  */
42
- protected override init(prefixProvider: PrefixProvider, prefix: string): void {
43
- super.init(prefixProvider, prefix, 1);
44
-
45
- // Capacity is always in number range.
46
- this._capacity = Number(CharacterSetCreator.powerOf10(this.referenceLength));
47
- }
60
+ abstract class AbstractNumericIdentifierCreator extends (
61
+ MixinAbstractIdentifierCreator(NumericIdentifierValidatorConstructor) as IdentifierCreatorConstructor<
62
+ TNumericIdentifierType,
63
+ NumericIdentifierValidation,
64
+ NumericIdentifierValidator<TNumericIdentifierType>
65
+ >
66
+ ) implements NumericIdentifierCreator<TNumericIdentifierType> {
67
+ /**
68
+ * Capacity.
69
+ */
70
+ readonly #capacity: number;
71
+
72
+ /**
73
+ * Tweak for sparse creation.
74
+ */
75
+ #tweak = 0n;
76
+
77
+ /**
78
+ * Constructor.
79
+ *
80
+ * @param prefixProvider
81
+ * Prefix provider.
82
+ *
83
+ * @param prefix
84
+ * Prefix within prefix provider to use to calculate reference length.
85
+ *
86
+ * @param args
87
+ * Originating constructor arguments.
88
+ */
89
+ constructor(prefixProvider: PrefixProvider, prefix: string, ...args: ConstructorParameters<IdentifierValidatorConstructorsEntry<TNumericIdentifierType>>) {
90
+ super(prefixProvider, prefix, 1, ...args);
91
+
92
+ // Capacity is always in number range.
93
+ this.#capacity = Number(CharacterSetCreator.powerOf10(this.referenceLength));
94
+ }
48
95
 
49
- /**
50
- * Get the leader type.
51
- */
52
- abstract get leaderType(): LeaderType;
96
+ /**
97
+ * @inheritDoc
98
+ */
99
+ get capacity(): number {
100
+ return this.#capacity;
101
+ }
53
102
 
54
- /**
55
- * @inheritDoc
56
- */
57
- get capacity(): number {
58
- return this._capacity;
59
- }
103
+ /**
104
+ * @inheritDoc
105
+ */
106
+ get tweak(): bigint {
107
+ return this.#tweak;
108
+ }
60
109
 
61
- /**
62
- * @inheritDoc
63
- */
64
- get tweak(): bigint {
65
- return this._tweak;
66
- }
110
+ /**
111
+ * @inheritDoc
112
+ */
113
+ set tweak(value: bigint) {
114
+ this.#tweak = value;
115
+ }
67
116
 
68
- /**
69
- * @inheritDoc
70
- */
71
- set tweak(value: bigint) {
72
- this._tweak = value;
73
- }
117
+ /**
118
+ * Build an identifier from a reference by merging it with the prefix and adding the check digit.
119
+ *
120
+ * @param reference
121
+ * Identifier reference.
122
+ *
123
+ * @returns
124
+ * Identifier.
125
+ */
126
+ #buildIdentifier(reference: string): string {
127
+ const partialIdentifier = this.leaderType === LeaderTypes.ExtensionDigit ? reference.substring(0, 1) + this.prefix + reference.substring(1) : this.prefix + reference;
128
+
129
+ return partialIdentifier + checkDigit(partialIdentifier);
130
+ }
74
131
 
75
- /**
76
- * Build an identifier from a reference by merging it with the prefix and adding the check digit.
77
- *
78
- * @param reference
79
- * Identifier reference.
80
- *
81
- * @returns
82
- * Identifier.
83
- */
84
- private buildIdentifier(reference: string): string {
85
- const partialIdentifier = this.leaderType === LeaderTypes.ExtensionDigit ? reference.substring(0, 1) + this.prefix + reference.substring(1) : this.prefix + reference;
132
+ /**
133
+ * @inheritDoc
134
+ */
135
+ create<TTransformerInput extends TransformerInput<number | bigint>>(valueOrValues: TTransformerInput, sparse = false): TransformerOutput<TTransformerInput, string> {
136
+ return NUMERIC_CREATOR.create(this.referenceLength, valueOrValues, Exclusions.None, sparse ? this.tweak : undefined, reference => this.#buildIdentifier(reference));
137
+ }
86
138
 
87
- return partialIdentifier + checkDigit(partialIdentifier);
88
- }
139
+ /**
140
+ * Create all identifiers from a partial identifier. Call is recursive until remaining reference
141
+ * length is 0.
142
+ *
143
+ * @param partialIdentifier
144
+ * Partial identifier. Initial value is `this.prefix`.
145
+ *
146
+ * @param remainingReferenceLength
147
+ * Remaining reference length. Initial value is `this.referenceLength`.
148
+ *
149
+ * @param extensionWeight
150
+ * If this value is not zero, the identifier has an extension digit, this call is setting it, and this value is
151
+ * applied to the calculation of the check digit.
152
+ *
153
+ * @param weight
154
+ * If the extension weight is zero, this value is applied to the calculation of the check digit.
155
+ *
156
+ * @param partialCheckDigitSum
157
+ * Partial check digit sum for the partial identifier.
158
+ *
159
+ * @yields
160
+ * Identifier.
161
+ */
162
+ static * createAllPartial(partialIdentifier: string, remainingReferenceLength: number, extensionWeight: number, weight: number, partialCheckDigitSum: number): Generator<string> {
163
+ if (remainingReferenceLength === 0) {
164
+ // Finalize check digit calculation and append.
165
+ yield partialIdentifier + NUMERIC_CREATOR.character(9 - (partialCheckDigitSum + 9) % 10);
166
+ } else {
167
+ const nextRemainingReferenceLength = remainingReferenceLength - 1;
89
168
 
90
- /**
91
- * @inheritDoc
92
- */
93
- create<TTransformerInput extends TransformerInput<number | bigint>>(valueOrValues: TTransformerInput, sparse = false): TransformerOutput<TTransformerInput, string> {
94
- return NUMERIC_CREATOR.create(this.referenceLength, valueOrValues, Exclusions.None, sparse ? this.tweak : undefined, reference => this.buildIdentifier(reference));
95
- }
169
+ let nextPartialCheckDigitSum = partialCheckDigitSum;
96
170
 
97
- /**
98
- * Create all identifiers from a partial identifier. Call is recursive until remaining reference
99
- * length is 0.
100
- *
101
- * @param partialIdentifier
102
- * Partial identifier. Initial value is `this.prefix`.
103
- *
104
- * @param remainingReferenceLength
105
- * Remaining reference length. Initial value is `this.referenceLength`.
106
- *
107
- * @param extensionWeight
108
- * If this value is not zero, the identifier has an extension digit, this call is setting it, and this value is
109
- * applied to the calculation of the check digit.
110
- *
111
- * @param weight
112
- * If the extension weight is zero, this value is applied to the calculation of the check digit.
113
- *
114
- * @param partialCheckDigitSum
115
- * Partial check digit sum for the partial identifier.
116
- *
117
- * @yields
118
- * Identifier.
119
- */
120
- private static * createAllPartial(partialIdentifier: string, remainingReferenceLength: number, extensionWeight: number, weight: number, partialCheckDigitSum: number): Generator<string> {
121
- if (remainingReferenceLength === 0) {
122
- // Finalize check digit calculation and append.
123
- yield partialIdentifier + NUMERIC_CREATOR.character(9 - (partialCheckDigitSum + 9) % 10);
124
- } else {
125
- const nextRemainingReferenceLength = remainingReferenceLength - 1;
171
+ if (extensionWeight !== 0) {
172
+ // Apply every digit to the extension digit.
173
+ for (const c of NUMERIC_CREATOR.characterSet) {
174
+ yield * AbstractNumericIdentifierCreator.createAllPartial(c + partialIdentifier, nextRemainingReferenceLength, 0, weight, nextPartialCheckDigitSum);
126
175
 
127
- let nextPartialCheckDigitSum = partialCheckDigitSum;
176
+ nextPartialCheckDigitSum += extensionWeight;
177
+ }
178
+ } else {
179
+ const nextWeight = 4 - weight;
128
180
 
129
- if (extensionWeight !== 0) {
130
- // Apply every digit to the extension digit.
131
- for (const c of NUMERIC_CREATOR.characterSet) {
132
- yield * AbstractNumericIdentifierCreator.createAllPartial(c + partialIdentifier, nextRemainingReferenceLength, 0, weight, nextPartialCheckDigitSum);
181
+ // Apply every digit to the current character in the identifier.
182
+ for (const c of NUMERIC_CREATOR.characterSet) {
183
+ yield * AbstractNumericIdentifierCreator.createAllPartial(partialIdentifier + c, nextRemainingReferenceLength, 0, nextWeight, nextPartialCheckDigitSum);
133
184
 
134
- nextPartialCheckDigitSum += extensionWeight;
185
+ nextPartialCheckDigitSum += weight;
186
+ }
135
187
  }
136
- } else {
137
- const nextWeight = 4 - weight;
138
-
139
- // Apply every digit to the current character in the identifier.
140
- for (const c of NUMERIC_CREATOR.characterSet) {
141
- yield * AbstractNumericIdentifierCreator.createAllPartial(partialIdentifier + c, nextRemainingReferenceLength, 0, nextWeight, nextPartialCheckDigitSum);
188
+ }
189
+ }
142
190
 
143
- nextPartialCheckDigitSum += weight;
191
+ /**
192
+ * @inheritDoc
193
+ */
194
+ createAll(): Iterable<string> {
195
+ const hasExtensionDigit = this.leaderType === LeaderTypes.ExtensionDigit;
196
+ const prefix = this.prefix;
197
+ const length = this.length;
198
+ const referenceLength = this.referenceLength;
199
+
200
+ // Start weight is for reference excluding extension digit, which has its weight calculated separately.
201
+ const startWeight = 3 - 2 * ((referenceLength + 1 - Number(hasExtensionDigit)) % 2);
202
+
203
+ // Returning separate Iterable object makes iteration repeatable.
204
+ return {
205
+ [Symbol.iterator]() {
206
+ return AbstractNumericIdentifierCreator.createAllPartial(prefix, referenceLength, hasExtensionDigit ? 3 - 2 * length % 2 : 0, startWeight, checkDigitSum(startWeight === 3, prefix));
144
207
  }
145
- }
208
+ };
146
209
  }
147
210
  }
148
211
 
149
- /**
150
- * @inheritDoc
151
- */
152
- createAll(): Iterable<string> {
153
- const hasExtensionDigit = this.leaderType === LeaderTypes.ExtensionDigit;
154
- const prefix = this.prefix;
155
- const length = this.length;
156
- const referenceLength = this.referenceLength;
157
-
158
- // Start weight is for reference excluding extension digit, which has its weight calculated separately.
159
- const startWeight = 3 - 2 * ((referenceLength + 1 - Number(hasExtensionDigit)) % 2);
160
-
161
- // Returning separate Iterable object makes iteration repeatable.
162
- return {
163
- [Symbol.iterator]() {
164
- return AbstractNumericIdentifierCreator.createAllPartial(prefix, referenceLength, hasExtensionDigit ? 3 - 2 * length % 2 : 0, startWeight, checkDigitSum(startWeight === 3, prefix));
165
- }
166
- };
167
- }
212
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- Base class was upcast to type with statically known members for mixin, downcast result.
213
+ return AbstractNumericIdentifierCreator as AbstractConstructor<
214
+ ConstructorParameters<typeof AbstractNumericIdentifierCreator>,
215
+ unknown
216
+ > as AbstractConstructor<
217
+ ConstructorParameters<typeof AbstractNumericIdentifierCreator>,
218
+ IdentifierTypeValidator<TNumericIdentifierType> & AbstractNumericIdentifierCreator
219
+ >;
168
220
  }
@@ -0,0 +1,9 @@
1
+ import { type CharacterSetCreator, NUMERIC_CREATOR } from "@aidc-toolkit/utility";
2
+ import { AI39_CREATOR, AI82_CREATOR } from "./character-set.js";
3
+ import { type ContentCharacterSet, ContentCharacterSets } from "./content-character-set.js";
4
+
5
+ export const CONTENT_CHARACTER_SET_CREATORS: Record<ContentCharacterSet, CharacterSetCreator> = {
6
+ [ContentCharacterSets.Numeric]: NUMERIC_CREATOR,
7
+ [ContentCharacterSets.AI82]: AI82_CREATOR,
8
+ [ContentCharacterSets.AI39]: AI39_CREATOR
9
+ };
@@ -5,41 +5,40 @@ import {
5
5
  type TransformerInput,
6
6
  type TransformerOutput
7
7
  } from "@aidc-toolkit/utility";
8
- import { Mixin } from "ts-mixer";
9
- import { AbstractNumericIdentifierCreator } from "./abstract-numeric-identifier-creator.js";
8
+ import { MixinAbstractNumericIdentifierCreator } from "./abstract-numeric-identifier-creator.js";
10
9
  import { checkDigit, priceOrWeightCheckDigit } from "./check.js";
11
- import { type GTINBaseType, GTINTypes } from "./gtin-type.js";
10
+ import { type GTINBaseLength, GTINLengths } from "./gtin-length.js";
11
+ import type { GTINType } from "./gtin-type.js";
12
12
  import { GTINValidator } from "./gtin-validator.js";
13
13
  import { i18nextGS1 } from "./locale/i18n.js";
14
14
  import type { PrefixProvider } from "./prefix-provider.js";
15
15
 
16
16
  /**
17
- * GTIN creator. Applicable to GTIN-13, GTIN-12, and GTIN-8 types; no applicable to GTIN-14 type.
17
+ * GTIN creator. Applicable to GTIN-13, GTIN-12, and GTIN-8 types; not applicable to GTIN-14 type.
18
18
  */
19
- export class GTINCreator extends Mixin(GTINValidator, AbstractNumericIdentifierCreator) {
19
+ export class GTINCreator extends MixinAbstractNumericIdentifierCreator<
20
+ GTINType
21
+ >(GTINValidator) {
20
22
  /**
21
23
  * Validation parameters for required indicator digit.
22
24
  */
23
- private static readonly REQUIRED_INDICATOR_DIGIT_VALIDATION: CharacterSetValidation = {
25
+ static readonly #REQUIRED_INDICATOR_DIGIT_VALIDATION: CharacterSetValidation = {
24
26
  minimumLength: 1,
25
27
  maximumLength: 1,
26
28
  component: () => i18nextGS1.t("Identifier.indicatorDigit")
27
29
  };
28
30
 
29
31
  /**
30
- * Constructor. Typically called internally by a prefix manager but may be called by other code with another prefix
31
- * provider type.
32
+ * Constructor.
32
33
  *
33
34
  * @param prefixProvider
34
35
  * Prefix provider.
35
36
  *
36
- * @param gtinBaseType
37
- * GTIN base type (all except GTIN-14).
37
+ * @param gtinBaseLength
38
+ * GTIN base length (all except GTIN-14).
38
39
  */
39
- constructor(prefixProvider: PrefixProvider, gtinBaseType: GTINBaseType) {
40
- super(gtinBaseType);
41
-
42
- this.init(prefixProvider, prefixProvider.prefix);
40
+ constructor(prefixProvider: PrefixProvider, gtinBaseLength: GTINBaseLength) {
41
+ super(prefixProvider, prefixProvider.prefix, gtinBaseLength);
43
42
  }
44
43
 
45
44
  /**
@@ -69,9 +68,9 @@ export class GTINCreator extends Mixin(GTINValidator, AbstractNumericIdentifierC
69
68
  * GTIN-14(s).
70
69
  */
71
70
  createGTIN14<TTransformerInput extends TransformerInput<number | bigint>>(indicatorDigit: string, valueOrValues: TTransformerInput, sparse = false): TransformerOutput<TTransformerInput, string> {
72
- NUMERIC_CREATOR.validate(indicatorDigit, GTINCreator.REQUIRED_INDICATOR_DIGIT_VALIDATION);
71
+ NUMERIC_CREATOR.validate(indicatorDigit, GTINCreator.#REQUIRED_INDICATOR_DIGIT_VALIDATION);
73
72
 
74
- return NUMERIC_CREATOR.create(GTINTypes.GTIN13 - this.prefixProvider.gs1CompanyPrefix.length - 1, valueOrValues, Exclusions.None, sparse ? this.tweak : undefined, (reference) => {
73
+ return NUMERIC_CREATOR.create(GTINLengths.GTIN13 - this.prefixProvider.gs1CompanyPrefix.length - 1, valueOrValues, Exclusions.None, sparse ? this.tweak : undefined, (reference) => {
75
74
  const partialIdentifier = indicatorDigit + this.prefixProvider.gs1CompanyPrefix + reference;
76
75
 
77
76
  return partialIdentifier + checkDigit(partialIdentifier);
@@ -1,6 +1,7 @@
1
- import type { IdentifierTypes } from "./identifier-type.js";
1
+ import type { GTINBaseLength } from "./gtin-length.js";
2
+ import type { GTINType } from "./gtin-type.js";
3
+ import type { LeaderTypes } from "./leader-type.js";
2
4
  import type { NumericIdentifierDescriptor } from "./numeric-identifier-descriptor.js";
3
- import type { LeaderTypes } from "./numeric-identifier-type.js";
4
5
 
5
6
  /**
6
7
  * GTIN descriptor.
@@ -9,7 +10,12 @@ export interface GTINDescriptor extends NumericIdentifierDescriptor {
9
10
  /**
10
11
  * @inheritDoc
11
12
  */
12
- readonly identifierType: typeof IdentifierTypes.GTIN;
13
+ readonly identifierType: GTINType;
14
+
15
+ /**
16
+ * @inheritDoc
17
+ */
18
+ readonly length: GTINBaseLength;
13
19
 
14
20
  /**
15
21
  * @inheritDoc
@@ -0,0 +1,61 @@
1
+ import { omit } from "@aidc-toolkit/core";
2
+ import { type PrefixType, PrefixTypes } from "./prefix-type.js";
3
+
4
+ /**
5
+ * GTIN lengths. The numeric values are equal to the lengths of the GTINs.
6
+ */
7
+ export const GTINLengths = {
8
+ /**
9
+ * GTIN-13.
10
+ */
11
+ GTIN13: 13,
12
+
13
+ /**
14
+ * GTIN-12.
15
+ */
16
+ GTIN12: 12,
17
+
18
+ /**
19
+ * GTIN-8.
20
+ */
21
+ GTIN8: 8,
22
+
23
+ /**
24
+ * GTIN-14.
25
+ */
26
+ GTIN14: 14
27
+ } as const;
28
+
29
+ /**
30
+ * GTIN length key.
31
+ */
32
+ export type GTINLengthKey = keyof typeof GTINLengths;
33
+
34
+ /**
35
+ * GTIN length.
36
+ */
37
+ export type GTINLength = typeof GTINLengths[GTINLengthKey];
38
+
39
+ /**
40
+ * GTIN base lengths (all except GTIN-14).
41
+ */
42
+ export const GTINBaseLengths = omit(GTINLengths, "GTIN14");
43
+
44
+ /**
45
+ * GTIN base length key.
46
+ */
47
+ export type GTINBaseLengthKey = keyof typeof GTINBaseLengths;
48
+
49
+ /**
50
+ * GTIN length.
51
+ */
52
+ export type GTINBaseLength = typeof GTINBaseLengths[GTINBaseLengthKey];
53
+
54
+ /**
55
+ * GTIN base lengths by prefix type. Used to determine the GTIN length supported by a prefix type.
56
+ */
57
+ export const GTIN_BASE_TYPES: Readonly<Record<PrefixType, GTINBaseLength>> = {
58
+ [PrefixTypes.GS1CompanyPrefix]: GTINLengths.GTIN13,
59
+ [PrefixTypes.UPCCompanyPrefix]: GTINLengths.GTIN12,
60
+ [PrefixTypes.GS18Prefix]: GTINLengths.GTIN8
61
+ };
package/src/gtin-type.ts CHANGED
@@ -1,29 +1,12 @@
1
- import { type PrefixType, PrefixTypes } from "./prefix-type.js";
1
+ import { pick } from "@aidc-toolkit/core";
2
+ import { NumericIdentifierTypes } from "./numeric-identifier-type.js";
2
3
 
3
4
  /**
4
- * GTIN types. The numeric values are equal to the lengths of the GTIN types.
5
+ * GTIN types.
5
6
  */
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;
7
+ export const GTINTypes = pick(NumericIdentifierTypes,
8
+ NumericIdentifierTypes.GTIN
9
+ );
27
10
 
28
11
  /**
29
12
  * GTIN type key.
@@ -34,17 +17,3 @@ export type GTINTypeKey = keyof typeof GTINTypes;
34
17
  * GTIN type.
35
18
  */
36
19
  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. Used to determine the GTIN type supported by a 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
- };