(isbn13)
| 45 | * @returns The ISBN-10 representation. |
| 46 | */ |
| 47 | export function convertIsbn13ToIsbn10(isbn13) { |
| 48 | if (!isValidIsbn(isbn13)) { |
| 49 | return null; |
| 50 | } |
| 51 | |
| 52 | if (isbn13.length === 10) { |
| 53 | return isbn13; |
| 54 | } |
| 55 | |
| 56 | const equalPart = isbn13.slice(3, -1); |
| 57 | const checkSum = equalPart |
| 58 | .split('') |
| 59 | .map((d, i) => parseInt(d, 10) * (i + 1)) |
| 60 | .reduce((sum, parcel) => sum + parcel, 0); |
| 61 | const lastDigit = checkSum % 11; |
| 62 | |
| 63 | return equalPart + (lastDigit === 10 ? 'X' : lastDigit); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Convert a valid ISBN-10 string to a current ISBN-13. |
no test coverage detected