(isbn)
| 11 | * @returns true if the ISBN provided is valid |
| 12 | */ |
| 13 | export function isValidIsbn(isbn) { |
| 14 | const isbnDigits = isbn.toUpperCase(); |
| 15 | |
| 16 | if (isbnDigits.length !== 10 && isbnDigits.length !== 13) { |
| 17 | return false; |
| 18 | } |
| 19 | |
| 20 | if (!isbnDigits.match(ISBN_PATTERN)) { |
| 21 | return false; |
| 22 | } |
| 23 | |
| 24 | if (isbnDigits.length === 10) { |
| 25 | const checkSum = isbnDigits |
| 26 | .split('') |
| 27 | .map((d, i) => (10 - i) * (d === 'X' ? 10 : parseInt(d, 10))) |
| 28 | .reduce((sum, parcel) => sum + parcel, 0); |
| 29 | |
| 30 | return checkSum % 11 === 0; |
| 31 | } |
| 32 | |
| 33 | const checkSum = isbnDigits |
| 34 | .split('') |
| 35 | .map((d, i) => ((i + 1) % 2 === 0 ? 3 : 1) * parseInt(d, 10)) |
| 36 | .reduce((sum, parcel) => sum + parcel, 0); |
| 37 | |
| 38 | return checkSum % 10 === 0; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Convert a valid ISBN-13 string to a obsolete ISBN-10. |
no outgoing calls
no test coverage detected