npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

what-credit-card

v1.1.0

Published

Validate credit card numbers using Luhn algorithm and detect card type (Visa, MasterCard, Amex, etc.)

Readme

what-credit-card

npm npm downloads

Validate credit card numbers using the Luhn algorithm and detect card type (Visa, MasterCard, Amex, etc.)

Features

  • ✅ Validates credit card numbers using the Luhn algorithm
  • 🏷️ Detects card type (Visa, MasterCard, American Express, Discover, JCB, Diners Club)
  • 🛡️ Input sanitization (removes all non-digit characters)
  • ❌ Comprehensive error handling
  • 📝 TypeScript definitions included
  • 🚀 Zero dependencies

Install

npm install what-credit-card

Usage

Basic Usage (Legacy API)

const whatCreditCard = require('what-credit-card');

console.log(whatCreditCard('4111111111111111')); // 'visa'
console.log(whatCreditCard('5555555555554444')); // 'mastercard'
console.log(whatCreditCard('1234567890123456')); // 'invalid'

Advanced Usage (New API)

const { validate, isValid, getType } = require('what-credit-card');

// Handles various formatting automatically
const result = validate('4111-1111.1111 1111');
console.log(result);
// {
//   isValid: true,
//   type: 'visa',
//   cleanNumber: '4111111111111111'
// }

// Just check if valid
console.log(isValid('4111 1111 1111 1111')); // true

// Just get card type  
console.log(getType('4111.1111.1111.1111')); // 'visa'

Error Handling

const { validate } = require('what-credit-card');

const result = validate('invalid-input');
console.log(result);
// {
//   isValid: false,
//   type: null,
//   cleanNumber: null,
//   error: 'Card number cannot be empty after removing formatting'
// }

Supported Card Types

  • Visa: Cards starting with 4
  • MasterCard: Cards starting with 5
  • American Express: Cards starting with 34 or 37
  • Discover: Cards starting with 6011 or 65
  • JCB: Cards starting with 2131, 1800, or 35
  • Diners Club: Cards starting with 30, 36, or 38
  • Maestro: Cards starting with 50, 56-58, 6304, 6390, or 67
  • UnionPay: Cards starting with 62
  • Mir: Russian cards starting with 2200-2204
  • Elo: Brazilian cards (various prefixes)
  • Hipercard: Brazilian cards starting with 606282 or 3841
  • Troy: Turkish cards starting with 9792

API

whatCreditCard(cardNumber) (Default Export)

Returns the card type as a string or 'invalid' if the card number is invalid.

Parameters:

  • cardNumber (string|number): The credit card number

Returns:

  • string: Card type ('visa', 'mastercard', 'amex', 'discover', 'jcb', 'dinersclub') or 'invalid'

validate(cardNumber)

Performs full validation and returns detailed information.

Parameters:

  • cardNumber (string|number): The credit card number

Returns:

  • object: Validation result with isValid, type, cleanNumber, and optional error

isValid(cardNumber)

Checks if the card number is valid using the Luhn algorithm.

Parameters:

  • cardNumber (string|number): The credit card number

Returns:

  • boolean: True if valid, false otherwise

getType(cardNumber)

Returns the card type without validation.

Parameters:

  • cardNumber (string|number): The credit card number

Returns:

  • string|null: Card type or null if unknown

CARD_TYPES

Array of supported card types.

Returns:

  • string[]: ['amex', 'dinersclub', 'jcb', 'visa', 'mastercard', 'maestro', 'discover', 'hipercard', 'elo', 'mir', 'troy', 'unionpay']

formatCardNumber(cardNumber)

Formats a card number with appropriate spacing based on card type.

Parameters:

  • cardNumber (string|number): The credit card number

Returns:

  • string: Formatted card number with spaces

Example:

formatCardNumber('4532015112830366'); // Returns: '4532 0151 1283 0366'
formatCardNumber('378282246310005');  // Returns: '3782 822463 10005' (Amex format)

maskCardNumber(cardNumber, options?)

Masks a card number for secure display.

Parameters:

  • cardNumber (string|number): The credit card number
  • options (object, optional):
    • maskChar (string): Character to use for masking (default: '*')
    • showFirst (number): Number of first digits to show (default: 4)
    • showLast (number): Number of last digits to show (default: 4)

Returns:

  • string: Masked and formatted card number

Example:

maskCardNumber('4532015112830366');                           // Returns: '4532 **** **** 0366'
maskCardNumber('4532015112830366', { maskChar: 'X' });       // Returns: '4532 XXXX XXXX 0366'
maskCardNumber('4532015112830366', { showFirst: 6, showLast: 2 }); // Returns: '4532 01** **** **66'

clearCache()

Clears the internal memoization cache for performance optimization.

Returns:

  • void

Example:

clearCache(); // Clears all cached validation results

TypeScript

This package includes TypeScript definitions:

import whatCreditCard, { validate, ValidationResult } from 'what-credit-card';

const result: ValidationResult = validate('4111111111111111');
const cardType: string = whatCreditCard('4111111111111111');

License

MIT © Chirag Chauhan