Comparing version 0.2.0 to 0.3.0
@@ -33,2 +33,3 @@ 'use strict' | ||
exports.NONSTRING_ERR = 'Barcode must be a string' | ||
exports.EMPTY_CODE_ERR = 'Barcode must not be an empty string' | ||
exports.FORMAT_ERR = 'Barcode is not of a valid format' |
@@ -5,2 +5,10 @@ 'use strict' | ||
function isGTIN (barcode) { | ||
if (typeof barcode !== 'string') throw new Error(common.NONSTRING_ERR) | ||
if (!barcode) throw new Error(common.EMPTY_CODE_ERR) | ||
return ( | ||
/^(\d{12,14}|\d{8})$/.test(barcode) | ||
) | ||
} | ||
function validate (barcode) { | ||
@@ -42,6 +50,6 @@ validateBarcodeInput(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) | ||
if (!isGTIN(barcode)) throw new Error(common.FORMAT_ERR) | ||
} | ||
exports.isGTIN = isGTIN | ||
exports.validate = validate | ||
@@ -48,0 +56,0 @@ exports.minify = minify |
{ | ||
"name": "gtin", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "GTIN (UPC, EAN, ITF, etc.) utilities.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -18,2 +18,3 @@ gtin | ||
* [gtin.isGTIN(barcode)](#user-content-gtin-isGTIN) | ||
* [gtin.validate(barcode)](#user-content-gtin-validate) | ||
@@ -28,2 +29,16 @@ * [gtin.minify(barcode)](#user-content-gtin-minify) | ||
<a id='gtin-isGTIN'></a> | ||
`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. | ||
```js | ||
import {isGTIN} from 'gtin' | ||
isGTIN('1234') // false | ||
isGTIN('12341238') // true | ||
isGTIN('') // Error thrown | ||
isGTIN(123) // Error thrown | ||
``` | ||
<a id='gtin-validate'></a> | ||
@@ -30,0 +45,0 @@ `gtin.validate(barcode)` |
@@ -171,1 +171,43 @@ 'use strict' | ||
}) | ||
test('gtin.isGTIN(barcode) (ok)', t => { | ||
t.plan(8) | ||
const cases = [ | ||
['abcdabcdabcd', false], | ||
['1234', false], | ||
['123412341', false], | ||
['12341234123412341', false], | ||
['12341234', true], | ||
['123412341234', true], | ||
['1234123412343', true], | ||
['12341234123434', true] | ||
] | ||
for (let testCase of cases) { | ||
t.is( | ||
gtin.isGTIN(testCase[0]), | ||
testCase[1] | ||
) | ||
} | ||
}) | ||
test('gtin.isGTIN(barcode) (error)', t => { | ||
t.plan(4) | ||
const cases = [ | ||
[123, 'Barcode must be a string'], | ||
[{}, 'Barcode must be a string'], | ||
[undefined, 'Barcode must be a string'], | ||
['', 'Barcode must not be an empty string'] | ||
] | ||
for (let testCase of cases) { | ||
try { | ||
gtin.isGTIN(testCase[0]) | ||
t.fail('Error not thrown') | ||
} catch (err) { | ||
t.is(err.message, testCase[1]) | ||
} | ||
} | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16904
427
158