@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.
Files changed (101) hide show
  1. package/dist/gcp-length-cache.d.ts +42 -0
  2. package/dist/gcp-length-cache.d.ts.map +1 -0
  3. package/dist/gcp-length-cache.js +96 -0
  4. package/dist/gcp-length-cache.js.map +1 -0
  5. package/dist/gcp-length-data.d.ts +54 -0
  6. package/dist/gcp-length-data.d.ts.map +1 -0
  7. package/dist/gcp-length-data.js +29 -0
  8. package/dist/gcp-length-data.js.map +1 -0
  9. package/dist/gcp-length.d.ts +61 -0
  10. package/dist/gcp-length.d.ts.map +1 -0
  11. package/dist/gcp-length.js +301 -0
  12. package/dist/gcp-length.js.map +1 -0
  13. package/dist/gtin-creator.d.ts +0 -17
  14. package/dist/gtin-creator.d.ts.map +1 -1
  15. package/dist/gtin-creator.js +1 -93
  16. package/dist/gtin-creator.js.map +1 -1
  17. package/dist/gtin-validator.d.ts +1 -46
  18. package/dist/gtin-validator.d.ts.map +1 -1
  19. package/dist/gtin-validator.js +31 -125
  20. package/dist/gtin-validator.js.map +1 -1
  21. package/dist/identifier-validator.d.ts +1 -4
  22. package/dist/identifier-validator.d.ts.map +1 -1
  23. package/dist/identifier-validator.js +2 -5
  24. package/dist/identifier-validator.js.map +1 -1
  25. package/dist/index.d.ts +5 -1
  26. package/dist/index.d.ts.map +1 -1
  27. package/dist/index.js +4 -0
  28. package/dist/index.js.map +1 -1
  29. package/dist/locale/en/locale-resources.d.ts +2 -1
  30. package/dist/locale/en/locale-resources.d.ts.map +1 -1
  31. package/dist/locale/en/locale-resources.js +3 -2
  32. package/dist/locale/en/locale-resources.js.map +1 -1
  33. package/dist/locale/fr/locale-resources.d.ts +2 -1
  34. package/dist/locale/fr/locale-resources.d.ts.map +1 -1
  35. package/dist/locale/fr/locale-resources.js +3 -2
  36. package/dist/locale/fr/locale-resources.js.map +1 -1
  37. package/dist/locale/i18n.d.ts +0 -3
  38. package/dist/locale/i18n.d.ts.map +1 -1
  39. package/dist/locale/i18n.js +2 -5
  40. package/dist/locale/i18n.js.map +1 -1
  41. package/dist/non-numeric-identifier-validator.js +1 -1
  42. package/dist/non-numeric-identifier-validator.js.map +1 -1
  43. package/dist/numeric-identifier-validator.js +2 -2
  44. package/dist/numeric-identifier-validator.js.map +1 -1
  45. package/dist/prefix-manager.d.ts +35 -0
  46. package/dist/prefix-manager.d.ts.map +1 -1
  47. package/dist/prefix-manager.js +56 -0
  48. package/dist/prefix-manager.js.map +1 -1
  49. package/dist/prefix-validator.d.ts +5 -4
  50. package/dist/prefix-validator.d.ts.map +1 -1
  51. package/dist/prefix-validator.js +18 -22
  52. package/dist/prefix-validator.js.map +1 -1
  53. package/dist/serializable-numeric-identifier-validator.d.ts +26 -0
  54. package/dist/serializable-numeric-identifier-validator.d.ts.map +1 -1
  55. package/dist/serializable-numeric-identifier-validator.js +19 -0
  56. package/dist/serializable-numeric-identifier-validator.js.map +1 -1
  57. package/dist/variable-measure.d.ts +68 -0
  58. package/dist/variable-measure.d.ts.map +1 -0
  59. package/dist/variable-measure.js +210 -0
  60. package/dist/variable-measure.js.map +1 -0
  61. package/dist/verified-by-gs1.d.ts +22 -0
  62. package/dist/verified-by-gs1.d.ts.map +1 -0
  63. package/dist/verified-by-gs1.js +46 -0
  64. package/dist/verified-by-gs1.js.map +1 -0
  65. package/package.json +8 -8
  66. package/src/gcp-length-cache.ts +117 -0
  67. package/src/gcp-length-data.ts +68 -0
  68. package/src/gcp-length.ts +418 -0
  69. package/src/gtin-creator.ts +1 -117
  70. package/src/gtin-validator.ts +42 -173
  71. package/src/identifier-validator.ts +2 -5
  72. package/src/index.ts +7 -1
  73. package/src/locale/en/locale-resources.ts +3 -2
  74. package/src/locale/fr/locale-resources.ts +3 -2
  75. package/src/locale/i18n.ts +2 -5
  76. package/src/non-numeric-identifier-validator.ts +1 -1
  77. package/src/numeric-identifier-validator.ts +2 -2
  78. package/src/prefix-manager.ts +65 -0
  79. package/src/prefix-validator.ts +19 -23
  80. package/src/serializable-numeric-identifier-validator.ts +36 -0
  81. package/src/variable-measure.ts +268 -0
  82. package/src/verified-by-gs1.ts +54 -0
  83. package/test/creator.test.ts +5 -5
  84. package/test/data/gcpprefixformatlist-1.json +662625 -0
  85. package/test/data/gcpprefixformatlist-2.json +735431 -0
  86. package/test/gcp-length.test.ts +405 -0
  87. package/test/gtin-creator.ts +4 -4
  88. package/test/gtin-validator.test.ts +205 -113
  89. package/test/gtin-validator.ts +30 -0
  90. package/test/identifier-creator.ts +6 -6
  91. package/test/non-numeric-identifier-creator.ts +0 -8
  92. package/test/non-serializable-numeric-identifier-creator.ts +4 -54
  93. package/test/numeric-identifier-creator.ts +4 -4
  94. package/test/prefix-manager.test.ts +5 -5
  95. package/test/serializable-numeric-identifier-creator.ts +32 -19
  96. package/test/validator.test.ts +6 -6
  97. package/test/variable-measure-rcn.test.ts +63 -68
  98. package/test/verified-by-gs1.test.ts +55 -0
  99. package/tsconfig-src.json +7 -1
  100. package/tsconfig-src.tsbuildinfo +1 -0
  101. package/tsconfig-tsup.json +7 -0
@@ -1,122 +1,214 @@
1
1
  import { describe, expect, test } from "vitest";
2
- import {
3
- type GTINBaseLength,
4
- GTINLengths,
5
- GTINLevels,
6
- GTINValidator,
7
- IdentifierTypes,
8
- LeaderTypes,
9
- type PrefixType,
10
- PrefixTypes
11
- } from "../src/index.js";
12
- import { validateNumericIdentifierValidator } from "./numeric-identifier-validator.js";
13
-
14
- export function validateGTINValidator(validator: GTINValidator, isCreator: boolean, gtinBaseLength: GTINBaseLength): void {
15
- let prefixType: PrefixType;
16
-
17
- switch (gtinBaseLength) {
18
- case GTINLengths.GTIN13:
19
- prefixType = PrefixTypes.GS1CompanyPrefix;
20
- break;
21
-
22
- case GTINLengths.GTIN12:
23
- prefixType = PrefixTypes.UPCCompanyPrefix;
24
- break;
25
-
26
- case GTINLengths.GTIN8:
27
- prefixType = PrefixTypes.GS18Prefix;
28
- break;
29
- }
2
+ import { type GTINLevel, GTINLevels, GTINValidator } from "../src/index.js";
30
3
 
31
- validateNumericIdentifierValidator(validator, IdentifierTypes.GTIN, prefixType, gtinBaseLength, LeaderTypes.IndicatorDigit);
4
+ interface GTINValidation {
5
+ gtin: string;
6
+
7
+ errorMessages: Record<GTINLevel, string | null>;
32
8
  }
33
9
 
10
+ const GTIN_VALIDATIONS: GTINValidation[] = [
11
+ // GTIN-13.
12
+ {
13
+ gtin: "9521873000122",
14
+ errorMessages: {
15
+ [GTINLevels.Any]: null,
16
+ [GTINLevels.RetailConsumer]: null,
17
+ [GTINLevels.OtherThanRetailConsumer]: null
18
+ }
19
+ },
20
+
21
+ // GTIN-13 as GTIN-14.
22
+ {
23
+ gtin: "09521873000122",
24
+ errorMessages: {
25
+ [GTINLevels.Any]: null,
26
+ [GTINLevels.RetailConsumer]: "GTIN not supported at retail consumer trade item level",
27
+ [GTINLevels.OtherThanRetailConsumer]: null
28
+ }
29
+ },
30
+
31
+ // GTIN-12.
32
+ {
33
+ gtin: "614141773985",
34
+ errorMessages: {
35
+ [GTINLevels.Any]: null,
36
+ [GTINLevels.RetailConsumer]: null,
37
+ [GTINLevels.OtherThanRetailConsumer]: null
38
+ }
39
+ },
40
+
41
+ // GTIN-12 as GTIN-13.
42
+ {
43
+ gtin: "0614141773985",
44
+ errorMessages: {
45
+ [GTINLevels.Any]: null,
46
+ [GTINLevels.RetailConsumer]: "GTIN not supported at retail consumer trade item level",
47
+ [GTINLevels.OtherThanRetailConsumer]: null
48
+ }
49
+ },
50
+
51
+ // GTIN-12 as GTIN-14.
52
+ {
53
+ gtin: "00614141773985",
54
+ errorMessages: {
55
+ [GTINLevels.Any]: null,
56
+ [GTINLevels.RetailConsumer]: "GTIN not supported at retail consumer trade item level",
57
+ [GTINLevels.OtherThanRetailConsumer]: null
58
+ }
59
+ },
60
+
61
+ // Zero-suppressed GTIN-12.
62
+ {
63
+ gtin: "09867539",
64
+ errorMessages: {
65
+ [GTINLevels.Any]: null,
66
+ [GTINLevels.RetailConsumer]: null,
67
+ [GTINLevels.OtherThanRetailConsumer]: "GTIN not supported at other than retail consumer trade item level"
68
+ }
69
+ },
70
+
71
+ // GTIN-8.
72
+ {
73
+ gtin: "95216843",
74
+ errorMessages: {
75
+ [GTINLevels.Any]: null,
76
+ [GTINLevels.RetailConsumer]: null,
77
+ [GTINLevels.OtherThanRetailConsumer]: "GTIN not supported at other than retail consumer trade item level"
78
+ }
79
+ },
80
+
81
+ // GTIN-8 as GTIN-12.
82
+ {
83
+ gtin: "000095216843",
84
+ errorMessages: {
85
+ [GTINLevels.Any]: "U.P.C. Company Prefix can't start with \"0000\"",
86
+ [GTINLevels.RetailConsumer]: "U.P.C. Company Prefix can't start with \"0000\"",
87
+ [GTINLevels.OtherThanRetailConsumer]: "U.P.C. Company Prefix can't start with \"0000\""
88
+ }
89
+ },
90
+
91
+ // GTIN-8 as GTIN-13.
92
+ {
93
+ gtin: "0000095216843",
94
+ errorMessages: {
95
+ [GTINLevels.Any]: null,
96
+ [GTINLevels.RetailConsumer]: "GTIN not supported at retail consumer trade item level",
97
+ [GTINLevels.OtherThanRetailConsumer]: "GTIN not supported at other than retail consumer trade item level"
98
+ }
99
+ },
100
+
101
+ // GTIN-8 as GTIN-14.
102
+ {
103
+ gtin: "00000095216843",
104
+ errorMessages: {
105
+ [GTINLevels.Any]: null,
106
+ [GTINLevels.RetailConsumer]: "GTIN not supported at retail consumer trade item level",
107
+ [GTINLevels.OtherThanRetailConsumer]: "GTIN not supported at other than retail consumer trade item level"
108
+ }
109
+ },
110
+
111
+ // GTIN-14 from GTIN-13.
112
+ {
113
+ gtin: "19521873000129",
114
+ errorMessages: {
115
+ [GTINLevels.Any]: null,
116
+ [GTINLevels.RetailConsumer]: "GTIN not supported at retail consumer trade item level",
117
+ [GTINLevels.OtherThanRetailConsumer]: null
118
+ }
119
+ },
120
+
121
+ // GTIN-14 from GTIN-12.
122
+ {
123
+ gtin: "20614141773989",
124
+ errorMessages: {
125
+ [GTINLevels.Any]: null,
126
+ [GTINLevels.RetailConsumer]: "GTIN not supported at retail consumer trade item level",
127
+ [GTINLevels.OtherThanRetailConsumer]: null
128
+ }
129
+ },
130
+
131
+ // GTIN-14 from GTIN-8.
132
+ {
133
+ gtin: "30000095216844",
134
+ errorMessages: {
135
+ [GTINLevels.Any]: null,
136
+ [GTINLevels.RetailConsumer]: "GTIN not supported at retail consumer trade item level",
137
+ [GTINLevels.OtherThanRetailConsumer]: null
138
+ }
139
+ },
140
+
141
+ // Invalid character in GTIN-13.
142
+ {
143
+ gtin: "9521873000l22",
144
+ errorMessages: {
145
+ [GTINLevels.Any]: "Invalid character 'l' at position 11",
146
+ [GTINLevels.RetailConsumer]: "Invalid character 'l' at position 11",
147
+ [GTINLevels.OtherThanRetailConsumer]: "Invalid character 'l' at position 11"
148
+ }
149
+ },
150
+
151
+ // Invalid character in GTIN-14.
152
+ {
153
+ gtin: "30000O95216844",
154
+ errorMessages: {
155
+ [GTINLevels.Any]: "Invalid character 'O' at position 6",
156
+ [GTINLevels.RetailConsumer]: "Invalid character 'O' at position 6",
157
+ [GTINLevels.OtherThanRetailConsumer]: "Invalid character 'O' at position 6"
158
+ }
159
+ },
160
+
161
+ // Invalid length.
162
+ {
163
+ gtin: "95217800031",
164
+ errorMessages: {
165
+ [GTINLevels.Any]: "GTIN must be 13, 12, 8, or 14 digits long",
166
+ [GTINLevels.RetailConsumer]: "GTIN must be 13, 12, 8, or 14 digits long",
167
+ [GTINLevels.OtherThanRetailConsumer]: "GTIN must be 13, 12, 8, or 14 digits long"
168
+ }
169
+ },
170
+
171
+ // Invalid check digit.
172
+ {
173
+ gtin: "614141773991",
174
+ errorMessages: {
175
+ [GTINLevels.Any]: "Invalid check digit",
176
+ [GTINLevels.RetailConsumer]: "Invalid check digit",
177
+ [GTINLevels.OtherThanRetailConsumer]: "Invalid check digit"
178
+ }
179
+ },
180
+
181
+ // Invalid zero-suppressed GTIN-12.
182
+ {
183
+ gtin: "09800037",
184
+ errorMessages: {
185
+ [GTINLevels.Any]: "Invalid zero-suppressed GTIN-12",
186
+ [GTINLevels.RetailConsumer]: "Invalid zero-suppressed GTIN-12",
187
+ [GTINLevels.OtherThanRetailConsumer]: "Invalid zero-suppressed GTIN-12"
188
+ }
189
+ }
190
+ ];
191
+
34
192
  describe("GTIN validation and normalization", () => {
35
193
  test("Validation", () => {
36
- expect(() => {
37
- GTINValidator.validateAny("9521873000122", GTINLevels.Any);
38
- }).not.toThrow(RangeError);
39
- expect(() => {
40
- GTINValidator.validateAny("19521873000129", GTINLevels.Any);
41
- }).not.toThrow(RangeError);
42
- expect(() => {
43
- GTINValidator.validateAny("9521873000160", GTINLevels.Any);
44
- }).not.toThrow(RangeError);
45
- expect(() => {
46
- GTINValidator.validateAny("95216843", GTINLevels.Any);
47
- }).not.toThrow(RangeError);
48
- expect(() => {
49
- GTINValidator.validateAny("95217800031", GTINLevels.Any);
50
- }).toThrow("GTIN must be 13, 12, 8, or 14 digits long");
51
- expect(() => {
52
- GTINValidator.validateAny("614141773985", GTINLevels.Any);
53
- }).not.toThrow(RangeError);
54
- expect(() => {
55
- GTINValidator.validateAny("614141773991", GTINLevels.Any);
56
- }).toThrow("Invalid check digit");
57
- expect(() => {
58
- GTINValidator.validateAny("09867539", GTINLevels.Any);
59
- }).not.toThrow(RangeError);
60
- expect(() => {
61
- GTINValidator.validateAny("09800037", GTINLevels.Any);
62
- }).toThrow("Invalid zero-suppressed GTIN-12");
63
- expect(() => {
64
- GTINValidator.validateAny("9521873000122", GTINLevels.RetailConsumer);
65
- }).not.toThrow(RangeError);
66
- expect(() => {
67
- GTINValidator.validateAny("19521873000129", GTINLevels.RetailConsumer);
68
- }).toThrow("GTIN not supported at retail consumer trade item level");
69
- expect(() => {
70
- GTINValidator.validateAny("9521873000160", GTINLevels.RetailConsumer);
71
- }).not.toThrow(RangeError);
72
- expect(() => {
73
- GTINValidator.validateAny("95216843", GTINLevels.RetailConsumer);
74
- }).not.toThrow(RangeError);
75
- expect(() => {
76
- GTINValidator.validateAny("95217800031", GTINLevels.RetailConsumer);
77
- }).toThrow("GTIN must be 13, 12, 8, or 14 digits long");
78
- expect(() => {
79
- GTINValidator.validateAny("614141773985", GTINLevels.RetailConsumer);
80
- }).not.toThrow(RangeError);
81
- expect(() => {
82
- GTINValidator.validateAny("0614141773985", GTINLevels.RetailConsumer);
83
- }).toThrow("GTIN-13 at retail consumer trade item level can't start with zero");
84
- expect(() => {
85
- GTINValidator.validateAny("614141773991", GTINLevels.RetailConsumer);
86
- }).toThrow("Invalid check digit");
87
- expect(() => {
88
- GTINValidator.validateAny("09867539", GTINLevels.RetailConsumer);
89
- }).not.toThrow(RangeError);
90
- expect(() => {
91
- GTINValidator.validateAny("09800037", GTINLevels.RetailConsumer);
92
- }).toThrow("Invalid zero-suppressed GTIN-12");
93
- expect(() => {
94
- GTINValidator.validateAny("9521873000122", GTINLevels.OtherThanRetailConsumer);
95
- }).not.toThrow(RangeError);
96
- expect(() => {
97
- GTINValidator.validateAny("19521873000129", GTINLevels.OtherThanRetailConsumer);
98
- }).not.toThrow(RangeError);
99
- expect(() => {
100
- GTINValidator.validateAny("9521873000160", GTINLevels.OtherThanRetailConsumer);
101
- }).not.toThrow(RangeError);
102
- expect(() => {
103
- GTINValidator.validateAny("95216843", GTINLevels.OtherThanRetailConsumer);
104
- }).toThrow("GTIN not supported at other than retail consumer trade item level");
105
- expect(() => {
106
- GTINValidator.validateAny("95217800031", GTINLevels.OtherThanRetailConsumer);
107
- }).toThrow("GTIN must be 13, 12, 8, or 14 digits long");
108
- expect(() => {
109
- GTINValidator.validateAny("614141773985", GTINLevels.OtherThanRetailConsumer);
110
- }).not.toThrow(RangeError);
111
- expect(() => {
112
- GTINValidator.validateAny("614141773991", GTINLevels.OtherThanRetailConsumer);
113
- }).toThrow("Invalid check digit");
114
- expect(() => {
115
- GTINValidator.validateAny("09867539", GTINLevels.OtherThanRetailConsumer);
116
- }).toThrow("GTIN not supported at other than retail consumer trade item level");
117
- expect(() => {
118
- GTINValidator.validateAny("09800037", GTINLevels.OtherThanRetailConsumer);
119
- }).toThrow("Invalid zero-suppressed GTIN-12");
194
+ for (const gtinValidation of GTIN_VALIDATIONS) {
195
+ const gtin = gtinValidation.gtin;
196
+
197
+ for (const gtinLevel of Object.values(GTINLevels)) {
198
+ const expectMessage = `${gtin}:${gtinLevel}`;
199
+ const errorMessage = gtinValidation.errorMessages[gtinLevel];
200
+
201
+ if (errorMessage === null) {
202
+ expect(() => {
203
+ GTINValidator.validateAny(gtin, gtinLevel);
204
+ }, expectMessage).not.toThrow(RangeError);
205
+ } else {
206
+ expect(() => {
207
+ GTINValidator.validateAny(gtin, gtinLevel);
208
+ }, expectMessage).toThrow(errorMessage);
209
+ }
210
+ }
211
+ }
120
212
  });
121
213
 
122
214
  test("Normalization", () => {
@@ -0,0 +1,30 @@
1
+ import {
2
+ type GTINBaseLength,
3
+ GTINLengths,
4
+ type GTINValidator,
5
+ IdentifierTypes,
6
+ LeaderTypes,
7
+ type PrefixType,
8
+ PrefixTypes
9
+ } from "../src/index.js";
10
+ import { validateNumericIdentifierValidator } from "./numeric-identifier-validator.js";
11
+
12
+ export function validateGTINValidator(validator: GTINValidator, isCreator: boolean, gtinBaseLength: GTINBaseLength): void {
13
+ let prefixType: PrefixType;
14
+
15
+ switch (gtinBaseLength) {
16
+ case GTINLengths.GTIN13:
17
+ prefixType = PrefixTypes.GS1CompanyPrefix;
18
+ break;
19
+
20
+ case GTINLengths.GTIN12:
21
+ prefixType = PrefixTypes.UPCCompanyPrefix;
22
+ break;
23
+
24
+ case GTINLengths.GTIN8:
25
+ prefixType = PrefixTypes.GS18Prefix;
26
+ break;
27
+ }
28
+
29
+ validateNumericIdentifierValidator(validator, IdentifierTypes.GTIN, prefixType, gtinBaseLength, LeaderTypes.IndicatorDigit);
30
+ }
@@ -8,9 +8,9 @@ import {
8
8
  type PrefixManager,
9
9
  PrefixTypes
10
10
  } from "../src/index.js";
11
- import { validateGTINValidator } from "./gtin-validator.test.js";
12
- import { validateNonGTINNumericIdentifierValidator } from "./non-gtin-numeric-identifier-validator.js";
11
+ import { validateGTINValidator } from "./gtin-validator.js";
13
12
  import { validateNonNumericIdentifierValidator } from "./non-numeric-identifier-validator.js";
13
+ import { validateNonSerializableNumericIdentifierValidator } from "./non-serializable-numeric-identifier-validator.js";
14
14
  import { validateSerializableNumericIdentifierValidator } from "./serializable-numeric-identifier-validator.js";
15
15
 
16
16
  export function validateIdentifierCreators(prefixManager: PrefixManager): void {
@@ -52,14 +52,14 @@ export function validateIdentifierCreators(prefixManager: PrefixManager): void {
52
52
  expect(prefixManager.cpidCreator).toBe(prefixManager.cpidCreator);
53
53
  expect(prefixManager.gmnCreator).toBe(prefixManager.gmnCreator);
54
54
 
55
- validateNonGTINNumericIdentifierValidator(prefixManager.glnCreator, true, IdentifierTypes.GLN, 13, LeaderTypes.None);
56
- validateNonGTINNumericIdentifierValidator(prefixManager.ssccCreator, true, IdentifierTypes.SSCC, 18, LeaderTypes.ExtensionDigit);
55
+ validateNonSerializableNumericIdentifierValidator(prefixManager.glnCreator, true, IdentifierTypes.GLN, 13, LeaderTypes.None);
56
+ validateNonSerializableNumericIdentifierValidator(prefixManager.ssccCreator, true, IdentifierTypes.SSCC, 18, LeaderTypes.ExtensionDigit);
57
57
  validateSerializableNumericIdentifierValidator(prefixManager.graiCreator, true, IdentifierTypes.GRAI, 13, LeaderTypes.None, 16, ContentCharacterSets.AI82);
58
58
  validateNonNumericIdentifierValidator(prefixManager.giaiCreator, true, IdentifierTypes.GIAI, 30, ContentCharacterSets.AI82, false);
59
- validateNonGTINNumericIdentifierValidator(prefixManager.gsrnCreator, true, IdentifierTypes.GSRN, 18, LeaderTypes.None);
59
+ validateNonSerializableNumericIdentifierValidator(prefixManager.gsrnCreator, true, IdentifierTypes.GSRN, 18, LeaderTypes.None);
60
60
  validateSerializableNumericIdentifierValidator(prefixManager.gdtiCreator, true, IdentifierTypes.GDTI, 13, LeaderTypes.None, 17, ContentCharacterSets.AI82);
61
61
  validateNonNumericIdentifierValidator(prefixManager.gincCreator, true, IdentifierTypes.GINC, 30, ContentCharacterSets.AI82, false);
62
- validateNonGTINNumericIdentifierValidator(prefixManager.gsinCreator, true, IdentifierTypes.GSIN, 17, LeaderTypes.None);
62
+ validateNonSerializableNumericIdentifierValidator(prefixManager.gsinCreator, true, IdentifierTypes.GSIN, 17, LeaderTypes.None);
63
63
  validateSerializableNumericIdentifierValidator(prefixManager.gcnCreator, true, IdentifierTypes.GCN, 13, LeaderTypes.None, 12, ContentCharacterSets.Numeric);
64
64
  validateNonNumericIdentifierValidator(prefixManager.cpidCreator, true, IdentifierTypes.CPID, 30, ContentCharacterSets.AI39, false);
65
65
  validateNonNumericIdentifierValidator(prefixManager.gmnCreator, true, IdentifierTypes.GMN, 25, ContentCharacterSets.AI82, true);
@@ -68,14 +68,6 @@ export function testNonNumericIdentifierCreator(creator: NonNumericIdentifierCre
68
68
  expect(index).toBe(referenceCount);
69
69
  });
70
70
 
71
- test("Position offset", () => {
72
- expect(() => {
73
- creator.validate(creator.create("ABC123"), {
74
- positionOffset: 4
75
- });
76
- }).not.toThrow(RangeError);
77
- });
78
-
79
71
  test("Not all numeric", () => {
80
72
  expect(() => {
81
73
  creator.validate(creator.create("01234"), {
@@ -1,64 +1,14 @@
1
1
  import { expect, test } from "vitest";
2
2
  import {
3
- ContentCharacterSets,
4
- isSerializableNumericIdentifierCreator,
5
- type SerializableNumericIdentifierCreator
3
+ isNonSerializableNumericIdentifierCreator,
4
+ type NonSerializableNumericIdentifierCreator
6
5
  } from "../src/index.js";
7
6
  import { testNonGTINNumericIdentifierCreator } from "./non-gtin-numeric-identifier-creator.js";
8
7
 
9
- export function testSerializableNumericIdentifierCreator(creator: SerializableNumericIdentifierCreator): void {
8
+ export function testNonSerializableNumericIdentifierCreator(creator: NonSerializableNumericIdentifierCreator): void {
10
9
  testNonGTINNumericIdentifierCreator(creator, () => {
11
10
  test("Mapping", () => {
12
- expect(isSerializableNumericIdentifierCreator(creator)).toBe(true);
13
- });
14
- }, () => {
15
- test("Serialization", () => {
16
- const identifier = creator.create(0, true);
17
- const serial = "12345678";
18
- const serializedIdentifier = identifier + serial;
19
- const serials = [serial, "23456789", "34567890", "456789012"];
20
- const serializedIdentifiers = serials.map(serial => identifier + serial);
21
-
22
- expect(creator.createSerialized(0, serial, true)).toBe(serializedIdentifier);
23
- expect(creator.concatenate(identifier, serial)).toBe(serializedIdentifier);
24
- expect(Array.from(creator.createSerialized(0, serials, true))).toStrictEqual(serializedIdentifiers);
25
- expect(Array.from(creator.concatenate(identifier, serials))).toStrictEqual(serializedIdentifiers);
26
-
27
- const fullLengthSerial = "0".repeat(creator.serialComponentLength);
28
- const fullLengthPlusOneSerial = fullLengthSerial + "0";
29
- const fullLengthPlusOneSerialErrorMessage = `Length ${creator.serialComponentLength + 1} of serial component must be less than or equal to ${creator.serialComponentLength}`;
30
-
31
- expect(() => creator.createSerialized(0, fullLengthSerial, true)).not.toThrow(RangeError);
32
- expect(() => creator.concatenate(identifier, fullLengthSerial)).not.toThrow(RangeError);
33
- expect(() => Array.from(creator.createSerialized(0, [...serials, fullLengthSerial], true))).not.toThrow(RangeError);
34
- expect(() => Array.from(creator.concatenate(identifier, [...serials, fullLengthSerial]))).not.toThrow(RangeError);
35
- expect(() => creator.createSerialized(0, fullLengthPlusOneSerial, true)).toThrow(fullLengthPlusOneSerialErrorMessage);
36
- expect(() => creator.concatenate(identifier, fullLengthPlusOneSerial)).toThrow(fullLengthPlusOneSerialErrorMessage);
37
- expect(() => Array.from(creator.createSerialized(0, [...serials, fullLengthPlusOneSerial], true))).toThrow(fullLengthPlusOneSerialErrorMessage);
38
- expect(() => Array.from(creator.concatenate(identifier, [...serials, fullLengthPlusOneSerial]))).toThrow(fullLengthPlusOneSerialErrorMessage);
39
-
40
- let invalidSerial: string;
41
-
42
- switch (creator.serialComponentCharacterSet) {
43
- case ContentCharacterSets.Numeric:
44
- invalidSerial = "1234A5678";
45
- break;
46
-
47
- case ContentCharacterSets.AI82:
48
- invalidSerial = "ABCD~1234";
49
- break;
50
-
51
- case ContentCharacterSets.AI39:
52
- invalidSerial = "ABCD%1234";
53
- break;
54
- }
55
-
56
- const invalidSerialErrorMessage = `Invalid character '${invalidSerial.charAt(4)}' at position 5 of serial component`;
57
-
58
- expect(() => creator.createSerialized(0, invalidSerial, true)).toThrow(invalidSerialErrorMessage);
59
- expect(() => creator.concatenate(identifier, invalidSerial)).toThrow(invalidSerialErrorMessage);
60
- expect(() => Array.from(creator.createSerialized(0, [...serials, invalidSerial], true))).toThrow(invalidSerialErrorMessage);
61
- expect(() => Array.from(creator.concatenate(identifier, [...serials, invalidSerial]))).toThrow(invalidSerialErrorMessage);
11
+ expect(isNonSerializableNumericIdentifierCreator(creator)).toBe(true);
62
12
  });
63
13
  });
64
14
  }
@@ -62,8 +62,8 @@ export function testNumericIdentifierCreator<TNumericIdentifierType extends Nume
62
62
  expect(index).toBe(referenceCount);
63
63
  expect(sequenceIterator.next().value).toBeUndefined();
64
64
 
65
- const randomValues = new Array<number>();
66
- const identifiers = new Array<string>();
65
+ const randomValues: number[] = [];
66
+ const identifiers: string[] = [];
67
67
 
68
68
  for (let i = 0; i < 1000; i++) {
69
69
  const randomValue = Math.floor(Math.random() * creator.capacity);
@@ -99,8 +99,8 @@ export function testNumericIdentifierCreator<TNumericIdentifierType extends Nume
99
99
  expect(sequential).toBe(false);
100
100
  expect(index).toBe(sparseReferenceCount);
101
101
 
102
- const randomValues = new Array<number>();
103
- const identifiers = new Array<string>();
102
+ const randomValues: number[] = [];
103
+ const identifiers: string[] = [];
104
104
 
105
105
  for (let i = 0; i < 1000; i++) {
106
106
  const randomValue = Math.floor(Math.random() * creator.capacity);
@@ -15,7 +15,7 @@ describe("Prefix manager", () => {
15
15
 
16
16
  const gtin14 = prefixManager.gtinCreator.createGTIN14("5", 0);
17
17
 
18
- expect(gtin14.startsWith("5" + prefixManager.gs1CompanyPrefix)).toBe(true);
18
+ expect(gtin14.startsWith(`5${prefixManager.gs1CompanyPrefix}`)).toBe(true);
19
19
  expect(gtin14.length).toBe(14);
20
20
  }
21
21
 
@@ -64,7 +64,7 @@ describe("Prefix manager", () => {
64
64
 
65
65
  expect(prefixManager.prefixType).toBe(PrefixTypes.UPCCompanyPrefix);
66
66
  expect(prefixManager.prefix).toBe("614141");
67
- expect(prefixManager.gs1CompanyPrefix).toBe("0" + prefixManager.prefix);
67
+ expect(prefixManager.gs1CompanyPrefix).toBe(`0${prefixManager.prefix}`);
68
68
  expect(prefixManager.upcCompanyPrefix).toBe(prefixManager.prefix);
69
69
  expect(prefixManager.gs18Prefix).toBeUndefined();
70
70
 
@@ -79,7 +79,7 @@ describe("Prefix manager", () => {
79
79
 
80
80
  expect(prefixManager.prefixType).toBe(PrefixTypes.GS18Prefix);
81
81
  expect(prefixManager.prefix).toBe("952");
82
- expect(prefixManager.gs1CompanyPrefix).toBe("00000" + prefixManager.prefix);
82
+ expect(prefixManager.gs1CompanyPrefix).toBe(`00000${prefixManager.prefix}`);
83
83
  expect(prefixManager.upcCompanyPrefix).toBeUndefined();
84
84
  expect(prefixManager.gs18Prefix).toBe(prefixManager.prefix);
85
85
 
@@ -96,8 +96,8 @@ describe("Prefix manager", () => {
96
96
 
97
97
  expect(() => PrefixManager.get(PrefixTypes.GS1CompanyPrefix, "952123A56789")).toThrow("Invalid character 'A' at position 7 of GS1 Company Prefix");
98
98
 
99
- expect(() => PrefixManager.get(PrefixTypes.UPCCompanyPrefix, "61414")).toThrow("Length 5 of U.P.C. Company Prefix must be greater than or equal to 6");
100
- expect(() => PrefixManager.get(PrefixTypes.UPCCompanyPrefix, "614141")).not.toThrow(RangeError);
99
+ expect(() => PrefixManager.get(PrefixTypes.UPCCompanyPrefix, "6141")).toThrow("Length 4 of U.P.C. Company Prefix must be greater than or equal to 5");
100
+ expect(() => PrefixManager.get(PrefixTypes.UPCCompanyPrefix, "61414")).not.toThrow(RangeError);
101
101
  expect(() => PrefixManager.get(PrefixTypes.UPCCompanyPrefix, "61414112345")).not.toThrow(RangeError);
102
102
  expect(() => PrefixManager.get(PrefixTypes.UPCCompanyPrefix, "614141123456")).toThrow("Length 12 of U.P.C. Company Prefix must be less than or equal to 11");
103
103
  expect(() => PrefixManager.get(PrefixTypes.UPCCompanyPrefix, "000614")).not.toThrow(RangeError);
@@ -13,29 +13,42 @@ export function testSerializableNumericIdentifierCreator(creator: SerializableNu
13
13
  });
14
14
  }, () => {
15
15
  test("Serialization", () => {
16
- const identifier = creator.create(0, true);
17
- const serial = "12345678";
18
- const serializedIdentifier = identifier + serial;
19
- const serials = [serial, "23456789", "34567890", "456789012"];
20
- const serializedIdentifiers = serials.map(serial => identifier + serial);
16
+ const baseIdentifier = creator.create(0, true);
17
+ const serialComponent = "12345678";
18
+ const serializedIdentifier = baseIdentifier + serialComponent;
19
+ const serialComponents = [serialComponent, "23456789", "34567890", "456789012"];
20
+ const serializedIdentifiers = serialComponents.map(serial => baseIdentifier + serial);
21
21
 
22
- expect(creator.createSerialized(0, serial, true)).toBe(serializedIdentifier);
23
- expect(creator.concatenate(identifier, serial)).toBe(serializedIdentifier);
24
- expect(Array.from(creator.createSerialized(0, serials, true))).toStrictEqual(serializedIdentifiers);
25
- expect(Array.from(creator.concatenate(identifier, serials))).toStrictEqual(serializedIdentifiers);
22
+ expect(creator.split(baseIdentifier)).toStrictEqual({
23
+ baseIdentifier,
24
+ serialComponent: ""
25
+ });
26
+
27
+ expect(creator.createSerialized(0, serialComponent, true)).toBe(serializedIdentifier);
28
+ expect(creator.split(creator.createSerialized(0, serialComponent, true))).toStrictEqual({
29
+ baseIdentifier,
30
+ serialComponent
31
+ });
32
+ expect(creator.concatenate(baseIdentifier, serialComponent)).toBe(serializedIdentifier);
33
+ expect(creator.split(creator.concatenate(baseIdentifier, serialComponent))).toStrictEqual({
34
+ baseIdentifier,
35
+ serialComponent
36
+ });
37
+ expect(Array.from(creator.createSerialized(0, serialComponents, true))).toStrictEqual(serializedIdentifiers);
38
+ expect(Array.from(creator.concatenate(baseIdentifier, serialComponents))).toStrictEqual(serializedIdentifiers);
26
39
 
27
40
  const fullLengthSerial = "0".repeat(creator.serialComponentLength);
28
- const fullLengthPlusOneSerial = fullLengthSerial + "0";
41
+ const fullLengthPlusOneSerial = `${fullLengthSerial}0`;
29
42
  const fullLengthPlusOneSerialErrorMessage = `Length ${creator.serialComponentLength + 1} of serial component must be less than or equal to ${creator.serialComponentLength}`;
30
43
 
31
44
  expect(() => creator.createSerialized(0, fullLengthSerial, true)).not.toThrow(RangeError);
32
- expect(() => creator.concatenate(identifier, fullLengthSerial)).not.toThrow(RangeError);
33
- expect(() => Array.from(creator.createSerialized(0, [...serials, fullLengthSerial], true))).not.toThrow(RangeError);
34
- expect(() => Array.from(creator.concatenate(identifier, [...serials, fullLengthSerial]))).not.toThrow(RangeError);
45
+ expect(() => creator.concatenate(baseIdentifier, fullLengthSerial)).not.toThrow(RangeError);
46
+ expect(() => Array.from(creator.createSerialized(0, [...serialComponents, fullLengthSerial], true))).not.toThrow(RangeError);
47
+ expect(() => Array.from(creator.concatenate(baseIdentifier, [...serialComponents, fullLengthSerial]))).not.toThrow(RangeError);
35
48
  expect(() => creator.createSerialized(0, fullLengthPlusOneSerial, true)).toThrow(fullLengthPlusOneSerialErrorMessage);
36
- expect(() => creator.concatenate(identifier, fullLengthPlusOneSerial)).toThrow(fullLengthPlusOneSerialErrorMessage);
37
- expect(() => Array.from(creator.createSerialized(0, [...serials, fullLengthPlusOneSerial], true))).toThrow(fullLengthPlusOneSerialErrorMessage);
38
- expect(() => Array.from(creator.concatenate(identifier, [...serials, fullLengthPlusOneSerial]))).toThrow(fullLengthPlusOneSerialErrorMessage);
49
+ expect(() => creator.concatenate(baseIdentifier, fullLengthPlusOneSerial)).toThrow(fullLengthPlusOneSerialErrorMessage);
50
+ expect(() => Array.from(creator.createSerialized(0, [...serialComponents, fullLengthPlusOneSerial], true))).toThrow(fullLengthPlusOneSerialErrorMessage);
51
+ expect(() => Array.from(creator.concatenate(baseIdentifier, [...serialComponents, fullLengthPlusOneSerial]))).toThrow(fullLengthPlusOneSerialErrorMessage);
39
52
 
40
53
  let invalidSerial: string;
41
54
 
@@ -56,9 +69,9 @@ export function testSerializableNumericIdentifierCreator(creator: SerializableNu
56
69
  const invalidSerialErrorMessage = `Invalid character '${invalidSerial.charAt(4)}' at position 5 of serial component`;
57
70
 
58
71
  expect(() => creator.createSerialized(0, invalidSerial, true)).toThrow(invalidSerialErrorMessage);
59
- expect(() => creator.concatenate(identifier, invalidSerial)).toThrow(invalidSerialErrorMessage);
60
- expect(() => Array.from(creator.createSerialized(0, [...serials, invalidSerial], true))).toThrow(invalidSerialErrorMessage);
61
- expect(() => Array.from(creator.concatenate(identifier, [...serials, invalidSerial]))).toThrow(invalidSerialErrorMessage);
72
+ expect(() => creator.concatenate(baseIdentifier, invalidSerial)).toThrow(invalidSerialErrorMessage);
73
+ expect(() => Array.from(creator.createSerialized(0, [...serialComponents, invalidSerial], true))).toThrow(invalidSerialErrorMessage);
74
+ expect(() => Array.from(creator.concatenate(baseIdentifier, [...serialComponents, invalidSerial]))).toThrow(invalidSerialErrorMessage);
62
75
  });
63
76
  });
64
77
  }
@@ -17,9 +17,9 @@ import {
17
17
  isSerializableNumericIdentifierValidator,
18
18
  LeaderTypes
19
19
  } from "../src/index.js";
20
- import { validateGTINValidator } from "./gtin-validator.test.js";
21
- import { validateNonGTINNumericIdentifierValidator } from "./non-gtin-numeric-identifier-validator.js";
20
+ import { validateGTINValidator } from "./gtin-validator.js";
22
21
  import { validateNonNumericIdentifierValidator } from "./non-numeric-identifier-validator.js";
22
+ import { validateNonSerializableNumericIdentifierValidator } from "./non-serializable-numeric-identifier-validator.js";
23
23
  import { validateSerializableNumericIdentifierValidator } from "./serializable-numeric-identifier-validator.js";
24
24
 
25
25
  describe("Validators", () => {
@@ -62,14 +62,14 @@ describe("Validators", () => {
62
62
  validateGTINValidator(IdentifierValidators.GTIN[GTINLengths.GTIN13], false, GTINLengths.GTIN13);
63
63
  validateGTINValidator(IdentifierValidators.GTIN[GTINLengths.GTIN12], false, GTINLengths.GTIN12);
64
64
  validateGTINValidator(IdentifierValidators.GTIN[GTINLengths.GTIN8], false, GTINLengths.GTIN8);
65
- validateNonGTINNumericIdentifierValidator(IdentifierValidators.GLN, false, IdentifierTypes.GLN, 13, LeaderTypes.None);
66
- validateNonGTINNumericIdentifierValidator(IdentifierValidators.SSCC, false, IdentifierTypes.SSCC, 18, LeaderTypes.ExtensionDigit);
65
+ validateNonSerializableNumericIdentifierValidator(IdentifierValidators.GLN, false, IdentifierTypes.GLN, 13, LeaderTypes.None);
66
+ validateNonSerializableNumericIdentifierValidator(IdentifierValidators.SSCC, false, IdentifierTypes.SSCC, 18, LeaderTypes.ExtensionDigit);
67
67
  validateSerializableNumericIdentifierValidator(IdentifierValidators.GRAI, false, IdentifierTypes.GRAI, 13, LeaderTypes.None, 16, ContentCharacterSets.AI82);
68
68
  validateNonNumericIdentifierValidator(IdentifierValidators.GIAI, false, IdentifierTypes.GIAI, 30, ContentCharacterSets.AI82, false);
69
- validateNonGTINNumericIdentifierValidator(IdentifierValidators.GSRN, false, IdentifierTypes.GSRN, 18, LeaderTypes.None);
69
+ validateNonSerializableNumericIdentifierValidator(IdentifierValidators.GSRN, false, IdentifierTypes.GSRN, 18, LeaderTypes.None);
70
70
  validateSerializableNumericIdentifierValidator(IdentifierValidators.GDTI, false, IdentifierTypes.GDTI, 13, LeaderTypes.None, 17, ContentCharacterSets.AI82);
71
71
  validateNonNumericIdentifierValidator(IdentifierValidators.GINC, false, IdentifierTypes.GINC, 30, ContentCharacterSets.AI82, false);
72
- validateNonGTINNumericIdentifierValidator(IdentifierValidators.GSIN, false, IdentifierTypes.GSIN, 17, LeaderTypes.None);
72
+ validateNonSerializableNumericIdentifierValidator(IdentifierValidators.GSIN, false, IdentifierTypes.GSIN, 17, LeaderTypes.None);
73
73
  validateSerializableNumericIdentifierValidator(IdentifierValidators.GCN, false, IdentifierTypes.GCN, 13, LeaderTypes.None, 12, ContentCharacterSets.Numeric);
74
74
  validateNonNumericIdentifierValidator(IdentifierValidators.CPID, false, IdentifierTypes.CPID, 30, ContentCharacterSets.AI39, false);
75
75
  validateNonNumericIdentifierValidator(IdentifierValidators.GMN, false, IdentifierTypes.GMN, 25, ContentCharacterSets.AI82, true);