1.0.2 • Published 8 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 astring
or 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-E
or aGTIN-8
code.- 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-12
string for theUPC-E
code.
- 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-14
GTIN-13
=EAN
=EAN-13
GTIN-12
=UPC
=UPC-A
GTIN-8
=EAN-8
"UPC-E"
: Code is 8 digits long, and theupce
argument istrue
.null
: Code is of an invalid format.
real
: The interpreted format.- Can be one of the
format
values. - If given code is
00000032435887
, theformat
isGTIN-14
, butreal
isGTIN-8
.
- Can be one of the
valid
: Whether the checksum is valid.expanded
:GTIN-12
code expanded from the givenUPC-E
code.- Only applies if format is 'UPC-E'.
- note: The expanded
UPC-A
code can be aGTIN-8
code. 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}