This package is a port of a very good existing package called fast-luhn created by @cybermatt.
It runs in NodeJs/Browser environments through WebAssembly, with very good speed and contains the necessary functions to work with the luhn algorithm quickly and efficiently.
Below is an overview of the package.
Package can be installed using npm cli
npm i @credit-card-stuffs/luhn
You can import like any other npm package
import luhn from "@credit-card-stuffs/luhn"
Check if string is valid by luhn algorithm. Return bool.
luhn.validate("471629309440")
// false
Calculate next digit for string of numbers. Return int.
luhn.digit("47162930944")
// 7
Add luhn-check digit to string of numbers. Return string.
luhn.complete("2398560146")
// 23985601469
Generate luhn-valid string of numbers with length. Return string.
luhn.generate(50)
// 04648626855744999947592497373533751558978979404719
Tests can be run with command
$ cargo test
You can build the package from source code
First of all you need to resolve some dependencies to get started.
-
- Rust
-
- NodeJs
-
- Wasm-pack
$ git clone https://github.com/credit-card-stuffs/luhn
$ cd luhn
$ wasm-pack build --target nodejs
After that you should see a folder called pkg/
in which it contains the generated Js files.