Skip to content

Commit a31555e

Browse files
authored
🐛 Fix check digit calculation for ISBN10 (#292)
1 parent d8272c4 commit a31555e

File tree

2 files changed

+3
-5
lines changed

2 files changed

+3
-5
lines changed

pydantic_extra_types/isbn.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@ def isbn10_digit_calc(isbn: str) -> str:
2121
The calculated last digit of the ISBN-10 value.
2222
"""
2323
total = sum(int(digit) * (10 - idx) for idx, digit in enumerate(isbn[:9]))
24-
25-
for check_digit in range(1, 11):
26-
if (total + check_digit) % 11 == 0:
27-
valid_check_digit = 'X' if check_digit == 10 else str(check_digit)
28-
24+
diff = (11 - total) % 11
25+
valid_check_digit = 'X' if diff == 10 else str(diff)
2926
return valid_check_digit
3027

3128

tests/test_isbn.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ def test_isbn13_early_digits(input_isbn: Any, output_isbn: str, valid: bool) ->
122122
('080442957X', '9780804429573', True), # ISBN-10 ending in "X" as input
123123
('9788584390670', '9788584390670', True), # ISBN-13 Starting with 978
124124
('9790306406156', '9790306406156', True), # ISBN-13 starting with 979
125+
('8306018060', '9788306018066', True), # ISBN-10 as input
125126
# Invalid ISBNs
126127
('8537809663', None, False), # ISBN-10 as input with wrong last digit
127128
('9788537809661', None, False), # ISBN-13 as input with wrong last digit

0 commit comments

Comments
 (0)