@aidc-toolkit/gs1 1.0.39-beta → 1.0.41-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 (39) hide show
  1. package/README.md +84 -101
  2. package/dist/gcp-length-cache.d.ts +6 -1
  3. package/dist/gcp-length-cache.d.ts.map +1 -1
  4. package/dist/gcp-length-cache.js +7 -2
  5. package/dist/gcp-length-cache.js.map +1 -1
  6. package/dist/gcp-length-tree.d.ts +33 -0
  7. package/dist/gcp-length-tree.d.ts.map +1 -0
  8. package/dist/gcp-length-tree.js +13 -0
  9. package/dist/gcp-length-tree.js.map +1 -0
  10. package/dist/gcp-length.d.ts +63 -49
  11. package/dist/gcp-length.d.ts.map +1 -1
  12. package/dist/gcp-length.js +319 -262
  13. package/dist/gcp-length.js.map +1 -1
  14. package/dist/index.d.ts +2 -1
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +2 -1
  17. package/dist/index.js.map +1 -1
  18. package/dist/locale/en/locale-resources.d.ts +2 -0
  19. package/dist/locale/en/locale-resources.d.ts.map +1 -1
  20. package/dist/locale/en/locale-resources.js +3 -1
  21. package/dist/locale/en/locale-resources.js.map +1 -1
  22. package/dist/locale/fr/locale-resources.d.ts +2 -0
  23. package/dist/locale/fr/locale-resources.d.ts.map +1 -1
  24. package/dist/locale/fr/locale-resources.js +3 -1
  25. package/dist/locale/fr/locale-resources.js.map +1 -1
  26. package/dist/prefix-manager.d.ts +0 -35
  27. package/dist/prefix-manager.d.ts.map +1 -1
  28. package/dist/prefix-manager.js +0 -56
  29. package/dist/prefix-manager.js.map +1 -1
  30. package/package.json +4 -4
  31. package/src/gcp-length-cache.ts +7 -2
  32. package/src/gcp-length-tree.ts +39 -0
  33. package/src/gcp-length.ts +348 -297
  34. package/src/index.ts +3 -1
  35. package/src/locale/en/locale-resources.ts +3 -1
  36. package/src/locale/fr/locale-resources.ts +3 -1
  37. package/src/prefix-manager.ts +0 -65
  38. package/test/gcp-length.test.ts +39 -31
  39. package/tsconfig-src.tsbuildinfo +1 -1
package/src/index.ts CHANGED
@@ -68,6 +68,8 @@ export * from "./identifier-creators.js";
68
68
 
69
69
  export * from "./variable-measure.js";
70
70
 
71
+ export * from "./prefix-manager.js";
72
+
71
73
  export * from "./gcp-length-data.js";
72
74
  export * from "./gcp-length-cache.js";
73
- export * from "./prefix-manager.js";
75
+ export * from "./gcp-length.js";
@@ -37,7 +37,9 @@ export default {
37
37
  gs1CompanyPrefixCantStartWith000000: "GS1 Company Prefix can't start with \"000000\"",
38
38
  upcCompanyPrefixCantStartWith0000: "U.P.C. Company Prefix can't start with \"0000\"",
39
39
  gs18PrefixCantStartWith0: "GS1-8 Prefix can't start with \"0\"",
40
- identifierTypeNotSupportedByGS18Prefix: "{{identifierType}} not supported by GS1-8 Prefix",
40
+ identifierTypeNotSupportedByGS18Prefix: "{{identifierType}} not supported by GS1-8 Prefix"
41
+ },
42
+ GCPLength: {
41
43
  gs1CompanyPrefixLengthDataFileNotFound: "GS1 Company Prefix length data \"{{key}}\" not found",
42
44
  gs1CompanyPrefixLengthDataNotLoaded: "GS1 Company Prefix length data not loaded"
43
45
  }
@@ -37,7 +37,9 @@ export default {
37
37
  gs1CompanyPrefixCantStartWith000000: "Le préfixe de l'entreprise GS1 ne peut pas commencer par \"000000\"",
38
38
  upcCompanyPrefixCantStartWith0000: "Le préfixe de l'entreprise U.P.C. ne peut pas commencer par \"0000\"",
39
39
  gs18PrefixCantStartWith0: "Le préfixe GS1-8 ne peut pas commencer par \"0\"",
40
- identifierTypeNotSupportedByGS18Prefix: "{{identifierType}} non pris en charge par le préfixe GS1-8",
40
+ identifierTypeNotSupportedByGS18Prefix: "{{identifierType}} non pris en charge par le préfixe GS1-8"
41
+ },
42
+ GCPLength: {
41
43
  gs1CompanyPrefixLengthDataFileNotFound: "Données de longueur du préfixe d'entreprise GS1 «{{key}}» introuvables",
42
44
  gs1CompanyPrefixLengthDataNotLoaded: "Les données relatives à la longueur du préfixe d'entreprise GS1 n'ont pas été chargées"
43
45
  }
@@ -1,4 +1,3 @@
1
- import * as GCPLength from "./gcp-length.js";
2
1
  import type { GTINCreator } from "./gtin-creator.js";
3
2
  import { GTIN_BASE_TYPES } from "./gtin-length.js";
4
3
  import type { GTINType } from "./gtin-type.js";
@@ -18,7 +17,6 @@ import type { PrefixProvider } from "./prefix-provider.js";
18
17
  import { type PrefixType, PrefixTypes } from "./prefix-type.js";
19
18
  import { PrefixValidator } from "./prefix-validator.js";
20
19
  import type { SerializableNumericIdentifierCreator } from "./serializable-numeric-identifier-creator.js";
21
- import type { GCPLengthCache } from "./gcp-length-cache.js";
22
20
 
23
21
  /**
24
22
  * Prefix manager. This is the core class for identifier creation.
@@ -101,11 +99,6 @@ export class PrefixManager implements PrefixProvider {
101
99
  */
102
100
  readonly #identifierCreators: Partial<IdentifierCreatorsRecord> = {};
103
101
 
104
- /**
105
- * GS1 Company Prefix length root.
106
- */
107
- static #gcpLengthRoot: GCPLength.Root | undefined = undefined;
108
-
109
102
  /**
110
103
  * Constructor.
111
104
  *
@@ -357,62 +350,4 @@ export class PrefixManager implements PrefixProvider {
357
350
  get gmnCreator(): NonNumericIdentifierCreator {
358
351
  return this.getIdentifierCreator(IdentifierTypes.GMN);
359
352
  }
360
-
361
- /**
362
- * Load GS1 Company Prefix length data.
363
- *
364
- * @param gcpLengthCache
365
- * GS1 Company Prefix length cache.
366
- */
367
- static async loadGCPLengthData(gcpLengthCache: GCPLengthCache): Promise<void> {
368
- PrefixManager.#gcpLengthRoot = await GCPLength.loadData(gcpLengthCache);
369
- }
370
-
371
- /**
372
- * Get the length of a GS1 Company Prefix for an identifier.
373
- *
374
- * @param identifierType
375
- * Identifier type.
376
- *
377
- * @param identifier
378
- * Identifier.
379
- *
380
- * @returns
381
- * Length of GS1 Company Prefix, 0 if not a GS1 Company Prefix, or -1 if not found.
382
- */
383
- static gcpLength(identifierType: IdentifierType, identifier: string): number {
384
- if (PrefixManager.#gcpLengthRoot === undefined) {
385
- throw new RangeError(i18nextGS1.t("Prefix.gs1CompanyPrefixLengthDataNotLoaded"));
386
- }
387
-
388
- return GCPLength.getFor(PrefixManager.#gcpLengthRoot, identifierType, identifier);
389
- }
390
-
391
- /**
392
- * Get the date/time the GS1 Company Prefix length data was last updated.
393
- *
394
- * @returns
395
- * Date/time the GS1 Company Prefix length data was last updated.
396
- */
397
- static gcpLengthDateTime(): Date {
398
- if (PrefixManager.#gcpLengthRoot === undefined) {
399
- throw new RangeError(i18nextGS1.t("Prefix.gs1CompanyPrefixLengthDataNotLoaded"));
400
- }
401
-
402
- return PrefixManager.#gcpLengthRoot.dateTime;
403
- }
404
-
405
- /**
406
- * Get the disclaimer for the GS1 Company Prefix length data.
407
- *
408
- * @returns
409
- * Disclaimer for the GS1 Company Prefix length data.
410
- */
411
- static gcpLengthDisclaimer(): string {
412
- if (PrefixManager.#gcpLengthRoot === undefined) {
413
- throw new RangeError(i18nextGS1.t("Prefix.gs1CompanyPrefixLengthDataNotLoaded"));
414
- }
415
-
416
- return PrefixManager.#gcpLengthRoot.disclaimer;
417
- }
418
353
  }
@@ -2,8 +2,9 @@ import { type AppDataStorage, LocalAppDataStorage } from "@aidc-toolkit/core";
2
2
  import * as fs from "node:fs";
3
3
  import * as path from "node:path";
4
4
  import { afterAll, beforeAll, describe, expect, test } from "vitest";
5
- import * as GCPLength from "../src/gcp-length.js";
5
+ import * as GCPLengthTree from "../src/gcp-length-tree.js";
6
6
  import {
7
+ GCPLength,
7
8
  GCPLengthCache,
8
9
  type GCPLengthData,
9
10
  type GCPLengthHeader,
@@ -102,23 +103,23 @@ const BINARY_2_HEADER_PATH = path.resolve(DATA_DIRECTORY, "gcp-length-header-2.j
102
103
  const BINARY_2_DATA_PATH = path.resolve(DATA_DIRECTORY, "gcp-length-data-2.bin");
103
104
 
104
105
  describe("GS1 Company Prefix length", () => {
105
- function verifyEqual(prefix: string, node1: GCPLength.Node | undefined, node2: GCPLength.Node | undefined): void {
106
+ function verifyEqual(prefix: string, node1: GCPLengthTree.Node | undefined, node2: GCPLengthTree.Node | undefined): void {
106
107
  if (node1 !== undefined) {
107
108
  if (node2 === undefined) {
108
109
  throw new RangeError(`Prefix ${prefix}: node1 defined, node2 undefined`);
109
110
  }
110
111
 
111
- if (GCPLength.isLeaf(node1)) {
112
- if (!GCPLength.isLeaf(node2)) {
112
+ if (GCPLengthTree.isLeaf(node1)) {
113
+ if (!GCPLengthTree.isLeaf(node2)) {
113
114
  throw new RangeError(`Prefix ${prefix}: node1 is leaf, node2 is branch`);
114
115
  }
115
116
 
116
117
  if (node1.length !== node2.length) {
117
118
  throw new RangeError(`Prefix ${prefix}: node1.length = ${node1.length}, node2.length = ${node2.length}`);
118
119
  }
119
- } else if (!GCPLength.isLeaf(node2)) {
120
+ } else if (!GCPLengthTree.isLeaf(node2)) {
120
121
  for (let index = 0; index < 10; index++) {
121
- verifyEqual(`${prefix}${index}`, node1.childNodes[index], (node2 as GCPLength.Branch).childNodes[index]);
122
+ verifyEqual(`${prefix}${index}`, node1.childNodes[index], node2.childNodes[index]);
122
123
  }
123
124
  } else {
124
125
  throw new RangeError(`Prefix ${prefix}: node1 is branch, node2 is leaf`);
@@ -173,13 +174,15 @@ describe("GS1 Company Prefix length", () => {
173
174
  const gcpLengthCacheBinary1Source = new GCPLengthCacheBinarySource(appDataStorage, 1);
174
175
  const gcpLengthCacheBinary2Source = new GCPLengthCacheBinarySource(appDataStorage, 2);
175
176
 
176
- let root1: GCPLength.Root | undefined;
177
- let root2: GCPLength.Root | undefined;
177
+ let root1: GCPLengthTree.Root | undefined;
178
+ let root2: GCPLengthTree.Root | undefined;
178
179
  let nextCheckDateTime: Date | undefined;
179
180
 
181
+ const gcpLengthJSON1Source = new GCPLength(gcpLengthCacheJSON1Source);
182
+
180
183
  // Binary not available, JSON 1 available.
181
- await expect(GCPLength.loadData(gcpLengthCacheJSON1Source).then((root) => {
182
- root1 = root;
184
+ await expect(gcpLengthJSON1Source.load().then(() => {
185
+ root1 = gcpLengthJSON1Source.root;
183
186
  })).resolves.not.toThrowError(RangeError);
184
187
 
185
188
  nextCheckDateTime = await gcpLengthCacheJSON1Source.nextCheckDateTime;
@@ -191,9 +194,11 @@ describe("GS1 Company Prefix length", () => {
191
194
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- Known to be defined.
192
195
  expect(new Date().getTime() + 24 * 60 * 60 * 1000 - nextCheckDateTime!.getTime()).toBeLessThan(10000);
193
196
 
197
+ const gcpLengthBinary1Source = new GCPLength(gcpLengthCacheBinary1Source);
198
+
194
199
  // Binary available, binary 1 not available but not in next check date/time window.
195
- await expect(GCPLength.loadData(gcpLengthCacheBinary1Source).then((root) => {
196
- root2 = root;
200
+ await expect(gcpLengthBinary1Source.load().then(() => {
201
+ root2 = gcpLengthBinary1Source.root;
197
202
  })).resolves.not.toThrowError(RangeError);
198
203
 
199
204
  expect(root2).not.toBeUndefined();
@@ -206,9 +211,11 @@ describe("GS1 Company Prefix length", () => {
206
211
  saveBinary(1);
207
212
  rm(NEXT_CHECK_DATE_TIME_PATH);
208
213
 
214
+ const gcpLengthJSON2Source = new GCPLength(gcpLengthCacheJSON2Source);
215
+
209
216
  // Binary not available, JSON 2 available.
210
- await expect(GCPLength.loadData(gcpLengthCacheJSON2Source).then((root) => {
211
- root2 = root;
217
+ await expect(gcpLengthJSON2Source.load().then(() => {
218
+ root2 = gcpLengthJSON2Source.root;
212
219
  })).resolves.not.toThrowError(RangeError);
213
220
 
214
221
  nextCheckDateTime = await gcpLengthCacheJSON2Source.nextCheckDateTime;
@@ -219,9 +226,11 @@ describe("GS1 Company Prefix length", () => {
219
226
  saveBinary(2);
220
227
  restoreBinary(1);
221
228
 
229
+ const gcpLengthBinary2Source = new GCPLength(gcpLengthCacheBinary2Source);
230
+
222
231
  // Binary available, binary 2 available and more recent but not in next check date/time window.
223
- await expect(GCPLength.loadData(gcpLengthCacheBinary2Source).then((root) => {
224
- root2 = root;
232
+ await expect(gcpLengthBinary2Source.load().then(() => {
233
+ root2 = gcpLengthBinary2Source.root;
225
234
  })).resolves.not.toThrowError(RangeError);
226
235
 
227
236
  // No change.
@@ -232,8 +241,8 @@ describe("GS1 Company Prefix length", () => {
232
241
  await gcpLengthCacheBinary2Source.update(new Date());
233
242
 
234
243
  // Binary 1 available, binary 2 available and more recent and in next check date/time window.
235
- await expect(GCPLength.loadData(gcpLengthCacheBinary2Source).then((root) => {
236
- root2 = root;
244
+ await expect(gcpLengthBinary2Source.load().then(() => {
245
+ root2 = gcpLengthBinary2Source.root;
237
246
  })).resolves.not.toThrowError(RangeError);
238
247
 
239
248
  expect(root2).not.toBeUndefined();
@@ -259,23 +268,20 @@ describe("GS1 Company Prefix length", () => {
259
268
  }
260
269
  }(new (await LocalAppDataStorage)(DATA_DIRECTORY), 2);
261
270
 
262
- const tempRoot = await GCPLength.loadData(gcpLengthCacheJSON2Source);
271
+ const gcpLengthJSON2Source = new GCPLength(gcpLengthCacheJSON2Source);
263
272
 
264
- expect(tempRoot).not.toBeUndefined();
265
-
266
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- Known to be defined.
267
- const root = tempRoot!;
273
+ await expect(gcpLengthJSON2Source.load()).resolves.not.toThrowError(RangeError);
268
274
 
269
275
  function testIdentifiers(prefixManager: PrefixManager, prefixLength: number): void {
270
- expect(GCPLength.getFor(root, IdentifierTypes.GTIN, prefixManager.gtinCreator.create(0, true))).toBe(prefixLength);
271
- expect(GCPLength.getFor(root, IdentifierTypes.GTIN, prefixManager.gtinCreator.createGTIN14("4", 0, true))).toBe(prefixLength);
276
+ expect(gcpLengthJSON2Source.lengthOf(IdentifierTypes.GTIN, prefixManager.gtinCreator.create(0, true))).toBe(prefixLength);
277
+ expect(gcpLengthJSON2Source.lengthOf(IdentifierTypes.GTIN, prefixManager.gtinCreator.createGTIN14("4", 0, true))).toBe(prefixLength);
272
278
 
273
279
  if (prefixManager.prefixType !== PrefixTypes.GS18Prefix) {
274
- expect(GCPLength.getFor(root, IdentifierTypes.GLN, prefixManager.glnCreator.create(0, true))).toBe(prefixLength);
275
- expect(GCPLength.getFor(root, IdentifierTypes.SSCC, prefixManager.ssccCreator.create(0, true))).toBe(prefixLength);
276
- expect(GCPLength.getFor(root, IdentifierTypes.GRAI, prefixManager.graiCreator.create(0, true))).toBe(prefixLength);
277
- expect(GCPLength.getFor(root, IdentifierTypes.GRAI, prefixManager.graiCreator.createSerialized(0, "ABCD1234", true))).toBe(prefixLength);
278
- expect(GCPLength.getFor(root, IdentifierTypes.GIAI, prefixManager.giaiCreator.create("EFGH5678"))).toBe(prefixLength);
280
+ expect(gcpLengthJSON2Source.lengthOf(IdentifierTypes.GLN, prefixManager.glnCreator.create(0, true))).toBe(prefixLength);
281
+ expect(gcpLengthJSON2Source.lengthOf(IdentifierTypes.SSCC, prefixManager.ssccCreator.create(0, true))).toBe(prefixLength);
282
+ expect(gcpLengthJSON2Source.lengthOf(IdentifierTypes.GRAI, prefixManager.graiCreator.create(0, true))).toBe(prefixLength);
283
+ expect(gcpLengthJSON2Source.lengthOf(IdentifierTypes.GRAI, prefixManager.graiCreator.createSerialized(0, "ABCD1234", true))).toBe(prefixLength);
284
+ expect(gcpLengthJSON2Source.lengthOf(IdentifierTypes.GIAI, prefixManager.giaiCreator.create("EFGH5678"))).toBe(prefixLength);
279
285
  }
280
286
  }
281
287
 
@@ -335,7 +341,9 @@ describe("GS1 Company Prefix length", () => {
335
341
  }
336
342
  }(new (await LocalAppDataStorage)(DATA_DIRECTORY));
337
343
 
338
- await expect(GCPLength.loadData(gcpLengthCache)).resolves.not.toThrowError(RangeError);
344
+ const gcpLength = new GCPLength(gcpLengthCache);
345
+
346
+ await expect(gcpLength.load()).resolves.not.toThrowError(RangeError);
339
347
 
340
348
  expect(savedNextCheckDateTime).not.toBeUndefined();
341
349
  expect(savedCacheDateTime).not.toBeUndefined();