MCPcopy Create free account
hub / github.com/BrasilAPI/BrasilAPI / convertIsbn10ToIsbn13

Function convertIsbn10ToIsbn13

services/isbn/tools.js:72–89  ·  view source on GitHub ↗
(isbn10)

Source from the content-addressed store, hash-verified

70 * @returns The ISBN-13 representation.
71 */
72export 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}

Callers 1

searchInCblFunction · 0.90

Calls 1

isValidIsbnFunction · 0.85

Tested by

no test coverage detected