@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.
Files changed (155) hide show
  1. package/dist/index.cjs +4813 -0
  2. package/dist/index.cjs.map +1 -0
  3. package/dist/index.d.cts +1949 -0
  4. package/dist/index.d.ts +1949 -36
  5. package/dist/index.js +4727 -19
  6. package/dist/index.js.map +1 -1
  7. package/package.json +8 -12
  8. package/src/abstract-identifier-creator.ts +97 -0
  9. package/src/abstract-identifier-validator.ts +140 -0
  10. package/{dist/numeric-identifier-creator.js → src/abstract-numeric-identifier-creator.ts} +61 -28
  11. package/src/abstract-numeric-identifier-validator.ts +69 -0
  12. package/src/character-set.ts +24 -9
  13. package/src/check.ts +9 -9
  14. package/src/content-character-set.ts +29 -0
  15. package/src/creators.ts +113 -0
  16. package/src/descriptors.ts +332 -0
  17. package/src/gtin-creator.ts +9 -8
  18. package/src/gtin-descriptor.ts +18 -0
  19. package/src/gtin-type.ts +50 -0
  20. package/src/gtin-validator.ts +40 -64
  21. package/src/identifier-creator.ts +11 -75
  22. package/src/identifier-descriptor.ts +30 -0
  23. package/src/identifier-type.ts +6 -1
  24. package/src/identifier-validator.ts +12 -188
  25. package/src/index.ts +43 -19
  26. package/src/locale/en/locale-resources.ts +0 -1
  27. package/src/locale/fr/locale-resources.ts +0 -1
  28. package/src/locale/i18n.ts +3 -5
  29. package/src/locale/i18next.d.ts +1 -1
  30. package/src/non-gtin-numeric-identifier-creator.ts +6 -12
  31. package/src/non-gtin-numeric-identifier-descriptor.ts +24 -0
  32. package/src/non-gtin-numeric-identifier-type.ts +7 -0
  33. package/src/non-gtin-numeric-identifier-validator.ts +8 -42
  34. package/src/non-numeric-identifier-creator.ts +8 -18
  35. package/src/non-numeric-identifier-descriptor.ts +29 -0
  36. package/src/non-numeric-identifier-type.ts +7 -0
  37. package/src/non-numeric-identifier-validator.ts +17 -55
  38. package/src/numeric-identifier-creator.ts +20 -163
  39. package/src/numeric-identifier-descriptor.ts +23 -0
  40. package/src/numeric-identifier-type.ts +44 -0
  41. package/src/numeric-identifier-validator.ts +13 -116
  42. package/src/prefix-manager.ts +94 -152
  43. package/src/prefix-provider.ts +3 -3
  44. package/src/prefix-type.ts +6 -1
  45. package/src/prefix-validator.ts +143 -81
  46. package/src/serializable-numeric-identifier-creator.ts +5 -15
  47. package/src/serializable-numeric-identifier-descriptor.ts +29 -0
  48. package/src/serializable-numeric-identifier-type.ts +9 -0
  49. package/src/serializable-numeric-identifier-validator.ts +18 -46
  50. package/src/validators.ts +203 -0
  51. package/test/creator.test.ts +4 -6
  52. package/test/gtin-creator.ts +6 -2
  53. package/test/gtin-validator.test.ts +6 -9
  54. package/test/identifier-creator.ts +5 -4
  55. package/test/identifier-validator.ts +2 -2
  56. package/test/non-gtin-numeric-identifier-creator.ts +9 -93
  57. package/test/non-gtin-numeric-identifier-validator.ts +2 -2
  58. package/test/non-numeric-identifier-creator.ts +93 -0
  59. package/test/non-numeric-identifier-validator.ts +2 -2
  60. package/test/numeric-identifier-creator.ts +11 -5
  61. package/test/numeric-identifier-validator.ts +4 -8
  62. package/test/prefix-manager.test.ts +1 -1
  63. package/test/serializable-numeric-identifier-creator.ts +11 -3
  64. package/test/serializable-numeric-identifier-validator.ts +2 -2
  65. package/test/validator.test.ts +63 -39
  66. package/tsup.config.ts +3 -0
  67. package/typedoc.json +1 -3
  68. package/dist/character-set.d.ts +0 -22
  69. package/dist/character-set.d.ts.map +0 -1
  70. package/dist/character-set.js +0 -36
  71. package/dist/character-set.js.map +0 -1
  72. package/dist/check.d.ts +0 -81
  73. package/dist/check.d.ts.map +0 -1
  74. package/dist/check.js +0 -218
  75. package/dist/check.js.map +0 -1
  76. package/dist/gtin-creator.d.ts +0 -68
  77. package/dist/gtin-creator.d.ts.map +0 -1
  78. package/dist/gtin-creator.js +0 -158
  79. package/dist/gtin-creator.js.map +0 -1
  80. package/dist/gtin-validator.d.ts +0 -202
  81. package/dist/gtin-validator.d.ts.map +0 -1
  82. package/dist/gtin-validator.js +0 -470
  83. package/dist/gtin-validator.js.map +0 -1
  84. package/dist/identifier-creator.d.ts +0 -72
  85. package/dist/identifier-creator.d.ts.map +0 -1
  86. package/dist/identifier-creator.js +0 -50
  87. package/dist/identifier-creator.js.map +0 -1
  88. package/dist/identifier-type.d.ts +0 -58
  89. package/dist/identifier-type.d.ts.map +0 -1
  90. package/dist/identifier-type.js +0 -54
  91. package/dist/identifier-type.js.map +0 -1
  92. package/dist/identifier-validator.d.ts +0 -174
  93. package/dist/identifier-validator.d.ts.map +0 -1
  94. package/dist/identifier-validator.js +0 -145
  95. package/dist/identifier-validator.js.map +0 -1
  96. package/dist/index.d.ts.map +0 -1
  97. package/dist/locale/en/locale-resources.d.ts +0 -43
  98. package/dist/locale/en/locale-resources.d.ts.map +0 -1
  99. package/dist/locale/en/locale-resources.js +0 -42
  100. package/dist/locale/en/locale-resources.js.map +0 -1
  101. package/dist/locale/fr/locale-resources.d.ts +0 -43
  102. package/dist/locale/fr/locale-resources.d.ts.map +0 -1
  103. package/dist/locale/fr/locale-resources.js +0 -42
  104. package/dist/locale/fr/locale-resources.js.map +0 -1
  105. package/dist/locale/i18n.d.ts +0 -27
  106. package/dist/locale/i18n.d.ts.map +0 -1
  107. package/dist/locale/i18n.js +0 -37
  108. package/dist/locale/i18n.js.map +0 -1
  109. package/dist/non-gtin-numeric-identifier-creator.d.ts +0 -30
  110. package/dist/non-gtin-numeric-identifier-creator.d.ts.map +0 -1
  111. package/dist/non-gtin-numeric-identifier-creator.js +0 -30
  112. package/dist/non-gtin-numeric-identifier-creator.js.map +0 -1
  113. package/dist/non-gtin-numeric-identifier-validator.d.ts +0 -41
  114. package/dist/non-gtin-numeric-identifier-validator.d.ts.map +0 -1
  115. package/dist/non-gtin-numeric-identifier-validator.js +0 -40
  116. package/dist/non-gtin-numeric-identifier-validator.js.map +0 -1
  117. package/dist/non-numeric-identifier-creator.d.ts +0 -55
  118. package/dist/non-numeric-identifier-creator.d.ts.map +0 -1
  119. package/dist/non-numeric-identifier-creator.js +0 -93
  120. package/dist/non-numeric-identifier-creator.js.map +0 -1
  121. package/dist/non-numeric-identifier-validator.d.ts +0 -78
  122. package/dist/non-numeric-identifier-validator.d.ts.map +0 -1
  123. package/dist/non-numeric-identifier-validator.js +0 -95
  124. package/dist/non-numeric-identifier-validator.js.map +0 -1
  125. package/dist/numeric-identifier-creator.d.ts +0 -121
  126. package/dist/numeric-identifier-creator.d.ts.map +0 -1
  127. package/dist/numeric-identifier-creator.js.map +0 -1
  128. package/dist/numeric-identifier-validator.d.ts +0 -76
  129. package/dist/numeric-identifier-validator.d.ts.map +0 -1
  130. package/dist/numeric-identifier-validator.js +0 -84
  131. package/dist/numeric-identifier-validator.js.map +0 -1
  132. package/dist/prefix-manager.d.ts +0 -224
  133. package/dist/prefix-manager.d.ts.map +0 -1
  134. package/dist/prefix-manager.js +0 -369
  135. package/dist/prefix-manager.js.map +0 -1
  136. package/dist/prefix-provider.d.ts +0 -27
  137. package/dist/prefix-provider.d.ts.map +0 -1
  138. package/dist/prefix-provider.js +0 -2
  139. package/dist/prefix-provider.js.map +0 -1
  140. package/dist/prefix-type.d.ts +0 -22
  141. package/dist/prefix-type.d.ts.map +0 -1
  142. package/dist/prefix-type.js +0 -18
  143. package/dist/prefix-type.js.map +0 -1
  144. package/dist/prefix-validator.d.ts +0 -58
  145. package/dist/prefix-validator.d.ts.map +0 -1
  146. package/dist/prefix-validator.js +0 -154
  147. package/dist/prefix-validator.js.map +0 -1
  148. package/dist/serializable-numeric-identifier-creator.d.ts +0 -86
  149. package/dist/serializable-numeric-identifier-creator.d.ts.map +0 -1
  150. package/dist/serializable-numeric-identifier-creator.js +0 -116
  151. package/dist/serializable-numeric-identifier-creator.js.map +0 -1
  152. package/dist/serializable-numeric-identifier-validator.d.ts +0 -79
  153. package/dist/serializable-numeric-identifier-validator.d.ts.map +0 -1
  154. package/dist/serializable-numeric-identifier-validator.js +0 -99
  155. 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.23-beta",
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:core": "rimraf dist && tsc --project",
24
- "build:dev": "npm run build:core -- node_modules/@aidc-toolkit/dev/tsconfig-build-dev.json",
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.14"
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 { CharacterSetCreator, Exclusions, NUMERIC_CREATOR } from "@aidc-toolkit/utility";
2
- import { checkDigit, checkDigitSum } from "./check.js";
3
- import { AbstractIdentifierCreator } from "./identifier-creator.js";
4
- import { LeaderTypes } from "./numeric-identifier-validator.js";
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
- * creator.
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
- * Get the tweak for sparse creation.
62
+ * @inheritDoc
40
63
  */
41
- get tweak() {
64
+ get tweak(): bigint {
42
65
  return this._tweak;
43
66
  }
67
+
44
68
  /**
45
- * Set the tweak for sparse creation.
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
- * is applied to the calculation of the check digit.
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
+ }
@@ -1,8 +1,8 @@
1
- import { CharacterSetCreator, type CharacterSetValidator, Exclusions } from "@aidc-toolkit/utility";
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 https://www.gs1.org/genspecs | GS1
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 https://www.gs1.org/genspecs |
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 https://www.gs1.org/genspecs | GS1
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 https://www.gs1.org/genspecs |
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.js";
3
- import { i18nextGS1 } from "./locale/i18n.js";
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 https://www.gs1.org/genspecs |
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 https://www.gs1.org/genspecs | GS1
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 section 7.9.3 of the {@link
133
- * https://www.gs1.org/genspecs | GS1 General Specifications}.
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://www.gs1.org/genspecs | GS1 General Specifications}.
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];