Skip to content

Commit

Permalink
Merge pull request #12 from JuanMaRuiz/fix/validator
Browse files Browse the repository at this point in the history
Fix/validator
  • Loading branch information
JuanMaRuiz committed Sep 28, 2021
2 parents 99d8d4a + 85e99cf commit da616ac
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
@@ -1,9 +1,14 @@
## [1.1.2] - 2020-12-29
## [2.0.1] - 2021-09-28
### Fix
* Fix validator for valid isbn-13 returned as false

## [2.0.0] - 2021-09-06
### Chore
* Breaking change. Convert package to esm module.
## [2.0.0] - 2021-09-06
### Fix
* Fix validator for valid isbn-13 returned as false

## [1.1.2] - 2020-12-29
## [1.1.1] - 2020-12-27
### Fix
* Fix validator to cover isbn-10 special case (ends with "X")
Expand Down
3 changes: 1 addition & 2 deletions __test__/validator.test.js
@@ -1,7 +1,5 @@
import validate from './../src/validator.js';

// Valid ISBN 8497364678, 84-481-2231-3, 978-84-415-2682-2

test('validate method should return TRUE if passed value is a valid 10 numbers ISBN ', () => {
expect(validate('84 - 481 - 2231 - 3')).toBe(true);
expect(validate('84 - 481 2231- 3')).toBe(true);
Expand All @@ -20,6 +18,7 @@ test('validate method should return FALSE if passed string does not have the cor
test('validate method should return TRUE if passed value is a valid 13 numbers ISBN ', () => {
expect(validate('978-84-415-2682-2')).toBe(true);
expect(validate('978-34-2628-155-0')).toBe(true);
expect(validate('978-2-27-509083-2')).toBe(true);
});

test('validate method should return FALSE if passed value is NOT a valid 13 numbers ISBN ', () => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "isbn-util",
"version": "2.0.0",
"version": "2.0.1",
"description": "",
"main": "index.js",
"type": "module",
Expand Down
10 changes: 8 additions & 2 deletions src/validator.js
Expand Up @@ -21,7 +21,7 @@ const IsValidIsbn = isbn => {
result = controlDigit.toUpperCase() === 'X' ? modulus === 10 : modulus === parseInt(controlDigit);

} else {
const elementsSum = isbnAsArray.map((el, index) => (index + 1) % 2 === 0 ? el * 1 : el * 3).reduce((acc, curr) => acc + curr);
const elementsSum = isbnAsArray.map((el, index) => index % 2 === 0 ? el * 1 : el * 3).reduce((acc, curr) => acc + curr);

result = getControlDigit(elementsSum) === parseInt(controlDigit);
}
Expand All @@ -30,7 +30,13 @@ const IsValidIsbn = isbn => {
};

const getControlDigit = (sum, acc = 0) => {
return sum % 10 !== 0 ? getControlDigit(sum + 1, acc + 1) : acc;
if (sum % 10 !== 0) {
return getControlDigit(sum + 1, acc + 1);
} else if (sum % 10 === 0 && acc === 0) {
return 0;
} else {
return acc;
}
};

const validate = (isbn) => {
Expand Down

0 comments on commit da616ac

Please sign in to comment.