Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

gtin

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gtin - npm Package Compare versions

Comparing version 0.1.1 to 0.2.0

lib/common.js

47

lib/index.js
'use strict'
var common = require('./common')
function validate (barcode) {
validateBarcodeInput(barcode)
var checkDigit = barcode.slice(-1)
var generatedCheckDigit = common.generateCheckDigit(
barcode.slice(0, -1)
)
return (checkDigit === generatedCheckDigit)
}
function getFormat (barcode) {
validateBarcodeInput(barcode)
return 'GTIN-' + barcode.length
}
function minify (barcode) {
validateBarcodeInput(barcode)
var format = +getFormat(barcode).replace('GTIN-', '')
switch (format) {
case 14:
return barcode.replace(/^(0{6}|0{1,2})/, '')
case 13:
return barcode.replace(/^(0{5}|0{1})/, '')
case 12:
return barcode.replace(/^0{4}/, '')
case 8:
return barcode
}
}
function getRealFormat (barcode) {
return getFormat(minify(barcode))
}
function validateBarcodeInput (barcode) {
if (typeof barcode !== 'string') throw new Error(common.NONSTRING_ERR)
if (!/^(\d{12,14}|\d{8})$/.test(barcode)) throw new Error(common.FORMAT_ERR)
}
exports.validate = validate
exports.minify = minify
exports.getFormat = getFormat
exports.getRealFormat = getRealFormat
exports.upce = require('./upce')

31

lib/upce.js
'use strict'
var padStart = require('lodash.padstart')
const common = require('./common')
exports.expand = function (barcode) {
if (typeof barcode !== 'string') throw new Error('Barcode must be a string')
if (!/^\d{6,8}$/.test(barcode)) throw new Error('Barcode is not of a valid format')
if (typeof barcode !== 'string') throw new Error(common.NONSTRING_ERR)
if (!/^\d{6,8}$/.test(barcode)) throw new Error(common.FORMAT_ERR)
return setCheckDigit(

@@ -47,4 +47,4 @@ setNumberSystem(

exports.compress = function (barcode) {
if (typeof barcode !== 'string') throw new Error('Barcode must be a string')
if (!/^\d{10,12}$/.test(barcode)) throw new Error('Barcode is not of a valid format')
if (typeof barcode !== 'string') throw new Error(common.NONSTRING_ERR)
if (!/^\d{10,12}$/.test(barcode)) throw new Error(common.FORMAT_ERR)
return compressBarcode(

@@ -92,3 +92,3 @@ setCheckDigit(

(barcode.length !== 12 && barcode.length !== 8)
? barcode + generateCheckDigit(barcode)
? barcode + common.generateCheckDigit(barcode)
: barcode

@@ -98,21 +98,2 @@ )

function generateCheckDigit (barcode) {
return String(
(
10 - (
(
padStart(barcode, 13, '0')
.split('')
.map(function (num, idx) {
return (+num) * ((idx % 2 === 0) ? 3 : 1)
})
.reduce(function (prev, cur) {
return prev + cur
})
) % 10
)
) % 10
)
}
function setNumberSystem (barcode) {

@@ -119,0 +100,0 @@ return (

{
"name": "gtin",
"version": "0.1.1",
"version": "0.2.0",
"description": "GTIN (UPC, EAN, ITF, etc.) utilities.",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

@@ -18,2 +18,83 @@ gtin

* [gtin.validate(barcode)](#user-content-gtin-validate)
* [gtin.minify(barcode)](#user-content-gtin-minify)
* [gtin.getFormat(barcode)](#user-content-gtin-getFormat)
* [gtin.getRealFormat(barcode)](#user-content-gtin-getRealFormat)
* [gtin.upce.compress(barcode)](#user-content-gtin-upce-compress)
* [gtin.upce.expand(barcode)](#user-content-gtin-upce-expand)
---
<a id='gtin-validate'></a>
`gtin.validate(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: `validate(upce.expand('01278906'))`
```js
import {validate} from 'gtin'
validate('12341238') // true
validate('1234123412344') // true
validate('12341234123413') // true
validate('012000007897') // true
validate('012000007896') // false
validate('abc') // Error thrown
validate(123) // Error thrown
validate('123') // Error thrown
```
<a id='gtin-minify'></a>
`gtin.minify(barcode)`
---
Minifies GTIN to smallest possible representation, by stripping as many leading zeroes as possible. Does not compress to UPC-E.
```js
import {minify} from 'gtin'
minify('00000012341238') // '12341238'
minify('0000012341238') // '12341238'
minify('01234123412344') // '1234123412344
minify('001234123412344') // Error thrown
minify('abc') // Error thrown
minify(123) // Error thrown
minify('123') // Error thrown
```
<a id='gtin-getFormat'></a>
`gtin.getFormat(barcode)`
---
Gets the format of the given barcode. Does not validate checksum.
```js
import {getFormat} from 'gtin'
getFormat('12341238') // 'GTIN-8'
getFormat('123412341234') // 'GTIN-12'
getFormat('1234123412344') // 'GTIN-13'
getFormat('01234123412344') // 'GTIN-14'
getFormat('123412381') // Error thrown
getFormat('abc') // Error thrown
getFormat(123) // Error thrown
getFormat('123') // Error thrown
```
<a id='gtin-getRealFormat'></a>
`gtin.getRealFormat(barcode)`
---
Gets the real format of the given barcode, by minifying it first.
```js
import {getFormat} from 'gtin'
getFormat('1234123412344') // 'GTIN-13'
getFormat('01234123412344') // 'GTIN-13'
getFormat('123412381') // Error thrown
getFormat('abc') // Error thrown
getFormat(123) // Error thrown
getFormat('123') // Error thrown
```
<a id='gtin-upce-compress'></a>
`gtin.upce.compress(barcode)`

@@ -41,2 +122,3 @@ ---

<a id='gtin-upce-expand'></a>
`gtin.upce.expand(barcode)`

@@ -43,0 +125,0 @@ ---

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc