1.0.2 • Published 10 years ago
which-gtin v1.0.2
Deprecated in favor of xbpf/gtin.
which-gtin
Determine what GTIN format the code is, what its real format is, and if it valid or not. Supports UPC-E (number systems 0 and 1).
whichGTIN(code, upce): Determine which GTIN format the code is in.
code: Code to check. Can be either astringor anumber.- warning: Be careful using plain numbers! If you input
0123, it will be interpreted as"123". Use strings if you want to be safe.
- warning: Be careful using plain numbers! If you input
upce: Whether to check if an 8-digit code is aUPC-Eor aGTIN-8code.- Only applies if the code given is exactly 8 digits long.
- Even if
true, if the code is padded with zeroes, e.g.00000032435887, the function will assume and validate forGTIN-8.
- Even if
- If the code is valid
UPC-E, then the returned object will have the propertyexpanded, which will contain the expandedGTIN-12string for theUPC-Ecode.
- Only applies if the code given is exactly 8 digits long.
Returns an object that follows this format:
{
format: 'GTIN-14',
real: 'GTIN-8',
valid: true,
expanded: null
}format: The given format. Determined from the code length."GTIN-14","GTIN-13","GTIN-12","GTIN-8": Code is 14, 13, 12, 8 digits long, respectively.GTIN-14=ITF-14GTIN-13=EAN=EAN-13GTIN-12=UPC=UPC-AGTIN-8=EAN-8
"UPC-E": Code is 8 digits long, and theupceargument istrue.null: Code is of an invalid format.
real: The interpreted format.- Can be one of the
formatvalues. - If given code is
00000032435887, theformatisGTIN-14, butrealisGTIN-8.
- Can be one of the
valid: Whether the checksum is valid.expanded:GTIN-12code expanded from the givenUPC-Ecode.- Only applies if format is 'UPC-E'.
- note: The expanded
UPC-Acode can be aGTIN-8code. If you want to be 100% accurate, run the expanded value through the function again.
import whichGTIN from 'which-gtin'
whichGTIN('00000032435887', true)
// {format: 'GTIN-14', real: 'GTIN-8', valid: true, expanded: null}
whichGTIN('123')
// {format: null, real: null, valid: false, expanded: null}
whichGTIN('32435887', false)
// {format: `GTIN-8`, real: `GTIN-8`, valid: true, expanded: null}