@aidc-toolkit/gs1 0.9.20-beta → 1.0.22-beta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/character-set.d.ts +4 -4
- package/dist/character-set.d.ts.map +1 -1
- package/dist/character-set.js +7 -7
- package/dist/character-set.js.map +1 -1
- package/dist/gtin-creator.d.ts +68 -0
- package/dist/gtin-creator.d.ts.map +1 -0
- package/dist/gtin-creator.js +158 -0
- package/dist/gtin-creator.js.map +1 -0
- package/dist/gtin-validator.d.ts +202 -0
- package/dist/gtin-validator.d.ts.map +1 -0
- package/dist/gtin-validator.js +470 -0
- package/dist/gtin-validator.js.map +1 -0
- package/dist/identifier-creator.d.ts +72 -0
- package/dist/identifier-creator.d.ts.map +1 -0
- package/dist/identifier-creator.js +50 -0
- package/dist/identifier-creator.js.map +1 -0
- package/dist/identifier-type.d.ts +58 -0
- package/dist/identifier-type.d.ts.map +1 -0
- package/dist/identifier-type.js +54 -0
- package/dist/identifier-type.js.map +1 -0
- package/dist/identifier-validator.d.ts +174 -0
- package/dist/identifier-validator.d.ts.map +1 -0
- package/dist/identifier-validator.js +145 -0
- package/dist/identifier-validator.js.map +1 -0
- package/dist/index.d.ts +16 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +16 -1
- package/dist/index.js.map +1 -1
- package/dist/locale/en/locale-strings.d.ts +3 -3
- package/dist/locale/en/locale-strings.js +3 -3
- package/dist/locale/en/locale-strings.js.map +1 -1
- package/dist/locale/fr/locale-strings.d.ts +3 -3
- package/dist/locale/fr/locale-strings.js +3 -3
- package/dist/locale/fr/locale-strings.js.map +1 -1
- package/dist/non-gtin-numeric-identifier-creator.d.ts +30 -0
- package/dist/non-gtin-numeric-identifier-creator.d.ts.map +1 -0
- package/dist/non-gtin-numeric-identifier-creator.js +30 -0
- package/dist/non-gtin-numeric-identifier-creator.js.map +1 -0
- package/dist/non-gtin-numeric-identifier-validator.d.ts +41 -0
- package/dist/non-gtin-numeric-identifier-validator.d.ts.map +1 -0
- package/dist/non-gtin-numeric-identifier-validator.js +40 -0
- package/dist/non-gtin-numeric-identifier-validator.js.map +1 -0
- package/dist/non-numeric-identifier-creator.d.ts +55 -0
- package/dist/non-numeric-identifier-creator.d.ts.map +1 -0
- package/dist/non-numeric-identifier-creator.js +93 -0
- package/dist/non-numeric-identifier-creator.js.map +1 -0
- package/dist/non-numeric-identifier-validator.d.ts +78 -0
- package/dist/non-numeric-identifier-validator.d.ts.map +1 -0
- package/dist/non-numeric-identifier-validator.js +95 -0
- package/dist/non-numeric-identifier-validator.js.map +1 -0
- package/dist/numeric-identifier-creator.d.ts +121 -0
- package/dist/numeric-identifier-creator.d.ts.map +1 -0
- package/dist/numeric-identifier-creator.js +135 -0
- package/dist/numeric-identifier-creator.js.map +1 -0
- package/dist/numeric-identifier-validator.d.ts +76 -0
- package/dist/numeric-identifier-validator.d.ts.map +1 -0
- package/dist/numeric-identifier-validator.js +84 -0
- package/dist/numeric-identifier-validator.js.map +1 -0
- package/dist/prefix-manager.d.ts +224 -0
- package/dist/prefix-manager.d.ts.map +1 -0
- package/dist/prefix-manager.js +369 -0
- package/dist/prefix-manager.js.map +1 -0
- package/dist/prefix-provider.d.ts +27 -0
- package/dist/prefix-provider.d.ts.map +1 -0
- package/dist/prefix-provider.js +2 -0
- package/dist/prefix-provider.js.map +1 -0
- package/dist/prefix-type.d.ts +22 -0
- package/dist/prefix-type.d.ts.map +1 -0
- package/dist/prefix-type.js +18 -0
- package/dist/prefix-type.js.map +1 -0
- package/dist/prefix-validator.d.ts +58 -0
- package/dist/prefix-validator.d.ts.map +1 -0
- package/dist/prefix-validator.js +154 -0
- package/dist/prefix-validator.js.map +1 -0
- package/dist/serializable-numeric-identifier-creator.d.ts +86 -0
- package/dist/serializable-numeric-identifier-creator.d.ts.map +1 -0
- package/dist/serializable-numeric-identifier-creator.js +116 -0
- package/dist/serializable-numeric-identifier-creator.js.map +1 -0
- package/dist/serializable-numeric-identifier-validator.d.ts +79 -0
- package/dist/serializable-numeric-identifier-validator.d.ts.map +1 -0
- package/dist/serializable-numeric-identifier-validator.js +99 -0
- package/dist/serializable-numeric-identifier-validator.js.map +1 -0
- package/gs1.iml +4 -1
- package/package.json +2 -3
- package/src/character-set.ts +7 -7
- package/src/gtin-creator.ts +195 -0
- package/src/gtin-validator.ts +564 -0
- package/src/identifier-creator.ts +97 -0
- package/src/identifier-type.ts +69 -0
- package/src/identifier-validator.ts +235 -0
- package/src/index.ts +16 -1
- package/src/locale/en/locale-strings.ts +3 -3
- package/src/locale/fr/locale-strings.ts +3 -3
- package/src/non-gtin-numeric-identifier-creator.ts +33 -0
- package/src/non-gtin-numeric-identifier-validator.ts +54 -0
- package/src/non-numeric-identifier-creator.ts +111 -0
- package/src/non-numeric-identifier-validator.ts +128 -0
- package/src/numeric-identifier-creator.ts +200 -0
- package/src/numeric-identifier-validator.ts +128 -0
- package/src/prefix-manager.ts +446 -0
- package/src/prefix-provider.ts +31 -0
- package/src/prefix-type.ts +24 -0
- package/src/prefix-validator.ts +191 -0
- package/src/serializable-numeric-identifier-creator.ts +128 -0
- package/src/serializable-numeric-identifier-validator.ts +124 -0
- package/test/check.test.ts +0 -4
- package/test/creator.test.ts +30 -0
- package/test/gtin-creator.ts +239 -0
- package/test/gtin-validator.test.ts +149 -0
- package/test/identifier-creator.ts +84 -0
- package/test/identifier-validator.ts +8 -0
- package/test/non-gtin-numeric-identifier-creator.ts +98 -0
- package/test/non-gtin-numeric-identifier-validator.ts +6 -0
- package/test/non-numeric-identifier-validator.ts +24 -0
- package/test/numeric-identifier-creator.ts +132 -0
- package/test/numeric-identifier-validator.ts +23 -0
- package/test/prefix-manager.test.ts +112 -0
- package/test/serializable-numeric-identifier-creator.ts +56 -0
- package/test/serializable-numeric-identifier-validator.ts +24 -0
- package/test/setup.ts +4 -0
- package/test/sparse.test.ts +56 -0
- package/test/utility.ts +22 -0
- package/test/validator.test.ts +52 -0
- package/test/variable-measure-rcn.test.ts +201 -0
- package/vitest.config.ts +7 -0
- package/dist/idkey.d.ts +0 -1346
- package/dist/idkey.d.ts.map +0 -1
- package/dist/idkey.js +0 -2024
- package/dist/idkey.js.map +0 -1
- package/src/idkey.ts +0 -2532
- package/test/idkey.test.ts +0 -1247
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type CharacterSetValidation,
|
|
3
|
+
Exclusions,
|
|
4
|
+
NUMERIC_CREATOR,
|
|
5
|
+
type TransformerInput,
|
|
6
|
+
type TransformerOutput
|
|
7
|
+
} from "@aidc-toolkit/utility";
|
|
8
|
+
import { Mixin } from "ts-mixer";
|
|
9
|
+
import { checkDigit, priceOrWeightCheckDigit } from "./check.js";
|
|
10
|
+
import { type GTINType, GTINTypes, GTINValidator } from "./gtin-validator.js";
|
|
11
|
+
import { i18nextGS1 } from "./locale/i18n.js";
|
|
12
|
+
import { AbstractNumericIdentifierCreator } from "./numeric-identifier-creator.js";
|
|
13
|
+
import type { PrefixProvider } from "./prefix-provider";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* GTIN creator. Applicable to GTIN-13, GTIN-12, and GTIN-8 types; no applicable to GTIN-14 type.
|
|
17
|
+
*/
|
|
18
|
+
export class GTINCreator extends Mixin(GTINValidator, AbstractNumericIdentifierCreator) {
|
|
19
|
+
/**
|
|
20
|
+
* Validation parameters for required indicator digit.
|
|
21
|
+
*/
|
|
22
|
+
private static readonly REQUIRED_INDICATOR_DIGIT_VALIDATION: CharacterSetValidation = {
|
|
23
|
+
minimumLength: 1,
|
|
24
|
+
maximumLength: 1,
|
|
25
|
+
component: () => i18nextGS1.t("Identifier.indicatorDigit")
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Constructor. Typically called internally by a prefix manager but may be called by other code with another prefix
|
|
30
|
+
* provider type.
|
|
31
|
+
*
|
|
32
|
+
* @param prefixProvider
|
|
33
|
+
* Prefix provider.
|
|
34
|
+
*
|
|
35
|
+
* @param gtinType
|
|
36
|
+
* GTIN type.
|
|
37
|
+
*/
|
|
38
|
+
constructor(prefixProvider: PrefixProvider, gtinType: GTINType) {
|
|
39
|
+
super(gtinType);
|
|
40
|
+
|
|
41
|
+
this.init(prefixProvider, prefixProvider.prefix);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @inheritDoc
|
|
46
|
+
*/
|
|
47
|
+
override get prefix(): string {
|
|
48
|
+
return this.prefixProvider.prefix;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Create GTIN-14(s) with an indicator digit and reference(s) based on numeric value(s). The value(s) is/are
|
|
53
|
+
* converted to reference(s) of the appropriate length using {@linkcode NUMERIC_CREATOR}.
|
|
54
|
+
*
|
|
55
|
+
* @template TTransformerInput
|
|
56
|
+
* Transformer input type.
|
|
57
|
+
*
|
|
58
|
+
* @param indicatorDigit
|
|
59
|
+
* Indicator digit.
|
|
60
|
+
*
|
|
61
|
+
* @param valueOrValues
|
|
62
|
+
* Numeric value(s).
|
|
63
|
+
*
|
|
64
|
+
* @param sparse
|
|
65
|
+
* If true, the value(s) is/are mapped to a sparse sequence resistant to discovery. Default is false.
|
|
66
|
+
*
|
|
67
|
+
* @returns
|
|
68
|
+
* GTIN-14(s).
|
|
69
|
+
*/
|
|
70
|
+
createGTIN14<TTransformerInput extends TransformerInput<number | bigint>>(indicatorDigit: string, valueOrValues: TTransformerInput, sparse = false): TransformerOutput<TTransformerInput, string> {
|
|
71
|
+
NUMERIC_CREATOR.validate(indicatorDigit, GTINCreator.REQUIRED_INDICATOR_DIGIT_VALIDATION);
|
|
72
|
+
|
|
73
|
+
return NUMERIC_CREATOR.create(GTINTypes.GTIN13 - this.prefixProvider.gs1CompanyPrefix.length - 1, valueOrValues, Exclusions.None, sparse ? this.tweak : undefined, (reference) => {
|
|
74
|
+
const partialIdentifier = indicatorDigit + this.prefixProvider.gs1CompanyPrefix + reference;
|
|
75
|
+
|
|
76
|
+
return partialIdentifier + checkDigit(partialIdentifier);
|
|
77
|
+
});
|
|
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
|
+
}
|