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:

    1. Generate five 16-digit values with prefix 4.
    2. Change the input to 51 without generating again.
    3. Select Show JSON. Every record must still contain "prefix": "4" and its original number.
    4. 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 generatorExpected local routeFailure exposed
    41234567checkout-a-mockSix-digit rule swallows a more specific route
    41234568checkout-b-mockTwo eight-digit routes collapse into one
    41234599legacy-mockSix-digit fallback is lost
    49999999default-mockUnknown 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 actionExpected result
    00-1234, length 19Prefix becomes the string 001234; output stays 19 digits
    51xVisible error; the letter is not quietly discarded
    Paste 51 followed by a line breakVisible error; the textarea preserves the bad character
    Nine prefix digitsError; no silent truncation to eight
    Quantity 0 or 26Error; supported batch size is 1–25
    Edit settings after generationExisting 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.

    Frequently Asked Questions

    Does the tool identify an issuing bank?
    No. It completes a supplied prefix. Network format, issuer allocation and account status are different questions; no lookup is performed.
    Why does my exported prefix differ from the input box?
    Changing a control does not change an existing batch. The output retains its generating prefix and length until you press Generate again.
    Which separators can I enter?
    ASCII spaces, nonbreaking spaces and hyphens are removed. Letters, line breaks and non-ASCII digits are rejected. After normalization the prefix must contain one to eight digits.
    Are arbitrary output lengths supported by every network?
    No. The 12–19 range is this tool’s testing envelope. Use network-specific profiles when a test requires a recognized brand and supported length.