@aidc-toolkit/gs1 1.0.31-beta → 1.0.33-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/README.md +290 -17
- package/dist/character-set.d.ts +17 -2
- package/dist/character-set.d.ts.map +1 -1
- package/dist/character-set.js +44 -11
- package/dist/character-set.js.map +1 -1
- package/dist/gcp-length-cache.d.ts +81 -0
- package/dist/gcp-length-cache.d.ts.map +1 -0
- package/dist/gcp-length-cache.js +232 -0
- package/dist/gcp-length-cache.js.map +1 -0
- package/dist/gcp-length-data.d.ts +108 -0
- package/dist/gcp-length-data.d.ts.map +1 -0
- package/dist/gcp-length-data.js +53 -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 +300 -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 +39 -35
- package/dist/locale/en/locale-resources.d.ts.map +1 -1
- package/dist/locale/en/locale-resources.js +6 -2
- package/dist/locale/en/locale-resources.js.map +1 -1
- package/dist/locale/fr/locale-resources.d.ts +39 -35
- package/dist/locale/fr/locale-resources.d.ts.map +1 -1
- package/dist/locale/fr/locale-resources.js +6 -2
- package/dist/locale/fr/locale-resources.js.map +1 -1
- package/dist/locale/i18n.d.ts +4 -4
- package/dist/locale/i18n.d.ts.map +1 -1
- package/dist/locale/i18n.js +6 -7
- 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.d.ts.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 +12 -11
- package/src/character-set.ts +55 -11
- package/src/gcp-length-cache.ts +271 -0
- package/src/gcp-length-data.ts +136 -0
- package/src/gcp-length.ts +380 -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 +7 -3
- package/src/locale/fr/locale-resources.ts +7 -3
- package/src/locale/i18n.ts +7 -8
- package/src/locale/i18next.d.ts +2 -0
- package/src/non-numeric-identifier-validator.ts +1 -1
- package/src/numeric-identifier-validator.ts +3 -3
- 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/character-set.test.ts +46 -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 +344 -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,210 @@
|
|
|
1
|
+
import { NUMERIC_CREATOR } from "@aidc-toolkit/utility";
|
|
2
|
+
import { checkDigit, hasValidCheckDigit, isValidPriceOrWeightCheckDigit, priceOrWeightCheckDigit } from "./check.js";
|
|
3
|
+
import { i18nextGS1 } from "./locale/i18n.js";
|
|
4
|
+
/**
|
|
5
|
+
* Variable measure trade item support functions.
|
|
6
|
+
*/
|
|
7
|
+
// eslint-disable-next-line @typescript-eslint/no-extraneous-class -- Wrapper for future functionality.
|
|
8
|
+
export class VariableMeasure {
|
|
9
|
+
/**
|
|
10
|
+
* Parse a Restricted Circulation Number (RCN) using a variable measure trade item format. The format is a 12- or
|
|
11
|
+
* 13-character string (for RCN-12 or RCN-13 respectively), containing the following:
|
|
12
|
+
*
|
|
13
|
+
* - '2' - The first character of the RCN.
|
|
14
|
+
* - '0'-'9' - The second character of the RCN (RCN-13 only).
|
|
15
|
+
* - 'I' - One or more, in sequence, for the item reference.
|
|
16
|
+
* - 'P' - One or more, in sequence, for the price or weight.
|
|
17
|
+
* - 'V' - Zero or one, for the price or weight check digit.
|
|
18
|
+
* - 'C' - The check digit of the entire RCN.
|
|
19
|
+
*
|
|
20
|
+
* The 'I', 'P', and 'V' formats may be in any order.
|
|
21
|
+
*
|
|
22
|
+
* Some examples:
|
|
23
|
+
*
|
|
24
|
+
* - `2IIIIIVPPPPC` - RCN-12 with a five-digit item reference, a price or weight check digit, and a four-digit price
|
|
25
|
+
* or weight.
|
|
26
|
+
* - `23IIIIVPPPPPC` - RCN-13 with a four-digit item reference, a price or weight check digit, and a five-digit price
|
|
27
|
+
* or weight.
|
|
28
|
+
* - `2IIIIIIPPPPC` - RCN-12 with a six-digit item reference and a four-digit price or eight.
|
|
29
|
+
* - `29IIIIIPPPPPC` - RCN-13 with a five-digit item reference and a five-digit price or weight.
|
|
30
|
+
*
|
|
31
|
+
* @param format
|
|
32
|
+
* Format.
|
|
33
|
+
*
|
|
34
|
+
* @param rcn
|
|
35
|
+
* RCN.
|
|
36
|
+
*
|
|
37
|
+
* @returns
|
|
38
|
+
* RCN reference.
|
|
39
|
+
*/
|
|
40
|
+
static parseRCN(format, rcn) {
|
|
41
|
+
const formatLength = format.length;
|
|
42
|
+
if (rcn.length !== formatLength) {
|
|
43
|
+
throw new RangeError(i18nextGS1.t("Identifier.invalidRCNLength"));
|
|
44
|
+
}
|
|
45
|
+
let validFormat = formatLength === 12 || formatLength === 13;
|
|
46
|
+
let validRCNPrefix = true;
|
|
47
|
+
let buildingItemReference = false;
|
|
48
|
+
let itemReference = "";
|
|
49
|
+
let buildingPriceOrWeight = false;
|
|
50
|
+
let priceOrWeight = "";
|
|
51
|
+
let priceOrWeightCheckDigit = "";
|
|
52
|
+
for (let index = 0; validFormat && index < formatLength; index++) {
|
|
53
|
+
const formatChar = format.charAt(index);
|
|
54
|
+
const rcnChar = rcn.charAt(index);
|
|
55
|
+
if (index === 0) {
|
|
56
|
+
validFormat = formatChar === "2";
|
|
57
|
+
validRCNPrefix = rcnChar === "2";
|
|
58
|
+
}
|
|
59
|
+
else if (formatLength === 13 && index === 1) {
|
|
60
|
+
validFormat = NUMERIC_CREATOR.characterIndex(formatChar) !== undefined;
|
|
61
|
+
validRCNPrefix = rcnChar === formatChar;
|
|
62
|
+
}
|
|
63
|
+
else if (index === formatLength - 1) {
|
|
64
|
+
validFormat = formatChar === "C";
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
switch (formatChar) {
|
|
68
|
+
case "I":
|
|
69
|
+
if (!buildingItemReference) {
|
|
70
|
+
// Item reference can't appear more than once.
|
|
71
|
+
validFormat = itemReference === "";
|
|
72
|
+
buildingItemReference = true;
|
|
73
|
+
buildingPriceOrWeight = false;
|
|
74
|
+
}
|
|
75
|
+
itemReference += rcnChar;
|
|
76
|
+
break;
|
|
77
|
+
case "P":
|
|
78
|
+
if (!buildingPriceOrWeight) {
|
|
79
|
+
// Price or weight can't appear more than once.
|
|
80
|
+
validFormat = priceOrWeight === "";
|
|
81
|
+
buildingPriceOrWeight = true;
|
|
82
|
+
buildingItemReference = false;
|
|
83
|
+
}
|
|
84
|
+
priceOrWeight += rcnChar;
|
|
85
|
+
break;
|
|
86
|
+
case "V":
|
|
87
|
+
// Price or weight check digit can't appear more than once.
|
|
88
|
+
validFormat = priceOrWeightCheckDigit === "";
|
|
89
|
+
buildingItemReference = false;
|
|
90
|
+
buildingPriceOrWeight = false;
|
|
91
|
+
priceOrWeightCheckDigit = rcnChar;
|
|
92
|
+
break;
|
|
93
|
+
default:
|
|
94
|
+
validFormat = false;
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
validFormat &&= itemReference !== "" && priceOrWeight !== "";
|
|
100
|
+
if (!validFormat) {
|
|
101
|
+
throw new RangeError(i18nextGS1.t("Identifier.invalidVariableMeasureRCNFormat"));
|
|
102
|
+
}
|
|
103
|
+
if (!validRCNPrefix) {
|
|
104
|
+
throw new RangeError(i18nextGS1.t("Identifier.invalidVariableMeasureRCNPrefix"));
|
|
105
|
+
}
|
|
106
|
+
if (priceOrWeightCheckDigit !== "" && !isValidPriceOrWeightCheckDigit(priceOrWeight, priceOrWeightCheckDigit)) {
|
|
107
|
+
throw new RangeError(i18nextGS1.t("Identifier.invalidVariableMeasurePriceOrWeight"));
|
|
108
|
+
}
|
|
109
|
+
if (!hasValidCheckDigit(rcn)) {
|
|
110
|
+
throw new RangeError(i18nextGS1.t("Identifier.invalidCheckDigit"));
|
|
111
|
+
}
|
|
112
|
+
return {
|
|
113
|
+
itemReference: Number(itemReference),
|
|
114
|
+
priceOrWeight: Number(priceOrWeight)
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Create a Restricted Circulation Number (RCN) using a variable measure trade item format. See {@linkcode parseRCN}
|
|
119
|
+
* for format details.
|
|
120
|
+
*
|
|
121
|
+
* @param format
|
|
122
|
+
* Format.
|
|
123
|
+
*
|
|
124
|
+
* @param itemReference
|
|
125
|
+
* Item reference.
|
|
126
|
+
*
|
|
127
|
+
* @param priceOrWeight
|
|
128
|
+
* Price or weight (whole number only).
|
|
129
|
+
*
|
|
130
|
+
* @returns
|
|
131
|
+
* RCN-12 or RCN-13.
|
|
132
|
+
*/
|
|
133
|
+
static createRCN(format, itemReference, priceOrWeight) {
|
|
134
|
+
const formatLength = format.length;
|
|
135
|
+
let validFormat = formatLength === 12 || formatLength === 13;
|
|
136
|
+
let rcnPrefix = "";
|
|
137
|
+
let buildingItemReference = false;
|
|
138
|
+
let itemReferenceString = "";
|
|
139
|
+
let itemReferenceLength = 0;
|
|
140
|
+
let buildingPriceOrWeight = false;
|
|
141
|
+
let priceOrWeightString = "";
|
|
142
|
+
let priceOrWeightLength = 0;
|
|
143
|
+
let calculatePriceOrWeightCheckDigit = false;
|
|
144
|
+
// RCN may be built in almost any order, so defer to builders that will be in ordered array.
|
|
145
|
+
const rcnPrefixBuilder = (partialRCN) => partialRCN + rcnPrefix;
|
|
146
|
+
const itemReferenceBuilder = (partialRCN) => partialRCN + itemReferenceString;
|
|
147
|
+
const priceOrWeightBuilder = (partialRCN) => partialRCN + priceOrWeightString;
|
|
148
|
+
const priceOrWeightCheckDigitBuilder = (partialRCN) => partialRCN + priceOrWeightCheckDigit(priceOrWeightString);
|
|
149
|
+
const checkDigitBuilder = (partialRCN) => partialRCN + checkDigit(partialRCN);
|
|
150
|
+
const rcnBuilders = [rcnPrefixBuilder];
|
|
151
|
+
for (let index = 0; validFormat && index < formatLength; index++) {
|
|
152
|
+
const formatChar = format.charAt(index);
|
|
153
|
+
if (index === 0) {
|
|
154
|
+
validFormat = formatChar === "2";
|
|
155
|
+
rcnPrefix = formatChar;
|
|
156
|
+
}
|
|
157
|
+
else if (formatLength === 13 && index === 1) {
|
|
158
|
+
validFormat = NUMERIC_CREATOR.characterIndex(formatChar) !== undefined;
|
|
159
|
+
rcnPrefix += formatChar;
|
|
160
|
+
}
|
|
161
|
+
else if (index === formatLength - 1) {
|
|
162
|
+
validFormat = formatChar === "C";
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
switch (formatChar) {
|
|
166
|
+
case "I":
|
|
167
|
+
if (!buildingItemReference) {
|
|
168
|
+
// Item reference can't appear more than once.
|
|
169
|
+
validFormat = itemReferenceLength === 0;
|
|
170
|
+
buildingItemReference = true;
|
|
171
|
+
buildingPriceOrWeight = false;
|
|
172
|
+
rcnBuilders.push(itemReferenceBuilder);
|
|
173
|
+
}
|
|
174
|
+
itemReferenceLength++;
|
|
175
|
+
break;
|
|
176
|
+
case "P":
|
|
177
|
+
if (!buildingPriceOrWeight) {
|
|
178
|
+
// Price or weight can't appear more than once.
|
|
179
|
+
validFormat = priceOrWeightLength === 0;
|
|
180
|
+
buildingPriceOrWeight = true;
|
|
181
|
+
buildingItemReference = false;
|
|
182
|
+
rcnBuilders.push(priceOrWeightBuilder);
|
|
183
|
+
}
|
|
184
|
+
priceOrWeightLength++;
|
|
185
|
+
break;
|
|
186
|
+
case "V":
|
|
187
|
+
// Price or weight check digit can't appear more than once.
|
|
188
|
+
validFormat = !calculatePriceOrWeightCheckDigit;
|
|
189
|
+
buildingItemReference = false;
|
|
190
|
+
buildingPriceOrWeight = false;
|
|
191
|
+
calculatePriceOrWeightCheckDigit = true;
|
|
192
|
+
rcnBuilders.push(priceOrWeightCheckDigitBuilder);
|
|
193
|
+
break;
|
|
194
|
+
default:
|
|
195
|
+
validFormat = false;
|
|
196
|
+
break;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
validFormat &&= itemReferenceLength !== 0 && priceOrWeightLength !== 0;
|
|
201
|
+
if (!validFormat) {
|
|
202
|
+
throw new RangeError(i18nextGS1.t("Identifier.invalidVariableMeasureRCNFormat"));
|
|
203
|
+
}
|
|
204
|
+
itemReferenceString = NUMERIC_CREATOR.create(itemReferenceLength, itemReference);
|
|
205
|
+
priceOrWeightString = NUMERIC_CREATOR.create(priceOrWeightLength, priceOrWeight);
|
|
206
|
+
rcnBuilders.push(checkDigitBuilder);
|
|
207
|
+
return rcnBuilders.reduce((partialRCN, rcnBuilder) => rcnBuilder(partialRCN), "");
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
//# sourceMappingURL=variable-measure.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"variable-measure.js","sourceRoot":"","sources":["../src/variable-measure.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,UAAU,EAAE,kBAAkB,EAAE,8BAA8B,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAC;AACrH,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAiB9C;;GAEG;AACH,uGAAuG;AACvG,MAAM,OAAO,eAAe;IACxB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA8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K,CAAC,CAAC;4BAExC,qBAAqB,GAAG,IAAI,CAAC;4BAC7B,qBAAqB,GAAG,KAAK,CAAC;4BAE9B,WAAW,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC;wBAC3C,CAAC;wBAED,mBAAmB,EAAE,CAAC;wBACtB,MAAM;oBAEV,KAAK,GAAG;wBACJ,2DAA2D;wBAC3D,WAAW,GAAG,CAAC,gCAAgC,CAAC;wBAEhD,qBAAqB,GAAG,KAAK,CAAC;wBAC9B,qBAAqB,GAAG,KAAK,CAAC;wBAE9B,gCAAgC,GAAG,IAAI,CAAC;wBAExC,WAAW,CAAC,IAAI,CAAC,8BAA8B,CAAC,CAAC;wBACjD,MAAM;oBAEV;wBACI,WAAW,GAAG,KAAK,CAAC;wBACpB,MAAM;gBACd,CAAC;YACL,CAAC;QACL,CAAC;QAED,WAAW,KAAK,mBAAmB,KAAK,CAAC,IAAI,mBAAmB,KAAK,CAAC,CAAC;QAEvE,IAAI,CAAC,WAAW,EAAE,CAAC;YACf,MAAM,IAAI,UAAU,CAAC,UAAU,CAAC,CAAC,CAAC,4CAA4C,CAAC,CAAC,CAAC;QACrF,CAAC;QAED,mBAAmB,GAAG,eAAe,CAAC,MAAM,CAAC,mBAAmB,EAAE,aAAa,CAAC,CAAC;QACjF,mBAAmB,GAAG,eAAe,CAAC,MAAM,CAAC,mBAAmB,EAAE,aAAa,CAAC,CAAC;QAEjF,WAAW,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC;QAEpC,OAAO,WAAW,CAAC,MAAM,CAAC,CAAC,UAAU,EAAE,UAAU,EAAE,EAAE,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,EAAE,CAAC,CAAC;IACtF,CAAC;CACJ"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Hyperlink } from "@aidc-toolkit/core";
|
|
2
|
+
import { type IdentifierType } from "./identifier-type.js";
|
|
3
|
+
/**
|
|
4
|
+
* Create a Verified by GS1 hyperlink.
|
|
5
|
+
*
|
|
6
|
+
* @param identifierType
|
|
7
|
+
* Identifier type.
|
|
8
|
+
*
|
|
9
|
+
* @param identifier
|
|
10
|
+
* Identifier.
|
|
11
|
+
*
|
|
12
|
+
* @param text
|
|
13
|
+
* Text for hyperlink. If not provided, the identifier is used.
|
|
14
|
+
*
|
|
15
|
+
* @param details
|
|
16
|
+
* Details to display when hovering over hyperlink.
|
|
17
|
+
*
|
|
18
|
+
* @returns
|
|
19
|
+
* Verified by GS1 hyperlink.
|
|
20
|
+
*/
|
|
21
|
+
export declare function verifiedByGS1(identifierType: IdentifierType, identifier: string, text?: string | undefined, details?: string): Hyperlink;
|
|
22
|
+
//# sourceMappingURL=verified-by-gs1.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"verified-by-gs1.d.ts","sourceRoot":"","sources":["../src/verified-by-gs1.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAEpD,OAAO,EAAE,KAAK,cAAc,EAAmB,MAAM,sBAAsB,CAAC;AAK5E;;;;;;;;;;;;;;;;;GAiBG;AAEH,wBAAgB,aAAa,CAAC,cAAc,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,EAAE,IAAI,GAAE,MAAM,GAAG,SAAqB,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,CA2BnJ"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { GTINValidator } from "./gtin-validator.js";
|
|
2
|
+
import { IdentifierTypes } from "./identifier-type.js";
|
|
3
|
+
import { IdentifierValidators } from "./identifier-validators.js";
|
|
4
|
+
const VERIFIED_BY_GS1_REFERENCE_BASE = "https://www.gs1.org/services/verified-by-gs1/results?";
|
|
5
|
+
/**
|
|
6
|
+
* Create a Verified by GS1 hyperlink.
|
|
7
|
+
*
|
|
8
|
+
* @param identifierType
|
|
9
|
+
* Identifier type.
|
|
10
|
+
*
|
|
11
|
+
* @param identifier
|
|
12
|
+
* Identifier.
|
|
13
|
+
*
|
|
14
|
+
* @param text
|
|
15
|
+
* Text for hyperlink. If not provided, the identifier is used.
|
|
16
|
+
*
|
|
17
|
+
* @param details
|
|
18
|
+
* Details to display when hovering over hyperlink.
|
|
19
|
+
*
|
|
20
|
+
* @returns
|
|
21
|
+
* Verified by GS1 hyperlink.
|
|
22
|
+
*/
|
|
23
|
+
// eslint-disable-next-line @typescript-eslint/no-useless-default-assignment -- Undefined is necessary to allow bypass of text.
|
|
24
|
+
export function verifiedByGS1(identifierType, identifier, text = undefined, details) {
|
|
25
|
+
let normalizedIdentifier;
|
|
26
|
+
let useKeyTypeParameter;
|
|
27
|
+
if (identifierType === IdentifierTypes.GTIN) {
|
|
28
|
+
// Normalization will validate resulting GTIN.
|
|
29
|
+
normalizedIdentifier = GTINValidator.normalize(identifier);
|
|
30
|
+
useKeyTypeParameter = true;
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
const identifierValidator = IdentifierValidators[identifierType];
|
|
34
|
+
identifierValidator.validate(identifier);
|
|
35
|
+
normalizedIdentifier = identifier;
|
|
36
|
+
useKeyTypeParameter = identifierType === IdentifierTypes.GLN;
|
|
37
|
+
}
|
|
38
|
+
const lowerCaseIdentifierType = identifierType.toLowerCase();
|
|
39
|
+
const reference = useKeyTypeParameter ? `${VERIFIED_BY_GS1_REFERENCE_BASE}${lowerCaseIdentifierType}=${normalizedIdentifier}` : `${VERIFIED_BY_GS1_REFERENCE_BASE}key=${normalizedIdentifier}&key_type=${lowerCaseIdentifierType}`;
|
|
40
|
+
return {
|
|
41
|
+
reference,
|
|
42
|
+
text: text ?? identifier,
|
|
43
|
+
details
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=verified-by-gs1.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"verified-by-gs1.js","sourceRoot":"","sources":["../src/verified-by-gs1.ts"],"names":[],"mappings":"AACA,OAAO,EAAE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33-beta",
|
|
4
4
|
"description": "GS1 AIDC Toolkit",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
|
-
"homepage": "https://aidc-toolkit.com
|
|
7
|
+
"homepage": "https://aidc-toolkit.com",
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|
|
10
10
|
"url": "git+https://github.com/aidc-toolkit/gs1.git"
|
|
@@ -20,18 +20,19 @@
|
|
|
20
20
|
},
|
|
21
21
|
"scripts": {
|
|
22
22
|
"lint": "eslint",
|
|
23
|
-
"tsc
|
|
24
|
-
"build:
|
|
25
|
-
"build:
|
|
26
|
-
"build:
|
|
27
|
-
"
|
|
23
|
+
"tsc-src": "tsc --project tsconfig-src.json",
|
|
24
|
+
"build:alpha": "rimraf dist tsconfig-src.tsbuildinfo && npm run tsc-src -- --declarationMap --sourceMap",
|
|
25
|
+
"build:beta": "npm run build:alpha",
|
|
26
|
+
"build:prod": "npm run tsc-src -- --noEmit && tsup --tsconfig tsconfig-tsup.json",
|
|
27
|
+
"build:doc": "npm run build:alpha",
|
|
28
|
+
"test": "tsc --project tsconfig-test.json --noEmit && vitest run"
|
|
28
29
|
},
|
|
29
30
|
"devDependencies": {
|
|
30
|
-
"@aidc-toolkit/dev": "1.0.
|
|
31
|
-
"vitest": "^4.0.
|
|
31
|
+
"@aidc-toolkit/dev": "1.0.33-beta",
|
|
32
|
+
"vitest": "^4.0.17"
|
|
32
33
|
},
|
|
33
34
|
"dependencies": {
|
|
34
|
-
"@aidc-toolkit/core": "1.0.
|
|
35
|
-
"@aidc-toolkit/utility": "1.0.
|
|
35
|
+
"@aidc-toolkit/core": "1.0.33-beta",
|
|
36
|
+
"@aidc-toolkit/utility": "1.0.33-beta"
|
|
36
37
|
}
|
|
37
38
|
}
|
package/src/character-set.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
CharacterSetCreator,
|
|
3
|
+
type CharacterSetValidation,
|
|
4
|
+
CharacterSetValidator,
|
|
5
|
+
Exclusions, utilityNS
|
|
6
|
+
} from "@aidc-toolkit/utility";
|
|
7
|
+
import { i18nextGS1 } from "./locale/i18n.js";
|
|
2
8
|
|
|
3
9
|
/**
|
|
4
10
|
* GS1 AI encodable character set 82 creator as defined in section 7.11 of the {@link
|
|
@@ -38,17 +44,55 @@ export const AI39_CREATOR = new CharacterSetCreator([
|
|
|
38
44
|
*/
|
|
39
45
|
export const AI39_VALIDATOR = AI39_CREATOR as CharacterSetValidator;
|
|
40
46
|
|
|
47
|
+
/**
|
|
48
|
+
* GS1 AI encodable character set 64 validator with additional base64 validation of length (multiple of 4) and position
|
|
49
|
+
* of equal sign (last or last two characters).
|
|
50
|
+
*/
|
|
51
|
+
class AI64CharacterSetValidator extends CharacterSetValidator {
|
|
52
|
+
/**
|
|
53
|
+
* Constructor.
|
|
54
|
+
*/
|
|
55
|
+
constructor() {
|
|
56
|
+
super(([
|
|
57
|
+
"-",
|
|
58
|
+
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
|
|
59
|
+
"=",
|
|
60
|
+
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
|
|
61
|
+
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
|
|
62
|
+
"_",
|
|
63
|
+
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
|
|
64
|
+
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
|
|
65
|
+
]));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* @inheritDoc
|
|
70
|
+
*/
|
|
71
|
+
override validate(s: string, validation?: CharacterSetValidation): void {
|
|
72
|
+
super.validate(s, validation);
|
|
73
|
+
|
|
74
|
+
const length = s.length;
|
|
75
|
+
|
|
76
|
+
if (length % 4 !== 0) {
|
|
77
|
+
throw new RangeError(i18nextGS1.t("AI64CharacterSetValidator.lengthMustBeMultipleOf4", {
|
|
78
|
+
length
|
|
79
|
+
}));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const equalIndex = s.search(/={1,2}/u);
|
|
83
|
+
|
|
84
|
+
if (equalIndex !== -1 && equalIndex < length - 2) {
|
|
85
|
+
throw new RangeError(i18nextGS1.t("CharacterSetValidator.invalidCharacterAtPosition", {
|
|
86
|
+
ns: utilityNS,
|
|
87
|
+
c: "=",
|
|
88
|
+
position: equalIndex
|
|
89
|
+
}));
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
41
94
|
/**
|
|
42
95
|
* GS1 AI encodable character set 64 validator as defined in section 7.11 of the {@link
|
|
43
96
|
* https://ref.gs1.org/standards/genspecs/ | GS1 General Specifications}. Doesn't support any exclusions.
|
|
44
97
|
*/
|
|
45
|
-
export const AI64_VALIDATOR = new
|
|
46
|
-
"-",
|
|
47
|
-
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
|
|
48
|
-
"=",
|
|
49
|
-
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
|
|
50
|
-
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
|
|
51
|
-
"_",
|
|
52
|
-
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
|
|
53
|
-
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
|
|
54
|
-
]);
|
|
98
|
+
export const AI64_VALIDATOR = new AI64CharacterSetValidator();
|