@aidc-toolkit/gs1 1.0.23-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 +4813 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1949 -0
- package/dist/index.d.ts +1949 -36
- package/dist/index.js +4727 -19
- package/dist/index.js.map +1 -1
- package/package.json +8 -12
- package/src/abstract-identifier-creator.ts +97 -0
- package/src/abstract-identifier-validator.ts +140 -0
- package/{dist/numeric-identifier-creator.js → src/abstract-numeric-identifier-creator.ts} +61 -28
- package/src/abstract-numeric-identifier-validator.ts +69 -0
- package/src/character-set.ts +24 -9
- package/src/check.ts +9 -9
- 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 +9 -8
- package/src/gtin-descriptor.ts +18 -0
- package/src/gtin-type.ts +50 -0
- package/src/gtin-validator.ts +40 -64
- 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 +43 -19
- package/src/locale/en/locale-resources.ts +0 -1
- package/src/locale/fr/locale-resources.ts +0 -1
- package/src/locale/i18n.ts +3 -5
- package/src/locale/i18next.d.ts +1 -1
- package/src/non-gtin-numeric-identifier-creator.ts +6 -12
- 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 +8 -18
- 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 +17 -55
- 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 +94 -152
- package/src/prefix-provider.ts +3 -3
- package/src/prefix-type.ts +6 -1
- package/src/prefix-validator.ts +143 -81
- package/src/serializable-numeric-identifier-creator.ts +5 -15
- 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 +18 -46
- package/src/validators.ts +203 -0
- package/test/creator.test.ts +4 -6
- package/test/gtin-creator.ts +6 -2
- package/test/gtin-validator.test.ts +6 -9
- package/test/identifier-creator.ts +5 -4
- package/test/identifier-validator.ts +2 -2
- package/test/non-gtin-numeric-identifier-creator.ts +9 -93
- package/test/non-gtin-numeric-identifier-validator.ts +2 -2
- package/test/non-numeric-identifier-creator.ts +93 -0
- package/test/non-numeric-identifier-validator.ts +2 -2
- package/test/numeric-identifier-creator.ts +11 -5
- package/test/numeric-identifier-validator.ts +4 -8
- package/test/prefix-manager.test.ts +1 -1
- package/test/serializable-numeric-identifier-creator.ts +11 -3
- package/test/serializable-numeric-identifier-validator.ts +2 -2
- package/test/validator.test.ts +63 -39
- package/tsup.config.ts +3 -0
- package/typedoc.json +1 -3
- package/dist/character-set.d.ts +0 -22
- package/dist/character-set.d.ts.map +0 -1
- package/dist/character-set.js +0 -36
- package/dist/character-set.js.map +0 -1
- package/dist/check.d.ts +0 -81
- package/dist/check.d.ts.map +0 -1
- package/dist/check.js +0 -218
- package/dist/check.js.map +0 -1
- package/dist/gtin-creator.d.ts +0 -68
- package/dist/gtin-creator.d.ts.map +0 -1
- package/dist/gtin-creator.js +0 -158
- package/dist/gtin-creator.js.map +0 -1
- package/dist/gtin-validator.d.ts +0 -202
- package/dist/gtin-validator.d.ts.map +0 -1
- package/dist/gtin-validator.js +0 -470
- package/dist/gtin-validator.js.map +0 -1
- package/dist/identifier-creator.d.ts +0 -72
- package/dist/identifier-creator.d.ts.map +0 -1
- package/dist/identifier-creator.js +0 -50
- package/dist/identifier-creator.js.map +0 -1
- package/dist/identifier-type.d.ts +0 -58
- package/dist/identifier-type.d.ts.map +0 -1
- package/dist/identifier-type.js +0 -54
- package/dist/identifier-type.js.map +0 -1
- package/dist/identifier-validator.d.ts +0 -174
- package/dist/identifier-validator.d.ts.map +0 -1
- package/dist/identifier-validator.js +0 -145
- package/dist/identifier-validator.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/locale/en/locale-resources.d.ts +0 -43
- package/dist/locale/en/locale-resources.d.ts.map +0 -1
- package/dist/locale/en/locale-resources.js +0 -42
- package/dist/locale/en/locale-resources.js.map +0 -1
- package/dist/locale/fr/locale-resources.d.ts +0 -43
- package/dist/locale/fr/locale-resources.d.ts.map +0 -1
- package/dist/locale/fr/locale-resources.js +0 -42
- package/dist/locale/fr/locale-resources.js.map +0 -1
- package/dist/locale/i18n.d.ts +0 -27
- package/dist/locale/i18n.d.ts.map +0 -1
- package/dist/locale/i18n.js +0 -37
- package/dist/locale/i18n.js.map +0 -1
- package/dist/non-gtin-numeric-identifier-creator.d.ts +0 -30
- package/dist/non-gtin-numeric-identifier-creator.d.ts.map +0 -1
- package/dist/non-gtin-numeric-identifier-creator.js +0 -30
- package/dist/non-gtin-numeric-identifier-creator.js.map +0 -1
- package/dist/non-gtin-numeric-identifier-validator.d.ts +0 -41
- package/dist/non-gtin-numeric-identifier-validator.d.ts.map +0 -1
- package/dist/non-gtin-numeric-identifier-validator.js +0 -40
- package/dist/non-gtin-numeric-identifier-validator.js.map +0 -1
- package/dist/non-numeric-identifier-creator.d.ts +0 -55
- package/dist/non-numeric-identifier-creator.d.ts.map +0 -1
- package/dist/non-numeric-identifier-creator.js +0 -93
- package/dist/non-numeric-identifier-creator.js.map +0 -1
- package/dist/non-numeric-identifier-validator.d.ts +0 -78
- package/dist/non-numeric-identifier-validator.d.ts.map +0 -1
- package/dist/non-numeric-identifier-validator.js +0 -95
- package/dist/non-numeric-identifier-validator.js.map +0 -1
- package/dist/numeric-identifier-creator.d.ts +0 -121
- package/dist/numeric-identifier-creator.d.ts.map +0 -1
- package/dist/numeric-identifier-creator.js.map +0 -1
- package/dist/numeric-identifier-validator.d.ts +0 -76
- package/dist/numeric-identifier-validator.d.ts.map +0 -1
- package/dist/numeric-identifier-validator.js +0 -84
- package/dist/numeric-identifier-validator.js.map +0 -1
- package/dist/prefix-manager.d.ts +0 -224
- package/dist/prefix-manager.d.ts.map +0 -1
- package/dist/prefix-manager.js +0 -369
- package/dist/prefix-manager.js.map +0 -1
- package/dist/prefix-provider.d.ts +0 -27
- package/dist/prefix-provider.d.ts.map +0 -1
- package/dist/prefix-provider.js +0 -2
- package/dist/prefix-provider.js.map +0 -1
- package/dist/prefix-type.d.ts +0 -22
- package/dist/prefix-type.d.ts.map +0 -1
- package/dist/prefix-type.js +0 -18
- package/dist/prefix-type.js.map +0 -1
- package/dist/prefix-validator.d.ts +0 -58
- package/dist/prefix-validator.d.ts.map +0 -1
- package/dist/prefix-validator.js +0 -154
- package/dist/prefix-validator.js.map +0 -1
- package/dist/serializable-numeric-identifier-creator.d.ts +0 -86
- package/dist/serializable-numeric-identifier-creator.d.ts.map +0 -1
- package/dist/serializable-numeric-identifier-creator.js +0 -116
- package/dist/serializable-numeric-identifier-creator.js.map +0 -1
- package/dist/serializable-numeric-identifier-validator.d.ts +0 -79
- package/dist/serializable-numeric-identifier-validator.d.ts.map +0 -1
- package/dist/serializable-numeric-identifier-validator.js +0 -99
- package/dist/serializable-numeric-identifier-validator.js.map +0 -1
package/src/gtin-validator.ts
CHANGED
|
@@ -1,40 +1,12 @@
|
|
|
1
1
|
import { type CharacterSetValidation, NUMERIC_CREATOR } from "@aidc-toolkit/utility";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import { type
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
* GTIN types. The numeric values are equal to the lengths of the GTIN types.
|
|
11
|
-
*/
|
|
12
|
-
export const GTINTypes = {
|
|
13
|
-
/**
|
|
14
|
-
* GTIN-13.
|
|
15
|
-
*/
|
|
16
|
-
GTIN13: 13,
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* GTIN-12.
|
|
20
|
-
*/
|
|
21
|
-
GTIN12: 12,
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* GTIN-8.
|
|
25
|
-
*/
|
|
26
|
-
GTIN8: 8,
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* GTIN-14.
|
|
30
|
-
*/
|
|
31
|
-
GTIN14: 14
|
|
32
|
-
} as const;
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* GTIN type.
|
|
36
|
-
*/
|
|
37
|
-
export type GTINType = typeof GTINTypes[keyof typeof GTINTypes];
|
|
2
|
+
import { AbstractNumericIdentifierValidator } from "./abstract-numeric-identifier-validator";
|
|
3
|
+
import { checkDigit, hasValidCheckDigit, isValidPriceOrWeightCheckDigit } from "./check";
|
|
4
|
+
import { IdentifierDescriptors } from "./descriptors";
|
|
5
|
+
import type { GTINDescriptor } from "./gtin-descriptor";
|
|
6
|
+
import { type GTINBaseType, type GTINType, GTINTypes } from "./gtin-type";
|
|
7
|
+
import { i18nextGS1 } from "./locale/i18n";
|
|
8
|
+
import { type PrefixType, PrefixTypes } from "./prefix-type";
|
|
9
|
+
import { PrefixValidator } from "./prefix-validator";
|
|
38
10
|
|
|
39
11
|
/**
|
|
40
12
|
* Levels at which GTIN is to be validated.
|
|
@@ -56,10 +28,15 @@ export const GTINLevels = {
|
|
|
56
28
|
OtherThanRetailConsumer: 2
|
|
57
29
|
} as const;
|
|
58
30
|
|
|
31
|
+
/**
|
|
32
|
+
* GTIN level key.
|
|
33
|
+
*/
|
|
34
|
+
export type GTINLevelKey = keyof typeof GTINLevels;
|
|
35
|
+
|
|
59
36
|
/**
|
|
60
37
|
* GTIN level.
|
|
61
38
|
*/
|
|
62
|
-
export type GTINLevel = typeof GTINLevels[
|
|
39
|
+
export type GTINLevel = typeof GTINLevels[GTINLevelKey];
|
|
63
40
|
|
|
64
41
|
/**
|
|
65
42
|
* Restricted Circulation Number reference.
|
|
@@ -79,7 +56,7 @@ export interface RCNReference {
|
|
|
79
56
|
/**
|
|
80
57
|
* GTIN validator.
|
|
81
58
|
*/
|
|
82
|
-
export class GTINValidator extends AbstractNumericIdentifierValidator {
|
|
59
|
+
export class GTINValidator extends AbstractNumericIdentifierValidator<GTINDescriptor> {
|
|
83
60
|
/**
|
|
84
61
|
* Validation parameters for optional indicator digit.
|
|
85
62
|
*/
|
|
@@ -97,35 +74,30 @@ export class GTINValidator extends AbstractNumericIdentifierValidator {
|
|
|
97
74
|
maximumLength: 8
|
|
98
75
|
};
|
|
99
76
|
|
|
77
|
+
/**
|
|
78
|
+
* Prefix type.
|
|
79
|
+
*/
|
|
80
|
+
private readonly _prefixType: PrefixType;
|
|
81
|
+
|
|
100
82
|
/**
|
|
101
83
|
* Constructor.
|
|
102
84
|
*
|
|
103
|
-
* @param
|
|
104
|
-
* GTIN type.
|
|
85
|
+
* @param gtinBaseType
|
|
86
|
+
* GTIN base type (all except GTIN-14).
|
|
105
87
|
*/
|
|
106
|
-
constructor(
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
// Determine the prefix type based on the GTIN type.
|
|
110
|
-
switch (gtinType) {
|
|
111
|
-
case GTINTypes.GTIN13:
|
|
112
|
-
prefixType = PrefixTypes.GS1CompanyPrefix;
|
|
113
|
-
break;
|
|
88
|
+
constructor(gtinBaseType: GTINBaseType) {
|
|
89
|
+
const identifierDescriptor = IdentifierDescriptors.GTIN[gtinBaseType];
|
|
114
90
|
|
|
115
|
-
|
|
116
|
-
prefixType = PrefixTypes.UPCCompanyPrefix;
|
|
117
|
-
break;
|
|
118
|
-
|
|
119
|
-
case GTINTypes.GTIN8:
|
|
120
|
-
prefixType = PrefixTypes.GS18Prefix;
|
|
121
|
-
break;
|
|
91
|
+
super(identifierDescriptor);
|
|
122
92
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
throw new Error("Not supported");
|
|
126
|
-
}
|
|
93
|
+
this._prefixType = identifierDescriptor.prefixType;
|
|
94
|
+
}
|
|
127
95
|
|
|
128
|
-
|
|
96
|
+
/**
|
|
97
|
+
* @inheritDoc
|
|
98
|
+
*/
|
|
99
|
+
override get prefixType(): PrefixType {
|
|
100
|
+
return this._prefixType;
|
|
129
101
|
}
|
|
130
102
|
|
|
131
103
|
/**
|
|
@@ -542,23 +514,27 @@ export class GTINValidator extends AbstractNumericIdentifierValidator {
|
|
|
542
514
|
/**
|
|
543
515
|
* GTIN-13 validator.
|
|
544
516
|
*/
|
|
517
|
+
// Defined here because of circular reference.
|
|
545
518
|
export const GTIN13_VALIDATOR = new GTINValidator(GTINTypes.GTIN13);
|
|
546
519
|
|
|
547
520
|
/**
|
|
548
521
|
* GTIN-12 validator.
|
|
549
522
|
*/
|
|
523
|
+
// Defined here because of circular reference.
|
|
550
524
|
export const GTIN12_VALIDATOR = new GTINValidator(GTINTypes.GTIN12);
|
|
551
525
|
|
|
552
526
|
/**
|
|
553
527
|
* GTIN-8 validator.
|
|
554
528
|
*/
|
|
529
|
+
// Defined here because of circular reference.
|
|
555
530
|
export const GTIN8_VALIDATOR = new GTINValidator(GTINTypes.GTIN8);
|
|
556
531
|
|
|
557
532
|
/**
|
|
558
533
|
* GTIN validators indexed by prefix type.
|
|
559
534
|
*/
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
[
|
|
563
|
-
[
|
|
535
|
+
// Defined here because of circular reference.
|
|
536
|
+
export const GTIN_VALIDATORS: Readonly<Record<GTINBaseType, GTINValidator>> = {
|
|
537
|
+
[GTINTypes.GTIN13]: GTIN13_VALIDATOR,
|
|
538
|
+
[GTINTypes.GTIN12]: GTIN12_VALIDATOR,
|
|
539
|
+
[GTINTypes.GTIN8]: GTIN8_VALIDATOR
|
|
564
540
|
};
|
|
@@ -1,16 +1,20 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
3
|
-
import type { ContentCharacterSet, IdentifierValidation, IdentifierValidator } from "./identifier-validator.js";
|
|
1
|
+
import type { IdentifierDescriptor } from "./identifier-descriptor";
|
|
2
|
+
import type { IdentifierValidation, IdentifierValidator } from "./identifier-validator";
|
|
4
3
|
import type { PrefixProvider } from "./prefix-provider";
|
|
5
|
-
import type { PrefixType } from "./prefix-type.js";
|
|
6
4
|
|
|
7
5
|
/**
|
|
8
6
|
* Identifier creator. Creates an identifier based on its definition in section 3 of the {@link
|
|
9
|
-
* https://
|
|
7
|
+
* https://ref.gs1.org/standards/genspecs/ | GS1 General Specifications}.
|
|
10
8
|
*
|
|
11
|
-
* Keys are created based on a prefix defined in a prefix
|
|
9
|
+
* Keys are created based on a prefix defined in a prefix provider to which the identifier creator is bound.
|
|
10
|
+
*
|
|
11
|
+
* @template TIdentifierDescriptor
|
|
12
|
+
* Identifier descriptor type.
|
|
13
|
+
*
|
|
14
|
+
* @template TIdentifierValidation
|
|
15
|
+
* Identifier validation type.
|
|
12
16
|
*/
|
|
13
|
-
export interface IdentifierCreator extends IdentifierValidator {
|
|
17
|
+
export interface IdentifierCreator<TIdentifierDescriptor extends IdentifierDescriptor = IdentifierDescriptor, TIdentifierValidation extends IdentifierValidation = IdentifierValidation> extends IdentifierValidator<TIdentifierDescriptor, TIdentifierValidation> {
|
|
14
18
|
/**
|
|
15
19
|
* Get the prefix provider to which this identifier creator is bound.
|
|
16
20
|
*/
|
|
@@ -27,71 +31,3 @@ export interface IdentifierCreator extends IdentifierValidator {
|
|
|
27
31
|
*/
|
|
28
32
|
get referenceLength(): number;
|
|
29
33
|
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Abstract identifier creator. Implements common functionality for an identifier creator, bound to a
|
|
33
|
-
* {@link PrefixProvider}.
|
|
34
|
-
*/
|
|
35
|
-
export abstract class AbstractIdentifierCreator implements IdentifierCreator {
|
|
36
|
-
/**
|
|
37
|
-
* Prefix provider.
|
|
38
|
-
*/
|
|
39
|
-
private _prefixProvider!: PrefixProvider;
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Reference length.
|
|
43
|
-
*/
|
|
44
|
-
private _referenceLength!: number;
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Initialize the prefix manager. This method is in lieu of a constructor due to the mixin architecture.
|
|
48
|
-
*
|
|
49
|
-
* @param prefixProvider
|
|
50
|
-
* Prefix provider.
|
|
51
|
-
*
|
|
52
|
-
* @param prefix
|
|
53
|
-
* Prefix within prefix manager to use to calculate reference length.
|
|
54
|
-
*
|
|
55
|
-
* @param checkAllowance
|
|
56
|
-
* Number of characters to allow for check digit or check character pair.
|
|
57
|
-
*/
|
|
58
|
-
protected init(prefixProvider: PrefixProvider, prefix: string, checkAllowance: number): void {
|
|
59
|
-
this._prefixProvider = prefixProvider;
|
|
60
|
-
|
|
61
|
-
// Reference length allows for prefix and optionally check digit or check character pair.
|
|
62
|
-
this._referenceLength = this.length - prefix.length - checkAllowance;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
abstract get identifierType(): IdentifierType;
|
|
66
|
-
|
|
67
|
-
abstract get prefixType(): PrefixType;
|
|
68
|
-
|
|
69
|
-
abstract get length(): number;
|
|
70
|
-
|
|
71
|
-
abstract get referenceCharacterSet(): ContentCharacterSet;
|
|
72
|
-
|
|
73
|
-
abstract get referenceCreator(): CharacterSetCreator;
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* @inheritDoc
|
|
77
|
-
*/
|
|
78
|
-
get prefixProvider(): PrefixProvider {
|
|
79
|
-
return this._prefixProvider;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* @inheritDoc
|
|
84
|
-
*/
|
|
85
|
-
get prefix(): string {
|
|
86
|
-
return this.prefixProvider.gs1CompanyPrefix;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* @inheritDoc
|
|
91
|
-
*/
|
|
92
|
-
get referenceLength(): number {
|
|
93
|
-
return this._referenceLength;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
abstract validate(identifier: string, validation?: IdentifierValidation): void;
|
|
97
|
-
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { ContentCharacterSet } from "./content-character-set";
|
|
2
|
+
import type { IdentifierType } from "./identifier-type";
|
|
3
|
+
import type { PrefixType } from "./prefix-type";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Identifier descriptor. Instantiations of this type are used to parameterize validators and creators.
|
|
7
|
+
*/
|
|
8
|
+
export interface IdentifierDescriptor {
|
|
9
|
+
/**
|
|
10
|
+
* Identifier type. Per the GS1 General Specifications, the identifier type determines the remaining properties.
|
|
11
|
+
*/
|
|
12
|
+
readonly identifierType: IdentifierType;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Prefix type supported by the identifier type. For all identifier types except the GTIN, this is a GS1 Company
|
|
16
|
+
* Prefix. For the GTIN, the prefix type determines the length.
|
|
17
|
+
*/
|
|
18
|
+
readonly prefixType: PrefixType;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Length. For numeric identifier types, the length is fixed; for non-numeric identifier types, the length is the
|
|
22
|
+
* maximum.
|
|
23
|
+
*/
|
|
24
|
+
readonly length: number;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Reference character set.
|
|
28
|
+
*/
|
|
29
|
+
readonly referenceCharacterSet: ContentCharacterSet;
|
|
30
|
+
}
|
package/src/identifier-type.ts
CHANGED
|
@@ -63,7 +63,12 @@ export const IdentifierTypes = {
|
|
|
63
63
|
GMN: "GMN"
|
|
64
64
|
} as const;
|
|
65
65
|
|
|
66
|
+
/**
|
|
67
|
+
* Identifier type key.
|
|
68
|
+
*/
|
|
69
|
+
export type IdentifierTypeKey = keyof typeof IdentifierTypes;
|
|
70
|
+
|
|
66
71
|
/**
|
|
67
72
|
* Identifier type.
|
|
68
73
|
*/
|
|
69
|
-
export type IdentifierType = typeof IdentifierTypes[
|
|
74
|
+
export type IdentifierType = typeof IdentifierTypes[IdentifierTypeKey];
|
|
@@ -1,69 +1,34 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
NUMERIC_CREATOR,
|
|
4
|
-
type StringValidation,
|
|
5
|
-
type StringValidator
|
|
6
|
-
} from "@aidc-toolkit/utility";
|
|
7
|
-
import { AI39_CREATOR, AI82_CREATOR } from "./character-set.js";
|
|
8
|
-
import type { IdentifierType } from "./identifier-type.js";
|
|
9
|
-
import type { PrefixType } from "./prefix-type.js";
|
|
10
|
-
import { PrefixValidator } from "./prefix-validator.js";
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Character sets supported by the reference portion of an identifier or the serial component of a numeric identifier.
|
|
14
|
-
*/
|
|
15
|
-
export const ContentCharacterSets = {
|
|
16
|
-
/**
|
|
17
|
-
* Numeric.
|
|
18
|
-
*/
|
|
19
|
-
Numeric: "Numeric",
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* GS1 AI encodable character set 82.
|
|
23
|
-
*/
|
|
24
|
-
AI82: "AI82",
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* GS1 AI encodable character set 39.
|
|
28
|
-
*/
|
|
29
|
-
AI39: "AI39"
|
|
30
|
-
} as const;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Content character set.
|
|
34
|
-
*/
|
|
35
|
-
export type ContentCharacterSet = typeof ContentCharacterSets[keyof typeof ContentCharacterSets];
|
|
1
|
+
import type { CharacterSetCreator, StringValidation, StringValidator } from "@aidc-toolkit/utility";
|
|
2
|
+
import type { IdentifierDescriptor } from "./identifier-descriptor";
|
|
36
3
|
|
|
37
4
|
/**
|
|
38
5
|
* Identifier validation parameters.
|
|
39
6
|
*/
|
|
40
7
|
export interface IdentifierValidation extends StringValidation {
|
|
41
|
-
/**
|
|
42
|
-
* Position offset within a larger string. Strings are sometimes composed of multiple substrings; this parameter
|
|
43
|
-
* ensures that the error notes the proper position in the string.
|
|
44
|
-
*/
|
|
45
|
-
positionOffset?: number | undefined;
|
|
46
8
|
}
|
|
47
9
|
|
|
48
10
|
/**
|
|
49
11
|
* Identifier validator. Validates an identifier against its definition in section 3 of the {@link
|
|
50
|
-
* https://
|
|
12
|
+
* https://ref.gs1.org/standards/genspecs/ | GS1 General Specifications}.
|
|
13
|
+
*
|
|
14
|
+
* @template TIdentifierDescriptor
|
|
15
|
+
* Identifier descriptor type.
|
|
51
16
|
*
|
|
52
17
|
* @template TIdentifierValidation
|
|
53
18
|
* Identifier validation type.
|
|
54
19
|
*/
|
|
55
|
-
export interface IdentifierValidator<TIdentifierValidation extends IdentifierValidation = IdentifierValidation> extends StringValidator<TIdentifierValidation> {
|
|
20
|
+
export interface IdentifierValidator<TIdentifierDescriptor extends IdentifierDescriptor = IdentifierDescriptor, TIdentifierValidation extends IdentifierValidation = IdentifierValidation> extends StringValidator<TIdentifierValidation> {
|
|
56
21
|
/**
|
|
57
22
|
* Get the identifier type. Per the GS1 General Specifications, the identifier type determines the remaining
|
|
58
23
|
* properties.
|
|
59
24
|
*/
|
|
60
|
-
get identifierType():
|
|
25
|
+
get identifierType(): TIdentifierDescriptor["identifierType"];
|
|
61
26
|
|
|
62
27
|
/**
|
|
63
|
-
* Get the prefix type supported by the identifier type. For all identifier types except the GTIN, this is
|
|
64
|
-
*
|
|
28
|
+
* Get the prefix type supported by the identifier type. For all identifier types except the GTIN, this is a GS1
|
|
29
|
+
* Company Prefix. For the GTIN, the prefix type determines the length.
|
|
65
30
|
*/
|
|
66
|
-
get prefixType():
|
|
31
|
+
get prefixType(): TIdentifierDescriptor["prefixType"];
|
|
67
32
|
|
|
68
33
|
/**
|
|
69
34
|
* Get the length. For numeric identifier types, the length is fixed; for alphanumeric identifier types, the length
|
|
@@ -74,7 +39,7 @@ export interface IdentifierValidator<TIdentifierValidation extends IdentifierVal
|
|
|
74
39
|
/**
|
|
75
40
|
* Get the reference character set.
|
|
76
41
|
*/
|
|
77
|
-
get referenceCharacterSet():
|
|
42
|
+
get referenceCharacterSet(): TIdentifierDescriptor["referenceCharacterSet"];
|
|
78
43
|
|
|
79
44
|
/**
|
|
80
45
|
* Get the reference creator.
|
|
@@ -92,144 +57,3 @@ export interface IdentifierValidator<TIdentifierValidation extends IdentifierVal
|
|
|
92
57
|
*/
|
|
93
58
|
validate: (identifier: string, validation?: TIdentifierValidation) => void;
|
|
94
59
|
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Abstract identifier validator. Implements common functionality for an identifier validator.
|
|
98
|
-
*/
|
|
99
|
-
export abstract class AbstractIdentifierValidator<TIdentifierValidation extends IdentifierValidation = IdentifierValidation> implements IdentifierValidator<TIdentifierValidation> {
|
|
100
|
-
private static readonly CHARACTER_SET_CREATORS: Record<ContentCharacterSet, CharacterSetCreator> = {
|
|
101
|
-
[ContentCharacterSets.Numeric]: NUMERIC_CREATOR,
|
|
102
|
-
[ContentCharacterSets.AI82]: AI82_CREATOR,
|
|
103
|
-
[ContentCharacterSets.AI39]: AI39_CREATOR
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Identifier type.
|
|
108
|
-
*/
|
|
109
|
-
private readonly _identifierType: IdentifierType;
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Prefix type.
|
|
113
|
-
*/
|
|
114
|
-
private readonly _prefixType: PrefixType;
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Length.
|
|
118
|
-
*/
|
|
119
|
-
private readonly _length: number;
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Reference character set.
|
|
123
|
-
*/
|
|
124
|
-
private readonly _referenceCharacterSet: ContentCharacterSet;
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Reference creator.
|
|
128
|
-
*/
|
|
129
|
-
private readonly _referenceCreator: CharacterSetCreator;
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Get the character set creator for a character set.
|
|
133
|
-
*
|
|
134
|
-
* @param characterSet
|
|
135
|
-
* Character set.
|
|
136
|
-
*
|
|
137
|
-
* @returns
|
|
138
|
-
* Character set creator.
|
|
139
|
-
*/
|
|
140
|
-
protected static creatorFor(characterSet: ContentCharacterSet): CharacterSetCreator {
|
|
141
|
-
return AbstractIdentifierValidator.CHARACTER_SET_CREATORS[characterSet];
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Constructor.
|
|
146
|
-
*
|
|
147
|
-
* @param identifierType
|
|
148
|
-
* Identifier type.
|
|
149
|
-
*
|
|
150
|
-
* @param prefixType
|
|
151
|
-
* Prefix type.
|
|
152
|
-
*
|
|
153
|
-
* @param length
|
|
154
|
-
* Length.
|
|
155
|
-
*
|
|
156
|
-
* @param referenceCharacterSet
|
|
157
|
-
* Reference character set.
|
|
158
|
-
*/
|
|
159
|
-
protected constructor(identifierType: IdentifierType, prefixType: PrefixType, length: number, referenceCharacterSet: ContentCharacterSet) {
|
|
160
|
-
this._identifierType = identifierType;
|
|
161
|
-
this._prefixType = prefixType;
|
|
162
|
-
this._length = length;
|
|
163
|
-
this._referenceCharacterSet = referenceCharacterSet;
|
|
164
|
-
this._referenceCreator = AbstractIdentifierValidator.creatorFor(referenceCharacterSet);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* @inheritDoc
|
|
169
|
-
*/
|
|
170
|
-
get identifierType(): IdentifierType {
|
|
171
|
-
return this._identifierType;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* @inheritDoc
|
|
176
|
-
*/
|
|
177
|
-
get prefixType(): PrefixType {
|
|
178
|
-
return this._prefixType;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* @inheritDoc
|
|
183
|
-
*/
|
|
184
|
-
get length(): number {
|
|
185
|
-
return this._length;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* @inheritDoc
|
|
190
|
-
*/
|
|
191
|
-
get referenceCharacterSet(): ContentCharacterSet {
|
|
192
|
-
return this._referenceCharacterSet;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* @inheritDoc
|
|
197
|
-
*/
|
|
198
|
-
get referenceCreator(): CharacterSetCreator {
|
|
199
|
-
return this._referenceCreator;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
/**
|
|
203
|
-
* Pad an identifier on the left with zero-value character for validation purposes. This is done to align an
|
|
204
|
-
* identifier with a position offset for any error message that may be thrown by the reference validator.
|
|
205
|
-
*
|
|
206
|
-
* @param identifier
|
|
207
|
-
* Identifier.
|
|
208
|
-
*
|
|
209
|
-
* @param validation
|
|
210
|
-
* Identifier validation parameters.
|
|
211
|
-
*
|
|
212
|
-
* @returns
|
|
213
|
-
* Padded identifier.
|
|
214
|
-
*/
|
|
215
|
-
protected padIdentifier(identifier: string, validation: IdentifierValidation | undefined): string {
|
|
216
|
-
// Identifier is returned as is if position offset is undefined.
|
|
217
|
-
return validation?.positionOffset === undefined ? identifier : this.referenceCreator.character(0).repeat(validation.positionOffset).concat(identifier);
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* Validate the prefix within an identifier.
|
|
222
|
-
*
|
|
223
|
-
* @param partialIdentifier
|
|
224
|
-
* Partial identifier.
|
|
225
|
-
*
|
|
226
|
-
* @param positionOffset
|
|
227
|
-
* Position offset within a larger string.
|
|
228
|
-
*/
|
|
229
|
-
protected validatePrefix(partialIdentifier: string, positionOffset?: number): void {
|
|
230
|
-
// Delegate to prefix validator with support for U.P.C. Company Prefix but not GS1-8 Prefix.
|
|
231
|
-
PrefixValidator.validate(this.prefixType, true, false, partialIdentifier, true, this.referenceCharacterSet === ContentCharacterSets.Numeric, positionOffset);
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
abstract validate(identifier: string, validation?: TIdentifierValidation): void;
|
|
235
|
-
}
|
package/src/index.ts
CHANGED
|
@@ -14,22 +14,46 @@
|
|
|
14
14
|
* See the License for the specific language governing permissions and
|
|
15
15
|
* limitations under the License.
|
|
16
16
|
*/
|
|
17
|
-
export * from "./locale/i18n
|
|
18
|
-
|
|
19
|
-
export * from "./
|
|
20
|
-
|
|
21
|
-
export * from "./
|
|
22
|
-
|
|
23
|
-
export * from "./
|
|
24
|
-
export * from "./
|
|
25
|
-
|
|
26
|
-
export * from "./
|
|
27
|
-
|
|
28
|
-
export * from "./
|
|
29
|
-
export * from "./identifier-
|
|
30
|
-
export * from "./
|
|
31
|
-
export * from "./gtin-
|
|
32
|
-
export * from "./
|
|
33
|
-
export * from "./
|
|
34
|
-
|
|
35
|
-
export * from "./
|
|
17
|
+
export * from "./locale/i18n";
|
|
18
|
+
|
|
19
|
+
export * from "./character-set";
|
|
20
|
+
|
|
21
|
+
export * from "./check";
|
|
22
|
+
|
|
23
|
+
export * from "./prefix-type";
|
|
24
|
+
export * from "./prefix-validator";
|
|
25
|
+
|
|
26
|
+
export * from "./content-character-set";
|
|
27
|
+
|
|
28
|
+
export * from "./identifier-type";
|
|
29
|
+
export * from "./numeric-identifier-type";
|
|
30
|
+
export * from "./gtin-type";
|
|
31
|
+
export type * from "./non-gtin-numeric-identifier-type";
|
|
32
|
+
export type * from "./serializable-numeric-identifier-type";
|
|
33
|
+
export type * from "./non-numeric-identifier-type";
|
|
34
|
+
|
|
35
|
+
export type * from "./identifier-descriptor";
|
|
36
|
+
export type * from "./numeric-identifier-descriptor";
|
|
37
|
+
export type * from "./gtin-descriptor";
|
|
38
|
+
export type * from "./non-gtin-numeric-identifier-descriptor";
|
|
39
|
+
export type * from "./serializable-numeric-identifier-descriptor";
|
|
40
|
+
export type * from "./non-numeric-identifier-descriptor";
|
|
41
|
+
export * from "./descriptors";
|
|
42
|
+
|
|
43
|
+
export type * from "./identifier-validator";
|
|
44
|
+
export type * from "./numeric-identifier-validator";
|
|
45
|
+
export { GTINLevels, type GTINLevel, type RCNReference, GTINValidator } from "./gtin-validator";
|
|
46
|
+
export * from "./non-gtin-numeric-identifier-validator";
|
|
47
|
+
export * from "./serializable-numeric-identifier-validator";
|
|
48
|
+
export * from "./non-numeric-identifier-validator";
|
|
49
|
+
export * from "./validators";
|
|
50
|
+
|
|
51
|
+
export type * from "./identifier-creator";
|
|
52
|
+
export type * from "./numeric-identifier-creator";
|
|
53
|
+
export * from "./gtin-creator";
|
|
54
|
+
export * from "./non-gtin-numeric-identifier-creator";
|
|
55
|
+
export * from "./serializable-numeric-identifier-creator";
|
|
56
|
+
export * from "./non-numeric-identifier-creator";
|
|
57
|
+
export * from "./creators";
|
|
58
|
+
|
|
59
|
+
export * from "./prefix-manager";
|
|
@@ -30,7 +30,6 @@ export default {
|
|
|
30
30
|
gs1CompanyPrefix: "GS1 Company Prefix",
|
|
31
31
|
upcCompanyPrefix: "U.P.C. Company Prefix",
|
|
32
32
|
gs18Prefix: "GS1-8 Prefix",
|
|
33
|
-
invalidPrefixType: "Invalid prefix type",
|
|
34
33
|
gs1CompanyPrefixCantStartWith0: "GS1 Company Prefix can't start with \"0\"",
|
|
35
34
|
gs1CompanyPrefixCantStartWith00000: "GS1 Company Prefix can't start with \"00000\"",
|
|
36
35
|
gs1CompanyPrefixCantStartWith000000: "GS1 Company Prefix can't start with \"000000\"",
|
|
@@ -30,7 +30,6 @@ export default {
|
|
|
30
30
|
gs1CompanyPrefix: "Préfixe de l'entreprise GS1",
|
|
31
31
|
upcCompanyPrefix: "Préfixe de l'entreprise U.P.C.",
|
|
32
32
|
gs18Prefix: "Préfixe GS1-8",
|
|
33
|
-
invalidPrefixType: "Type de préfixe invalide",
|
|
34
33
|
gs1CompanyPrefixCantStartWith0: "Le préfixe de l'entreprise GS1 ne peut pas commencer par \"0\"",
|
|
35
34
|
gs1CompanyPrefixCantStartWith00000: "Le préfixe de l'entreprise GS1 ne peut pas commencer par \"00000\"",
|
|
36
35
|
gs1CompanyPrefixCantStartWith000000: "Le préfixe de l'entreprise GS1 ne peut pas commencer par \"000000\"",
|
package/src/locale/i18n.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { i18nCoreInit, type I18nEnvironment } from "@aidc-toolkit/core";
|
|
2
2
|
import { i18nUtilityInit, utilityResources } from "@aidc-toolkit/utility";
|
|
3
3
|
import i18next, { type i18n, type Resource } from "i18next";
|
|
4
|
-
import enLocaleResources from "./en/locale-resources
|
|
5
|
-
import frLocaleResources from "./fr/locale-resources
|
|
4
|
+
import enLocaleResources from "./en/locale-resources";
|
|
5
|
+
import frLocaleResources from "./fr/locale-resources";
|
|
6
6
|
|
|
7
7
|
export const gs1NS = "aidct_gs1";
|
|
8
8
|
|
|
@@ -11,8 +11,6 @@ export const gs1NS = "aidct_gs1";
|
|
|
11
11
|
*/
|
|
12
12
|
export type GS1LocaleResources = typeof enLocaleResources;
|
|
13
13
|
|
|
14
|
-
i18nAssertValidResources(enLocaleResources, "fr", frLocaleResources);
|
|
15
|
-
|
|
16
14
|
/**
|
|
17
15
|
* GS1 resources.
|
|
18
16
|
*/
|
package/src/locale/i18next.d.ts
CHANGED