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

Function isValidIsbn

services/isbn/tools.js:13–39  ·  view source on GitHub ↗
(isbn)

Source from the content-addressed store, hash-verified

11 * @returns true if the ISBN provided is valid
12 */
13export 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.

Callers 4

getBookByIsbnFunction · 0.90
isbn-tools.test.jsFile · 0.90
convertIsbn13ToIsbn10Function · 0.85
convertIsbn10ToIsbn13Function · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected