Getting started
@evanion/luhn 3.0.0 and its patch releases up to 3.0.2, the version on npm. Values on this page were produced by running the source at f9b1d93, a later commit than the 3.0.0 tag, and are not re-executed.
By the end of this page, your code will be able to print an order code with its check character and refuse a code where the check character is misread (e.g., reading an l as a 1).
Prerequisites
- Node 20 or newer, or any modern browser.
- An ES module (ESM) project. If you are using CommonJS, you will need Node 20.19, 22.12 or newer to load ES modules via
require(). - Dependencies: None. The package has no peer or runtime dependencies.
Install luhn
Run the command for your preferred package manager:
npm install @evanion/luhnUsing the Default Instance
The most common use case is the default 36-character dictionary (0-9, a-z). For this, you don’t need any configuration—just import the Luhn instance.
import { , } from '@evanion/luhn';
.; // -> '0123456789abcdefghijklmnopqrstuvwxyz'
. === ; // -> true
.; // -> 36
.; // -> truePrint an Order Code with a Check Character
@evanion/luhn computes the check character but leaves the final string formatting to you.
Imagine your database holds an order number like order-2026-0042. You want to print an order code on the receipt—the order number plus a check character—so that it can be validated locally.
import { } from '@evanion/luhn';
// 1. Generate a check character and append it to the order number
function (: string): string {
const { } = .();
return `${}-${}`;
}
const = ('order-2026-0042'); // -> 'order-2026-0042-l'
// 2. Validate the code a customer reads back, locally
.().; // -> true
.('order-2026-0042-1').; // -> falseIn this example, Luhn.generate returns the check character as checksum. We append it with a hyphen for readability. When a customer reads the code back, Luhn.validate ignores the hyphen and other non-dictionary characters (reading only the phrase), allowing it to verify the code regardless of how it was formatted.
If a customer reads order-2026-0042-l as order-2026-0042-1, validate will immediately return isValid: false. Your system can then ask the customer to repeat the code before ever querying the database for an order that doesn’t exist.
Next Steps
Your code now prints order codes and refuses misread ones. Generating and validating explores the full result objects returned by the API and shows how to use the phrase to extract the original order ID from a customer’s input.