Prefix fixture generator
Keep a chosen prefix and add a Luhn check digit. For local tests; no issuer or account lookup.
Spaces, nonbreaking spaces and hyphens are removed. Letters and line breaks are rejected.
Use this generator when a test needs a specific prefix. Use the network generator when the requirement is a recognized brand and length. A number that passes Luhn still says nothing about an issuer record or payment acceptance.
Reproduce a stale-export bug
A subtle failure happens when a form keeps old output but labels it with newly edited controls. The regression case is:
- Generate five 16-digit values with prefix
4. - Change the input to
51without generating again. - Select Show JSON. Every record must still contain
"prefix": "4"and its original number. - Generate again. Only this new batch should carry
"prefix": "51".
The tool keeps an immutable snapshot. Copy all returns one number per line; JSON includes number, prefix and length; CSV exports the same fields. The first two are strings. The quantity and total length are validated rather than silently clamped.
export function prefixBatch(input, length, count, rng = cryptoRandom) {
const { status, digits: prefix } = normalizeInput(input);
if (status !== 'ok' || prefix.length > 8) throw new TypeError('Use 1–8 ASCII prefix digits; spaces and hyphens are allowed, other characters are not');
integer(length, 12, 19, 'Total length');
integer(count, 1, 25, 'Quantity');
const records = Array.from({ length: count }, () => {
const payload = prefix + chars(rng, DIGITS, length - prefix.length - 1);
return Object.freeze({ number: payload + luhnCheckDigit(payload), prefix, length });
});
// Export these records. Do not reread edited controls to describe an older batch.
return Object.freeze({ prefix, length, records: Object.freeze(records) });
}This excerpt comes directly from the module used above. Its helper functions and imports are included in the downloadable project below.
Test six- and eight-digit routing together
The following prefixes are invented keys in a local mock, not bank assignments. The longer matching rule must win even when the six-digit rule appears first in the table.
| Prefix supplied to the generator | Expected local route | Failure exposed |
|---|---|---|
41234567 | checkout-a-mock | Six-digit rule swallows a more specific route |
41234568 | checkout-b-mock | Two eight-digit routes collapse into one |
41234599 | legacy-mock | Six-digit fallback is lost |
49999999 | default-mock | Unknown input has no explicit fallback |
// These are invented local routing rules, not issuer or acquirer assignments.
const routes = [
{ prefix: '412345', target: 'legacy-mock' },
{ prefix: '41234567', target: 'checkout-a-mock' },
{ prefix: '41234568', target: 'checkout-b-mock' }
];
export function fixtureRoute(number) {
return routes.filter(row => number.startsWith(row.prefix))
.sort((a, b) => b.prefix.length - a.prefix.length)[0]?.target ?? 'default-mock';
}The tests generate a Luhn-valid number for each prefix before calling this function. This checks the route without making a processor request. For prefix candidates and incomplete input states, use the prefix analyser.
Input and serialization cases
| Input or action | Expected result |
|---|---|
00-1234, length 19 | Prefix becomes the string 001234; output stays 19 digits |
51x | Visible error; the letter is not quietly discarded |
Paste 51 followed by a line break | Visible error; the textarea preserves the bad character |
| Nine prefix digits | Error; no silent truncation to eight |
| Quantity 0 or 26 | Error; supported batch size is 1–25 |
| Edit settings after generation | Existing output keeps its original metadata |
CSV quoting preserves file text but does not control spreadsheet type inference. Import number and prefix columns as text. For application fixtures, JSON avoids that import step. Run the result through the card validator to examine checksum and profile results separately: an arbitrary prefix/length combination need not match a supported network.
Run these cases locally
Download the complete data-tool example project
, extract it and run node --test data-tools.test.mjs with Node.js 22 or later. It includes routing, malformed-input, snapshot and all output-length tests. The fixture management guide explains how to keep the bundle, manifest and expected results together.
Updated 8 November 2026: exports now use the generating snapshot; invalid characters and overlong prefixes are reported explicitly.