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

Function convertIsbn13ToIsbn10

services/isbn/tools.js:47–64  ·  view source on GitHub ↗
(isbn13)

Source from the content-addressed store, hash-verified

45 * @returns The ISBN-10 representation.
46 */
47export 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.

Callers 1

searchInCblFunction · 0.90

Calls 1

isValidIsbnFunction · 0.85

Tested by

no test coverage detected