Collection of check digit algorithms implemented in JavaScript
npm install cdigit

cdigit - Collection of check digit algorithms implemented in JavaScript
Node.js:
``javascript
import { luhn } from "cdigit";
// Luhn (a.k.a. Mod 10) algorithm
console.log(luhn.compute("1234")); // "4"
console.log(luhn.generate("1234")); // "12344"
console.log(luhn.validate("12344")); // true
`
Command-line:
`bash`Damm algorithm
npx cdigit --algo damm compute 1234
npx cdigit --algo damm generate 1234
npx cdigit --algo damm validate 12340
| Algorithm | cdigit name | Input string | Check character(s) |
| ---------- | ----------- | ------------- | ------------------ |
| [Luhn] | luhn | Numeric (0-9) | 1 digit (0-9) |
| [Verhoeff] | verhoeff | Numeric (0-9) | 1 digit (0-9) |
| [Damm] | damm | Numeric (0-9) | 1 digit (0-9) |
[Luhn]: https://en.wikipedia.org/wiki/Luhn_algorithm
[Verhoeff]: https://en.wikipedia.org/wiki/Verhoeff_algorithm
[Damm]: https://en.wikipedia.org/wiki/Damm_algorithm
ISO/IEC 7064 describes eight generic check digit (character) systems for
numeric, alphabetic, and alphanumeric strings. ISO/IEC 7064 specifies two types
of systems that use the same algorithm with different parameters: Pure systems
(MOD 11-2, MOD 37-2, MOD 97-10, MOD 661-26, and MOD 1271-36) and Hybrid systems
(MOD 11,10, MOD 27,26, and MOD 37,36).
| Algorithm | cdigit name | Input string | Check character(s) |
| ------------------------- | ----------- | --------------------- | ----------------------------------- |
| ISO/IEC 7064, MOD 11-2 | mod11_2 | Numeric (0-9) | 1 digit or 'X' (0-9X) |
| ISO/IEC 7064, MOD 37-2 | mod37_2 | Alphanumeric (0-9A-Z) | 1 digit, letter, or '\' (0-9A-Z\) |
| ISO/IEC 7064, MOD 97-10 | mod97_10 | Numeric (0-9) | 2 digits (0-9) |
| ISO/IEC 7064, MOD 661-26 | mod661_26 | Alphabetic (A-Z) | 2 letters (A-Z) |
| ISO/IEC 7064, MOD 1271-36 | mod1271_36 | Alphanumeric (0-9A-Z) | 2 digits or letters (0-9A-Z) |
| ISO/IEC 7064, MOD 11,10 | mod11_10 | Numeric (0-9) | 1 digit (0-9) |
| ISO/IEC 7064, MOD 27,26 | mod27_26 | Alphabetic (A-Z) | 1 letter (A-Z) |
| ISO/IEC 7064, MOD 37,36 | mod37_36 | Alphanumeric (0-9A-Z) | 1 digit or letter (0-9A-Z) |
[ISO/IEC 7064]: https://www.iso.org/standard/31531.html
GTINs are internationally unified product identification numbers that are often
(or historically) referred to as [UPC], [EAN], [ISBN]-13, etc. GTINs have
several variations in length but share the identical check digit algorithm;
therefore, cdigit currently provides only one generic gtin object for GTINsgtin
and other [GS1 data structures]. Note that the object does not check the
length or semantic validity of a given GTIN string.
| Algorithm | cdigit name | Input string | Check character(s) | Also known as |
| --------- | ----------- | ------------- | ------------------ | ------------------------- |
| GTIN-8 | gtin | Numeric (0-9) | 1 digit (0-9) | EAN-8 |
| GTIN-12 | gtin | Numeric (0-9) | 1 digit (0-9) | UPC, UPC-A |
| GTIN-13 | gtin | Numeric (0-9) | 1 digit (0-9) | EAN, [JAN], ISBN-13, etc. |
| GTIN-14 | gtin | Numeric (0-9) | 1 digit (0-9) | EAN, UCC-14 |
[GTIN]: https://www.gs1.org/standards/id-keys/gtin
[UPC]: https://en.wikipedia.org/wiki/Universal_Product_Code
[EAN]: https://en.wikipedia.org/wiki/International_Article_Number
[ISBN]: https://en.wikipedia.org/wiki/International_Standard_Book_Number
[GS1 data structures]: https://www.gs1.org/standards/id-keys
[JAN]: https://en.wikipedia.org/wiki/International_Article_Number#Japanese_Article_Number
Load algorithm objects using the cdigit names listed in
Supported Algorithms section.
`javascript`
import { mod97_10 } from "cdigit";
Algorithm objects implement the following methods:
Checks if a protected string is valid per the algorithm.
`javascript`
console.log(mod97_10.validate("123482")); // true
Generates the protected string from the argument using the algorithm. The
generated string consists of the original bare string and computed check
character(s), which are combined in accordance with the algorithm.
`javascript`
console.log(mod97_10.generate("1234")); // "123482"
Generates the check character(s) from the argument using the algorithm. Unlike
generate(), this method returns the check character(s) only.
`javascript`
console.log(mod97_10.compute("1234")); // "82"
Generates the check character(s) from the argument using the algorithm. This
method is an alphabet-independent equivalent of compute(), where the return
value and argument are both represented as arrays of each digit's numerical
value.
`javascript`
console.log(mod97_10.computeFromNumVals([1, 2, 3, 4])); // [8, 2]
See example.js for usage examples.
`
Usage: cdigit [options] [command]
Options:
-a, --algo
-h, --help display help for command
Commands:
validate
generate
compute
`
-a, --algo option accepts the names listed in Supported Algorithmsluhn
section and defaults to or the value ofCDIGIT_CLI_DEFAULT_ALGO` environment variable (if set).
Copyright (c) 2018-2023 LiosK
Licensed under either of
- Apache License, Version 2.0
- MIT license
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
dual licensed as above, without any additional terms or conditions.