4.0.1 • Published 1 year ago

cdigit v4.0.1

Weekly downloads
8,422
License
(MIT OR Apache-2....
Repository
github
Last release
1 year ago

cdigit: Check Digit Algorithms in JS

npm License

cdigit - Collection of check digit algorithms implemented in JavaScript

Synopsis

Node.js:

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:

# Damm algorithm
npx cdigit --algo damm compute 1234
npx cdigit --algo damm generate 1234
npx cdigit --algo damm validate 12340

Supported Algorithms

Generic Algorithms

Algorithmcdigit nameInput stringCheck character(s)
LuhnluhnNumeric (0-9)1 digit (0-9)
VerhoeffverhoeffNumeric (0-9)1 digit (0-9)
DammdammNumeric (0-9)1 digit (0-9)

ISO/IEC 7064 Family

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).

Algorithmcdigit nameInput stringCheck character(s)
ISO/IEC 7064, MOD 11-2mod11_2Numeric (0-9)1 digit or 'X' (0-9X)
ISO/IEC 7064, MOD 37-2mod37_2Alphanumeric (0-9A-Z)1 digit, letter, or '*' (0-9A-Z*)
ISO/IEC 7064, MOD 97-10mod97_10Numeric (0-9)2 digits (0-9)
ISO/IEC 7064, MOD 661-26mod661_26Alphabetic (A-Z)2 letters (A-Z)
ISO/IEC 7064, MOD 1271-36mod1271_36Alphanumeric (0-9A-Z)2 digits or letters (0-9A-Z)
ISO/IEC 7064, MOD 11,10mod11_10Numeric (0-9)1 digit (0-9)
ISO/IEC 7064, MOD 27,26mod27_26Alphabetic (A-Z)1 letter (A-Z)
ISO/IEC 7064, MOD 37,36mod37_36Alphanumeric (0-9A-Z)1 digit or letter (0-9A-Z)

GTIN (Global Trade Item Number) Family

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 GTINs and other GS1 data structures. Note that the gtin object does not check the length or semantic validity of a given GTIN string.

Algorithmcdigit nameInput stringCheck character(s)Also known as
GTIN-8gtinNumeric (0-9)1 digit (0-9)EAN-8
GTIN-12gtinNumeric (0-9)1 digit (0-9)UPC, UPC-A
GTIN-13gtinNumeric (0-9)1 digit (0-9)EAN, JAN, ISBN-13, etc.
GTIN-14gtinNumeric (0-9)1 digit (0-9)EAN, UCC-14

Usage - Node.js

Load algorithm objects using the cdigit names listed in Supported Algorithms section.

import { mod97_10 } from "cdigit";

Algorithm objects implement the following methods:

validate(strWithCheckChars: string): boolean

Checks if a protected string is valid per the algorithm.

console.log(mod97_10.validate("123482")); // true

generate(strWithoutCheckChars: string): string

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.

console.log(mod97_10.generate("1234")); // "123482"

compute(strWithoutCheckChars: string): string

Generates the check character(s) from the argument using the algorithm. Unlike generate(), this method returns the check character(s) only.

console.log(mod97_10.compute("1234")); // "82"

computeFromNumVals(numValsWithoutCheckChars: number[]): number[]

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.

console.log(mod97_10.computeFromNumVals([1, 2, 3, 4])); // [8, 2]

See example.js for usage examples.

Usage - Command-line

Usage: cdigit [options] [command]

Options:
  -a, --algo <name>  specify check digit algorithm by name
  -h, --help         display help for command

Commands:
  validate <string>  check if string is valid
  generate <string>  append check character(s) to string
  compute <string>   print check character(s) computed from string

-a, --algo <name> option accepts the names listed in Supported Algorithms section and defaults to luhn or the value of CDIGIT_CLI_DEFAULT_ALGO environment variable (if set).

License

Copyright (c) 2018-2023 LiosK

Licensed under either of

at your option.

Contribution

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.

See Also

4.0.1

1 year ago

4.0.0-alpha.1

1 year ago

4.0.0-alpha.0

1 year ago

3.2.1

1 year ago

3.2.0

1 year ago

4.0.0

1 year ago

3.1.4

1 year ago

3.1.3

2 years ago

3.1.2

2 years ago

3.1.1

2 years ago

3.1.0

2 years ago

3.0.1

3 years ago

3.0.0-beta.1

3 years ago

2.6.7

3 years ago

3.0.0

3 years ago

2.6.6

3 years ago

2.6.5

3 years ago

2.6.4

3 years ago

2.6.3

3 years ago

2.6.2

3 years ago

2.6.1

3 years ago

2.6.0

3 years ago

2.5.8

3 years ago

2.5.7

3 years ago

2.5.6

4 years ago

2.5.5

4 years ago

2.5.4

4 years ago

2.5.3

4 years ago

2.5.2

4 years ago

2.5.1

4 years ago

2.5.0

4 years ago

2.5.0-rc1

4 years ago

2.5.0-rc0

4 years ago

2.4.1

4 years ago

2.4.0

4 years ago

2.3.7

5 years ago

2.3.6

5 years ago

2.3.5

5 years ago

2.3.4

5 years ago

2.3.3

5 years ago

2.3.2

5 years ago

2.3.1

5 years ago

2.3.0

5 years ago

2.2.3

5 years ago

2.2.2

5 years ago

2.2.1

5 years ago

2.2.0

5 years ago

2.1.1

5 years ago

2.1.0

5 years ago

2.0.0

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago