1// TypeScript / JavaScript SGTIN-96 Utils
2
3export const GS1_PARTITION_TABLE = [
4 { value: 0, partition: 0, companyPrefixBits: 40, itemReferenceBits: 4, companyDigits: 12, itemDigits: 1 },
5 { value: 1, partition: 1, companyPrefixBits: 37, itemReferenceBits: 7, companyDigits: 11, itemDigits: 2 },
6 { value: 2, partition: 2, companyPrefixBits: 34, itemReferenceBits: 10, companyDigits: 10, itemDigits: 3 },
7 { value: 3, partition: 3, companyPrefixBits: 30, itemReferenceBits: 14, companyDigits: 9, itemDigits: 4 },
8 { value: 4, partition: 4, companyPrefixBits: 27, itemReferenceBits: 17, companyDigits: 8, itemDigits: 5 },
9 { value: 5, partition: 5, companyPrefixBits: 24, itemReferenceBits: 20, companyDigits: 7, itemDigits: 6 },
10 { value: 6, partition: 6, companyPrefixBits: 20, itemReferenceBits: 24, companyDigits: 6, itemDigits: 7 },
11];
12
13export function encodeSgtin96(gtin: string, serial: string, filter: number) {
14 gtin = gtin.padStart(14, "0");
15 const p = GS1_PARTITION_TABLE.find(rule => {
16 const cp = parseInt(gtin.slice(1, 1 + rule.companyDigits));
17 const ir = parseInt(gtin[0] + gtin.slice(1 + rule.companyDigits, 13));
18 return cp < (1 << rule.companyPrefixBits) && ir < (1 << rule.itemReferenceBits);
19 });
20 if (!p) throw new Error("Invalid GTIN for SGTIN-96");
21
22 const cpVal = parseInt(gtin.slice(1, 1 + p.companyDigits));
23 const irVal = parseInt(gtin[0] + gtin.slice(1 + p.companyDigits, 13));
24
25 let b = (0x30).toString(2).padStart(8,"0");
26 b += filter.toString(2).padStart(3,"0");
27 b += p.partition.toString(2).padStart(3,"0");
28 b += cpVal.toString(2).padStart(p.companyPrefixBits,"0");
29 b += irVal.toString(2).padStart(p.itemReferenceBits,"0");
30 b += parseInt(serial).toString(2).padStart(38,"0");
31 return binaryToHex(b);
32}
33
34export function decodeSgtin96(hex: string) {
35 const b = hexToBinary(hex);
36 const pVal = parseInt(b.substring(11, 14), 2);
37 const rule = GS1_PARTITION_TABLE.find(r => r.partition === pVal);
38 if (!rule) throw new Error("Invalid Partition");
39
40 const cp = parseInt(b.substring(14, 14 + rule.companyPrefixBits), 2);
41 const ir = parseInt(b.substring(14 + rule.companyPrefixBits, 14 + rule.companyPrefixBits + rule.itemReferenceBits), 2);
42 const serial = parseInt(b.substring(14 + rule.companyPrefixBits + rule.itemReferenceBits, 96), 2);
43
44 const cpStr = cp.toString().padStart(rule.companyDigits, "0");
45 const irStr = ir.toString().padStart(rule.itemDigits, "0");
46 const gtinCore = irStr[0] + cpStr + irStr.substring(1);
47
48 // Check digit calculation
49 let sum = 0;
50 for (let i = 0; i < 13; i++) {
51 sum += parseInt(gtinCore[i]) * (i % 2 === 0 ? 3 : 1);
52 }
53 const check = (10 - (sum % 10)) % 10;
54
55 return {
56 gtin: gtinCore + check,
57 serial: serial.toString()
58 };
59}
60
61function binaryToHex(b: string) {
62 let hex = "";
63 for (let i = 0; i < b.length; i += 4) {
64 hex += parseInt(b.substring(i, i + 4), 2).toString(16).toUpperCase();
65 }
66 return hex;
67}
68
69function hexToBinary(h: string) {
70 let bin = "";
71 for (let i = 0; i < h.length; i++) {
72 bin += parseInt(h[i], 16).toString(2).padStart(4, "0");
73 }
74 return bin;
75}