@aidc-toolkit/gs1 0.9.20-beta → 1.0.22-beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (131) hide show
  1. package/dist/character-set.d.ts +4 -4
  2. package/dist/character-set.d.ts.map +1 -1
  3. package/dist/character-set.js +7 -7
  4. package/dist/character-set.js.map +1 -1
  5. package/dist/gtin-creator.d.ts +68 -0
  6. package/dist/gtin-creator.d.ts.map +1 -0
  7. package/dist/gtin-creator.js +158 -0
  8. package/dist/gtin-creator.js.map +1 -0
  9. package/dist/gtin-validator.d.ts +202 -0
  10. package/dist/gtin-validator.d.ts.map +1 -0
  11. package/dist/gtin-validator.js +470 -0
  12. package/dist/gtin-validator.js.map +1 -0
  13. package/dist/identifier-creator.d.ts +72 -0
  14. package/dist/identifier-creator.d.ts.map +1 -0
  15. package/dist/identifier-creator.js +50 -0
  16. package/dist/identifier-creator.js.map +1 -0
  17. package/dist/identifier-type.d.ts +58 -0
  18. package/dist/identifier-type.d.ts.map +1 -0
  19. package/dist/identifier-type.js +54 -0
  20. package/dist/identifier-type.js.map +1 -0
  21. package/dist/identifier-validator.d.ts +174 -0
  22. package/dist/identifier-validator.d.ts.map +1 -0
  23. package/dist/identifier-validator.js +145 -0
  24. package/dist/identifier-validator.js.map +1 -0
  25. package/dist/index.d.ts +16 -1
  26. package/dist/index.d.ts.map +1 -1
  27. package/dist/index.js +16 -1
  28. package/dist/index.js.map +1 -1
  29. package/dist/locale/en/locale-strings.d.ts +3 -3
  30. package/dist/locale/en/locale-strings.js +3 -3
  31. package/dist/locale/en/locale-strings.js.map +1 -1
  32. package/dist/locale/fr/locale-strings.d.ts +3 -3
  33. package/dist/locale/fr/locale-strings.js +3 -3
  34. package/dist/locale/fr/locale-strings.js.map +1 -1
  35. package/dist/non-gtin-numeric-identifier-creator.d.ts +30 -0
  36. package/dist/non-gtin-numeric-identifier-creator.d.ts.map +1 -0
  37. package/dist/non-gtin-numeric-identifier-creator.js +30 -0
  38. package/dist/non-gtin-numeric-identifier-creator.js.map +1 -0
  39. package/dist/non-gtin-numeric-identifier-validator.d.ts +41 -0
  40. package/dist/non-gtin-numeric-identifier-validator.d.ts.map +1 -0
  41. package/dist/non-gtin-numeric-identifier-validator.js +40 -0
  42. package/dist/non-gtin-numeric-identifier-validator.js.map +1 -0
  43. package/dist/non-numeric-identifier-creator.d.ts +55 -0
  44. package/dist/non-numeric-identifier-creator.d.ts.map +1 -0
  45. package/dist/non-numeric-identifier-creator.js +93 -0
  46. package/dist/non-numeric-identifier-creator.js.map +1 -0
  47. package/dist/non-numeric-identifier-validator.d.ts +78 -0
  48. package/dist/non-numeric-identifier-validator.d.ts.map +1 -0
  49. package/dist/non-numeric-identifier-validator.js +95 -0
  50. package/dist/non-numeric-identifier-validator.js.map +1 -0
  51. package/dist/numeric-identifier-creator.d.ts +121 -0
  52. package/dist/numeric-identifier-creator.d.ts.map +1 -0
  53. package/dist/numeric-identifier-creator.js +135 -0
  54. package/dist/numeric-identifier-creator.js.map +1 -0
  55. package/dist/numeric-identifier-validator.d.ts +76 -0
  56. package/dist/numeric-identifier-validator.d.ts.map +1 -0
  57. package/dist/numeric-identifier-validator.js +84 -0
  58. package/dist/numeric-identifier-validator.js.map +1 -0
  59. package/dist/prefix-manager.d.ts +224 -0
  60. package/dist/prefix-manager.d.ts.map +1 -0
  61. package/dist/prefix-manager.js +369 -0
  62. package/dist/prefix-manager.js.map +1 -0
  63. package/dist/prefix-provider.d.ts +27 -0
  64. package/dist/prefix-provider.d.ts.map +1 -0
  65. package/dist/prefix-provider.js +2 -0
  66. package/dist/prefix-provider.js.map +1 -0
  67. package/dist/prefix-type.d.ts +22 -0
  68. package/dist/prefix-type.d.ts.map +1 -0
  69. package/dist/prefix-type.js +18 -0
  70. package/dist/prefix-type.js.map +1 -0
  71. package/dist/prefix-validator.d.ts +58 -0
  72. package/dist/prefix-validator.d.ts.map +1 -0
  73. package/dist/prefix-validator.js +154 -0
  74. package/dist/prefix-validator.js.map +1 -0
  75. package/dist/serializable-numeric-identifier-creator.d.ts +86 -0
  76. package/dist/serializable-numeric-identifier-creator.d.ts.map +1 -0
  77. package/dist/serializable-numeric-identifier-creator.js +116 -0
  78. package/dist/serializable-numeric-identifier-creator.js.map +1 -0
  79. package/dist/serializable-numeric-identifier-validator.d.ts +79 -0
  80. package/dist/serializable-numeric-identifier-validator.d.ts.map +1 -0
  81. package/dist/serializable-numeric-identifier-validator.js +99 -0
  82. package/dist/serializable-numeric-identifier-validator.js.map +1 -0
  83. package/gs1.iml +4 -1
  84. package/package.json +2 -3
  85. package/src/character-set.ts +7 -7
  86. package/src/gtin-creator.ts +195 -0
  87. package/src/gtin-validator.ts +564 -0
  88. package/src/identifier-creator.ts +97 -0
  89. package/src/identifier-type.ts +69 -0
  90. package/src/identifier-validator.ts +235 -0
  91. package/src/index.ts +16 -1
  92. package/src/locale/en/locale-strings.ts +3 -3
  93. package/src/locale/fr/locale-strings.ts +3 -3
  94. package/src/non-gtin-numeric-identifier-creator.ts +33 -0
  95. package/src/non-gtin-numeric-identifier-validator.ts +54 -0
  96. package/src/non-numeric-identifier-creator.ts +111 -0
  97. package/src/non-numeric-identifier-validator.ts +128 -0
  98. package/src/numeric-identifier-creator.ts +200 -0
  99. package/src/numeric-identifier-validator.ts +128 -0
  100. package/src/prefix-manager.ts +446 -0
  101. package/src/prefix-provider.ts +31 -0
  102. package/src/prefix-type.ts +24 -0
  103. package/src/prefix-validator.ts +191 -0
  104. package/src/serializable-numeric-identifier-creator.ts +128 -0
  105. package/src/serializable-numeric-identifier-validator.ts +124 -0
  106. package/test/check.test.ts +0 -4
  107. package/test/creator.test.ts +30 -0
  108. package/test/gtin-creator.ts +239 -0
  109. package/test/gtin-validator.test.ts +149 -0
  110. package/test/identifier-creator.ts +84 -0
  111. package/test/identifier-validator.ts +8 -0
  112. package/test/non-gtin-numeric-identifier-creator.ts +98 -0
  113. package/test/non-gtin-numeric-identifier-validator.ts +6 -0
  114. package/test/non-numeric-identifier-validator.ts +24 -0
  115. package/test/numeric-identifier-creator.ts +132 -0
  116. package/test/numeric-identifier-validator.ts +23 -0
  117. package/test/prefix-manager.test.ts +112 -0
  118. package/test/serializable-numeric-identifier-creator.ts +56 -0
  119. package/test/serializable-numeric-identifier-validator.ts +24 -0
  120. package/test/setup.ts +4 -0
  121. package/test/sparse.test.ts +56 -0
  122. package/test/utility.ts +22 -0
  123. package/test/validator.test.ts +52 -0
  124. package/test/variable-measure-rcn.test.ts +201 -0
  125. package/vitest.config.ts +7 -0
  126. package/dist/idkey.d.ts +0 -1346
  127. package/dist/idkey.d.ts.map +0 -1
  128. package/dist/idkey.js +0 -2024
  129. package/dist/idkey.js.map +0 -1
  130. package/src/idkey.ts +0 -2532
  131. package/test/idkey.test.ts +0 -1247
@@ -0,0 +1,200 @@
1
+ import {
2
+ CharacterSetCreator,
3
+ Exclusions,
4
+ NUMERIC_CREATOR,
5
+ type TransformerInput,
6
+ type TransformerOutput
7
+ } from "@aidc-toolkit/utility";
8
+ import { checkDigit, checkDigitSum } from "./check.js";
9
+ import { AbstractIdentifierCreator, type IdentifierCreator } from "./identifier-creator.js";
10
+ import { type LeaderType, LeaderTypes, type NumericIdentifierValidator } from "./numeric-identifier-validator.js";
11
+ import type { PrefixProvider } from "./prefix-provider";
12
+
13
+ /**
14
+ * Numeric identifier creator. Creates one or many numeric identifiers.
15
+ */
16
+ export interface NumericIdentifierCreator extends NumericIdentifierValidator, IdentifierCreator {
17
+ /**
18
+ * Get the capacity (`10**referenceLength`).
19
+ */
20
+ get capacity(): number;
21
+
22
+ /**
23
+ * Create identifier(s) with reference(s) based on numeric value(s). The value(s) is/are converted to
24
+ * references of the appropriate length using {@linkcode NUMERIC_CREATOR}.
25
+ *
26
+ * @template TTransformerInput
27
+ * Transformer input type.
28
+ *
29
+ * @param valueOrValues
30
+ * Numeric value(s).
31
+ *
32
+ * @param sparse
33
+ * If true, the value(s) are mapped to a sparse sequence resistant to discovery. Default is false.
34
+ *
35
+ * @returns
36
+ * Identifier(s).
37
+ */
38
+ create: <TTransformerInput extends TransformerInput<number | bigint>>(valueOrValues: TTransformerInput, sparse?: boolean) => TransformerOutput<TTransformerInput, string>;
39
+
40
+ /**
41
+ * Create all identifiers for the prefix from `0` to `capacity - 1`.
42
+ *
43
+ * The implementation creates the strings only as needed using an internal generator function. Although the result
44
+ * is equivalent to calling `creator.create(new Sequence(0, creator.capacity))`, this method is significantly
45
+ * faster.
46
+ *
47
+ * @returns
48
+ * All identifiers for the prefix.
49
+ */
50
+ createAll: () => Iterable<string>;
51
+ }
52
+
53
+ /**
54
+ * Abstract numeric identifier creator. Implements common functionality for a numeric identifier
55
+ * creator.
56
+ */
57
+ export abstract class AbstractNumericIdentifierCreator extends AbstractIdentifierCreator implements NumericIdentifierCreator {
58
+ /**
59
+ * Capacity.
60
+ */
61
+ private _capacity!: number;
62
+
63
+ /**
64
+ * Tweak for sparse creation.
65
+ */
66
+ private _tweak = 0n;
67
+
68
+ /**
69
+ * Initialize the prefix provider. This method is in lieu of a constructor due to the mixin architecture.
70
+ *
71
+ * @param prefixProvider
72
+ * Prefix provider.
73
+ *
74
+ * @param prefix
75
+ * Prefix within prefix manager to use to calculate reference length.
76
+ */
77
+ protected override init(prefixProvider: PrefixProvider, prefix: string): void {
78
+ super.init(prefixProvider, prefix, 1);
79
+
80
+ // Capacity is always in number range.
81
+ this._capacity = Number(CharacterSetCreator.powerOf10(this.referenceLength));
82
+ }
83
+
84
+ abstract get leaderType(): LeaderType;
85
+
86
+ /**
87
+ * @inheritDoc
88
+ */
89
+ get capacity(): number {
90
+ return this._capacity;
91
+ }
92
+
93
+ /**
94
+ * Get the tweak for sparse creation.
95
+ */
96
+ get tweak(): bigint {
97
+ return this._tweak;
98
+ }
99
+
100
+ /**
101
+ * Set the tweak for sparse creation.
102
+ */
103
+ set tweak(value: bigint) {
104
+ this._tweak = value;
105
+ }
106
+
107
+ /**
108
+ * Build an identifier from a reference by merging it with the prefix and adding the check digit.
109
+ *
110
+ * @param reference
111
+ * Identifier reference.
112
+ *
113
+ * @returns
114
+ * Identifier.
115
+ */
116
+ private buildIdentifier(reference: string): string {
117
+ const partialIdentifier = this.leaderType === LeaderTypes.ExtensionDigit ? reference.substring(0, 1) + this.prefix + reference.substring(1) : this.prefix + reference;
118
+
119
+ return partialIdentifier + checkDigit(partialIdentifier);
120
+ }
121
+
122
+ /**
123
+ * @inheritDoc
124
+ */
125
+ create<TTransformerInput extends TransformerInput<number | bigint>>(valueOrValues: TTransformerInput, sparse = false): TransformerOutput<TTransformerInput, string> {
126
+ return NUMERIC_CREATOR.create(this.referenceLength, valueOrValues, Exclusions.None, sparse ? this.tweak : undefined, reference => this.buildIdentifier(reference));
127
+ }
128
+
129
+ /**
130
+ * Create all identifiers from a partial identifier. Call is recursive until remaining reference
131
+ * length is 0.
132
+ *
133
+ * @param partialIdentifier
134
+ * Partial identifier. Initial value is `this.prefix`.
135
+ *
136
+ * @param remainingReferenceLength
137
+ * Remaining reference length. Initial value is `this.referenceLength`.
138
+ *
139
+ * @param extensionWeight
140
+ * If this value is not zero, the identifier has an extension digit, this call is setting it, and this value
141
+ * is applied to the calculation of the check digit.
142
+ *
143
+ * @param weight
144
+ * If the extension weight is zero, this value is applied to the calculation of the check digit.
145
+ *
146
+ * @param partialCheckDigitSum
147
+ * Partial check digit sum for the partial identifier.
148
+ *
149
+ * @yields
150
+ * Identifier.
151
+ */
152
+ private static * createAllPartial(partialIdentifier: string, remainingReferenceLength: number, extensionWeight: number, weight: number, partialCheckDigitSum: number): Generator<string> {
153
+ if (remainingReferenceLength === 0) {
154
+ // Finalize check digit calculation and append.
155
+ yield partialIdentifier + NUMERIC_CREATOR.character(9 - (partialCheckDigitSum + 9) % 10);
156
+ } else {
157
+ const nextRemainingReferenceLength = remainingReferenceLength - 1;
158
+
159
+ let nextPartialCheckDigitSum = partialCheckDigitSum;
160
+
161
+ if (extensionWeight !== 0) {
162
+ // Apply every digit to the extension digit.
163
+ for (const c of NUMERIC_CREATOR.characterSet) {
164
+ yield * AbstractNumericIdentifierCreator.createAllPartial(c + partialIdentifier, nextRemainingReferenceLength, 0, weight, nextPartialCheckDigitSum);
165
+
166
+ nextPartialCheckDigitSum += extensionWeight;
167
+ }
168
+ } else {
169
+ const nextWeight = 4 - weight;
170
+
171
+ // Apply every digit to the current character in the identifier.
172
+ for (const c of NUMERIC_CREATOR.characterSet) {
173
+ yield * AbstractNumericIdentifierCreator.createAllPartial(partialIdentifier + c, nextRemainingReferenceLength, 0, nextWeight, nextPartialCheckDigitSum);
174
+
175
+ nextPartialCheckDigitSum += weight;
176
+ }
177
+ }
178
+ }
179
+ }
180
+
181
+ /**
182
+ * @inheritDoc
183
+ */
184
+ createAll(): Iterable<string> {
185
+ const hasExtensionDigit = this.leaderType === LeaderTypes.ExtensionDigit;
186
+ const prefix = this.prefix;
187
+ const length = this.length;
188
+ const referenceLength = this.referenceLength;
189
+
190
+ // Start weight is for reference excluding extension digit, which has its weight calculated separately.
191
+ const startWeight = 3 - 2 * ((referenceLength + 1 - Number(hasExtensionDigit)) % 2);
192
+
193
+ // Returning separate Iterable object makes iteration repeatable.
194
+ return {
195
+ [Symbol.iterator]() {
196
+ return AbstractNumericIdentifierCreator.createAllPartial(prefix, referenceLength, hasExtensionDigit ? 3 - 2 * length % 2 : 0, startWeight, checkDigitSum(startWeight === 3, prefix));
197
+ }
198
+ };
199
+ }
200
+ }
@@ -0,0 +1,128 @@
1
+ import { hasValidCheckDigit } from "./check.js";
2
+ import type { IdentifierType, IdentifierTypes } from "./identifier-type.js";
3
+ import {
4
+ AbstractIdentifierValidator,
5
+ ContentCharacterSets,
6
+ type IdentifierValidation,
7
+ type IdentifierValidator
8
+ } from "./identifier-validator.js";
9
+ import { i18nextGS1 } from "./locale/i18n.js";
10
+ import type { PrefixType } from "./prefix-type.js";
11
+
12
+ /**
13
+ * Numeric identifier type.
14
+ */
15
+ export type NumericIdentifierType =
16
+ typeof IdentifierTypes.GTIN |
17
+ typeof IdentifierTypes.GLN |
18
+ typeof IdentifierTypes.SSCC |
19
+ typeof IdentifierTypes.GRAI |
20
+ typeof IdentifierTypes.GSRN |
21
+ typeof IdentifierTypes.GDTI |
22
+ typeof IdentifierTypes.GSIN |
23
+ typeof IdentifierTypes.GCN;
24
+
25
+ /**
26
+ * Leader type.
27
+ */
28
+ export const LeaderTypes = {
29
+ /**
30
+ * No leader.
31
+ */
32
+ None: "None",
33
+
34
+ /**
35
+ * Indicator digit (GTIN only).
36
+ */
37
+ IndicatorDigit: "Indicator digit",
38
+
39
+ /**
40
+ * Extension digit (SSCC only).
41
+ */
42
+ ExtensionDigit: "Extension digit"
43
+ };
44
+
45
+ /**
46
+ * Leader type.
47
+ */
48
+ export type LeaderType = typeof LeaderTypes[keyof typeof LeaderTypes];
49
+
50
+ /**
51
+ * Numeric identifier validator. Validates a numeric identifier.
52
+ */
53
+ export interface NumericIdentifierValidator extends IdentifierValidator {
54
+ /**
55
+ * Get the leader type.
56
+ */
57
+ get leaderType(): LeaderType;
58
+ }
59
+
60
+ /**
61
+ * Abstract numeric identifier validator. Implements common functionality for a numeric identifier
62
+ * validator.
63
+ */
64
+ export abstract class AbstractNumericIdentifierValidator extends AbstractIdentifierValidator implements NumericIdentifierValidator {
65
+ /**
66
+ * Leader type.
67
+ */
68
+ private readonly _leaderType: LeaderType;
69
+
70
+ /**
71
+ * Prefix position, determined by the leader type.
72
+ */
73
+ private readonly _prefixPosition: number;
74
+
75
+ /**
76
+ * Constructor.
77
+ *
78
+ * @param identifierType
79
+ * Identifier type.
80
+ *
81
+ * @param prefixType
82
+ * Prefix type.
83
+ *
84
+ * @param length
85
+ * Length.
86
+ *
87
+ * @param leaderType
88
+ * Leader type.
89
+ */
90
+ protected constructor(identifierType: IdentifierType, prefixType: PrefixType, length: number, leaderType: LeaderType) {
91
+ super(identifierType, prefixType, length, ContentCharacterSets.Numeric);
92
+
93
+ this._leaderType = leaderType;
94
+ this._prefixPosition = Number(this.leaderType === LeaderTypes.ExtensionDigit);
95
+ }
96
+
97
+ /**
98
+ * @inheritDoc
99
+ */
100
+ get leaderType(): LeaderType {
101
+ return this._leaderType;
102
+ }
103
+
104
+ /**
105
+ * @inheritDoc
106
+ */
107
+ validate(identifier: string, validation?: IdentifierValidation): void {
108
+ // Validate the prefix, with care taken for its position within the identifier.
109
+ if (this._prefixPosition === 0) {
110
+ super.validatePrefix(identifier, validation?.positionOffset);
111
+ } else {
112
+ super.validatePrefix(identifier.substring(this._prefixPosition), validation?.positionOffset === undefined ? this._prefixPosition : validation.positionOffset + this._prefixPosition);
113
+ }
114
+
115
+ // Validate the length.
116
+ if (identifier.length !== this.length) {
117
+ throw new RangeError(i18nextGS1.t("Identifier.identifierTypeLength", {
118
+ identifierType: this.identifierType,
119
+ length: this.length
120
+ }));
121
+ }
122
+
123
+ // Validating the check digit will also validate the characters.
124
+ if (!hasValidCheckDigit(this.padIdentifier(identifier, validation))) {
125
+ throw new RangeError(i18nextGS1.t("Identifier.invalidCheckDigit"));
126
+ }
127
+ }
128
+ }