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 🙏

© 2024 – Pkg Stats / Ryan Hefner

credit-card-type-uz

v1.0.0

Published

A library for determining credit card type

Downloads

3

Readme

Credit Card Type Build Status npm version Bower

Credit Card Type provides a useful utility method for determining a credit card type from both fully qualified and partial numbers. This is not a validation library but rather a smaller component to help you build your own validation or UI library.

This library is designed for type-as-you-go detection (supports partial numbers) and is written in CommonJS so you can use it in Node, io.js, and the browser.

Download

To install via npm:

npm install credit-card-type

To install via Bower:

bower install credit-card-type

Example

var creditCardType = require('credit-card-type');

var visaCards = creditCardType('4111');
console.log(visaCards[0].type);  // 'visa'

var ambiguousCards = creditCardType('6');
console.log(ambiguousCards.length);       // 3
console.log(ambiguousCards[0].niceType);  // 'Discover'
console.log(ambiguousCards[1].niceType);  // 'UnionPay'
console.log(ambiguousCards[2].niceType);  // 'Maestro'

API

creditCardType(number: String)

creditCardType will return an array of objects, each with the following data:

| Key | Type | Description | |------------|----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | niceType | String | A pretty printed representation of the card brand.- Visa- MasterCard- American Express- Diners Club- Discover- JCB- UnionPay- Maestro | | type | String | A code-friendly presentation of the card brand (useful to class names in CSS). Please refer to Card Type "Constants" below for the list of possible values.- visa- master-card- american-express- diners-club- discover- jcb- unionpay- maestro | | gaps | Array | The expected indeces of gaps in a string representation of the card number. For example, in a Visa card, 4111 1111 1111 1111, there are expected spaces in the 4th, 8th, and 12th positions. This is useful in setting your own formatting rules. | | lengths | Array | The expected lengths of the card number as an array of strings (excluding spaces and / characters). | | code | Object | The information regarding the security code for the determined card. Learn more about the code object below. |

If no card types are found, this returns an empty array.

creditCardType.getTypeInfo(type: String)

getTypeInfo will return a singular object (with the same structure as creditCardType) corresponding with the specified type, or undefined if the specified type is invalid/unknown.

Card Type "Constants"

Named variables are provided for each of the supported card types:

  • VISA
  • MASTERCARD
  • AMERICAN_EXPRESS
  • DINERS_CLUB
  • DISCOVER
  • JCB
  • UNIONPAY
  • MAESTRO

code

Card brands provide different nomenclature for their security codes as well as varying lengths.

| Brand | Name | Size | |--------------------|-------|------| | Visa | CVV | 3 | | MasterCard | CVC | 3 | | American Express | CID | 4 | | Diners Club | CVV | 3 | | Discover | CID | 3 | | JCB | CVV | 3 | | UnionPay | CVN | 3 | | Maestro | CVC | 3 |

A full response for a Visa card will look like this:

{
  niceType: 'Visa',
  type: 'visa',
  gaps: [ 4, 8, 12 ],
  lengths: [16],
  code: { name: 'CVV', size: 3 }
}

Note: The response also includes an exactPattern regex and a prefixPattern regex. These are used internally to determine the card type, but they should not be relied on and may be removed in the next major version.

Advanced Usage

CommonJS:

var creditCardType = require('credit-card-type');
var getTypeInfo = require('credit-card-type').getTypeInfo;
var CardType = require('credit-card-type').types;

ES6:

import creditCardType, { getTypeInfo, types as CardType } from 'credit-card-type';

Filtering

creditCardType(cardNumber).filter(function(card) {
  return card.type == CardType.MASTERCARD || card.type == CardType.VISA;
});

Pretty Card Numbers

function prettyCardNumber(cardNumber, cardType) {
  var card = getTypeInfo(cardType);

  if (card) {
    var offsets = [0].concat(card.gaps).concat([cardNumber.length]);
    var components = [];

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

    return components.join(' ');
  }

  return cardNumber;
}

prettyCardNumber('xxxxxxxxxx343', CardType.AMERICAN_EXPRESS); // 'xxxx xxxxxx 343'

Development

We use nvm for managing our node versions, but you do not have to. Replace any nvm references with the tool of your choice below.

nvm install
npm install

All testing dependencies will be installed upon npm install and the test suite executed with npm test.