off by one in formatISBN

This commit is contained in:
rlx 2016-01-11 18:47:14 +05:30
parent 8a96a0a095
commit 0b7beec62d

View file

@ -653,13 +653,13 @@ Ox.formatISBN = function(isbn, length) {
var ret = ''; var ret = '';
function getCheckDigit(isbn) { function getCheckDigit(isbn) {
var mod = isbn.length == 10 ? 11 : 10 var mod = isbn.length == 10 ? 11 : 10
return (mod - Ox.sum( return (Ox.mod(mod - Ox.sum(
isbn.slice(0, -1).split('').map(function(digit, index) { isbn.slice(0, -1).split('').map(function(digit, index) {
return isbn.length == 10 return isbn.length == 10
? parseInt(digit) * (10 - index) ? parseInt(digit) * (10 - index)
: parseInt(digit) * (index % 2 == 0 ? 1 : 3); : parseInt(digit) * (index % 2 == 0 ? 1 : 3);
}) })
) % mod + '').replace('10', 'X'); ), mod) + '').replace('10', 'X');
} }
isbn = isbn.toUpperCase().replace(/[^\dX]/g, ''); isbn = isbn.toUpperCase().replace(/[^\dX]/g, '');
if (isbn.length == 10) { if (isbn.length == 10) {