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

bp-creditcards

v1.3.0

Published

Utility methods for formatting and validating credit cards

Downloads

11

Readme

creditcards Build Status Code Climate Test Coverage NPM version

Utility methods for formatting and validating credit cards. With a minimal footprint and a flexible API, it's suitable for both Node and the browser.

Install

# npm
$ npm install creditcards
# bower
$ bower install creditcards

API

validate(card)

  • Arguments:
    • card (object)
      • number (string)
      • expirationMonth (number)
      • expirationYear (number)
      • cvc (string)
  • Returns:
    • object
      • card
        • type (string) - the type of the provided card
        • number (string)
        • expirationMonth (number)
        • expirationYear (number)
        • cvc (string)
      • validCardNumber (boolean)
      • validExpirationMonth (boolean)
      • validExpirationYear (boolean)
      • validCvc (boolean)
      • expired (boolean) - whether the expiration date has passed

card

card.parse(number) -> String

Removes all non-numeric characters from a card number, including punctuation and spacing. If a non-string is provided, it returns an empty string.


card.type(number) -> String

Returns the matched card type, or undefined if there was no match. Valid card types are:

  • Visa
  • MasterCard
  • American Express
  • Diners Club
  • Discover
  • JCB

card.luhn(number) -> Boolean

Checks the card number's validity using the Luhn algorithm.

card.isValid(number [, type]) -> Boolean

Validates the number using card.luhn and checks that it matches any type (or a specific type if provided).

cvc

isValid(cvc [, type]) -> Boolean

Checks whether a card verification code is a valid 3-4 digit numeric string. If a type is provided, the length will be validated for the card type (4 for American Express, 3 for others).

expiration

isPast(month, year) -> Boolean

Checks whether a given month and year pair (both Number) are in the past.


expiration.month

expiration.month.parse(month) -> Number

Casts the provided month value to a Number. All of the following will be 5 after parsing:

  • 5
  • '05'
  • '5'

Returns undefined for non-numeric values.


expiration.month.isValid(month) -> Boolean

Checks whether the provided month (Number) is valid (between 1 and 12).


expiration.year

expiration.year.parse(year [, pad]) -> Number

Casts the provided year value to a Number. If pad is true, year is assumed to be a two digit number or numeric string. All of the following are equivalent:

  • expiration.year.parse(2014)
  • expiration.year.parse('2014')
  • expiration.year.parse('14', true)
  • expiration.year.parse(14, true)

Returns undefined for non-numeric values.


expiration.year.format(year [, strip]) -> Number

Casts the provided year (Number) to a String. If strip is true, year is assumed to be a four digit number and will be converted to a two digit number.

  • expiration.year.format(2014) === '2014'
  • expiration.year.format(2014, true) === '14'

expiration.year.isValid(year) -> Boolean

Checks whether the provided year (Number) is valid (> 0).


expiration.year.isPast(year) -> Boolean

Checks whether a given year (Number) is in the past.

Why Another Library?

There are lots of other useful credit card validation and parsing modules. creditcards specifically takes inspiration from credit-card, but there are many others, including the popular jQuery.payment. creditcards was specifically designed for browser use for handling payment forms. That means:

  1. Browserified, it's only a few kilobytes, even before minification.
  2. It provides an API for parsing user inputs.
  3. It has no DOM-related code. You can use it in Node and in the browser you get full control over how your card inputs are handled.
  4. The card type is optional.

Clients

  • angular-credit-cards: A set of Angular directives for building credit card forms powered by creditcards

License

MIT