API reference › @evolu/common › Type › Isbn
const Isbn: brand(
"Isbn",
String,
(value: string) => {
if (!/^97(?:8\d|9[1-9])\d{9}$/u.test(value)) {
return err<IsbnError>({ type: "Isbn", value });
}
let sum = 0;
for (let index = 0; index < 13; index++) {
sum += (value.charCodeAt(index) - 48) * (index % 2 === 0 ? 1 : 3);
}
return sum % 10 === 0 ? ok() : err<IsbnError>({ type: "Isbn", value });
},
formatIsbnError,
);
Defined in: packages/common/src/Type.ts:9810
International Standard Book Number in the 13-digit form of ISO 2108, without hyphens.
Isbn accepts 13 ASCII digits starting with 978 or 979 and verifies the check digit: the digits weighted alternately by 1 and 3 from the left sum to a multiple of 10. The prefix 9790 is rejected, because it is reserved for ISMN, which numbers printed music.
Isbn accepts one spelling of each ISBN, so equal ISBNs are equal strings. It does not normalize. Remove the hyphens and spaces of the printed form before validating it. Convert a 10-digit ISBN first: prefix 978 to its first nine digits and append the check digit that makes the weighted sum a multiple of 10. Every ISBN-10 has an ISBN-13, and books published since 2007 carry one.
Example
import { assertEqual, assertErr, assertOk, Isbn } from "@evolu/common";
const value = "9780306406157";
assertOk(Isbn.fromUnknown(value), value);
assertErr(Isbn.fromUnknown("9780306406158"));
const printed = "978-0-306-40615-7";
const invalid = Isbn.fromUnknown(printed);
assertErr(invalid);
assertEqual(invalid.error, { type: "Isbn", value: printed });
assertOk(Isbn.fromUnknown(printed.replaceAll("-", "")), value);
// The ISBN-10 of the same book.
assertErr(Isbn.fromUnknown("0306406152"));