diff --git a/CHANGELOG.md b/CHANGELOG.md index 4986026..ceca8b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## [1.1.1] - 2020-12-27 +### Fix +* Fix validator to cover isbn-10 special case (ends with "X") ## [1.1.0] - 2020-09-07 ### Added * Add "prefix" option to generator to produce output with or without 'isbn-[type]: ' string. diff --git a/__test__/validator.test.js b/__test__/validator.test.js index 81a51c2..c2a3c5d 100644 --- a/__test__/validator.test.js +++ b/__test__/validator.test.js @@ -8,6 +8,7 @@ test('validator should return TRUE if passed value is a valid 10 numbers ISBN ', expect(validator('84 481 2231 3')).toBe(true); expect(validator('8448122313')).toBe(true); expect(validator('84-481-2231-3')).toBe(true); + expect(validator('006095485X')).toBe(true); }); test('should return FALSE if passed string does not have the correct lenght', () => { diff --git a/package.json b/package.json index 528c806..17a43b8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "isbn-util", - "version": "1.1.0", + "version": "1.1.1", "description": "", "main": "index.js", "scripts": { diff --git a/src/validator.js b/src/validator.js index 1de8f05..74798f1 100644 --- a/src/validator.js +++ b/src/validator.js @@ -9,16 +9,18 @@ const hasCorrectFormat = value => value.length === validLengths.MIN || value.len const IsValidIsbn = isbn => { let result = false; const isbnAsArray = Array.from(isbn); + const controlDigit = isbnAsArray.pop(); if (isbn.length === validLengths.MIN ) { const sumOfIsbnNumbers = isbnAsArray.map((el, index) => { return el * (index + 1); }).reduce((acc, curr) => acc + curr ); - result = (sumOfIsbnNumbers % 11) === 0; + const modulus = sumOfIsbnNumbers % 11; + + result = controlDigit.toUpperCase() === 'X' ? modulus === 10 : modulus === parseInt(controlDigit); } else { - const controlDigit = isbnAsArray.pop(); const elementsSum = isbnAsArray.map((el, index) => (index + 1) % 2 === 0 ? el * 1 : el * 3).reduce((acc, curr) => acc + curr); const sumModule = elementsSum % 10;