gtin-validator
gtin-validator allows you to validate and work with GTIN, UPC, and other product code formats.
npm i @linvix-sistemas/gtin-validator
yarn add @linvix-sistemas/gtin-validator
import {
isValidGTIN,
isValidPrefixOfGTIN,
isValidCheckDigitOnGTIN,
isValidStringLengthForGTIN,
removeGTINLeadingZerosToUpcOrGTIN13,
getFormatOfGTIN,
getFormatOfMinifiedGTIN,
calculateCheckDigitForGTIN,
generateRandomGTIN,
generateArrayOfRandomGTINs,
} from "@linvix-sistemas/gtin-validator";
Examples
isValidGTIN(productCode)
Returns true if the provided productCode is a valid GTIN (valid length and check digit). On invalid productCodes, returns false by default or throws an error if "error" is passed as the second parameter.
import { isValidGTIN } from "gtin-validator";
isValidGTIN("078000053401");
isValidGTIN("078000053401", "boolean");
isValidGTIN("078000053401", "error");
isValidGTIN("01012345678905");
isValidGTIN("01012345678905", "boolean");
isValidGTIN("01012345678905", "error");
isValidGTIN("", "error");
isValidGTIN("84656", "error");
isValidGTIN("722510035005", "error");
isValidGTIN(722510035005);
isValidGTIN(722510035005, "error");
isValidGTIN("722510035003", "error");
isValidGTIN("722510035003", "boolean");
isValidGTIN("722510035003");
isValidGTIN("00012345678905");
isValidCheckDigitOnGTIN(productCode)
Returns true if the provided productCode is a valid GTIN and has a valid check digit. If the check digit is invalid, returns false by default or throws an error if "error" is passed as the second parameter.
import { isValidCheckDigitOnGTIN } from "gtin-validator";
isValidCheckDigitOnGTIN("078000053401");
isValidCheckDigitOnGTIN("078000053401", "boolean");
isValidCheckDigitOnGTIN("078000053401", "error");
isValidCheckDigitOnGTIN("01012345678905");
isValidCheckDigitOnGTIN("01012345678905", "boolean");
isValidCheckDigitOnGTIN("01012345678905", "error");
isValidCheckDigitOnGTIN("", "error");
isValidCheckDigitOnGTIN("84656", "error");
isValidCheckDigitOnGTIN("722510035005", "error");
isValidCheckDigitOnGTIN(722510035005);
isValidCheckDigitOnGTIN(722510035005, "error");
isValidCheckDigitOnGTIN("722510035003", "error");
isValidCheckDigitOnGTIN("722510035003", "boolean");
isValidCheckDigitOnGTIN("722510035003");
isValidCheckDigitOnGTIN("00012345678905");
isValidStringLengthForGTIN(productCode)
Returns true if the provided productCode is a length for a GTIN (a 8, 12, 13, or 14 string of digits). If the length of the string is invalid, returns false by default or throws an error if "error" is passed as the second parameter.
This function does NOT check that a productCode has a valid check digit.
import { isValidStringLengthForGTIN } from "gtin-validator";
isValidStringLengthForGTIN("45664");
isValidStringLengthForGTIN("45664", "boolean");
isValidStringLengthForGTIN("45664", "error");
isValidStringLengthForGTIN("89722510035005");
isValidStringLengthForGTIN("8722510035005");
isValidStringLengthForGTIN("722510035005");
isValidStringLengthForGTIN("78000053401", "boolean");
isValidStringLengthForGTIN("78000053401", "error");
isValidStringLengthForGTIN(722510035005);
isValidStringLengthForGTIN(722510035005);
removeGTINLeadingZerosToUpcOrGTIN13(productCode)
Removes leading zeros on valid 13 or 14 digit long GTIN's, returning a shorter valid GTIN if possible. Throws an error if the the provided productCode is not a valid GTIN. Only a valid GTIN with a length of 12-14 digits is acceptable to pass as a productCode param.
import { removeGTINLeadingZerosToUpcOrGTIN13 } from "gtin-validator";
removeGTINLeadingZerosToUpcOrGTIN13("00000012341238");
removeGTINLeadingZerosToUpcOrGTIN13("0000012341238");
removeGTINLeadingZerosToUpcOrGTIN13("000012341238");
removeGTINLeadingZerosToUpcOrGTIN13("00614141453245");
removeGTINLeadingZerosToUpcOrGTIN13("00012345678905");
removeGTINLeadingZerosToUpcOrGTIN13("01234123412344");
removeGTINLeadingZerosToUpcOrGTIN13("078000053401");
removeGTINLeadingZerosToUpcOrGTIN13("722510035005");
removeGTINLeadingZerosToUpcOrGTIN13("710882350628");
removeGTINLeadingZerosToUpcOrGTIN13("5901234123457");
removeGTINLeadingZerosToUpcOrGTIN13("792510035005");
removeGTINLeadingZerosToUpcOrGTIN13("");
removeGTINLeadingZerosToUpcOrGTIN13(84658555);
removeGTINLeadingZerosToUpcOrGTIN13(null);
removeGTINLeadingZerosToUpcOrGTIN13("84656");
removeGTINLeadingZerosToUpcOrGTIN13("84645456");
getFormatOfGTIN(productCode)
Returns the GTIN format ("GTIN-8" | "GTIN-12" | "GTIN-13" | "GTIN-14") of the provided productCode or throws an error on an invalid GTIN.
import { getFormatOfGTIN } from "gtin-validator";
getFormatOfGTIN("722510035005");
getFormatOfGTIN("5901234123457");
getFormatOfGTIN("0000012341238");
getFormatOfGTIN("00012345678905");
getFormatOfGTIN("12341238");
getFormatOfGTIN("84656");
getFormatOfGTIN("846548556815868");
getFormatOfGTIN(84658555);
getFormatOfGTIN("00012345678906");
getFormatOfMinifiedGTIN(productCode)
Returns the GTIN format ("GTIN-8" | "GTIN-12" | "GTIN-13" | "GTIN-14") of the provided productCode after removing its unnecessary leading zeros (runs removeGTINLeadingZerosToUpcOrGTIN13(productCode) before getting the productCode's format). Throws an error on an invalid GTIN.
import { getFormatOfMinifiedGTIN } from "gtin-validator";
getFormatOfMinifiedGTIN("722510035005");
getFormatOfMinifiedGTIN("0722510035005");
getFormatOfMinifiedGTIN("5901234123457");
getFormatOfMinifiedGTIN("05901234123457");
getFormatOfMinifiedGTIN("50004544639356");
getFormatOfMinifiedGTIN("00614141453245");
getFormatOfMinifiedGTIN("12341238");
getFormatOfGTIN("84656");
getFormatOfGTIN("846548556815868");
getFormatOfGTIN(84658555);
calculateCheckDigitForGTIN(productCodeWithoutCheckDigit)
Calculates check digit for GTIN strings of digits 7 or 11-13 characters long. Valid productCodeWithoutCheckDigit params are string of digits 7 or 11-13 characters long.
import { calculateCheckDigitForGTIN } from "gtin-validator";
calculateCheckDigitForGTIN("72251003500");
calculateCheckDigitForGTIN("590123412345");
calculateCheckDigitForGTIN("1234123");
calculateCheckDigitForGTIN("00001234123");
calculateCheckDigitForGTIN("000001234123");
calculateCheckDigitForGTIN("0000001234123");
calculateCheckDigitForGTIN("");
calculateCheckDigitForGTIN("123456");
calculateCheckDigitForGTIN("12345678");
calculateCheckDigitForGTIN("1234567890");
calculateCheckDigitForGTIN("12345678901234");
calculateCheckDigitForGTIN(1234567);
calculateCheckDigitForGTIN(123456789012);
generateRandomGTIN(gtinType)
Generates a random GTIN string, including a valid check digit of the type provided in gtinType param ("GTIN-8" | "GTIN-12" | "GTIN-13" | "GTIN-14").
import { generateRandomGTIN } from "gtin-validator";
generateRandomGTIN("GTIN-8");
generateRandomGTIN("GTIN-12");
generateRandomGTIN("GTIN-13");
generateRandomGTIN("GTIN-14");
generateRandomGTIN();
generateRandomGTIN("");
generateRandomGTIN("gtin");
generateRandomGTIN("gtin-14");
generateArrayOfRandomGTINs(gtinType, numberOfGTINsToGenerate, guaranteeOnlyUniqueGTINs)
Generates an array of random GTINs of type GTIN-8" | "GTIN-12" | "GTIN-13" | "GTIN-14" or use "Any Valid GTIN" to potentially get a mix of different types.
Use "numberOfGTINsToGenerate" param to determine how many GTINs to generate.
By default, the guaranteeOnlyUniqueGTINs param is false, meaning the returned array of GTINs can contain duplicate GTINs. Pass true to guarantee each GTIN is unique, however, keep in mind this can dramatically increase the time it takes to generate the GTINs.
import { generateArrayOfRandomGTINs } from "gtin-validator";
generateArrayOfRandomGTINs("Any Valid GTIN", 1234);
generateArrayOfRandomGTINs("Any Valid GTIN", 1234, false);
generateArrayOfRandomGTINs("Any Valid GTIN", 1234, true);
generateArrayOfRandomGTINs("GTIN-12", 100000);
generateArrayOfRandomGTINs("GTIN-13", 123456, true);
This package was created by GunCompare and maintained by Linvix Sistemas;