From bb806e0768af803388b29ca337274640a70dad77 Mon Sep 17 00:00:00 2001 From: Eric Date: Sun, 18 Apr 2021 19:01:53 -0400 Subject: [PATCH] fix modifiedCode when given 290/291 isbn codes --- index.js | 8 +++++--- package.json | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index c8768f3..0853f62 100644 --- a/index.js +++ b/index.js @@ -139,7 +139,7 @@ const validatorMap = { const validate = (code, type) => { // 1980s era UPC codes were apparently 11 digits, not using a checksum. So, if we get 11, I // guess there's not much we can do besides pass it with a 0 prefixing it and hope it works. - const modifiedCode = code.length === 11 ? `0${code}` : code; + let modifiedCode = code.length === 11 ? `0${code}` : code; if (!type) { type = getTypeOfBarcode(modifiedCode); } @@ -148,11 +148,11 @@ const validate = (code, type) => { if (modifiedCode.startsWith('290')) { const [ junk1, junk2, junk3, ...rest ] = modifiedCode; const baseCode = rest.join(''); - modifiedCode = `978${baseCode}${getIsbn13Checksum(`978${baseCode}`)}`; + modifiedCode = `978${baseCode.slice(0, -1)}${getIsbn13Checksum(`978${baseCode}`)}`; } else if (modifiedCode.startsWith('291')) { const [ junk1, junk2, junk3, ...rest ] = modifiedCode; const baseCode = rest.join(''); - modifiedCode = `979${baseCode}${getIsbn13Checksum(`979${baseCode}`)}` + modifiedCode = `979${baseCode.slice(0, -1)}${getIsbn13Checksum(`979${baseCode}`)}` } } let valid = false; @@ -176,4 +176,6 @@ const validate = (code, type) => { // console.warn(validate('BOOTSTRA P')); // should be an invalid ASIN // console.warn(getUpcChecksum('02035616631')); // console.warn(validate('Test random text')); +// console.warn(validate('2900077274312')); +// console.warn(validate('2900538754575')); module.exports = validate; diff --git a/package.json b/package.json index acdbcd1..0c7d0d3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ericblade/barcode-validator", - "version": "2.4.0", + "version": "2.4.1", "description": "Node Javascript Barcode Validation for ISBN10, ISBN13, UPC, GTIN", "main": "./index.js", "types": "./index.d.ts",