@aidc-toolkit/gs1 1.0.24-beta → 1.0.25-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/index.cjs +562 -373
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +776 -415
- package/dist/index.d.ts +776 -415
- package/dist/index.js +549 -363
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
- package/src/abstract-identifier-creator.ts +97 -0
- package/src/abstract-identifier-validator.ts +140 -0
- package/src/abstract-numeric-identifier-creator.ts +168 -0
- package/src/abstract-numeric-identifier-validator.ts +69 -0
- package/src/character-set.ts +10 -10
- package/src/check.ts +7 -7
- package/src/content-character-set.ts +29 -0
- package/src/creators.ts +113 -0
- package/src/descriptors.ts +332 -0
- package/src/gtin-creator.ts +7 -6
- package/src/gtin-descriptor.ts +18 -0
- package/src/gtin-type.ts +50 -0
- package/src/gtin-validator.ts +36 -60
- package/src/identifier-creator.ts +11 -75
- package/src/identifier-descriptor.ts +30 -0
- package/src/identifier-type.ts +6 -1
- package/src/identifier-validator.ts +12 -188
- package/src/index.ts +29 -5
- package/src/locale/en/locale-resources.ts +0 -1
- package/src/locale/fr/locale-resources.ts +0 -1
- package/src/non-gtin-numeric-identifier-creator.ts +5 -11
- package/src/non-gtin-numeric-identifier-descriptor.ts +24 -0
- package/src/non-gtin-numeric-identifier-type.ts +7 -0
- package/src/non-gtin-numeric-identifier-validator.ts +8 -42
- package/src/non-numeric-identifier-creator.ts +5 -15
- package/src/non-numeric-identifier-descriptor.ts +29 -0
- package/src/non-numeric-identifier-type.ts +7 -0
- package/src/non-numeric-identifier-validator.ts +15 -53
- package/src/numeric-identifier-creator.ts +20 -163
- package/src/numeric-identifier-descriptor.ts +23 -0
- package/src/numeric-identifier-type.ts +44 -0
- package/src/numeric-identifier-validator.ts +13 -116
- package/src/prefix-manager.ts +84 -142
- package/src/prefix-provider.ts +2 -2
- package/src/prefix-type.ts +6 -1
- package/src/prefix-validator.ts +141 -79
- package/src/serializable-numeric-identifier-creator.ts +4 -14
- package/src/serializable-numeric-identifier-descriptor.ts +29 -0
- package/src/serializable-numeric-identifier-type.ts +9 -0
- package/src/serializable-numeric-identifier-validator.ts +17 -45
- package/src/validators.ts +203 -0
- package/test/creator.test.ts +2 -4
- package/test/gtin-creator.ts +5 -1
- package/test/gtin-validator.test.ts +5 -8
- package/test/identifier-creator.ts +1 -0
- package/test/identifier-validator.ts +2 -2
- package/test/non-gtin-numeric-identifier-creator.ts +8 -92
- package/test/non-gtin-numeric-identifier-validator.ts +1 -1
- package/test/non-numeric-identifier-creator.ts +93 -0
- package/test/numeric-identifier-creator.ts +9 -3
- package/test/numeric-identifier-validator.ts +3 -7
- package/test/serializable-numeric-identifier-creator.ts +10 -2
- package/test/validator.test.ts +59 -35
|
@@ -1,28 +1,18 @@
|
|
|
1
1
|
import { Exclusions, RegExpValidator } from "@aidc-toolkit/utility";
|
|
2
|
+
import { AbstractIdentifierValidator } from "./abstract-identifier-validator";
|
|
2
3
|
import { hasValidCheckCharacterPair } from "./check";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
AbstractIdentifierValidator,
|
|
6
|
-
type ContentCharacterSet,
|
|
7
|
-
ContentCharacterSets,
|
|
8
|
-
type IdentifierValidation
|
|
9
|
-
} from "./identifier-validator";
|
|
4
|
+
import { IdentifierDescriptors } from "./descriptors";
|
|
5
|
+
import type { IdentifierValidation } from "./identifier-validator";
|
|
10
6
|
import { i18nextGS1 } from "./locale/i18n";
|
|
11
|
-
import type {
|
|
12
|
-
import {
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Non-numeric identifier type.
|
|
16
|
-
*/
|
|
17
|
-
export type NonNumericIdentifierType = Exclude<IdentifierType, NumericIdentifierType>;
|
|
7
|
+
import type { NonNumericIdentifierDescriptor } from "./non-numeric-identifier-descriptor";
|
|
8
|
+
import type { NonNumericIdentifierType } from "./non-numeric-identifier-type";
|
|
18
9
|
|
|
19
10
|
/**
|
|
20
11
|
* Non-numeric identifier validation parameters.
|
|
21
12
|
*/
|
|
22
13
|
export interface NonNumericIdentifierValidation extends IdentifierValidation {
|
|
23
14
|
/**
|
|
24
|
-
* Exclusion support for reference. Prevents non-numeric identifier from being mistaken for numeric
|
|
25
|
-
* identifier.
|
|
15
|
+
* Exclusion support for reference. Prevents non-numeric identifier from being mistaken for numeric identifier.
|
|
26
16
|
*/
|
|
27
17
|
exclusion?: typeof Exclusions.None | typeof Exclusions.AllNumeric | undefined;
|
|
28
18
|
}
|
|
@@ -30,7 +20,7 @@ export interface NonNumericIdentifierValidation extends IdentifierValidation {
|
|
|
30
20
|
/**
|
|
31
21
|
* Non-numeric identifier validator.
|
|
32
22
|
*/
|
|
33
|
-
export class NonNumericIdentifierValidator extends AbstractIdentifierValidator<NonNumericIdentifierValidation> {
|
|
23
|
+
export class NonNumericIdentifierValidator extends AbstractIdentifierValidator<NonNumericIdentifierDescriptor, NonNumericIdentifierValidation> {
|
|
34
24
|
/**
|
|
35
25
|
* Validator to ensure that an identifier (minus check character pair) is not all numeric.
|
|
36
26
|
*/
|
|
@@ -53,20 +43,13 @@ export class NonNumericIdentifierValidator extends AbstractIdentifierValidator<N
|
|
|
53
43
|
*
|
|
54
44
|
* @param identifierType
|
|
55
45
|
* Identifier type.
|
|
56
|
-
*
|
|
57
|
-
* @param length
|
|
58
|
-
* Length.
|
|
59
|
-
*
|
|
60
|
-
* @param referenceCharacterSet
|
|
61
|
-
* Reference character set.
|
|
62
|
-
*
|
|
63
|
-
* @param requiresCheckCharacterPair
|
|
64
|
-
* True if the identifier requires a check character pair.
|
|
65
46
|
*/
|
|
66
|
-
constructor(identifierType:
|
|
67
|
-
|
|
47
|
+
constructor(identifierType: NonNumericIdentifierType) {
|
|
48
|
+
const identifierDescriptor = IdentifierDescriptors[identifierType];
|
|
49
|
+
|
|
50
|
+
super(identifierDescriptor);
|
|
68
51
|
|
|
69
|
-
this._requiresCheckCharacterPair = requiresCheckCharacterPair;
|
|
52
|
+
this._requiresCheckCharacterPair = identifierDescriptor.requiresCheckCharacterPair;
|
|
70
53
|
}
|
|
71
54
|
|
|
72
55
|
/**
|
|
@@ -88,15 +71,14 @@ export class NonNumericIdentifierValidator extends AbstractIdentifierValidator<N
|
|
|
88
71
|
validate(identifier: string, validation?: NonNumericIdentifierValidation): void {
|
|
89
72
|
const partialIdentifier = this.requiresCheckCharacterPair ? identifier.substring(0, identifier.length - 2) : identifier;
|
|
90
73
|
|
|
91
|
-
super.validatePrefix(partialIdentifier
|
|
74
|
+
super.validatePrefix(partialIdentifier);
|
|
92
75
|
|
|
93
76
|
if (!this.requiresCheckCharacterPair) {
|
|
94
77
|
this.referenceCreator.validate(identifier, {
|
|
95
|
-
maximumLength: this.length
|
|
96
|
-
positionOffset: validation?.positionOffset
|
|
78
|
+
maximumLength: this.length
|
|
97
79
|
});
|
|
98
80
|
// Validating the check character pair will also validate the characters.
|
|
99
|
-
} else if (!hasValidCheckCharacterPair(this.padIdentifier(identifier
|
|
81
|
+
} else if (!hasValidCheckCharacterPair(this.padIdentifier(identifier))) {
|
|
100
82
|
throw new RangeError(i18nextGS1.t("Identifier.invalidCheckCharacterPair"));
|
|
101
83
|
}
|
|
102
84
|
|
|
@@ -106,23 +88,3 @@ export class NonNumericIdentifierValidator extends AbstractIdentifierValidator<N
|
|
|
106
88
|
}
|
|
107
89
|
}
|
|
108
90
|
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* GIAI validator.
|
|
112
|
-
*/
|
|
113
|
-
export const GIAI_VALIDATOR = new NonNumericIdentifierValidator(IdentifierTypes.GIAI, 30, ContentCharacterSets.AI82);
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* GINC validator.
|
|
117
|
-
*/
|
|
118
|
-
export const GINC_VALIDATOR = new NonNumericIdentifierValidator(IdentifierTypes.GINC, 30, ContentCharacterSets.AI82);
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* CPID validator.
|
|
122
|
-
*/
|
|
123
|
-
export const CPID_VALIDATOR = new NonNumericIdentifierValidator(IdentifierTypes.CPID, 30, ContentCharacterSets.AI39);
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* GMN validator.
|
|
127
|
-
*/
|
|
128
|
-
export const GMN_VALIDATOR = new NonNumericIdentifierValidator(IdentifierTypes.GMN, 25, ContentCharacterSets.AI82, true);
|
|
@@ -1,27 +1,33 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
type TransformerInput,
|
|
6
|
-
type TransformerOutput
|
|
7
|
-
} from "@aidc-toolkit/utility";
|
|
8
|
-
import { checkDigit, checkDigitSum } from "./check";
|
|
9
|
-
import { AbstractIdentifierCreator, type IdentifierCreator } from "./identifier-creator";
|
|
10
|
-
import { type LeaderType, LeaderTypes, type NumericIdentifierValidator } from "./numeric-identifier-validator";
|
|
11
|
-
import type { PrefixProvider } from "./prefix-provider";
|
|
1
|
+
import type { TransformerInput, TransformerOutput } from "@aidc-toolkit/utility";
|
|
2
|
+
import type { IdentifierCreator } from "./identifier-creator";
|
|
3
|
+
import type { NumericIdentifierDescriptor } from "./numeric-identifier-descriptor";
|
|
4
|
+
import type { NumericIdentifierValidation, NumericIdentifierValidator } from "./numeric-identifier-validator";
|
|
12
5
|
|
|
13
6
|
/**
|
|
14
7
|
* Numeric identifier creator. Creates one or many numeric identifiers.
|
|
8
|
+
*
|
|
9
|
+
* @template TNumericIdentifierDescriptor
|
|
10
|
+
* Numeric identifier descriptor type.
|
|
15
11
|
*/
|
|
16
|
-
export interface NumericIdentifierCreator extends NumericIdentifierValidator
|
|
12
|
+
export interface NumericIdentifierCreator<TNumericIdentifierDescriptor extends NumericIdentifierDescriptor = NumericIdentifierDescriptor> extends NumericIdentifierValidator<TNumericIdentifierDescriptor>, IdentifierCreator<TNumericIdentifierDescriptor, NumericIdentifierValidation> {
|
|
17
13
|
/**
|
|
18
14
|
* Get the capacity (`10**referenceLength`).
|
|
19
15
|
*/
|
|
20
16
|
get capacity(): number;
|
|
21
17
|
|
|
22
18
|
/**
|
|
23
|
-
*
|
|
24
|
-
|
|
19
|
+
* Get the tweak for sparse creation.
|
|
20
|
+
*/
|
|
21
|
+
get tweak(): bigint;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Set the tweak for sparse creation.
|
|
25
|
+
*/
|
|
26
|
+
set tweak(value: bigint);
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Create identifier(s) with reference(s) based on numeric value(s). The value(s) is/are converted to references of
|
|
30
|
+
* the appropriate length using the numeric creator.
|
|
25
31
|
*
|
|
26
32
|
* @template TTransformerInput
|
|
27
33
|
* Transformer input type.
|
|
@@ -49,152 +55,3 @@ export interface NumericIdentifierCreator extends NumericIdentifierValidator, Id
|
|
|
49
55
|
*/
|
|
50
56
|
createAll: () => Iterable<string>;
|
|
51
57
|
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Abstract numeric identifier creator. Implements common functionality for a numeric identifier
|
|
55
|
-
* creator.
|
|
56
|
-
*/
|
|
57
|
-
export abstract class AbstractNumericIdentifierCreator extends AbstractIdentifierCreator implements NumericIdentifierCreator {
|
|
58
|
-
/**
|
|
59
|
-
* Capacity.
|
|
60
|
-
*/
|
|
61
|
-
private _capacity!: number;
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Tweak for sparse creation.
|
|
65
|
-
*/
|
|
66
|
-
private _tweak = 0n;
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Initialize the prefix provider. This method is in lieu of a constructor due to the mixin architecture.
|
|
70
|
-
*
|
|
71
|
-
* @param prefixProvider
|
|
72
|
-
* Prefix provider.
|
|
73
|
-
*
|
|
74
|
-
* @param prefix
|
|
75
|
-
* Prefix within prefix manager to use to calculate reference length.
|
|
76
|
-
*/
|
|
77
|
-
protected override init(prefixProvider: PrefixProvider, prefix: string): void {
|
|
78
|
-
super.init(prefixProvider, prefix, 1);
|
|
79
|
-
|
|
80
|
-
// Capacity is always in number range.
|
|
81
|
-
this._capacity = Number(CharacterSetCreator.powerOf10(this.referenceLength));
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
abstract get leaderType(): LeaderType;
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* @inheritDoc
|
|
88
|
-
*/
|
|
89
|
-
get capacity(): number {
|
|
90
|
-
return this._capacity;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Get the tweak for sparse creation.
|
|
95
|
-
*/
|
|
96
|
-
get tweak(): bigint {
|
|
97
|
-
return this._tweak;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Set the tweak for sparse creation.
|
|
102
|
-
*/
|
|
103
|
-
set tweak(value: bigint) {
|
|
104
|
-
this._tweak = value;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Build an identifier from a reference by merging it with the prefix and adding the check digit.
|
|
109
|
-
*
|
|
110
|
-
* @param reference
|
|
111
|
-
* Identifier reference.
|
|
112
|
-
*
|
|
113
|
-
* @returns
|
|
114
|
-
* Identifier.
|
|
115
|
-
*/
|
|
116
|
-
private buildIdentifier(reference: string): string {
|
|
117
|
-
const partialIdentifier = this.leaderType === LeaderTypes.ExtensionDigit ? reference.substring(0, 1) + this.prefix + reference.substring(1) : this.prefix + reference;
|
|
118
|
-
|
|
119
|
-
return partialIdentifier + checkDigit(partialIdentifier);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* @inheritDoc
|
|
124
|
-
*/
|
|
125
|
-
create<TTransformerInput extends TransformerInput<number | bigint>>(valueOrValues: TTransformerInput, sparse = false): TransformerOutput<TTransformerInput, string> {
|
|
126
|
-
return NUMERIC_CREATOR.create(this.referenceLength, valueOrValues, Exclusions.None, sparse ? this.tweak : undefined, reference => this.buildIdentifier(reference));
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Create all identifiers from a partial identifier. Call is recursive until remaining reference
|
|
131
|
-
* length is 0.
|
|
132
|
-
*
|
|
133
|
-
* @param partialIdentifier
|
|
134
|
-
* Partial identifier. Initial value is `this.prefix`.
|
|
135
|
-
*
|
|
136
|
-
* @param remainingReferenceLength
|
|
137
|
-
* Remaining reference length. Initial value is `this.referenceLength`.
|
|
138
|
-
*
|
|
139
|
-
* @param extensionWeight
|
|
140
|
-
* If this value is not zero, the identifier has an extension digit, this call is setting it, and this value
|
|
141
|
-
* is applied to the calculation of the check digit.
|
|
142
|
-
*
|
|
143
|
-
* @param weight
|
|
144
|
-
* If the extension weight is zero, this value is applied to the calculation of the check digit.
|
|
145
|
-
*
|
|
146
|
-
* @param partialCheckDigitSum
|
|
147
|
-
* Partial check digit sum for the partial identifier.
|
|
148
|
-
*
|
|
149
|
-
* @yields
|
|
150
|
-
* Identifier.
|
|
151
|
-
*/
|
|
152
|
-
private static * createAllPartial(partialIdentifier: string, remainingReferenceLength: number, extensionWeight: number, weight: number, partialCheckDigitSum: number): Generator<string> {
|
|
153
|
-
if (remainingReferenceLength === 0) {
|
|
154
|
-
// Finalize check digit calculation and append.
|
|
155
|
-
yield partialIdentifier + NUMERIC_CREATOR.character(9 - (partialCheckDigitSum + 9) % 10);
|
|
156
|
-
} else {
|
|
157
|
-
const nextRemainingReferenceLength = remainingReferenceLength - 1;
|
|
158
|
-
|
|
159
|
-
let nextPartialCheckDigitSum = partialCheckDigitSum;
|
|
160
|
-
|
|
161
|
-
if (extensionWeight !== 0) {
|
|
162
|
-
// Apply every digit to the extension digit.
|
|
163
|
-
for (const c of NUMERIC_CREATOR.characterSet) {
|
|
164
|
-
yield * AbstractNumericIdentifierCreator.createAllPartial(c + partialIdentifier, nextRemainingReferenceLength, 0, weight, nextPartialCheckDigitSum);
|
|
165
|
-
|
|
166
|
-
nextPartialCheckDigitSum += extensionWeight;
|
|
167
|
-
}
|
|
168
|
-
} else {
|
|
169
|
-
const nextWeight = 4 - weight;
|
|
170
|
-
|
|
171
|
-
// Apply every digit to the current character in the identifier.
|
|
172
|
-
for (const c of NUMERIC_CREATOR.characterSet) {
|
|
173
|
-
yield * AbstractNumericIdentifierCreator.createAllPartial(partialIdentifier + c, nextRemainingReferenceLength, 0, nextWeight, nextPartialCheckDigitSum);
|
|
174
|
-
|
|
175
|
-
nextPartialCheckDigitSum += weight;
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* @inheritDoc
|
|
183
|
-
*/
|
|
184
|
-
createAll(): Iterable<string> {
|
|
185
|
-
const hasExtensionDigit = this.leaderType === LeaderTypes.ExtensionDigit;
|
|
186
|
-
const prefix = this.prefix;
|
|
187
|
-
const length = this.length;
|
|
188
|
-
const referenceLength = this.referenceLength;
|
|
189
|
-
|
|
190
|
-
// Start weight is for reference excluding extension digit, which has its weight calculated separately.
|
|
191
|
-
const startWeight = 3 - 2 * ((referenceLength + 1 - Number(hasExtensionDigit)) % 2);
|
|
192
|
-
|
|
193
|
-
// Returning separate Iterable object makes iteration repeatable.
|
|
194
|
-
return {
|
|
195
|
-
[Symbol.iterator]() {
|
|
196
|
-
return AbstractNumericIdentifierCreator.createAllPartial(prefix, referenceLength, hasExtensionDigit ? 3 - 2 * length % 2 : 0, startWeight, checkDigitSum(startWeight === 3, prefix));
|
|
197
|
-
}
|
|
198
|
-
};
|
|
199
|
-
}
|
|
200
|
-
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { ContentCharacterSets } from "./content-character-set";
|
|
2
|
+
import type { IdentifierDescriptor } from "./identifier-descriptor";
|
|
3
|
+
import type { LeaderType, NumericIdentifierType } from "./numeric-identifier-type";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Numeric identifier descriptor.
|
|
7
|
+
*/
|
|
8
|
+
export interface NumericIdentifierDescriptor extends IdentifierDescriptor {
|
|
9
|
+
/**
|
|
10
|
+
* @inheritDoc
|
|
11
|
+
*/
|
|
12
|
+
readonly identifierType: NumericIdentifierType;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @inheritDoc
|
|
16
|
+
*/
|
|
17
|
+
readonly referenceCharacterSet: typeof ContentCharacterSets.Numeric;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Leader type.
|
|
21
|
+
*/
|
|
22
|
+
readonly leaderType: LeaderType;
|
|
23
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { IdentifierTypes } from "./identifier-type";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Numeric identifier type.
|
|
5
|
+
*/
|
|
6
|
+
export type NumericIdentifierType =
|
|
7
|
+
typeof IdentifierTypes.GTIN |
|
|
8
|
+
typeof IdentifierTypes.GLN |
|
|
9
|
+
typeof IdentifierTypes.SSCC |
|
|
10
|
+
typeof IdentifierTypes.GRAI |
|
|
11
|
+
typeof IdentifierTypes.GSRN |
|
|
12
|
+
typeof IdentifierTypes.GDTI |
|
|
13
|
+
typeof IdentifierTypes.GSIN |
|
|
14
|
+
typeof IdentifierTypes.GCN;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Leader type.
|
|
18
|
+
*/
|
|
19
|
+
export const LeaderTypes = {
|
|
20
|
+
/**
|
|
21
|
+
* No leader.
|
|
22
|
+
*/
|
|
23
|
+
None: "None",
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Indicator digit (GTIN only).
|
|
27
|
+
*/
|
|
28
|
+
IndicatorDigit: "Indicator digit",
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Extension digit (SSCC only).
|
|
32
|
+
*/
|
|
33
|
+
ExtensionDigit: "Extension digit"
|
|
34
|
+
} as const;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Leader type key.
|
|
38
|
+
*/
|
|
39
|
+
export type LeaderTypeKey = keyof typeof LeaderTypes;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Leader type.
|
|
43
|
+
*/
|
|
44
|
+
export type LeaderType = typeof LeaderTypes[LeaderTypeKey];
|
|
@@ -1,128 +1,25 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type {
|
|
3
|
-
import {
|
|
4
|
-
AbstractIdentifierValidator,
|
|
5
|
-
ContentCharacterSets,
|
|
6
|
-
type IdentifierValidation,
|
|
7
|
-
type IdentifierValidator
|
|
8
|
-
} from "./identifier-validator";
|
|
9
|
-
import { i18nextGS1 } from "./locale/i18n";
|
|
10
|
-
import type { PrefixType } from "./prefix-type";
|
|
1
|
+
import type { IdentifierValidation, IdentifierValidator } from "./identifier-validator";
|
|
2
|
+
import type { NumericIdentifierDescriptor } from "./numeric-identifier-descriptor";
|
|
11
3
|
|
|
12
4
|
/**
|
|
13
|
-
* Numeric identifier
|
|
5
|
+
* Numeric identifier validation parameters.
|
|
14
6
|
*/
|
|
15
|
-
export
|
|
16
|
-
typeof IdentifierTypes.GTIN |
|
|
17
|
-
typeof IdentifierTypes.GLN |
|
|
18
|
-
typeof IdentifierTypes.SSCC |
|
|
19
|
-
typeof IdentifierTypes.GRAI |
|
|
20
|
-
typeof IdentifierTypes.GSRN |
|
|
21
|
-
typeof IdentifierTypes.GDTI |
|
|
22
|
-
typeof IdentifierTypes.GSIN |
|
|
23
|
-
typeof IdentifierTypes.GCN;
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Leader type.
|
|
27
|
-
*/
|
|
28
|
-
export const LeaderTypes = {
|
|
29
|
-
/**
|
|
30
|
-
* No leader.
|
|
31
|
-
*/
|
|
32
|
-
None: "None",
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Indicator digit (GTIN only).
|
|
36
|
-
*/
|
|
37
|
-
IndicatorDigit: "Indicator digit",
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Extension digit (SSCC only).
|
|
41
|
-
*/
|
|
42
|
-
ExtensionDigit: "Extension digit"
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Leader type.
|
|
47
|
-
*/
|
|
48
|
-
export type LeaderType = typeof LeaderTypes[keyof typeof LeaderTypes];
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Numeric identifier validator. Validates a numeric identifier.
|
|
52
|
-
*/
|
|
53
|
-
export interface NumericIdentifierValidator extends IdentifierValidator {
|
|
7
|
+
export interface NumericIdentifierValidation extends IdentifierValidation {
|
|
54
8
|
/**
|
|
55
|
-
*
|
|
9
|
+
* Position offset within a larger string. Some numeric identifier types have the prefix offset by one.
|
|
56
10
|
*/
|
|
57
|
-
|
|
11
|
+
positionOffset?: number | undefined;
|
|
58
12
|
}
|
|
59
13
|
|
|
60
14
|
/**
|
|
61
|
-
*
|
|
62
|
-
*
|
|
15
|
+
* Numeric identifier validator.
|
|
16
|
+
*
|
|
17
|
+
* @template TNumericIdentifierDescriptor
|
|
18
|
+
* Numeric identifier descriptor type.
|
|
63
19
|
*/
|
|
64
|
-
export
|
|
20
|
+
export interface NumericIdentifierValidator<TNumericIdentifierDescriptor extends NumericIdentifierDescriptor = NumericIdentifierDescriptor> extends IdentifierValidator<TNumericIdentifierDescriptor, NumericIdentifierValidation> {
|
|
65
21
|
/**
|
|
66
|
-
*
|
|
67
|
-
*/
|
|
68
|
-
private readonly _leaderType: LeaderType;
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Prefix position, determined by the leader type.
|
|
72
|
-
*/
|
|
73
|
-
private readonly _prefixPosition: number;
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Constructor.
|
|
77
|
-
*
|
|
78
|
-
* @param identifierType
|
|
79
|
-
* Identifier type.
|
|
80
|
-
*
|
|
81
|
-
* @param prefixType
|
|
82
|
-
* Prefix type.
|
|
83
|
-
*
|
|
84
|
-
* @param length
|
|
85
|
-
* Length.
|
|
86
|
-
*
|
|
87
|
-
* @param leaderType
|
|
88
|
-
* Leader type.
|
|
89
|
-
*/
|
|
90
|
-
protected constructor(identifierType: IdentifierType, prefixType: PrefixType, length: number, leaderType: LeaderType) {
|
|
91
|
-
super(identifierType, prefixType, length, ContentCharacterSets.Numeric);
|
|
92
|
-
|
|
93
|
-
this._leaderType = leaderType;
|
|
94
|
-
this._prefixPosition = Number(this.leaderType === LeaderTypes.ExtensionDigit);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* @inheritDoc
|
|
99
|
-
*/
|
|
100
|
-
get leaderType(): LeaderType {
|
|
101
|
-
return this._leaderType;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* @inheritDoc
|
|
22
|
+
* Get the leader type.
|
|
106
23
|
*/
|
|
107
|
-
|
|
108
|
-
// Validate the prefix, with care taken for its position within the identifier.
|
|
109
|
-
if (this._prefixPosition === 0) {
|
|
110
|
-
super.validatePrefix(identifier, validation?.positionOffset);
|
|
111
|
-
} else {
|
|
112
|
-
super.validatePrefix(identifier.substring(this._prefixPosition), validation?.positionOffset === undefined ? this._prefixPosition : validation.positionOffset + this._prefixPosition);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
// Validate the length.
|
|
116
|
-
if (identifier.length !== this.length) {
|
|
117
|
-
throw new RangeError(i18nextGS1.t("Identifier.identifierTypeLength", {
|
|
118
|
-
identifierType: this.identifierType,
|
|
119
|
-
length: this.length
|
|
120
|
-
}));
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
// Validating the check digit will also validate the characters.
|
|
124
|
-
if (!hasValidCheckDigit(this.padIdentifier(identifier, validation))) {
|
|
125
|
-
throw new RangeError(i18nextGS1.t("Identifier.invalidCheckDigit"));
|
|
126
|
-
}
|
|
127
|
-
}
|
|
24
|
+
get leaderType(): TNumericIdentifierDescriptor["leaderType"];
|
|
128
25
|
}
|