Function to split GTIN4 into GS1 Company Prefix and GS1 ID
function splitGTIN(gtin) {
if (gtin.length !== 14) {
throw new Error("GTIN must be 14 digits long.");
}
// Extract GS1 Company Prefix: remove the first digit and take the next 7 digits
const gs1CompanyPrefix = gtin.substring(1, 8);
// Extract GS1 ID: take the first digit and the next 5 digits, excluding the last one
const gs1ID = gtin.charAt(0) + gtin.substring(8, 13);
return {
gs1CompanyPrefix: gs1CompanyPrefix,
gs1ID: gs1ID
};
}
NOTES:
We have assumed the character length for GTIN4, GS1 Company Prefix and GS1 ID as follows:
- GTIN4: 14 digits (eg: 00304091610507)
- GS1 Company prefix: 7 digits (eg: 0304091)
- GS1 ID: 6 digits (eg: 061050)