Skip to content

Commit

Permalink
(fix) Issue 4 - error validating isbn-10 ending with X
Browse files Browse the repository at this point in the history
* Add special case when isbn ends with X
  • Loading branch information
JuanMaRuiz committed Dec 27, 2020
1 parent d98433c commit 604d4e6
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
1 change: 1 addition & 0 deletions __test__/validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "isbn-util",
"version": "1.1.0",
"version": "1.1.1",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
6 changes: 4 additions & 2 deletions src/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 604d4e6

Please sign in to comment.