@tracepass/gs1-utils 0.1.0
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/LICENSE +202 -0
- package/README.md +53 -0
- package/dist/digital-link.d.ts +35 -0
- package/dist/digital-link.d.ts.map +1 -0
- package/dist/digital-link.js +52 -0
- package/dist/digital-link.js.map +1 -0
- package/dist/gln.d.ts +45 -0
- package/dist/gln.d.ts.map +1 -0
- package/dist/gln.js +74 -0
- package/dist/gln.js.map +1 -0
- package/dist/gtin.d.ts +19 -0
- package/dist/gtin.d.ts.map +1 -0
- package/dist/gtin.js +38 -0
- package/dist/gtin.js.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -0
- package/dist/language.d.ts +21 -0
- package/dist/language.d.ts.map +1 -0
- package/dist/language.js +49 -0
- package/dist/language.js.map +1 -0
- package/dist/qr.d.ts +19 -0
- package/dist/qr.d.ts.map +1 -0
- package/dist/qr.js +47 -0
- package/dist/qr.js.map +1 -0
- package/package.json +26 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<a href="https://www.tracepass.eu"><img src="https://www.tracepass.eu/tracepass-logo.svg" alt="TracePass" height="72"></a>
|
|
3
|
+
|
|
4
|
+
# @tracepass/gs1-utils
|
|
5
|
+
|
|
6
|
+
**GTIN and GLN check digits, GS1 Digital Link build and parse. Zero dependencies in the core; the optional `./qr` subpath asks for `qrcode` as a peer.**
|
|
7
|
+
|
|
8
|
+
[](../../LICENSE)
|
|
9
|
+
[](#)
|
|
10
|
+
|
|
11
|
+
Part of [**tracepass-open**](https://github.com/malinoto/tracepass-open) · Maintained by [TracePass](https://www.tracepass.eu)
|
|
12
|
+
</div>
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @tracepass/gs1-utils
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { validateGtin, validateGln, buildDigitalLinkUri, parseDigitalLinkUri } from "@tracepass/gs1-utils";
|
|
22
|
+
|
|
23
|
+
validateGtin("09520123456788"); // true — mod-10 check digit
|
|
24
|
+
validateGtin("09520123456780"); // false
|
|
25
|
+
validateGln("5012345678900"); // true — 13 digits, mod-10
|
|
26
|
+
|
|
27
|
+
buildDigitalLinkUri("id.example.com", "09520123456788", "SN-1");
|
|
28
|
+
// "https://id.example.com/01/09520123456788/21/SN-1"
|
|
29
|
+
|
|
30
|
+
parseDigitalLinkUri("https://id.example.com/01/09520123456788/21/SN-1");
|
|
31
|
+
// { gtin: "09520123456788", serialNumber: "SN-1" }
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
`buildDigitalLinkUri` takes a **bare domain** — `id.example.com`, not
|
|
35
|
+
`https://id.example.com`.
|
|
36
|
+
|
|
37
|
+
## QR codes are a separate subpath
|
|
38
|
+
|
|
39
|
+
Rendering a QR code needs a dependency, and identifier validation doesn't. So the core
|
|
40
|
+
stays dependency-free and QR lives behind its own entry point, with `qrcode` declared an
|
|
41
|
+
optional peer:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
npm install @tracepass/gs1-utils qrcode
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import { generateQrCode } from "@tracepass/gs1-utils/qr";
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## License
|
|
52
|
+
|
|
53
|
+
[Apache-2.0](../../LICENSE)
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GS1 Digital Link URI utilities.
|
|
3
|
+
*
|
|
4
|
+
* A Digital Link URI encodes a GTIN (AI 01) and serial number (AI 21) as
|
|
5
|
+
* path segments:
|
|
6
|
+
*
|
|
7
|
+
* https://example.com/01/<gtin>/21/<serial>
|
|
8
|
+
*
|
|
9
|
+
* See: https://www.gs1.org/standards/gs1-digital-link
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Build a GS1 Digital Link URI from components.
|
|
13
|
+
*
|
|
14
|
+
* @param domain - fully qualified domain (e.g. "id.tracepass.eu")
|
|
15
|
+
* @param gtin - 14-digit GTIN
|
|
16
|
+
* @param serialNumber - product serial number (percent-encoded if needed)
|
|
17
|
+
* @returns full URI, e.g. "https://id.tracepass.eu/01/01234567890128/21/ABC-123"
|
|
18
|
+
*/
|
|
19
|
+
export declare function buildDigitalLinkUri(domain: string, gtin: string, serialNumber: string): string;
|
|
20
|
+
/**
|
|
21
|
+
* Parse a GS1 Digital Link URI and extract GTIN + serial number.
|
|
22
|
+
* Accepts URIs with or without scheme.
|
|
23
|
+
*
|
|
24
|
+
* @returns parsed components or null if the URI doesn't match the expected pattern
|
|
25
|
+
*/
|
|
26
|
+
export declare function parseDigitalLinkUri(uri: string): {
|
|
27
|
+
gtin: string;
|
|
28
|
+
serialNumber: string;
|
|
29
|
+
} | null;
|
|
30
|
+
/**
|
|
31
|
+
* Build the path segments array for the [...uri] catch-all route.
|
|
32
|
+
* Returns e.g. ["01", "01234567890128", "21", "ABC-123"]
|
|
33
|
+
*/
|
|
34
|
+
export declare function buildDigitalLinkSegments(gtin: string, serialNumber: string): string[];
|
|
35
|
+
//# sourceMappingURL=digital-link.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"digital-link.d.ts","sourceRoot":"","sources":["../src/digital-link.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAOH;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CACjC,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,EACZ,YAAY,EAAE,MAAM,GACnB,MAAM,CAIR;AAED;;;;;GAKG;AACH,wBAAgB,mBAAmB,CACjC,GAAG,EAAE,MAAM,GACV;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,YAAY,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAY/C;AAED;;;GAGG;AACH,wBAAgB,wBAAwB,CACtC,IAAI,EAAE,MAAM,EACZ,YAAY,EAAE,MAAM,GACnB,MAAM,EAAE,CAEV"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GS1 Digital Link URI utilities.
|
|
3
|
+
*
|
|
4
|
+
* A Digital Link URI encodes a GTIN (AI 01) and serial number (AI 21) as
|
|
5
|
+
* path segments:
|
|
6
|
+
*
|
|
7
|
+
* https://example.com/01/<gtin>/21/<serial>
|
|
8
|
+
*
|
|
9
|
+
* See: https://www.gs1.org/standards/gs1-digital-link
|
|
10
|
+
*/
|
|
11
|
+
/** GS1 Application Identifier for GTIN */
|
|
12
|
+
const AI_GTIN = "01";
|
|
13
|
+
/** GS1 Application Identifier for serial number */
|
|
14
|
+
const AI_SERIAL = "21";
|
|
15
|
+
/**
|
|
16
|
+
* Build a GS1 Digital Link URI from components.
|
|
17
|
+
*
|
|
18
|
+
* @param domain - fully qualified domain (e.g. "id.tracepass.eu")
|
|
19
|
+
* @param gtin - 14-digit GTIN
|
|
20
|
+
* @param serialNumber - product serial number (percent-encoded if needed)
|
|
21
|
+
* @returns full URI, e.g. "https://id.tracepass.eu/01/01234567890128/21/ABC-123"
|
|
22
|
+
*/
|
|
23
|
+
export function buildDigitalLinkUri(domain, gtin, serialNumber) {
|
|
24
|
+
const cleanDomain = domain.replace(/\/+$/, "");
|
|
25
|
+
const encodedSerial = encodeURIComponent(serialNumber);
|
|
26
|
+
return `https://${cleanDomain}/${AI_GTIN}/${gtin}/${AI_SERIAL}/${encodedSerial}`;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Parse a GS1 Digital Link URI and extract GTIN + serial number.
|
|
30
|
+
* Accepts URIs with or without scheme.
|
|
31
|
+
*
|
|
32
|
+
* @returns parsed components or null if the URI doesn't match the expected pattern
|
|
33
|
+
*/
|
|
34
|
+
export function parseDigitalLinkUri(uri) {
|
|
35
|
+
// Strip scheme + authority to get the path.
|
|
36
|
+
// Handles https://domain/01/…, http://domain/01/…, or bare path /01/…
|
|
37
|
+
const match = uri.match(/\/01\/(\d{14})\/21\/([^/?#]+)/);
|
|
38
|
+
if (!match)
|
|
39
|
+
return null;
|
|
40
|
+
return {
|
|
41
|
+
gtin: match[1],
|
|
42
|
+
serialNumber: decodeURIComponent(match[2]),
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Build the path segments array for the [...uri] catch-all route.
|
|
47
|
+
* Returns e.g. ["01", "01234567890128", "21", "ABC-123"]
|
|
48
|
+
*/
|
|
49
|
+
export function buildDigitalLinkSegments(gtin, serialNumber) {
|
|
50
|
+
return [AI_GTIN, gtin, AI_SERIAL, encodeURIComponent(serialNumber)];
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=digital-link.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"digital-link.js","sourceRoot":"","sources":["../src/digital-link.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,0CAA0C;AAC1C,MAAM,OAAO,GAAG,IAAI,CAAC;AACrB,mDAAmD;AACnD,MAAM,SAAS,GAAG,IAAI,CAAC;AAEvB;;;;;;;GAOG;AACH,MAAM,UAAU,mBAAmB,CACjC,MAAc,EACd,IAAY,EACZ,YAAoB;IAEpB,MAAM,WAAW,GAAG,MAAM,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;IAC/C,MAAM,aAAa,GAAG,kBAAkB,CAAC,YAAY,CAAC,CAAC;IACvD,OAAO,WAAW,WAAW,IAAI,OAAO,IAAI,IAAI,IAAI,SAAS,IAAI,aAAa,EAAE,CAAC;AACnF,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,mBAAmB,CACjC,GAAW;IAEX,4CAA4C;IAC5C,sEAAsE;IACtE,MAAM,KAAK,GAAG,GAAG,CAAC,KAAK,CACrB,+BAA+B,CAChC,CAAC;IACF,IAAI,CAAC,KAAK;QAAE,OAAO,IAAI,CAAC;IAExB,OAAO;QACL,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC;QACd,YAAY,EAAE,kBAAkB,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;KAC3C,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,wBAAwB,CACtC,IAAY,EACZ,YAAoB;IAEpB,OAAO,CAAC,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,kBAAkB,CAAC,YAAY,CAAC,CAAC,CAAC;AACtE,CAAC"}
|
package/dist/gln.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GS1 GLN (Global Location Number) validation and check digit calculation.
|
|
3
|
+
*
|
|
4
|
+
* A GLN is a 13-digit string where the last digit is a check digit computed
|
|
5
|
+
* using the GS1 modulo-10 algorithm (alternating ×3/×1 from the right).
|
|
6
|
+
*
|
|
7
|
+
* Where GTIN identifies a *product*, GLN identifies a *party or place* —
|
|
8
|
+
* a legal entity, a physical site, or a functional role within an organisation.
|
|
9
|
+
* GLN is the GS1 Digital Link primary key for the `/414/{gln}` resolver path,
|
|
10
|
+
* and the canonical identifier for economic operators in EU compliance contexts
|
|
11
|
+
* (Battery Regulation 2023/1542 Articles 47–50, PPWR 2025/40 Article 11, etc.).
|
|
12
|
+
*
|
|
13
|
+
* The check-digit algorithm itself is identical to GTIN's, but because the
|
|
14
|
+
* data length is even (12 digits + 1 check digit = 13) instead of odd (13 + 1
|
|
15
|
+
* for GTIN-14), the ×3/×1 weight pattern is inverted relative to GTIN: the
|
|
16
|
+
* left-most data digit gets ×1 here, where GTIN-14's left-most gets ×3.
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* Calculate the GS1 check digit for the first 12 digits of a GLN.
|
|
20
|
+
* @param gln12 - exactly 12 numeric characters
|
|
21
|
+
* @returns the single check-digit character ("0"-"9")
|
|
22
|
+
*/
|
|
23
|
+
export declare function calculateCheckDigit(gln12: string): string;
|
|
24
|
+
/**
|
|
25
|
+
* Validate a 13-digit GLN string.
|
|
26
|
+
* Returns true only when the string is 13 numeric characters and the check
|
|
27
|
+
* digit matches.
|
|
28
|
+
*/
|
|
29
|
+
export declare function validateGln(gln: string): boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Coerce common user-input shapes into a clean 13-digit GLN string, or null
|
|
32
|
+
* if the cleaned value isn't a valid GLN.
|
|
33
|
+
*
|
|
34
|
+
* Handles:
|
|
35
|
+
* - leading/trailing whitespace
|
|
36
|
+
* - "GLN:" / "GLN " prefix (case-insensitive — Battery passport datasheets
|
|
37
|
+
* often write "GLN: 7610999999990" in body copy)
|
|
38
|
+
* - internal whitespace (some EDI feeds emit "7610 9999 9999 0")
|
|
39
|
+
* - hyphens used as visual separators (less common but seen)
|
|
40
|
+
*
|
|
41
|
+
* Does NOT auto-fix typos — the check digit must validate. A 13-digit string
|
|
42
|
+
* with a wrong check digit returns null, not a "corrected" GLN.
|
|
43
|
+
*/
|
|
44
|
+
export declare function normalizeGln(input: string): string | null;
|
|
45
|
+
//# sourceMappingURL=gln.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gln.d.ts","sourceRoot":"","sources":["../src/gln.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAiBzD;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAOhD;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CASzD"}
|
package/dist/gln.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GS1 GLN (Global Location Number) validation and check digit calculation.
|
|
3
|
+
*
|
|
4
|
+
* A GLN is a 13-digit string where the last digit is a check digit computed
|
|
5
|
+
* using the GS1 modulo-10 algorithm (alternating ×3/×1 from the right).
|
|
6
|
+
*
|
|
7
|
+
* Where GTIN identifies a *product*, GLN identifies a *party or place* —
|
|
8
|
+
* a legal entity, a physical site, or a functional role within an organisation.
|
|
9
|
+
* GLN is the GS1 Digital Link primary key for the `/414/{gln}` resolver path,
|
|
10
|
+
* and the canonical identifier for economic operators in EU compliance contexts
|
|
11
|
+
* (Battery Regulation 2023/1542 Articles 47–50, PPWR 2025/40 Article 11, etc.).
|
|
12
|
+
*
|
|
13
|
+
* The check-digit algorithm itself is identical to GTIN's, but because the
|
|
14
|
+
* data length is even (12 digits + 1 check digit = 13) instead of odd (13 + 1
|
|
15
|
+
* for GTIN-14), the ×3/×1 weight pattern is inverted relative to GTIN: the
|
|
16
|
+
* left-most data digit gets ×1 here, where GTIN-14's left-most gets ×3.
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* Calculate the GS1 check digit for the first 12 digits of a GLN.
|
|
20
|
+
* @param gln12 - exactly 12 numeric characters
|
|
21
|
+
* @returns the single check-digit character ("0"-"9")
|
|
22
|
+
*/
|
|
23
|
+
export function calculateCheckDigit(gln12) {
|
|
24
|
+
if (!/^\d{12}$/.test(gln12)) {
|
|
25
|
+
throw new Error("calculateCheckDigit expects exactly 12 digits");
|
|
26
|
+
}
|
|
27
|
+
let sum = 0;
|
|
28
|
+
for (let i = 0; i < 12; i++) {
|
|
29
|
+
const digit = Number(gln12[i]);
|
|
30
|
+
// Counted from the right, the rightmost data digit gets ×3, next ×1, etc.
|
|
31
|
+
// For 12 input digits the parity flips relative to GTIN-14: position 0
|
|
32
|
+
// (left-most) is at distance 11 from the right (odd) → ×1; position 1
|
|
33
|
+
// is at distance 10 (even) → ×3.
|
|
34
|
+
sum += i % 2 === 0 ? digit * 1 : digit * 3;
|
|
35
|
+
}
|
|
36
|
+
const remainder = sum % 10;
|
|
37
|
+
return remainder === 0 ? "0" : String(10 - remainder);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Validate a 13-digit GLN string.
|
|
41
|
+
* Returns true only when the string is 13 numeric characters and the check
|
|
42
|
+
* digit matches.
|
|
43
|
+
*/
|
|
44
|
+
export function validateGln(gln) {
|
|
45
|
+
if (!/^\d{13}$/.test(gln)) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
const expected = calculateCheckDigit(gln.slice(0, 12));
|
|
49
|
+
return gln[12] === expected;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Coerce common user-input shapes into a clean 13-digit GLN string, or null
|
|
53
|
+
* if the cleaned value isn't a valid GLN.
|
|
54
|
+
*
|
|
55
|
+
* Handles:
|
|
56
|
+
* - leading/trailing whitespace
|
|
57
|
+
* - "GLN:" / "GLN " prefix (case-insensitive — Battery passport datasheets
|
|
58
|
+
* often write "GLN: 7610999999990" in body copy)
|
|
59
|
+
* - internal whitespace (some EDI feeds emit "7610 9999 9999 0")
|
|
60
|
+
* - hyphens used as visual separators (less common but seen)
|
|
61
|
+
*
|
|
62
|
+
* Does NOT auto-fix typos — the check digit must validate. A 13-digit string
|
|
63
|
+
* with a wrong check digit returns null, not a "corrected" GLN.
|
|
64
|
+
*/
|
|
65
|
+
export function normalizeGln(input) {
|
|
66
|
+
if (typeof input !== "string")
|
|
67
|
+
return null;
|
|
68
|
+
const cleaned = input
|
|
69
|
+
.trim()
|
|
70
|
+
.replace(/^gln[:\s]+/i, "") // strip leading "GLN:" / "GLN " prefix
|
|
71
|
+
.replace(/[\s-]/g, ""); // strip whitespace + hyphens
|
|
72
|
+
return validateGln(cleaned) ? cleaned : null;
|
|
73
|
+
}
|
|
74
|
+
//# sourceMappingURL=gln.js.map
|
package/dist/gln.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gln.js","sourceRoot":"","sources":["../src/gln.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH;;;;GAIG;AACH,MAAM,UAAU,mBAAmB,CAAC,KAAa;IAC/C,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;QAC5B,MAAM,IAAI,KAAK,CAAC,+CAA+C,CAAC,CAAC;IACnE,CAAC;IAED,IAAI,GAAG,GAAG,CAAC,CAAC;IACZ,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC;QAC5B,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;QAC/B,0EAA0E;QAC1E,uEAAuE;QACvE,sEAAsE;QACtE,iCAAiC;QACjC,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC;IAC7C,CAAC;IAED,MAAM,SAAS,GAAG,GAAG,GAAG,EAAE,CAAC;IAC3B,OAAO,SAAS,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC,EAAE,GAAG,SAAS,CAAC,CAAC;AACxD,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,WAAW,CAAC,GAAW;IACrC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;QAC1B,OAAO,KAAK,CAAC;IACf,CAAC;IAED,MAAM,QAAQ,GAAG,mBAAmB,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;IACvD,OAAO,GAAG,CAAC,EAAE,CAAC,KAAK,QAAQ,CAAC;AAC9B,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,YAAY,CAAC,KAAa;IACxC,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,IAAI,CAAC;IAE3C,MAAM,OAAO,GAAG,KAAK;SAClB,IAAI,EAAE;SACN,OAAO,CAAC,aAAa,EAAE,EAAE,CAAC,CAAC,uCAAuC;SAClE,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC,CAAM,6BAA6B;IAE5D,OAAO,WAAW,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC;AAC/C,CAAC"}
|
package/dist/gtin.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GS1 GTIN validation and check digit calculation.
|
|
3
|
+
*
|
|
4
|
+
* A GTIN-14 is a 14-digit string. The last digit is a check digit computed
|
|
5
|
+
* using the GS1 modulo-10 algorithm (alternating ×3/×1 from the right).
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Calculate the GS1 check digit for the first 13 digits of a GTIN-14.
|
|
9
|
+
* @param gtin13 - exactly 13 numeric characters
|
|
10
|
+
* @returns the single check-digit character ("0"-"9")
|
|
11
|
+
*/
|
|
12
|
+
export declare function calculateCheckDigit(gtin13: string): string;
|
|
13
|
+
/**
|
|
14
|
+
* Validate a 14-digit GTIN string (GTIN-14).
|
|
15
|
+
* Returns true only when the string is 14 numeric characters and the check
|
|
16
|
+
* digit matches.
|
|
17
|
+
*/
|
|
18
|
+
export declare function validateGtin(gtin: string): boolean;
|
|
19
|
+
//# sourceMappingURL=gtin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gtin.d.ts","sourceRoot":"","sources":["../src/gtin.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAe1D;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAOlD"}
|
package/dist/gtin.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GS1 GTIN validation and check digit calculation.
|
|
3
|
+
*
|
|
4
|
+
* A GTIN-14 is a 14-digit string. The last digit is a check digit computed
|
|
5
|
+
* using the GS1 modulo-10 algorithm (alternating ×3/×1 from the right).
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Calculate the GS1 check digit for the first 13 digits of a GTIN-14.
|
|
9
|
+
* @param gtin13 - exactly 13 numeric characters
|
|
10
|
+
* @returns the single check-digit character ("0"-"9")
|
|
11
|
+
*/
|
|
12
|
+
export function calculateCheckDigit(gtin13) {
|
|
13
|
+
if (!/^\d{13}$/.test(gtin13)) {
|
|
14
|
+
throw new Error("calculateCheckDigit expects exactly 13 digits");
|
|
15
|
+
}
|
|
16
|
+
let sum = 0;
|
|
17
|
+
for (let i = 0; i < 13; i++) {
|
|
18
|
+
const digit = Number(gtin13[i]);
|
|
19
|
+
// positions 0,2,4,… get ×3; positions 1,3,5,… get ×1 (from left,
|
|
20
|
+
// for a 14-digit number the even-indexed positions are multiplied by 3)
|
|
21
|
+
sum += i % 2 === 0 ? digit * 3 : digit * 1;
|
|
22
|
+
}
|
|
23
|
+
const remainder = sum % 10;
|
|
24
|
+
return remainder === 0 ? "0" : String(10 - remainder);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Validate a 14-digit GTIN string (GTIN-14).
|
|
28
|
+
* Returns true only when the string is 14 numeric characters and the check
|
|
29
|
+
* digit matches.
|
|
30
|
+
*/
|
|
31
|
+
export function validateGtin(gtin) {
|
|
32
|
+
if (!/^\d{14}$/.test(gtin)) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
const expected = calculateCheckDigit(gtin.slice(0, 13));
|
|
36
|
+
return gtin[13] === expected;
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=gtin.js.map
|
package/dist/gtin.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gtin.js","sourceRoot":"","sources":["../src/gtin.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;;;GAIG;AACH,MAAM,UAAU,mBAAmB,CAAC,MAAc;IAChD,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;QAC7B,MAAM,IAAI,KAAK,CAAC,+CAA+C,CAAC,CAAC;IACnE,CAAC;IAED,IAAI,GAAG,GAAG,CAAC,CAAC;IACZ,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC;QAC5B,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;QAChC,iEAAiE;QACjE,wEAAwE;QACxE,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC;IAC7C,CAAC;IAED,MAAM,SAAS,GAAG,GAAG,GAAG,EAAE,CAAC;IAC3B,OAAO,SAAS,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC,EAAE,GAAG,SAAS,CAAC,CAAC;AACxD,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,YAAY,CAAC,IAAY;IACvC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;QAC3B,OAAO,KAAK,CAAC;IACf,CAAC;IAED,MAAM,QAAQ,GAAG,mBAAmB,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;IACxD,OAAO,IAAI,CAAC,EAAE,CAAC,KAAK,QAAQ,CAAC;AAC/B,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GS1 identifier utilities — GTIN, GLN, and Digital Link.
|
|
3
|
+
*
|
|
4
|
+
* Zero dependencies. QR-code rendering lives at the `@tracepass/gs1-utils/qr`
|
|
5
|
+
* subpath so the core stays dependency-free.
|
|
6
|
+
*/
|
|
7
|
+
export { buildDigitalLinkUri, parseDigitalLinkUri, buildDigitalLinkSegments, } from "./digital-link.js";
|
|
8
|
+
export { validateGln, normalizeGln, calculateCheckDigit as calculateGlnCheckDigit, } from "./gln.js";
|
|
9
|
+
export { validateGtin, calculateCheckDigit as calculateGtinCheckDigit, } from "./gtin.js";
|
|
10
|
+
export { parseAcceptLanguage, detectLanguage } from "./language.js";
|
|
11
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EACL,mBAAmB,EACnB,mBAAmB,EACnB,wBAAwB,GACzB,MAAM,mBAAmB,CAAC;AAK3B,OAAO,EACL,WAAW,EACX,YAAY,EACZ,mBAAmB,IAAI,sBAAsB,GAC9C,MAAM,UAAU,CAAC;AAElB,OAAO,EACL,YAAY,EACZ,mBAAmB,IAAI,uBAAuB,GAC/C,MAAM,WAAW,CAAC;AAEnB,OAAO,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GS1 identifier utilities — GTIN, GLN, and Digital Link.
|
|
3
|
+
*
|
|
4
|
+
* Zero dependencies. QR-code rendering lives at the `@tracepass/gs1-utils/qr`
|
|
5
|
+
* subpath so the core stays dependency-free.
|
|
6
|
+
*/
|
|
7
|
+
export { buildDigitalLinkUri, parseDigitalLinkUri, buildDigitalLinkSegments, } from "./digital-link.js";
|
|
8
|
+
// `calculateCheckDigit` exists in both gln.ts and gtin.ts with the same mod-10
|
|
9
|
+
// algorithm but different length rules, so each is re-exported under its own
|
|
10
|
+
// name rather than colliding at the barrel.
|
|
11
|
+
export { validateGln, normalizeGln, calculateCheckDigit as calculateGlnCheckDigit, } from "./gln.js";
|
|
12
|
+
export { validateGtin, calculateCheckDigit as calculateGtinCheckDigit, } from "./gtin.js";
|
|
13
|
+
export { parseAcceptLanguage, detectLanguage } from "./language.js";
|
|
14
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EACL,mBAAmB,EACnB,mBAAmB,EACnB,wBAAwB,GACzB,MAAM,mBAAmB,CAAC;AAE3B,+EAA+E;AAC/E,6EAA6E;AAC7E,4CAA4C;AAC5C,OAAO,EACL,WAAW,EACX,YAAY,EACZ,mBAAmB,IAAI,sBAAsB,GAC9C,MAAM,UAAU,CAAC;AAElB,OAAO,EACL,YAAY,EACZ,mBAAmB,IAAI,uBAAuB,GAC/C,MAAM,WAAW,CAAC;AAEnB,OAAO,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Language detection utilities for the public passport viewer.
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Parse an Accept-Language header into a sorted array of language codes.
|
|
6
|
+
*
|
|
7
|
+
* Example: "en-US,en;q=0.9,de;q=0.7" → ["en-US", "en", "de"]
|
|
8
|
+
*/
|
|
9
|
+
export declare function parseAcceptLanguage(header: string): string[];
|
|
10
|
+
/**
|
|
11
|
+
* Detect the best language for the passport viewer.
|
|
12
|
+
*
|
|
13
|
+
* Priority:
|
|
14
|
+
* 1. ?lang= query parameter
|
|
15
|
+
* 2. Accept-Language header (highest quality factor)
|
|
16
|
+
* 3. Fallback to "en"
|
|
17
|
+
*
|
|
18
|
+
* Returns a 2-letter ISO 639-1 code (e.g. "en", "de", "fr").
|
|
19
|
+
*/
|
|
20
|
+
export declare function detectLanguage(request: Request): string;
|
|
21
|
+
//# sourceMappingURL=language.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"language.d.ts","sourceRoot":"","sources":["../src/language.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAa5D;AAED;;;;;;;;;GASG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,CAkBvD"}
|
package/dist/language.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Language detection utilities for the public passport viewer.
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Parse an Accept-Language header into a sorted array of language codes.
|
|
6
|
+
*
|
|
7
|
+
* Example: "en-US,en;q=0.9,de;q=0.7" → ["en-US", "en", "de"]
|
|
8
|
+
*/
|
|
9
|
+
export function parseAcceptLanguage(header) {
|
|
10
|
+
if (!header)
|
|
11
|
+
return [];
|
|
12
|
+
return header
|
|
13
|
+
.split(",")
|
|
14
|
+
.map((part) => {
|
|
15
|
+
const [lang, qPart] = part.trim().split(";");
|
|
16
|
+
const q = qPart ? parseFloat(qPart.replace(/^\s*q\s*=\s*/, "")) : 1;
|
|
17
|
+
return { lang: lang.trim(), q: isNaN(q) ? 0 : q };
|
|
18
|
+
})
|
|
19
|
+
.sort((a, b) => b.q - a.q)
|
|
20
|
+
.map((entry) => entry.lang)
|
|
21
|
+
.filter(Boolean);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Detect the best language for the passport viewer.
|
|
25
|
+
*
|
|
26
|
+
* Priority:
|
|
27
|
+
* 1. ?lang= query parameter
|
|
28
|
+
* 2. Accept-Language header (highest quality factor)
|
|
29
|
+
* 3. Fallback to "en"
|
|
30
|
+
*
|
|
31
|
+
* Returns a 2-letter ISO 639-1 code (e.g. "en", "de", "fr").
|
|
32
|
+
*/
|
|
33
|
+
export function detectLanguage(request) {
|
|
34
|
+
const url = new URL(request.url);
|
|
35
|
+
const langParam = url.searchParams.get("lang");
|
|
36
|
+
if (langParam && /^[a-z]{2}(-[A-Z]{2})?$/.test(langParam)) {
|
|
37
|
+
return langParam.split("-")[0];
|
|
38
|
+
}
|
|
39
|
+
const acceptLanguage = request.headers.get("accept-language");
|
|
40
|
+
if (acceptLanguage) {
|
|
41
|
+
const langs = parseAcceptLanguage(acceptLanguage);
|
|
42
|
+
if (langs.length > 0) {
|
|
43
|
+
// Return just the 2-letter code
|
|
44
|
+
return langs[0].split("-")[0].toLowerCase();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return "en";
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=language.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"language.js","sourceRoot":"","sources":["../src/language.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;;;GAIG;AACH,MAAM,UAAU,mBAAmB,CAAC,MAAc;IAChD,IAAI,CAAC,MAAM;QAAE,OAAO,EAAE,CAAC;IAEvB,OAAO,MAAM;SACV,KAAK,CAAC,GAAG,CAAC;SACV,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE;QACZ,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAC7C,MAAM,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,UAAU,CAAC,KAAK,CAAC,OAAO,CAAC,cAAc,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QACpE,OAAO,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,EAAE,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;IACpD,CAAC,CAAC;SACD,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;SACzB,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC;SAC1B,MAAM,CAAC,OAAO,CAAC,CAAC;AACrB,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,cAAc,CAAC,OAAgB;IAC7C,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IACjC,MAAM,SAAS,GAAG,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IAE/C,IAAI,SAAS,IAAI,wBAAwB,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC;QAC1D,OAAO,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;IACjC,CAAC;IAED,MAAM,cAAc,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC;IAC9D,IAAI,cAAc,EAAE,CAAC;QACnB,MAAM,KAAK,GAAG,mBAAmB,CAAC,cAAc,CAAC,CAAC;QAClD,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACrB,gCAAgC;YAChC,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC;QAC9C,CAAC;IACH,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC"}
|
package/dist/qr.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* QR code generation for GS1 Digital Link URIs.
|
|
3
|
+
*
|
|
4
|
+
* Uppercases the scheme + domain portion so the QR encoder can use
|
|
5
|
+
* alphanumeric mode for those characters, producing a smaller (denser) code.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Generate a QR code PNG buffer for the given URI.
|
|
9
|
+
*
|
|
10
|
+
* - Error correction level M (15 % recovery)
|
|
11
|
+
* - Margin: 4 modules
|
|
12
|
+
* - Width: 300 px
|
|
13
|
+
* - Scheme + domain uppercased for smaller QR
|
|
14
|
+
*
|
|
15
|
+
* @param uri - full GS1 Digital Link URI
|
|
16
|
+
* @returns PNG image as a Node.js Buffer
|
|
17
|
+
*/
|
|
18
|
+
export declare function generateQrCode(uri: string): Promise<Buffer>;
|
|
19
|
+
//# sourceMappingURL=qr.d.ts.map
|
package/dist/qr.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"qr.d.ts","sourceRoot":"","sources":["../src/qr.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAsBH;;;;;;;;;;GAUG;AACH,wBAAsB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAWjE"}
|
package/dist/qr.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* QR code generation for GS1 Digital Link URIs.
|
|
3
|
+
*
|
|
4
|
+
* Uppercases the scheme + domain portion so the QR encoder can use
|
|
5
|
+
* alphanumeric mode for those characters, producing a smaller (denser) code.
|
|
6
|
+
*/
|
|
7
|
+
import QRCode from "qrcode";
|
|
8
|
+
/**
|
|
9
|
+
* Uppercase the scheme and domain of a URI to allow QR alphanumeric encoding.
|
|
10
|
+
* e.g. "https://id.tracepass.eu/01/…" → "HTTPS://ID.TRACEPASS.EU/01/…"
|
|
11
|
+
*
|
|
12
|
+
* Only the scheme + authority are uppercased; the path is left as-is because
|
|
13
|
+
* serial numbers may be case-sensitive.
|
|
14
|
+
*/
|
|
15
|
+
function uppercaseSchemeAndDomain(uri) {
|
|
16
|
+
try {
|
|
17
|
+
const url = new URL(uri);
|
|
18
|
+
const schemePlusDomain = `${url.protocol}//${url.host}`;
|
|
19
|
+
return uri.replace(schemePlusDomain, schemePlusDomain.toUpperCase());
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
// If not a valid URL, return as-is
|
|
23
|
+
return uri;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Generate a QR code PNG buffer for the given URI.
|
|
28
|
+
*
|
|
29
|
+
* - Error correction level M (15 % recovery)
|
|
30
|
+
* - Margin: 4 modules
|
|
31
|
+
* - Width: 300 px
|
|
32
|
+
* - Scheme + domain uppercased for smaller QR
|
|
33
|
+
*
|
|
34
|
+
* @param uri - full GS1 Digital Link URI
|
|
35
|
+
* @returns PNG image as a Node.js Buffer
|
|
36
|
+
*/
|
|
37
|
+
export async function generateQrCode(uri) {
|
|
38
|
+
const optimizedUri = uppercaseSchemeAndDomain(uri);
|
|
39
|
+
const buffer = await QRCode.toBuffer(optimizedUri, {
|
|
40
|
+
errorCorrectionLevel: "M",
|
|
41
|
+
margin: 4,
|
|
42
|
+
width: 300,
|
|
43
|
+
type: "png",
|
|
44
|
+
});
|
|
45
|
+
return buffer;
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=qr.js.map
|
package/dist/qr.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"qr.js","sourceRoot":"","sources":["../src/qr.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B;;;;;;GAMG;AACH,SAAS,wBAAwB,CAAC,GAAW;IAC3C,IAAI,CAAC;QACH,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC;QACzB,MAAM,gBAAgB,GAAG,GAAG,GAAG,CAAC,QAAQ,KAAK,GAAG,CAAC,IAAI,EAAE,CAAC;QACxD,OAAO,GAAG,CAAC,OAAO,CAAC,gBAAgB,EAAE,gBAAgB,CAAC,WAAW,EAAE,CAAC,CAAC;IACvE,CAAC;IAAC,MAAM,CAAC;QACP,mCAAmC;QACnC,OAAO,GAAG,CAAC;IACb,CAAC;AACH,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAAC,GAAW;IAC9C,MAAM,YAAY,GAAG,wBAAwB,CAAC,GAAG,CAAC,CAAC;IAEnD,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,QAAQ,CAAC,YAAY,EAAE;QACjD,oBAAoB,EAAE,GAAG;QACzB,MAAM,EAAE,CAAC;QACT,KAAK,EAAE,GAAG;QACV,IAAI,EAAE,KAAK;KACZ,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tracepass/gs1-utils",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "GS1 identifier utilities for Digital Product Passports — GTIN and GLN check-digit validation, GS1 Digital Link URI build/parse. Zero dependencies in the core; the optional QR subpath asks for qrcode as a peer.",
|
|
5
|
+
"keywords": ["gs1", "gtin", "gln", "digital-link", "digital-product-passport", "dpp", "barcode"],
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"author": "TracePass LTD (https://tracepass.eu)",
|
|
8
|
+
"homepage": "https://github.com/malinoto/tracepass-open/tree/main/packages/gs1-utils#readme",
|
|
9
|
+
"repository": { "type": "git", "url": "git+https://github.com/malinoto/tracepass-open.git", "directory": "packages/gs1-utils" },
|
|
10
|
+
"bugs": "https://github.com/malinoto/tracepass-open/issues",
|
|
11
|
+
"type": "module",
|
|
12
|
+
"main": "./dist/index.js",
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": { "types": "./dist/index.d.ts", "import": "./dist/index.js" },
|
|
16
|
+
"./qr": { "types": "./dist/qr.d.ts", "import": "./dist/qr.js" }
|
|
17
|
+
},
|
|
18
|
+
"files": ["dist", "README.md", "LICENSE"],
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"publishConfig": { "access": "public" },
|
|
21
|
+
"scripts": { "build": "tsc -p tsconfig.build.json", "clean": "rm -rf dist" },
|
|
22
|
+
"peerDependencies": { "qrcode": "^1.5.0" },
|
|
23
|
+
"peerDependenciesMeta": { "qrcode": { "optional": true } },
|
|
24
|
+
"devDependencies": { "typescript": "^5.6.3", "qrcode": "^1.5.4", "@types/qrcode": "^1.5.5" },
|
|
25
|
+
"engines": { "node": ">=18" }
|
|
26
|
+
}
|