Skip to content

Commit

Permalink
fix modifiedCode when given 290/291 isbn codes
Browse files Browse the repository at this point in the history
  • Loading branch information
ericblade committed Apr 18, 2021
1 parent e06e447 commit bb806e0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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;
Expand All @@ -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;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit bb806e0

Please sign in to comment.