(isbn10)
| 70 | * @returns The ISBN-13 representation. |
| 71 | */ |
| 72 | export function convertIsbn10ToIsbn13(isbn10) { |
| 73 | if (!isValidIsbn(isbn10)) { |
| 74 | return null; |
| 75 | } |
| 76 | |
| 77 | if (isbn10.length === 13) { |
| 78 | return isbn10; |
| 79 | } |
| 80 | |
| 81 | const newIsbn = `978${isbn10.slice(0, -1)}`; |
| 82 | const checkSum = newIsbn |
| 83 | .split('') |
| 84 | .map((d, i) => ((i + 1) % 2 === 0 ? 3 : 1) * parseInt(d, 10)) |
| 85 | .reduce((sum, parcel) => sum + parcel, 0); |
| 86 | const lastDigit = checkSum % 10; |
| 87 | |
| 88 | return newIsbn + (lastDigit !== 0 ? 10 - lastDigit : 0); |
| 89 | } |
no test coverage detected