Handle dashes in isbns

This commit is contained in:
Mouse Reeve 2020-10-30 12:43:02 -07:00
parent 9780879ce6
commit 3fc1f46897
2 changed files with 13 additions and 0 deletions

View file

@ -186,6 +186,7 @@ class Edition(Book):
def isbn_10_to_13(isbn_10):
''' convert an isbn 10 into an isbn 13 '''
isbn_10 = isbn_10.replace('-', '')
# drop the last character of the isbn 10 number (the original checkdigit)
converted = isbn_10[:9]
# add "978" to the front
@ -206,6 +207,8 @@ def isbn_13_to_10(isbn_13):
if isbn_13[:3] != '978':
return None
isbn_13 = isbn_13.replace('-', '')
# remove '978' and old checkdigit
converted = isbn_13[3:-1]
# calculate checkdigit

View file

@ -54,11 +54,21 @@ class Book(TestCase):
isbn_13 = isbn_10_to_13(isbn_10)
self.assertEqual(isbn_13, '9781788161671')
isbn_10 = '1-788-16167-X'
isbn_13 = isbn_10_to_13(isbn_10)
self.assertEqual(isbn_13, '9781788161671')
def test_isbn_13_to_10(self):
isbn_13 = '9781788161671'
isbn_10 = isbn_13_to_10(isbn_13)
self.assertEqual(isbn_10, '178816167X')
isbn_13 = '978-1788-16167-1'
isbn_10 = isbn_13_to_10(isbn_13)
self.assertEqual(isbn_10, '178816167X')
class Shelf(TestCase):
def setUp(self):