@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/package.json
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aidc-toolkit/gs1",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.25-beta",
|
|
4
4
|
"description": "GS1 AIDC Toolkit",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"homepage": "https://aidc-toolkit.com/",
|
|
8
|
-
"repository":
|
|
9
|
-
"type": "git",
|
|
10
|
-
"url": "git+https://github.com/aidc-toolkit/gs1.git"
|
|
11
|
-
},
|
|
8
|
+
"repository": "aidc-toolkit/gs1",
|
|
12
9
|
"bugs": {
|
|
13
10
|
"url": "https://github.com/aidc-toolkit/gs1/issues"
|
|
14
11
|
},
|
|
@@ -20,19 +17,18 @@
|
|
|
20
17
|
},
|
|
21
18
|
"scripts": {
|
|
22
19
|
"lint": "eslint",
|
|
23
|
-
"build:
|
|
24
|
-
"build:
|
|
25
|
-
"build:release": "npm run build:core -- node_modules/@aidc-toolkit/dev/tsconfig-build.json",
|
|
20
|
+
"build:dev": "tsup --define.mode=dev",
|
|
21
|
+
"build:release": "tsup",
|
|
26
22
|
"build:doc": "npm run build:dev",
|
|
27
23
|
"test": "vitest run"
|
|
28
24
|
},
|
|
29
25
|
"devDependencies": {
|
|
30
|
-
"@aidc-toolkit/dev": "beta",
|
|
31
|
-
"vitest": "^4.0.
|
|
26
|
+
"@aidc-toolkit/dev": "1.0.25-beta",
|
|
27
|
+
"vitest": "^4.0.15"
|
|
32
28
|
},
|
|
33
29
|
"dependencies": {
|
|
34
|
-
"@aidc-toolkit/core": "beta",
|
|
35
|
-
"@aidc-toolkit/utility": "beta",
|
|
30
|
+
"@aidc-toolkit/core": "1.0.25-beta",
|
|
31
|
+
"@aidc-toolkit/utility": "1.0.25-beta",
|
|
36
32
|
"ts-mixer": "^6.0.4"
|
|
37
33
|
}
|
|
38
34
|
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import type { CharacterSetCreator } from "@aidc-toolkit/utility";
|
|
2
|
+
import type { IdentifierCreator } from "./identifier-creator";
|
|
3
|
+
import type { IdentifierDescriptor } from "./identifier-descriptor";
|
|
4
|
+
import type { IdentifierValidation } from "./identifier-validator";
|
|
5
|
+
import type { PrefixProvider } from "./prefix-provider";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Abstract identifier creator. Implements common functionality for an identifier creator, bound to a {@link
|
|
9
|
+
* PrefixProvider | prefix provider}.
|
|
10
|
+
*
|
|
11
|
+
* @template TIdentifierDescriptor
|
|
12
|
+
* Identifier descriptor type.
|
|
13
|
+
*
|
|
14
|
+
* @template TIdentifierValidation
|
|
15
|
+
* Identifier validation type.
|
|
16
|
+
*/
|
|
17
|
+
export abstract class AbstractIdentifierCreator<TIdentifierDescriptor extends IdentifierDescriptor, TIdentifierValidation extends IdentifierValidation> implements IdentifierCreator<TIdentifierDescriptor, TIdentifierValidation> {
|
|
18
|
+
/**
|
|
19
|
+
* Prefix provider.
|
|
20
|
+
*/
|
|
21
|
+
private _prefixProvider!: PrefixProvider;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Reference length.
|
|
25
|
+
*/
|
|
26
|
+
private _referenceLength!: number;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Initialize the prefix provider. This method is in lieu of a constructor due to the mixin architecture.
|
|
30
|
+
*
|
|
31
|
+
* @param prefixProvider
|
|
32
|
+
* Prefix provider.
|
|
33
|
+
*
|
|
34
|
+
* @param prefix
|
|
35
|
+
* Prefix within prefix provider to use to calculate reference length.
|
|
36
|
+
*
|
|
37
|
+
* @param checkAllowance
|
|
38
|
+
* Number of characters to allow for check digit or check character pair.
|
|
39
|
+
*/
|
|
40
|
+
protected init(prefixProvider: PrefixProvider, prefix: string, checkAllowance: number): void {
|
|
41
|
+
this._prefixProvider = prefixProvider;
|
|
42
|
+
|
|
43
|
+
// Reference length allows for prefix and optionally check digit or check character pair.
|
|
44
|
+
this._referenceLength = this.length - prefix.length - checkAllowance;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @inheritDoc
|
|
49
|
+
*/
|
|
50
|
+
abstract get identifierType(): TIdentifierDescriptor["identifierType"];
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @inheritDoc
|
|
54
|
+
*/
|
|
55
|
+
abstract get prefixType(): TIdentifierDescriptor["prefixType"];
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @inheritDoc
|
|
59
|
+
*/
|
|
60
|
+
abstract get length(): number;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* @inheritDoc
|
|
64
|
+
*/
|
|
65
|
+
abstract get referenceCharacterSet(): TIdentifierDescriptor["referenceCharacterSet"];
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* @inheritDoc
|
|
69
|
+
*/
|
|
70
|
+
abstract get referenceCreator(): CharacterSetCreator;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* @inheritDoc
|
|
74
|
+
*/
|
|
75
|
+
get prefixProvider(): PrefixProvider {
|
|
76
|
+
return this._prefixProvider;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* @inheritDoc
|
|
81
|
+
*/
|
|
82
|
+
get prefix(): string {
|
|
83
|
+
return this.prefixProvider.gs1CompanyPrefix;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @inheritDoc
|
|
88
|
+
*/
|
|
89
|
+
get referenceLength(): number {
|
|
90
|
+
return this._referenceLength;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* @inheritDoc
|
|
95
|
+
*/
|
|
96
|
+
abstract validate(identifier: string, validation?: TIdentifierValidation): void;
|
|
97
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { type CharacterSetCreator, NUMERIC_CREATOR } from "@aidc-toolkit/utility";
|
|
2
|
+
import { AI39_CREATOR, AI82_CREATOR } from "./character-set";
|
|
3
|
+
import { type ContentCharacterSet, ContentCharacterSets } from "./content-character-set";
|
|
4
|
+
import type { IdentifierDescriptor } from "./identifier-descriptor";
|
|
5
|
+
import type { IdentifierValidation, IdentifierValidator } from "./identifier-validator";
|
|
6
|
+
import { PrefixTypes } from "./prefix-type";
|
|
7
|
+
import { PrefixValidator } from "./prefix-validator";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Abstract identifier validator. Implements common functionality for an identifier validator.
|
|
11
|
+
*
|
|
12
|
+
* @template TIdentifierDescriptor
|
|
13
|
+
* Identifier descriptor type.
|
|
14
|
+
*
|
|
15
|
+
* @template TIdentifierValidation
|
|
16
|
+
* Identifier validation type.
|
|
17
|
+
*/
|
|
18
|
+
export abstract class AbstractIdentifierValidator<TIdentifierDescriptor extends IdentifierDescriptor, TIdentifierValidation extends IdentifierValidation> implements IdentifierValidator<TIdentifierDescriptor, TIdentifierValidation> {
|
|
19
|
+
private static readonly CHARACTER_SET_CREATORS: Record<ContentCharacterSet, CharacterSetCreator> = {
|
|
20
|
+
[ContentCharacterSets.Numeric]: NUMERIC_CREATOR,
|
|
21
|
+
[ContentCharacterSets.AI82]: AI82_CREATOR,
|
|
22
|
+
[ContentCharacterSets.AI39]: AI39_CREATOR
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Identifier type.
|
|
27
|
+
*/
|
|
28
|
+
private readonly _identifierType: TIdentifierDescriptor["identifierType"];
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Length.
|
|
32
|
+
*/
|
|
33
|
+
private readonly _length: number;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Reference character set.
|
|
37
|
+
*/
|
|
38
|
+
private readonly _referenceCharacterSet: TIdentifierDescriptor["referenceCharacterSet"];
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Reference creator.
|
|
42
|
+
*/
|
|
43
|
+
private readonly _referenceCreator: CharacterSetCreator;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Get the character set creator for a character set.
|
|
47
|
+
*
|
|
48
|
+
* @param characterSet
|
|
49
|
+
* Character set.
|
|
50
|
+
*
|
|
51
|
+
* @returns
|
|
52
|
+
* Character set creator.
|
|
53
|
+
*/
|
|
54
|
+
protected static creatorFor(characterSet: ContentCharacterSet): CharacterSetCreator {
|
|
55
|
+
return AbstractIdentifierValidator.CHARACTER_SET_CREATORS[characterSet];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Constructor.
|
|
60
|
+
*
|
|
61
|
+
* @param identifierDescriptor
|
|
62
|
+
* Identifier descriptor.
|
|
63
|
+
*/
|
|
64
|
+
protected constructor(identifierDescriptor: IdentifierDescriptor) {
|
|
65
|
+
this._identifierType = identifierDescriptor.identifierType;
|
|
66
|
+
this._length = identifierDescriptor.length;
|
|
67
|
+
this._referenceCharacterSet = identifierDescriptor.referenceCharacterSet;
|
|
68
|
+
this._referenceCreator = AbstractIdentifierValidator.creatorFor(identifierDescriptor.referenceCharacterSet);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* @inheritDoc
|
|
73
|
+
*/
|
|
74
|
+
get identifierType(): TIdentifierDescriptor["identifierType"] {
|
|
75
|
+
return this._identifierType;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* @inheritDoc
|
|
80
|
+
*/
|
|
81
|
+
get prefixType(): TIdentifierDescriptor["prefixType"] {
|
|
82
|
+
// All identifier types except GTIN support only the GS1 Company Prefix.
|
|
83
|
+
return PrefixTypes.GS1CompanyPrefix;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @inheritDoc
|
|
88
|
+
*/
|
|
89
|
+
get length(): number {
|
|
90
|
+
return this._length;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* @inheritDoc
|
|
95
|
+
*/
|
|
96
|
+
get referenceCharacterSet(): TIdentifierDescriptor["referenceCharacterSet"] {
|
|
97
|
+
return this._referenceCharacterSet;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* @inheritDoc
|
|
102
|
+
*/
|
|
103
|
+
get referenceCreator(): CharacterSetCreator {
|
|
104
|
+
return this._referenceCreator;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Pad an identifier on the left with zero-value character for validation purposes. This is done to align an
|
|
109
|
+
* identifier with a position offset for any error message that may be thrown by the reference validator.
|
|
110
|
+
*
|
|
111
|
+
* @param identifier
|
|
112
|
+
* Identifier.
|
|
113
|
+
*
|
|
114
|
+
* @param positionOffset
|
|
115
|
+
* Position offset within a larger string.
|
|
116
|
+
*
|
|
117
|
+
* @returns
|
|
118
|
+
* Padded identifier.
|
|
119
|
+
*/
|
|
120
|
+
protected padIdentifier(identifier: string, positionOffset?: number): string {
|
|
121
|
+
// Identifier is returned as is if position offset is undefined.
|
|
122
|
+
return positionOffset === undefined ? identifier : this.referenceCreator.character(0).repeat(positionOffset).concat(identifier);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Validate the prefix within an identifier.
|
|
127
|
+
*
|
|
128
|
+
* @param partialIdentifier
|
|
129
|
+
* Partial identifier.
|
|
130
|
+
*
|
|
131
|
+
* @param positionOffset
|
|
132
|
+
* Position offset within a larger string.
|
|
133
|
+
*/
|
|
134
|
+
protected validatePrefix(partialIdentifier: string, positionOffset?: number): void {
|
|
135
|
+
// Delegate to prefix validator with support for U.P.C. Company Prefix but not GS1-8 Prefix.
|
|
136
|
+
PrefixValidator.validate(this.prefixType, true, false, partialIdentifier, true, this.referenceCharacterSet === ContentCharacterSets.Numeric, positionOffset);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
abstract validate(identifier: string, validation?: TIdentifierValidation): void;
|
|
140
|
+
}
|
|
@@ -1,20 +1,35 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import {
|
|
2
|
+
CharacterSetCreator,
|
|
3
|
+
Exclusions,
|
|
4
|
+
NUMERIC_CREATOR,
|
|
5
|
+
type TransformerInput,
|
|
6
|
+
type TransformerOutput
|
|
7
|
+
} from "@aidc-toolkit/utility";
|
|
8
|
+
import { AbstractIdentifierCreator } from "./abstract-identifier-creator";
|
|
9
|
+
import { checkDigit, checkDigitSum } from "./check";
|
|
10
|
+
import type { NumericIdentifierCreator } from "./numeric-identifier-creator";
|
|
11
|
+
import type { NumericIdentifierDescriptor } from "./numeric-identifier-descriptor";
|
|
12
|
+
import { type LeaderType, LeaderTypes } from "./numeric-identifier-type";
|
|
13
|
+
import type { NumericIdentifierValidation } from "./numeric-identifier-validator";
|
|
14
|
+
import type { PrefixProvider } from "./prefix-provider";
|
|
15
|
+
|
|
5
16
|
/**
|
|
6
|
-
* Abstract numeric identifier creator. Implements common functionality for a numeric identifier
|
|
7
|
-
*
|
|
17
|
+
* Abstract numeric identifier creator. Implements common functionality for a numeric identifier creator.
|
|
18
|
+
*
|
|
19
|
+
* @template TNumericIdentifierDescriptor
|
|
20
|
+
* Numeric identifier descriptor type.
|
|
8
21
|
*/
|
|
9
|
-
export class AbstractNumericIdentifierCreator extends AbstractIdentifierCreator {
|
|
22
|
+
export abstract class AbstractNumericIdentifierCreator<TNumericIdentifierDescriptor extends NumericIdentifierDescriptor> extends AbstractIdentifierCreator<TNumericIdentifierDescriptor, NumericIdentifierValidation> implements NumericIdentifierCreator<TNumericIdentifierDescriptor> {
|
|
10
23
|
/**
|
|
11
24
|
* Capacity.
|
|
12
25
|
*/
|
|
13
|
-
_capacity;
|
|
26
|
+
private _capacity!: number;
|
|
27
|
+
|
|
14
28
|
/**
|
|
15
29
|
* Tweak for sparse creation.
|
|
16
30
|
*/
|
|
17
|
-
_tweak = 0n;
|
|
31
|
+
private _tweak = 0n;
|
|
32
|
+
|
|
18
33
|
/**
|
|
19
34
|
* Initialize the prefix provider. This method is in lieu of a constructor due to the mixin architecture.
|
|
20
35
|
*
|
|
@@ -24,29 +39,39 @@ export class AbstractNumericIdentifierCreator extends AbstractIdentifierCreator
|
|
|
24
39
|
* @param prefix
|
|
25
40
|
* Prefix within prefix manager to use to calculate reference length.
|
|
26
41
|
*/
|
|
27
|
-
init(prefixProvider, prefix) {
|
|
42
|
+
protected override init(prefixProvider: PrefixProvider, prefix: string): void {
|
|
28
43
|
super.init(prefixProvider, prefix, 1);
|
|
44
|
+
|
|
29
45
|
// Capacity is always in number range.
|
|
30
46
|
this._capacity = Number(CharacterSetCreator.powerOf10(this.referenceLength));
|
|
31
47
|
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Get the leader type.
|
|
51
|
+
*/
|
|
52
|
+
abstract get leaderType(): LeaderType;
|
|
53
|
+
|
|
32
54
|
/**
|
|
33
55
|
* @inheritDoc
|
|
34
56
|
*/
|
|
35
|
-
get capacity() {
|
|
57
|
+
get capacity(): number {
|
|
36
58
|
return this._capacity;
|
|
37
59
|
}
|
|
60
|
+
|
|
38
61
|
/**
|
|
39
|
-
*
|
|
62
|
+
* @inheritDoc
|
|
40
63
|
*/
|
|
41
|
-
get tweak() {
|
|
64
|
+
get tweak(): bigint {
|
|
42
65
|
return this._tweak;
|
|
43
66
|
}
|
|
67
|
+
|
|
44
68
|
/**
|
|
45
|
-
*
|
|
69
|
+
* @inheritDoc
|
|
46
70
|
*/
|
|
47
|
-
set tweak(value) {
|
|
71
|
+
set tweak(value: bigint) {
|
|
48
72
|
this._tweak = value;
|
|
49
73
|
}
|
|
74
|
+
|
|
50
75
|
/**
|
|
51
76
|
* Build an identifier from a reference by merging it with the prefix and adding the check digit.
|
|
52
77
|
*
|
|
@@ -56,16 +81,19 @@ export class AbstractNumericIdentifierCreator extends AbstractIdentifierCreator
|
|
|
56
81
|
* @returns
|
|
57
82
|
* Identifier.
|
|
58
83
|
*/
|
|
59
|
-
buildIdentifier(reference) {
|
|
84
|
+
private buildIdentifier(reference: string): string {
|
|
60
85
|
const partialIdentifier = this.leaderType === LeaderTypes.ExtensionDigit ? reference.substring(0, 1) + this.prefix + reference.substring(1) : this.prefix + reference;
|
|
86
|
+
|
|
61
87
|
return partialIdentifier + checkDigit(partialIdentifier);
|
|
62
88
|
}
|
|
89
|
+
|
|
63
90
|
/**
|
|
64
91
|
* @inheritDoc
|
|
65
92
|
*/
|
|
66
|
-
create(valueOrValues, sparse = false) {
|
|
93
|
+
create<TTransformerInput extends TransformerInput<number | bigint>>(valueOrValues: TTransformerInput, sparse = false): TransformerOutput<TTransformerInput, string> {
|
|
67
94
|
return NUMERIC_CREATOR.create(this.referenceLength, valueOrValues, Exclusions.None, sparse ? this.tweak : undefined, reference => this.buildIdentifier(reference));
|
|
68
95
|
}
|
|
96
|
+
|
|
69
97
|
/**
|
|
70
98
|
* Create all identifiers from a partial identifier. Call is recursive until remaining reference
|
|
71
99
|
* length is 0.
|
|
@@ -77,8 +105,8 @@ export class AbstractNumericIdentifierCreator extends AbstractIdentifierCreator
|
|
|
77
105
|
* Remaining reference length. Initial value is `this.referenceLength`.
|
|
78
106
|
*
|
|
79
107
|
* @param extensionWeight
|
|
80
|
-
* If this value is not zero, the identifier has an extension digit, this call is setting it, and this value
|
|
81
|
-
*
|
|
108
|
+
* If this value is not zero, the identifier has an extension digit, this call is setting it, and this value is
|
|
109
|
+
* applied to the calculation of the check digit.
|
|
82
110
|
*
|
|
83
111
|
* @param weight
|
|
84
112
|
* If the extension weight is zero, this value is applied to the calculation of the check digit.
|
|
@@ -89,41 +117,47 @@ export class AbstractNumericIdentifierCreator extends AbstractIdentifierCreator
|
|
|
89
117
|
* @yields
|
|
90
118
|
* Identifier.
|
|
91
119
|
*/
|
|
92
|
-
static *createAllPartial(partialIdentifier, remainingReferenceLength, extensionWeight, weight, partialCheckDigitSum) {
|
|
120
|
+
private static * createAllPartial(partialIdentifier: string, remainingReferenceLength: number, extensionWeight: number, weight: number, partialCheckDigitSum: number): Generator<string> {
|
|
93
121
|
if (remainingReferenceLength === 0) {
|
|
94
122
|
// Finalize check digit calculation and append.
|
|
95
123
|
yield partialIdentifier + NUMERIC_CREATOR.character(9 - (partialCheckDigitSum + 9) % 10);
|
|
96
|
-
}
|
|
97
|
-
else {
|
|
124
|
+
} else {
|
|
98
125
|
const nextRemainingReferenceLength = remainingReferenceLength - 1;
|
|
126
|
+
|
|
99
127
|
let nextPartialCheckDigitSum = partialCheckDigitSum;
|
|
128
|
+
|
|
100
129
|
if (extensionWeight !== 0) {
|
|
101
130
|
// Apply every digit to the extension digit.
|
|
102
131
|
for (const c of NUMERIC_CREATOR.characterSet) {
|
|
103
|
-
yield* AbstractNumericIdentifierCreator.createAllPartial(c + partialIdentifier, nextRemainingReferenceLength, 0, weight, nextPartialCheckDigitSum);
|
|
132
|
+
yield * AbstractNumericIdentifierCreator.createAllPartial(c + partialIdentifier, nextRemainingReferenceLength, 0, weight, nextPartialCheckDigitSum);
|
|
133
|
+
|
|
104
134
|
nextPartialCheckDigitSum += extensionWeight;
|
|
105
135
|
}
|
|
106
|
-
}
|
|
107
|
-
else {
|
|
136
|
+
} else {
|
|
108
137
|
const nextWeight = 4 - weight;
|
|
138
|
+
|
|
109
139
|
// Apply every digit to the current character in the identifier.
|
|
110
140
|
for (const c of NUMERIC_CREATOR.characterSet) {
|
|
111
|
-
yield* AbstractNumericIdentifierCreator.createAllPartial(partialIdentifier + c, nextRemainingReferenceLength, 0, nextWeight, nextPartialCheckDigitSum);
|
|
141
|
+
yield * AbstractNumericIdentifierCreator.createAllPartial(partialIdentifier + c, nextRemainingReferenceLength, 0, nextWeight, nextPartialCheckDigitSum);
|
|
142
|
+
|
|
112
143
|
nextPartialCheckDigitSum += weight;
|
|
113
144
|
}
|
|
114
145
|
}
|
|
115
146
|
}
|
|
116
147
|
}
|
|
148
|
+
|
|
117
149
|
/**
|
|
118
150
|
* @inheritDoc
|
|
119
151
|
*/
|
|
120
|
-
createAll() {
|
|
152
|
+
createAll(): Iterable<string> {
|
|
121
153
|
const hasExtensionDigit = this.leaderType === LeaderTypes.ExtensionDigit;
|
|
122
154
|
const prefix = this.prefix;
|
|
123
155
|
const length = this.length;
|
|
124
156
|
const referenceLength = this.referenceLength;
|
|
157
|
+
|
|
125
158
|
// Start weight is for reference excluding extension digit, which has its weight calculated separately.
|
|
126
159
|
const startWeight = 3 - 2 * ((referenceLength + 1 - Number(hasExtensionDigit)) % 2);
|
|
160
|
+
|
|
127
161
|
// Returning separate Iterable object makes iteration repeatable.
|
|
128
162
|
return {
|
|
129
163
|
[Symbol.iterator]() {
|
|
@@ -132,4 +166,3 @@ export class AbstractNumericIdentifierCreator extends AbstractIdentifierCreator
|
|
|
132
166
|
};
|
|
133
167
|
}
|
|
134
168
|
}
|
|
135
|
-
//# sourceMappingURL=numeric-identifier-creator.js.map
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { AbstractIdentifierValidator } from "./abstract-identifier-validator";
|
|
2
|
+
import { hasValidCheckDigit } from "./check";
|
|
3
|
+
import { i18nextGS1 } from "./locale/i18n";
|
|
4
|
+
import type { NumericIdentifierDescriptor } from "./numeric-identifier-descriptor";
|
|
5
|
+
import { LeaderTypes } from "./numeric-identifier-type";
|
|
6
|
+
import type { NumericIdentifierValidation, NumericIdentifierValidator } from "./numeric-identifier-validator";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Abstract numeric identifier validator.
|
|
10
|
+
*
|
|
11
|
+
* @template TNumericIdentifierDescriptor
|
|
12
|
+
* Numeric identifier descriptor type.
|
|
13
|
+
*/
|
|
14
|
+
export abstract class AbstractNumericIdentifierValidator<TNumericIdentifierDescriptor extends NumericIdentifierDescriptor> extends AbstractIdentifierValidator<TNumericIdentifierDescriptor, NumericIdentifierValidation> implements NumericIdentifierValidator<TNumericIdentifierDescriptor> {
|
|
15
|
+
/**
|
|
16
|
+
* Leader type.
|
|
17
|
+
*/
|
|
18
|
+
private readonly _leaderType: TNumericIdentifierDescriptor["leaderType"];
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Prefix position, determined by the leader type.
|
|
22
|
+
*/
|
|
23
|
+
private readonly _prefixPosition: number;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Constructor.
|
|
27
|
+
*
|
|
28
|
+
* @param identifierDescriptor
|
|
29
|
+
* Identifier descriptor.
|
|
30
|
+
*/
|
|
31
|
+
constructor(identifierDescriptor: NumericIdentifierDescriptor) {
|
|
32
|
+
super(identifierDescriptor);
|
|
33
|
+
|
|
34
|
+
this._leaderType = identifierDescriptor.leaderType;
|
|
35
|
+
this._prefixPosition = Number(this.leaderType === LeaderTypes.ExtensionDigit);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @inheritDoc
|
|
40
|
+
*/
|
|
41
|
+
get leaderType(): TNumericIdentifierDescriptor["leaderType"] {
|
|
42
|
+
return this._leaderType;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @inheritDoc
|
|
47
|
+
*/
|
|
48
|
+
validate(identifier: string, validation?: NumericIdentifierValidation): void {
|
|
49
|
+
// Validate the prefix, with care taken for its position within the identifier.
|
|
50
|
+
if (this._prefixPosition === 0) {
|
|
51
|
+
super.validatePrefix(identifier, validation?.positionOffset);
|
|
52
|
+
} else {
|
|
53
|
+
super.validatePrefix(identifier.substring(this._prefixPosition), validation?.positionOffset === undefined ? this._prefixPosition : validation.positionOffset + this._prefixPosition);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Validate the length.
|
|
57
|
+
if (identifier.length !== this.length) {
|
|
58
|
+
throw new RangeError(i18nextGS1.t("Identifier.identifierTypeLength", {
|
|
59
|
+
identifierType: this.identifierType,
|
|
60
|
+
length: this.length
|
|
61
|
+
}));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Validating the check digit will also validate the characters.
|
|
65
|
+
if (!hasValidCheckDigit(this.padIdentifier(identifier, validation?.positionOffset))) {
|
|
66
|
+
throw new RangeError(i18nextGS1.t("Identifier.invalidCheckDigit"));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
package/src/character-set.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { CharacterSetCreator,
|
|
1
|
+
import { CharacterSetCreator, CharacterSetValidator, Exclusions } from "@aidc-toolkit/utility";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* GS1 AI encodable character set 82 creator as defined in section 7.11 of the {@link
|
|
5
|
-
* General Specifications}. Supports {@linkcode Exclusions.AllNumeric}.
|
|
4
|
+
* GS1 AI encodable character set 82 creator as defined in section 7.11 of the {@link
|
|
5
|
+
* https://ref.gs1.org/standards/genspecs/ | GS1 General Specifications}. Supports {@linkcode Exclusions.AllNumeric}.
|
|
6
6
|
*/
|
|
7
7
|
export const AI82_CREATOR = new CharacterSetCreator([
|
|
8
8
|
"!", "\"", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/",
|
|
@@ -16,14 +16,14 @@ export const AI82_CREATOR = new CharacterSetCreator([
|
|
|
16
16
|
], Exclusions.AllNumeric);
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
|
-
* GS1 AI encodable character set 82 validator as defined in section 7.11 of the {@link
|
|
20
|
-
* GS1 General Specifications}. Supports {@linkcode Exclusions.AllNumeric}.
|
|
19
|
+
* GS1 AI encodable character set 82 validator as defined in section 7.11 of the {@link
|
|
20
|
+
* https://ref.gs1.org/standards/genspecs/ | GS1 General Specifications}. Supports {@linkcode Exclusions.AllNumeric}.
|
|
21
21
|
*/
|
|
22
22
|
export const AI82_VALIDATOR = AI82_CREATOR as CharacterSetValidator;
|
|
23
23
|
|
|
24
24
|
/**
|
|
25
|
-
* GS1 AI encodable character set 39 creator as defined in section 7.11 of the {@link
|
|
26
|
-
* General Specifications}. Supports {@linkcode Exclusions.AllNumeric}.
|
|
25
|
+
* GS1 AI encodable character set 39 creator as defined in section 7.11 of the {@link
|
|
26
|
+
* https://ref.gs1.org/standards/genspecs/ | GS1 General Specifications}. Supports {@linkcode Exclusions.AllNumeric}.
|
|
27
27
|
*/
|
|
28
28
|
export const AI39_CREATOR = new CharacterSetCreator([
|
|
29
29
|
"#", "-", "/",
|
|
@@ -33,7 +33,22 @@ export const AI39_CREATOR = new CharacterSetCreator([
|
|
|
33
33
|
], Exclusions.AllNumeric);
|
|
34
34
|
|
|
35
35
|
/**
|
|
36
|
-
* GS1 AI encodable character set 39 validator as defined in section 7.11 of the {@link
|
|
37
|
-
* GS1 General Specifications}. Supports {@linkcode Exclusions.AllNumeric}.
|
|
36
|
+
* GS1 AI encodable character set 39 validator as defined in section 7.11 of the {@link
|
|
37
|
+
* https://ref.gs1.org/standards/genspecs/ | GS1 General Specifications}. Supports {@linkcode Exclusions.AllNumeric}.
|
|
38
38
|
*/
|
|
39
39
|
export const AI39_VALIDATOR = AI39_CREATOR as CharacterSetValidator;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* GS1 AI encodable character set 64 validator as defined in section 7.11 of the {@link
|
|
43
|
+
* https://ref.gs1.org/standards/genspecs/ | GS1 General Specifications}. Doesn't support any exclusions.
|
|
44
|
+
*/
|
|
45
|
+
export const AI64_VALIDATOR = new CharacterSetValidator([
|
|
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
|
+
]);
|
package/src/check.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { NUMERIC_CREATOR, utilityNS } from "@aidc-toolkit/utility";
|
|
2
|
-
import { AI82_CREATOR } from "./character-set
|
|
3
|
-
import { i18nextGS1 } from "./locale/i18n
|
|
2
|
+
import { AI82_CREATOR } from "./character-set";
|
|
3
|
+
import { i18nextGS1 } from "./locale/i18n";
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Results of multiplying digits by 3.
|
|
@@ -38,8 +38,8 @@ const INVERSE_FIVE_MINUS_WEIGHT_RESULTS: readonly number[] = [
|
|
|
38
38
|
];
|
|
39
39
|
|
|
40
40
|
/**
|
|
41
|
-
* Calculate the check digit sum for a numeric string as per section 7.9.1 of the {@link
|
|
42
|
-
* GS1 General Specifications}.
|
|
41
|
+
* Calculate the check digit sum for a numeric string as per section 7.9.1 of the {@link
|
|
42
|
+
* https://ref.gs1.org/standards/genspecs/ | GS1 General Specifications}.
|
|
43
43
|
*
|
|
44
44
|
* @param exchangeWeights
|
|
45
45
|
* If true, start the weights at 1 instead of 3 on the right.
|
|
@@ -71,8 +71,8 @@ export function checkDigitSum(exchangeWeights: boolean, s: string): number {
|
|
|
71
71
|
}
|
|
72
72
|
|
|
73
73
|
/**
|
|
74
|
-
* Calculate the check digit for a numeric string as per section 7.9.1 of the {@link
|
|
75
|
-
* General Specifications}.
|
|
74
|
+
* Calculate the check digit for a numeric string as per section 7.9.1 of the {@link
|
|
75
|
+
* https://ref.gs1.org/standards/genspecs/ | GS1 General Specifications}.
|
|
76
76
|
*
|
|
77
77
|
* @param s
|
|
78
78
|
* Numeric string.
|
|
@@ -129,8 +129,8 @@ function priceOrWeightSum(weightsResults: ReadonlyArray<readonly number[]>, s: s
|
|
|
129
129
|
}
|
|
130
130
|
|
|
131
131
|
/**
|
|
132
|
-
* Calculate the price or weight check digit for a four-or five-digit numeric string as per
|
|
133
|
-
* https://
|
|
132
|
+
* Calculate the price or weight check digit for a four-or five-digit numeric string as per sections 7.9.2-7.9.4 of the
|
|
133
|
+
* {@link https://ref.gs1.org/standards/genspecs/ | GS1 General Specifications}.
|
|
134
134
|
*
|
|
135
135
|
* @param s
|
|
136
136
|
* Numeric string exactly four or five characters long.
|
|
@@ -192,7 +192,7 @@ const CHECK_CHARACTERS = [
|
|
|
192
192
|
|
|
193
193
|
/**
|
|
194
194
|
* Calculate the check character for a GS1 AI encodable character set 82 string as per section 7.9.5 of the {@link
|
|
195
|
-
* https://
|
|
195
|
+
* https://ref.gs1.org/standards/genspecs/ | GS1 General Specifications}.
|
|
196
196
|
*
|
|
197
197
|
* @param s
|
|
198
198
|
* GS1 AI encodable character set 82 string.
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Character sets supported by the reference portion of an identifier or the serial component of a numeric identifier.
|
|
3
|
+
*/
|
|
4
|
+
export const ContentCharacterSets = {
|
|
5
|
+
/**
|
|
6
|
+
* Numeric.
|
|
7
|
+
*/
|
|
8
|
+
Numeric: "Numeric",
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* GS1 AI encodable character set 82.
|
|
12
|
+
*/
|
|
13
|
+
AI82: "AI82",
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* GS1 AI encodable character set 39.
|
|
17
|
+
*/
|
|
18
|
+
AI39: "AI39"
|
|
19
|
+
} as const;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Content character set key.
|
|
23
|
+
*/
|
|
24
|
+
export type ContentCharacterSetKey = keyof typeof ContentCharacterSets;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Content character set.
|
|
28
|
+
*/
|
|
29
|
+
export type ContentCharacterSet = typeof ContentCharacterSets[ContentCharacterSetKey];
|