package bookaml

  1. Overview
  2. Docs

Module Bookaml_ISBN

Module encapsulating ISBNs. It makes sure that any given ISBN is correct, taking advantage of the check digit present in every ISBN. It supports both 10 and 13-digit ISBNs.

Exceptions

exception Bad_ISBN_length of string
exception Bad_ISBN_checksum of string
exception Bad_ISBN_character of char

Type definitions

type +'a t constraint 'a = [< `ISBN10 | `ISBN13 ]

The type of valid ISBN numbers.

type pg_t = string

The PGOCAML-compatible type used for (de)serialising values of type t

Public functions and values

Conversion to/from strings

val of_string : string -> [> `ISBN10 | `ISBN13 ] t

Converts a string into an ISBN, represented by type t. Raises Invalid_arg if the string cannot be converted into a valid ISBN. Both 10 and 13-digit ISBNs are accepted. Moreover, the string may contain dashes, which are automatically removed if present.

val of_string10 : string -> [> `ISBN10 ] t

Converts a string containing a 10-digit ISBN into t.

val of_string13 : string -> [> `ISBN13 ] t

Converts a string containing a 13-digit ISBN into t.

val to_string : [< `ISBN10 | `ISBN13 ] t -> string

Converts a t (representing an ISBN) into a string.

Conversion to/from 10 and 13-digit ISBNs

val to_10 : [< `ISBN10 | `ISBN13 ] t -> [> `ISBN10 ] t option

Converts a given t into a 10-digit ISBN. If the supplied ISBN is already a 10-digit ISBN then it's returned unchanged. If the supplied ISBN is a backwards-compatible 13-digit ISBN (it starts with "978"), then it is converted. Otherwise, the conversion is not possible and None is returned.

val to_13 : [< `ISBN10 | `ISBN13 ] t -> [> `ISBN13 ] t

Converts a given t into a 13-digit ISBN. If the supplied ISBN is already a 13-digit ISBN then it's returned unchanged. If the supplied ISBN is a 10-digit ISBN then it's converted by prepending a "978" and recomputing the check digit.

Conversion to/from strings

val of_pg : pg_t -> [> `ISBN10 | `ISBN13 ] t

Deserialises from a PGOCAML-compatible format.

val of_pg10 : pg_t -> [> `ISBN10 ] t

Deserialises from a PGOCAML-compatible format. Should only be used for 10-digit ISBNs.

val of_pg13 : pg_t -> [> `ISBN13 ] t

Deserialises from a PGOCAML-compatible format. Should only be used for 13-digit ISBNs.

val to_pg : [< `ISBN10 | `ISBN13 ] t -> pg_t

Serialises into a PGOCAML-compatible format.

Validity checks

val is_valid : string -> bool

Does the given string represent a valid ISBN number? Both 10 and 13-digit ISBNs are accepted. Moreover, the string may contain dashes, which are automatically removed if present.

val is_valid10 : string -> bool

Does the given string represent a valid 10-digit ISBN number?

val is_valid13 : string -> bool

Does the given string represent a valid 13-digit ISBN number?

Identity checks

val is_10 : [< `ISBN10 | `ISBN13 ] t -> bool

Is the given t a 10-digit ISBN?

val is_13 : [< `ISBN10 | `ISBN13 ] t -> bool

Is the given t a 13-digit ISBN?