Module:Format ISBN/doc

This module formats ISBNs. By convention, printed ISBNs use either hyphens or spaces to separate the elements within the number. The module accepts a string that represents an ISBN, and returns a formatted ISBN string. The input ISBN must be valid. That is:
 * it must be 10 or 13 characters long after removing any existing hyphen or space formatting; and
 * for 13 character ISBNs, all the characters must be digits, and it must start with 978 or 979;
 * for 10 character ISBNs, all the characters except the final one must be digits, and the final character must be either a digit or X;
 * and, the final character must be the correct check digit.

The module's only public function is format. The formatted ISBN uses hyphens or spaces, with hyphens as the default. The default prefix is ISBN, but the prefix is settable. ISBNs with 10 characters may optionally be converted to equivalent 13 character ISBNs. For invalid input ISBNs, the error return value is settable, and the default is ISBN error.

Usage
Invoke with:

Parameters:
 * isbn: The ISBN to format. Must be supplied. Should be a valid 10 character or 13 character ISBN.
 * prefix: ISBN format prefix. Optional. Default is ISBN. If not empty, it is separated from the ISBN by.
 * use_spaces: Indicate formatting with spaces instead of hyphens. Boolean. Optional. Default is false.
 * use_conversion: Convert 10 character ISBNs to 13 character ones before formatting. Boolean. Optional. Default is false. This parameter has no effect if the input ISBN is a 13 character one.
 * error: Return value to use in place of ISBN error. Optional.

Parameters may also be supplied by position, in the order shown above. If using positional parameters, a shorthand way to set prefix to the empty string is to follow the positional isbn with a single pipe character. For example, equivalent to.

Examples
Using results in:

Using results in:

Using results in:

Using results in:

Using results in:

Using results in:

Using results in:

Using results in:

Canonical uses
To format an ISBN in article text:
 * Replace wikitext ISBN 978-1843560289
 * with
 * to generate:


 * Alternatively, replace wikitext ISBN 978-1843560289
 * with ISBN
 * to generate: ISBN

To format an ISBN in a book citation (or other template with an parameter):
 * Replace wikitext
 * with }}
 * to generate:

In all cases, ISBN magic links, wiki article categorisation, and so on will continue to work correctly. MediaWiki recognises and treats both formatted and unformatted valid ISBNs equivalently, and it checks for and triggers ISBN magic links only after all template and other processing is finished.

Technical notes
For efficiency, this module loads its table data using mw.loadData rather than Lua's require. Data loaded this way has some severe limitations. Several Lua features will not work correctly with it. And worse, these features do not fail, but rather they return incorrect or misleading results. For example #value always returns zero no matter how many elements the item contains, and unpack returns nil.

When editing this module, be very careful to ensure that the Lua language feature you are using will work correctly with a data set that is loaded in this way.