CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-credit-card-type

A library for determining credit card type from both complete and partial card numbers

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

Credit Card Type

Credit Card Type provides a utility for determining credit card types from both complete and partial card numbers. Designed for type-as-you-go detection, it supports progressive card type identification as users enter their card information, making it ideal for payment form validation and UI enhancement.

Package Information

  • Package Name: credit-card-type
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install credit-card-type

Core Imports

import creditCardType from "credit-card-type";

For CommonJS:

const creditCardType = require("credit-card-type");

Note: This module uses CommonJS export syntax (export = creditCardType), so TypeScript imports should use the default import syntax shown above.

Basic Usage

import creditCardType from "credit-card-type";

// Detect from partial number (type-as-you-go)
const partialResults = creditCardType("4111");
console.log(partialResults[0].type); // 'visa'
console.log(partialResults[0].niceType); // 'Visa'

// Multiple matches with ambiguous input
const ambiguousResults = creditCardType("6");
console.log(ambiguousResults.length); // 6 (multiple card types start with 6)
console.log(ambiguousResults[0].niceType); // 'Discover'
console.log(ambiguousResults[1].niceType); // 'UnionPay'

// Complete number detection
const completeResults = creditCardType("4111111111111111");
console.log(completeResults[0].gaps); // [4, 8, 12] - formatting positions
console.log(completeResults[0].lengths); // [16, 18, 19] - valid lengths
console.log(completeResults[0].code); // { name: 'CVV', size: 3 }

Capabilities

Card Type Detection

Determines credit card types from partial or complete card numbers.

/**
 * Determines credit card types from a card number string
 * @param cardNumber - Card number string (should be normalized, no spaces/letters)
 * @returns Array of matching card types, empty array if no matches
 */
function creditCardType(cardNumber: string): Array<CreditCardType>;

Card Type Information Retrieval

Gets detailed information about a specific card type.

/**
 * Returns card configuration for a specific card type
 * @param cardType - Card type identifier (e.g., 'visa', 'mastercard')
 * @returns Card configuration object or undefined if invalid type
 */
function getTypeInfo(cardType: string): CreditCardType | undefined;

Card Type Management

Add, remove, update, and reorder card types for custom detection scenarios.

/**
 * Adds a custom card type to the detection system
 * @param config - Card configuration object
 */
function addCard(config: CreditCardType): void;

/**
 * Removes a card type from detection
 * @param name - Card type name to remove
 */
function removeCard(name: string): void;

/**
 * Updates an existing card type configuration
 * @param cardType - Card type identifier to update
 * @param updates - Partial configuration object with updates
 */
function updateCard(cardType: string, updates: Partial<CreditCardType>): void;

/**
 * Changes the priority order for card type detection
 * @param name - Card type name to reorder
 * @param position - New position in the detection order (0-based)
 */
function changeOrder(name: string, position: number): void;

/**
 * Resets all custom modifications to original state
 */
function resetModifications(): void;

Card Type Constants

Named constants for all supported card types.

const types: {
  VISA: "visa";
  MASTERCARD: "mastercard";
  AMERICAN_EXPRESS: "american-express";
  DINERS_CLUB: "diners-club";
  DISCOVER: "discover";
  JCB: "jcb";
  UNIONPAY: "unionpay";
  VERVE: "verve";
  MAESTRO: "maestro";
  ELO: "elo";
  MIR: "mir";
  HIPER: "hiper";
  HIPERCARD: "hipercard";
};

Types

interface CreditCardType {
  /** Human-readable card brand name (e.g., "Visa", "Mastercard") */
  niceType: string;
  /** Code-friendly card type identifier (e.g., "visa", "mastercard") */
  type: string;
  /** Number patterns for matching: single numbers, ranges [min, max], or mixed arrays */
  patterns: (number | [number, number])[];
  /** Expected gap positions for formatting (e.g., [4, 8, 12] for "1234 5678 9012 3456") */
  gaps: number[];
  /** Valid card number lengths (excluding spaces) */
  lengths: number[];
  /** Security code information */
  code: {
    /** Security code name (e.g., "CVV", "CVC", "CID") */
    name: string;
    /** Security code length (3 or 4 digits) */
    size: number;
  };
  /** Pattern matching strength (set internally during detection) */
  matchStrength?: number;
}

type CreditCardTypeCardBrandId =
  | "american-express"
  | "diners-club"
  | "discover"
  | "elo"
  | "hiper"
  | "hipercard"
  | "jcb"
  | "verve"
  | "maestro"
  | "mastercard"
  | "mir"
  | "unionpay"
  | "visa";

Supported Card Types

The library includes built-in support for 13 major credit card brands:

BrandType IDNice TypePatternsSecurity CodeLengths
VisavisaVisa4CVV (3)16, 18, 19
MastercardmastercardMastercard51-55, 2221-2720CVC (3)16
American Expressamerican-expressAmerican Express34, 37CID (4)15
Diners Clubdiners-clubDiners Club300-305, 36, 38, 39CVV (3)14, 16, 19
DiscoverdiscoverDiscover6011, 644-649, 65CID (3)16, 19
JCBjcbJCB2131, 1800, 3528-3589CVV (3)16, 17, 18, 19
UnionPayunionpayUnionPay620, 62100-626999, etc.CVN (3)14-19
MaestromaestroMaestroVarious patternsCVC (3)12-19
EloeloEloVarious specific patternsCVE (3)16
MirmirMir2200-2204CVP2 (3)16-19
HiperhiperHiperSpecific patternsCVC (3)16
HipercardhipercardHipercard606282CVC (3)16
VerveverveVerveVarious patternsCVV (3)16, 18, 19

Pattern Matching

Card detection uses sophisticated pattern matching:

  • Number patterns: Match exact prefixes (e.g., 4 matches all Visa cards)
  • Range patterns: Match number ranges (e.g., [51, 55] matches Mastercard 51xxxx-55xxxx)
  • Partial matching: Works with incomplete numbers for real-time detection
  • Best match selection: When multiple patterns match, the most specific pattern wins

Advanced Usage Examples

Custom Card Types

import creditCardType from "credit-card-type";

// Add a custom card type
creditCardType.addCard({
  niceType: "Custom Bank Card",
  type: "custom-card",
  patterns: [9999, [8888, 8889]],
  gaps: [4, 8, 12],
  lengths: [16],
  code: {
    name: "CVV",
    size: 3,
  },
});

// Update existing card type
creditCardType.updateCard(creditCardType.types.VISA, {
  niceType: "Visa Custom",
  lengths: [13, 16, 19], // Add support for old 13-digit Visas
});

// Change detection priority
creditCardType.changeOrder("custom-card", 0); // Highest priority

// Reset all modifications
creditCardType.resetModifications();

Card Number Formatting

import creditCardType from "credit-card-type";

function formatCardNumber(cardNumber: string, cardType?: string): string {
  if (!cardType) {
    const results = creditCardType(cardNumber);
    if (results.length === 0) return cardNumber;
    cardType = results[0].type;
  }

  const typeInfo = creditCardType.getTypeInfo(cardType);
  if (!typeInfo) return cardNumber;

  const { gaps } = typeInfo;
  const offsets = [0, ...gaps, cardNumber.length];
  const components = [];

  for (let i = 0; offsets[i] < cardNumber.length; i++) {
    const start = offsets[i];
    const end = Math.min(offsets[i + 1], cardNumber.length);
    components.push(cardNumber.substring(start, end));
  }

  return components.join(" ");
}

// Usage
formatCardNumber("4111111111111111", "visa"); // "4111 1111 1111 1111"
formatCardNumber("378282246310005", "american-express"); // "3782 822463 10005"

Filtering Results

import creditCardType from "credit-card-type";

// Only allow specific card types
const allowedTypes = [creditCardType.types.VISA, creditCardType.types.MASTERCARD];

function getAcceptedCardTypes(cardNumber: string) {
  return creditCardType(cardNumber).filter((card) =>
    allowedTypes.includes(card.type as any)
  );
}

Install with Tessl CLI

npx tessl i tessl/npm-credit-card-type
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/credit-card-type@10.1.x
Publish Source
CLI
Badge
tessl/npm-credit-card-type badge