gtin
GTIN (UPC, EAN, ITF, etc.) utilities.
npm install gtin
gtin.isGTIN(barcode)
Returns true or false, depending on if the string given is a GTIN barcode.
Throws an error if an empty string or anything other than a string is provided.
NOTE: This does not validate the code by check digit. Validation is done
with gtin.isValid
.
import { isGTIN } from 'gtin'
isGTIN('1234')
isGTIN('12341238')
isGTIN('')
isGTIN(123)
gtin.isValid(barcode)
Validates a GTIN (14, 13, 12, or 8-digit) barcode by check digit. Barcode must
be a string.
To validate a UPC-E barcode, expand it first: isValid(upcE.expand('01278906'))
import { isValid } from 'gtin'
isValid('12341238')
isValid('1234123412344')
isValid('12341234123413')
isValid('012000007897')
isValid('012000007896')
isValid('abc')
isValid(123)
isValid('123')
gtin.minify(barcode)
Minifies GTIN to smallest possible representation, by stripping as many leading
zeroes as possible. Does not compress to UPC-E.
import { minify } from 'gtin'
minify('00000012341238')
minify('0000012341238')
minify('01234123412344')
minify('001234123412344')
minify('abc')
minify(123)
minify('123')
gtin.getFormat(barcode)
Gets the format of the given barcode. Does not validate checksum.
import { getFormat } from 'gtin'
getFormat('12341238')
getFormat('123412341234')
getFormat('1234123412344')
getFormat('01234123412344')
getFormat('123412381')
getFormat('abc')
getFormat(123)
getFormat('123')
gtin.getRealFormat(barcode)
Gets the real format of the given barcode, by minifying it first.
import { getRealFormat } from 'gtin'
getRealFormat('1234123412344')
getRealFormat('01234123412344')
getRealFormat('123412381')
getRealFormat('abc')
getRealFormat(123)
getRealFormat('123')
gtin.upcE.compress(barcode)
Compress a UPC-A barcode to an 8-digit UPC-E barcode. Does not validate
code by check digit. Barcode must be a string.
- 12-digit UPC-A: Number system and check digits are taken into account.
- 11-digit UPC-A: Number system 0 is assumed. Check digit is taken into account.
- 10-digit UPC-A: Number system 0 is assumed. Check digit is generated.
import { upcE } from 'gtin'
upcE.compress('1200000789')
upcE.compress('12000007897')
upcE.compress('012000007897')
upcE.compress('012000007896')
upcE.compress('012345678905')
upcE.compress(123)
upcE.compress('123')
upcE.compress('abc')
gtin.upcE.expand(barcode)
Expands a UPC-E barcode to a 12-digit UPC-A barcode. Does not validate
code by check digit. Barcode must be a string.
- 8-digit UPC-E: Number system and check digits are taken into account.
- 7-digit UPC-E: Number system 0 is assumed. Check digit is taken into account.
- 6-digit UPC-E: Number system 0 is assumed. Check digit is generated.
import { upcE } from 'gtin'
upcE.expand('127890')
upcE.expand('1278907')
upcE.expand('01278907')
upcE.expand('01278906')
upcE.expand('123412341')
upcE.expand(123)
upcE.expand('123')
upcE.expand('abc')