@aidc-toolkit/gs1 1.0.31-beta → 1.0.32-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.
- package/dist/gcp-length-cache.d.ts +42 -0
- package/dist/gcp-length-cache.d.ts.map +1 -0
- package/dist/gcp-length-cache.js +96 -0
- package/dist/gcp-length-cache.js.map +1 -0
- package/dist/gcp-length-data.d.ts +54 -0
- package/dist/gcp-length-data.d.ts.map +1 -0
- package/dist/gcp-length-data.js +29 -0
- package/dist/gcp-length-data.js.map +1 -0
- package/dist/gcp-length.d.ts +61 -0
- package/dist/gcp-length.d.ts.map +1 -0
- package/dist/gcp-length.js +301 -0
- package/dist/gcp-length.js.map +1 -0
- package/dist/gtin-creator.d.ts +0 -17
- package/dist/gtin-creator.d.ts.map +1 -1
- package/dist/gtin-creator.js +1 -93
- package/dist/gtin-creator.js.map +1 -1
- package/dist/gtin-validator.d.ts +1 -46
- package/dist/gtin-validator.d.ts.map +1 -1
- package/dist/gtin-validator.js +31 -125
- package/dist/gtin-validator.js.map +1 -1
- package/dist/identifier-validator.d.ts +1 -4
- package/dist/identifier-validator.d.ts.map +1 -1
- package/dist/identifier-validator.js +2 -5
- package/dist/identifier-validator.js.map +1 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/locale/en/locale-resources.d.ts +2 -1
- package/dist/locale/en/locale-resources.d.ts.map +1 -1
- package/dist/locale/en/locale-resources.js +3 -2
- package/dist/locale/en/locale-resources.js.map +1 -1
- package/dist/locale/fr/locale-resources.d.ts +2 -1
- package/dist/locale/fr/locale-resources.d.ts.map +1 -1
- package/dist/locale/fr/locale-resources.js +3 -2
- package/dist/locale/fr/locale-resources.js.map +1 -1
- package/dist/locale/i18n.d.ts +0 -3
- package/dist/locale/i18n.d.ts.map +1 -1
- package/dist/locale/i18n.js +2 -5
- package/dist/locale/i18n.js.map +1 -1
- package/dist/non-numeric-identifier-validator.js +1 -1
- package/dist/non-numeric-identifier-validator.js.map +1 -1
- package/dist/numeric-identifier-validator.js +2 -2
- package/dist/numeric-identifier-validator.js.map +1 -1
- package/dist/prefix-manager.d.ts +35 -0
- package/dist/prefix-manager.d.ts.map +1 -1
- package/dist/prefix-manager.js +56 -0
- package/dist/prefix-manager.js.map +1 -1
- package/dist/prefix-validator.d.ts +5 -4
- package/dist/prefix-validator.d.ts.map +1 -1
- package/dist/prefix-validator.js +18 -22
- package/dist/prefix-validator.js.map +1 -1
- package/dist/serializable-numeric-identifier-validator.d.ts +26 -0
- package/dist/serializable-numeric-identifier-validator.d.ts.map +1 -1
- package/dist/serializable-numeric-identifier-validator.js +19 -0
- package/dist/serializable-numeric-identifier-validator.js.map +1 -1
- package/dist/variable-measure.d.ts +68 -0
- package/dist/variable-measure.d.ts.map +1 -0
- package/dist/variable-measure.js +210 -0
- package/dist/variable-measure.js.map +1 -0
- package/dist/verified-by-gs1.d.ts +22 -0
- package/dist/verified-by-gs1.d.ts.map +1 -0
- package/dist/verified-by-gs1.js +46 -0
- package/dist/verified-by-gs1.js.map +1 -0
- package/package.json +8 -8
- package/src/gcp-length-cache.ts +117 -0
- package/src/gcp-length-data.ts +68 -0
- package/src/gcp-length.ts +418 -0
- package/src/gtin-creator.ts +1 -117
- package/src/gtin-validator.ts +42 -173
- package/src/identifier-validator.ts +2 -5
- package/src/index.ts +7 -1
- package/src/locale/en/locale-resources.ts +3 -2
- package/src/locale/fr/locale-resources.ts +3 -2
- package/src/locale/i18n.ts +2 -5
- package/src/non-numeric-identifier-validator.ts +1 -1
- package/src/numeric-identifier-validator.ts +2 -2
- package/src/prefix-manager.ts +65 -0
- package/src/prefix-validator.ts +19 -23
- package/src/serializable-numeric-identifier-validator.ts +36 -0
- package/src/variable-measure.ts +268 -0
- package/src/verified-by-gs1.ts +54 -0
- package/test/creator.test.ts +5 -5
- package/test/data/gcpprefixformatlist-1.json +662625 -0
- package/test/data/gcpprefixformatlist-2.json +735431 -0
- package/test/gcp-length.test.ts +405 -0
- package/test/gtin-creator.ts +4 -4
- package/test/gtin-validator.test.ts +205 -113
- package/test/gtin-validator.ts +30 -0
- package/test/identifier-creator.ts +6 -6
- package/test/non-numeric-identifier-creator.ts +0 -8
- package/test/non-serializable-numeric-identifier-creator.ts +4 -54
- package/test/numeric-identifier-creator.ts +4 -4
- package/test/prefix-manager.test.ts +5 -5
- package/test/serializable-numeric-identifier-creator.ts +32 -19
- package/test/validator.test.ts +6 -6
- package/test/variable-measure-rcn.test.ts +63 -68
- package/test/verified-by-gs1.test.ts +55 -0
- package/tsconfig-src.json +7 -1
- package/tsconfig-src.tsbuildinfo +1 -0
- package/tsconfig-tsup.json +7 -0
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
import { omit } from "@aidc-toolkit/core";
|
|
2
|
+
import type { GCPLengthCache } from "./gcp-length-cache.js";
|
|
3
|
+
import type { GCPLengthData, GCPLengthHeader } from "./gcp-length-data.js";
|
|
4
|
+
import { GTINLengths } from "./gtin-length.js";
|
|
5
|
+
import { GTINValidator } from "./gtin-validator.js";
|
|
6
|
+
import { type IdentifierType, IdentifierTypes } from "./identifier-type.js";
|
|
7
|
+
import { IdentifierValidators, isNumericIdentifierValidator } from "./identifier-validators.js";
|
|
8
|
+
import { LeaderTypes } from "./leader-type.js";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* GS1 Company Prefix length JSON source file format.
|
|
12
|
+
*/
|
|
13
|
+
interface GCPLengthJSON {
|
|
14
|
+
/**
|
|
15
|
+
* Disclaimer.
|
|
16
|
+
*/
|
|
17
|
+
_disclaimer: string[];
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Format list.
|
|
21
|
+
*/
|
|
22
|
+
GCPPrefixFormatList: {
|
|
23
|
+
/**
|
|
24
|
+
* ISO data/time the table was last updated.
|
|
25
|
+
*/
|
|
26
|
+
date: string;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Entries.
|
|
30
|
+
*/
|
|
31
|
+
entry: Array<{
|
|
32
|
+
/**
|
|
33
|
+
* Identification key prefix start.
|
|
34
|
+
*/
|
|
35
|
+
prefix: string;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Length of GS1 Company Prefix.
|
|
39
|
+
*/
|
|
40
|
+
gcpLength: number;
|
|
41
|
+
}>;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Leaf of GS1 Company Prefix length tree.
|
|
47
|
+
*/
|
|
48
|
+
export interface Leaf {
|
|
49
|
+
length: number;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Branch of GS1 Company Prefix length tree.
|
|
54
|
+
*/
|
|
55
|
+
export interface Branch {
|
|
56
|
+
childNodes: Array<Node | undefined>;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Root of GS1 Company Prefix length tree.
|
|
61
|
+
*/
|
|
62
|
+
export interface Root extends GCPLengthHeader, Branch {
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Node in GS1 Company Prefix length tree.
|
|
67
|
+
*/
|
|
68
|
+
export type Node = Root | Branch | Leaf;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Determine if a node is a leaf.
|
|
72
|
+
*
|
|
73
|
+
* @param node
|
|
74
|
+
* Node.
|
|
75
|
+
*
|
|
76
|
+
* @returns
|
|
77
|
+
* True if node is a leaf.
|
|
78
|
+
*/
|
|
79
|
+
export function isLeaf(node: Node): node is Leaf {
|
|
80
|
+
return "length" in node;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Entry in binary data to indicate that child node is undefined.
|
|
85
|
+
*/
|
|
86
|
+
const BINARY_UNDEFINED = 0x0F;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Entry in binary data to indicate that child node is a branch.
|
|
90
|
+
*/
|
|
91
|
+
const BINARY_BRANCH = 0x0E;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Build the GS1 Company Prefix length tree from a binary data array.
|
|
95
|
+
*
|
|
96
|
+
* @param binaryData
|
|
97
|
+
* Binary data array.
|
|
98
|
+
*
|
|
99
|
+
* @param childNodes
|
|
100
|
+
* Child nodes array to fill.
|
|
101
|
+
*
|
|
102
|
+
* @param startIndex
|
|
103
|
+
* Start index into binary data array.
|
|
104
|
+
*
|
|
105
|
+
* @returns
|
|
106
|
+
* End index into binary data array.
|
|
107
|
+
*/
|
|
108
|
+
function fromBinary(binaryData: Uint8Array, childNodes: Array<Node | undefined>, startIndex: number): number {
|
|
109
|
+
let endIndex = startIndex;
|
|
110
|
+
|
|
111
|
+
const decompressedLengths = new Array<number>(10);
|
|
112
|
+
|
|
113
|
+
// Decompress lengths for the child nodes.
|
|
114
|
+
for (let childNodeIndex = 0; childNodeIndex < 10; childNodeIndex += 2) {
|
|
115
|
+
const byte = binaryData[endIndex++];
|
|
116
|
+
|
|
117
|
+
decompressedLengths[childNodeIndex] = byte >> 4;
|
|
118
|
+
decompressedLengths[childNodeIndex + 1] = byte & 0x0F;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
for (let childNodeIndex = 0; childNodeIndex < 10; childNodeIndex++) {
|
|
122
|
+
const length = decompressedLengths[childNodeIndex];
|
|
123
|
+
|
|
124
|
+
if (length !== BINARY_UNDEFINED) {
|
|
125
|
+
let childNode: Node;
|
|
126
|
+
|
|
127
|
+
if (length === BINARY_BRANCH) {
|
|
128
|
+
childNode = {
|
|
129
|
+
childNodes: []
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
endIndex = fromBinary(binaryData, childNode.childNodes, endIndex);
|
|
133
|
+
} else {
|
|
134
|
+
childNode = {
|
|
135
|
+
length
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// eslint-disable-next-line no-param-reassign -- Purpose is to build tree.
|
|
140
|
+
childNodes[childNodeIndex] = childNode;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return endIndex;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Add an entry to the tree by recursively walking the tree to a leaf.
|
|
149
|
+
*
|
|
150
|
+
* @param branch
|
|
151
|
+
* Current branch.
|
|
152
|
+
*
|
|
153
|
+
* @param prefix
|
|
154
|
+
* Remainder of current prefix.
|
|
155
|
+
*
|
|
156
|
+
* @param length
|
|
157
|
+
* Current prefix length.
|
|
158
|
+
*
|
|
159
|
+
* @returns
|
|
160
|
+
* Number of branches added; used to determine size of binary array.
|
|
161
|
+
*/
|
|
162
|
+
function addEntry(branch: Branch, prefix: string, length: number): number {
|
|
163
|
+
const digit = Number(prefix.charAt(0));
|
|
164
|
+
|
|
165
|
+
const existingChildNode = branch.childNodes[digit];
|
|
166
|
+
|
|
167
|
+
let branchesAdded = 0;
|
|
168
|
+
|
|
169
|
+
if (prefix.length !== 1) {
|
|
170
|
+
let childBranch: Branch;
|
|
171
|
+
|
|
172
|
+
if (existingChildNode === undefined) {
|
|
173
|
+
childBranch = {
|
|
174
|
+
childNodes: []
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
branchesAdded++;
|
|
178
|
+
} else {
|
|
179
|
+
if (isLeaf(existingChildNode)) {
|
|
180
|
+
// File format error or application bug; localization not necessary.
|
|
181
|
+
throw new Error("Overlapping entry");
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
childBranch = existingChildNode;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// eslint-disable-next-line no-param-reassign -- Purpose is to build tree.
|
|
188
|
+
branch.childNodes[digit] = childBranch;
|
|
189
|
+
|
|
190
|
+
// Continue with remainder of prefix.
|
|
191
|
+
branchesAdded += addEntry(childBranch, prefix.substring(1), length);
|
|
192
|
+
} else {
|
|
193
|
+
if (existingChildNode !== undefined) {
|
|
194
|
+
// File format error or application bug; localization not necessary.
|
|
195
|
+
throw new Error("Duplicate entry");
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// eslint-disable-next-line no-param-reassign -- Purpose is to build tree.
|
|
199
|
+
branch.childNodes[digit] = {
|
|
200
|
+
length
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return branchesAdded;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Get the length of a child node if defined and it's a leaf, otherwise mark it with 0x0E if defined (branch) or 0x0F
|
|
209
|
+
* (no branch) if not.
|
|
210
|
+
*
|
|
211
|
+
* @param childNode
|
|
212
|
+
* Child node.
|
|
213
|
+
*
|
|
214
|
+
* @returns
|
|
215
|
+
* Child node length, 0x0E (branch), or 0x0F (undefined).
|
|
216
|
+
*/
|
|
217
|
+
function childNodeValue(childNode: Node | undefined): number {
|
|
218
|
+
return childNode !== undefined ? isLeaf(childNode) ? childNode.length : BINARY_BRANCH : BINARY_UNDEFINED;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Add a branch to a binary data array.
|
|
223
|
+
*
|
|
224
|
+
* @param binaryData
|
|
225
|
+
* Binary data array.
|
|
226
|
+
*
|
|
227
|
+
* @param branch
|
|
228
|
+
* Branch.
|
|
229
|
+
*
|
|
230
|
+
* @param startIndex
|
|
231
|
+
* Start index into binary data array.
|
|
232
|
+
*
|
|
233
|
+
* @returns
|
|
234
|
+
* End index into binary data array.
|
|
235
|
+
*/
|
|
236
|
+
function toBinary(binaryData: Uint8Array, branch: Branch, startIndex: number): number {
|
|
237
|
+
let endIndex = startIndex;
|
|
238
|
+
|
|
239
|
+
const childNodes = branch.childNodes;
|
|
240
|
+
|
|
241
|
+
// Add length or non-leaf indicators, compressing 10 nibbles into 5 bytes.
|
|
242
|
+
for (let childNodeIndex = 0; childNodeIndex < 10; childNodeIndex += 2) {
|
|
243
|
+
// eslint-disable-next-line no-param-reassign -- Purpose is to build array.
|
|
244
|
+
binaryData[endIndex++] = (childNodeValue(childNodes[childNodeIndex]) << 4) | childNodeValue(childNodes[childNodeIndex + 1]);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// Process child nodes.
|
|
248
|
+
for (const childNode of childNodes) {
|
|
249
|
+
if (childNode !== undefined && !isLeaf(childNode)) {
|
|
250
|
+
endIndex = toBinary(binaryData, childNode, endIndex);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return endIndex;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Add days to a date.
|
|
259
|
+
*
|
|
260
|
+
* @param date
|
|
261
|
+
* Date.
|
|
262
|
+
*
|
|
263
|
+
* @param days
|
|
264
|
+
* Days.
|
|
265
|
+
*
|
|
266
|
+
* @returns
|
|
267
|
+
* New date.
|
|
268
|
+
*/
|
|
269
|
+
function addDays(date: Date, days: number): Date {
|
|
270
|
+
const futureDate = new Date(date);
|
|
271
|
+
futureDate.setDate(futureDate.getDate() + days);
|
|
272
|
+
|
|
273
|
+
return futureDate;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Load the GS1 Company Prefix length data.
|
|
278
|
+
*
|
|
279
|
+
* @param gcpLengthCache
|
|
280
|
+
* GS1 Company Prefix length cache.
|
|
281
|
+
*
|
|
282
|
+
* @returns
|
|
283
|
+
* Root of tree.
|
|
284
|
+
*/
|
|
285
|
+
export async function loadData(gcpLengthCache: GCPLengthCache): Promise<Root | undefined> {
|
|
286
|
+
let root: Root | undefined = undefined;
|
|
287
|
+
|
|
288
|
+
let nextCheckDateTime = await gcpLengthCache.nextCheckDateTime;
|
|
289
|
+
let cacheDateTime = await gcpLengthCache.cacheDateTime;
|
|
290
|
+
|
|
291
|
+
const now = new Date();
|
|
292
|
+
const tomorrow = addDays(now, 1);
|
|
293
|
+
|
|
294
|
+
if (nextCheckDateTime === undefined || nextCheckDateTime.getTime() <= now.getTime()) {
|
|
295
|
+
const sourceDateTime = await gcpLengthCache.sourceDateTime;
|
|
296
|
+
|
|
297
|
+
if (cacheDateTime === undefined || cacheDateTime.getTime() < sourceDateTime.getTime()) {
|
|
298
|
+
const sourceData = await gcpLengthCache.sourceData;
|
|
299
|
+
|
|
300
|
+
let cacheData: GCPLengthData;
|
|
301
|
+
|
|
302
|
+
if (typeof sourceData !== "string") {
|
|
303
|
+
root = {
|
|
304
|
+
dateTime: sourceData.dateTime,
|
|
305
|
+
disclaimer: sourceData.disclaimer,
|
|
306
|
+
childNodes: []
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
fromBinary(sourceData.data, root.childNodes, 0);
|
|
310
|
+
|
|
311
|
+
cacheData = sourceData;
|
|
312
|
+
} else {
|
|
313
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- File format is known.
|
|
314
|
+
const gcpLength = JSON.parse(sourceData) as GCPLengthJSON;
|
|
315
|
+
|
|
316
|
+
root = {
|
|
317
|
+
dateTime: new Date(gcpLength.GCPPrefixFormatList.date),
|
|
318
|
+
// Join disclaimer as a single string.
|
|
319
|
+
disclaimer: `${gcpLength._disclaimer.reduce<string[]>((lines, line) => {
|
|
320
|
+
if (lines.length === 0 || lines[lines.length - 1] === "" || line === "") {
|
|
321
|
+
lines.push(line);
|
|
322
|
+
} else {
|
|
323
|
+
// Lines are part of the same paragraph.
|
|
324
|
+
lines.push(`${lines.pop()} ${line}`);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
return lines;
|
|
328
|
+
}, []).join("\n")}\n`,
|
|
329
|
+
childNodes: []
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
let branchesAdded = 1;
|
|
333
|
+
|
|
334
|
+
for (const entry of gcpLength.GCPPrefixFormatList.entry) {
|
|
335
|
+
branchesAdded += addEntry(root, entry.prefix, entry.gcpLength);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// Each branch has ten (some possibly undefined) entries, two per byte.
|
|
339
|
+
const data = new Uint8Array(branchesAdded * 5);
|
|
340
|
+
|
|
341
|
+
toBinary(data, root, 0);
|
|
342
|
+
|
|
343
|
+
cacheData = {
|
|
344
|
+
...omit(root, "childNodes"),
|
|
345
|
+
data
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// Next check date/time is a week from source date/time or tomorrow, whichever is later.
|
|
350
|
+
nextCheckDateTime = addDays(sourceDateTime, 7);
|
|
351
|
+
if (nextCheckDateTime.getTime() < tomorrow.getTime()) {
|
|
352
|
+
nextCheckDateTime = tomorrow;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
cacheDateTime = sourceDateTime;
|
|
356
|
+
|
|
357
|
+
await gcpLengthCache.update(nextCheckDateTime, cacheDateTime, cacheData);
|
|
358
|
+
} else {
|
|
359
|
+
// Next check date/time is tomorrow.
|
|
360
|
+
await gcpLengthCache.update(tomorrow);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// Root is undefined if cache data is still valid or retrying after prior failure.
|
|
365
|
+
if (root === undefined && cacheDateTime !== undefined) {
|
|
366
|
+
const cacheData = await gcpLengthCache.cacheData;
|
|
367
|
+
|
|
368
|
+
root = {
|
|
369
|
+
...omit(cacheData, "data"),
|
|
370
|
+
childNodes: []
|
|
371
|
+
};
|
|
372
|
+
|
|
373
|
+
fromBinary(cacheData.data, root.childNodes, 0);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
return root;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Get the length of a GS1 Company Prefix for an identifier.
|
|
381
|
+
*
|
|
382
|
+
* @param root
|
|
383
|
+
* Root of tree.
|
|
384
|
+
*
|
|
385
|
+
* @param identifierType
|
|
386
|
+
* Identifier type.
|
|
387
|
+
*
|
|
388
|
+
* @param identifier
|
|
389
|
+
* Identifier.
|
|
390
|
+
*
|
|
391
|
+
* @returns
|
|
392
|
+
* Length of GS1 Company Prefix, 0 if not a GS1 Company Prefix, or -1 if not found.
|
|
393
|
+
*/
|
|
394
|
+
export function getFor(root: Root, identifierType: IdentifierType, identifier: string): number {
|
|
395
|
+
let identifierPrefix: string;
|
|
396
|
+
|
|
397
|
+
if (identifierType === IdentifierTypes.GTIN) {
|
|
398
|
+
// Normalize the GTIN, pad to 14 digits, and extract the identifier prefix at the first character.
|
|
399
|
+
identifierPrefix = GTINValidator.normalize(identifier).padStart(GTINLengths.GTIN14, "0").substring(1);
|
|
400
|
+
} else {
|
|
401
|
+
const identifierValidator = IdentifierValidators[identifierType];
|
|
402
|
+
|
|
403
|
+
identifierValidator.validate(identifier);
|
|
404
|
+
|
|
405
|
+
identifierPrefix = !isNumericIdentifierValidator(identifierValidator) || identifierValidator.leaderType !== LeaderTypes.ExtensionDigit ? identifier : identifier.substring(1);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
let node: Node | undefined = root;
|
|
409
|
+
|
|
410
|
+
let digitIndex = 0;
|
|
411
|
+
|
|
412
|
+
// Traverse tree until exhausted or at a leaf.
|
|
413
|
+
while (node !== undefined && !isLeaf(node)) {
|
|
414
|
+
node = node.childNodes[Number(identifierPrefix.charAt(digitIndex++))];
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
return node?.length ?? -1;
|
|
418
|
+
}
|
package/src/gtin-creator.ts
CHANGED
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
type TransformerOutput
|
|
7
7
|
} from "@aidc-toolkit/utility";
|
|
8
8
|
import { MixinAbstractNumericIdentifierCreator } from "./abstract-numeric-identifier-creator.js";
|
|
9
|
-
import { checkDigit
|
|
9
|
+
import { checkDigit } from "./check.js";
|
|
10
10
|
import { type GTINBaseLength, GTINLengths } from "./gtin-length.js";
|
|
11
11
|
import type { GTINType } from "./gtin-type.js";
|
|
12
12
|
import { GTINValidator } from "./gtin-validator.js";
|
|
@@ -76,120 +76,4 @@ export class GTINCreator extends MixinAbstractNumericIdentifierCreator<
|
|
|
76
76
|
return partialIdentifier + checkDigit(partialIdentifier);
|
|
77
77
|
});
|
|
78
78
|
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Create a Restricted Circulation Number (RCN) using a variable measure trade item format. See {@linkcode
|
|
82
|
-
* GTINValidator.parseVariableMeasureRCN} for format details.
|
|
83
|
-
*
|
|
84
|
-
* @param format
|
|
85
|
-
* Format.
|
|
86
|
-
*
|
|
87
|
-
* @param itemReference
|
|
88
|
-
* Item reference.
|
|
89
|
-
*
|
|
90
|
-
* @param priceOrWeight
|
|
91
|
-
* Price or weight (whole number only).
|
|
92
|
-
*
|
|
93
|
-
* @returns
|
|
94
|
-
* RCN-12 or RCN-13.
|
|
95
|
-
*/
|
|
96
|
-
static createVariableMeasureRCN(format: string, itemReference: number, priceOrWeight: number): string {
|
|
97
|
-
const formatLength = format.length;
|
|
98
|
-
|
|
99
|
-
let validFormat = formatLength === 12 || formatLength === 13;
|
|
100
|
-
|
|
101
|
-
let rcnPrefix = "";
|
|
102
|
-
|
|
103
|
-
let buildingItemReference = false;
|
|
104
|
-
let itemReferenceString = "";
|
|
105
|
-
let itemReferenceLength = 0;
|
|
106
|
-
|
|
107
|
-
let buildingPriceOrWeight = false;
|
|
108
|
-
let priceOrWeightString = "";
|
|
109
|
-
let priceOrWeightLength = 0;
|
|
110
|
-
|
|
111
|
-
let calculatePriceOrWeightCheckDigit = false;
|
|
112
|
-
|
|
113
|
-
// RCN may be built in almost any order, so defer to builders that will be in ordered array.
|
|
114
|
-
const rcnPrefixBuilder = (partialRCN: string): string => partialRCN + rcnPrefix;
|
|
115
|
-
const itemReferenceBuilder = (partialRCN: string): string => partialRCN + itemReferenceString;
|
|
116
|
-
const priceOrWeightBuilder = (partialRCN: string): string => partialRCN + priceOrWeightString;
|
|
117
|
-
const priceOrWeightCheckDigitBuilder = (partialRCN: string): string => partialRCN + priceOrWeightCheckDigit(priceOrWeightString);
|
|
118
|
-
const checkDigitBuilder = (partialRCN: string): string => partialRCN + checkDigit(partialRCN);
|
|
119
|
-
|
|
120
|
-
const rcnBuilders = [rcnPrefixBuilder];
|
|
121
|
-
|
|
122
|
-
for (let index = 0; validFormat && index < formatLength; index++) {
|
|
123
|
-
const formatChar = format.charAt(index);
|
|
124
|
-
|
|
125
|
-
if (index === 0) {
|
|
126
|
-
validFormat = formatChar === "2";
|
|
127
|
-
rcnPrefix = formatChar;
|
|
128
|
-
} else if (formatLength === 13 && index === 1) {
|
|
129
|
-
validFormat = NUMERIC_CREATOR.characterIndex(formatChar) !== undefined;
|
|
130
|
-
rcnPrefix += formatChar;
|
|
131
|
-
} else if (index === formatLength - 1) {
|
|
132
|
-
validFormat = formatChar === "C";
|
|
133
|
-
} else {
|
|
134
|
-
switch (formatChar) {
|
|
135
|
-
case "I":
|
|
136
|
-
if (!buildingItemReference) {
|
|
137
|
-
// Item reference can't appear more than once.
|
|
138
|
-
validFormat = itemReferenceLength === 0;
|
|
139
|
-
|
|
140
|
-
buildingItemReference = true;
|
|
141
|
-
buildingPriceOrWeight = false;
|
|
142
|
-
|
|
143
|
-
rcnBuilders.push(itemReferenceBuilder);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
itemReferenceLength++;
|
|
147
|
-
break;
|
|
148
|
-
|
|
149
|
-
case "P":
|
|
150
|
-
if (!buildingPriceOrWeight) {
|
|
151
|
-
// Price or weight can't appear more than once.
|
|
152
|
-
validFormat = priceOrWeightLength === 0;
|
|
153
|
-
|
|
154
|
-
buildingPriceOrWeight = true;
|
|
155
|
-
buildingItemReference = false;
|
|
156
|
-
|
|
157
|
-
rcnBuilders.push(priceOrWeightBuilder);
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
priceOrWeightLength++;
|
|
161
|
-
break;
|
|
162
|
-
|
|
163
|
-
case "V":
|
|
164
|
-
// Price or weight check digit can't appear more than once.
|
|
165
|
-
validFormat = !calculatePriceOrWeightCheckDigit;
|
|
166
|
-
|
|
167
|
-
buildingItemReference = false;
|
|
168
|
-
buildingPriceOrWeight = false;
|
|
169
|
-
|
|
170
|
-
calculatePriceOrWeightCheckDigit = true;
|
|
171
|
-
|
|
172
|
-
rcnBuilders.push(priceOrWeightCheckDigitBuilder);
|
|
173
|
-
break;
|
|
174
|
-
|
|
175
|
-
default:
|
|
176
|
-
validFormat = false;
|
|
177
|
-
break;
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
validFormat &&= itemReferenceLength !== 0 && priceOrWeightLength !== 0;
|
|
183
|
-
|
|
184
|
-
if (!validFormat) {
|
|
185
|
-
throw new RangeError(i18nextGS1.t("Identifier.invalidVariableMeasureRCNFormat"));
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
itemReferenceString = NUMERIC_CREATOR.create(itemReferenceLength, itemReference);
|
|
189
|
-
priceOrWeightString = NUMERIC_CREATOR.create(priceOrWeightLength, priceOrWeight);
|
|
190
|
-
|
|
191
|
-
rcnBuilders.push(checkDigitBuilder);
|
|
192
|
-
|
|
193
|
-
return rcnBuilders.reduce((partialRCN, rcnBuilder) => rcnBuilder(partialRCN), "");
|
|
194
|
-
}
|
|
195
79
|
}
|