Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

credit-card-type

Package Overview
Dependencies
Maintainers
1
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

credit-card-type - npm Package Compare versions

Comparing version 4.0.3 to 4.1.0

5

CHANGELOG.md

@@ -0,1 +1,6 @@

4.1.0
=====
- Add `getTypeInfo` and `types` exports for getting type information such as number spacing given a type string such as `visa`.
4.0.3

@@ -2,0 +7,0 @@ =====

242

index.js
'use strict';
var types = [
{
niceType: 'Visa',
type: 'visa',
pattern: '^4\\d*$',
gaps: [4, 8, 12],
lengths: [16],
code: {
name: 'CVV',
size: 3
}
},
{
niceType: 'MasterCard',
type: 'master-card',
pattern: '^(5|5[1-5]\\d*|2|22|222|222[1-9]\\d*|2[3-6]\\d*|27[0-1]\\d*|2720\\d*)$',
gaps: [4, 8, 12],
lengths: [16],
code: {
name: 'CVC',
size: 3
}
},
{
niceType: 'American Express',
type: 'american-express',
pattern: '^3([47]\\d*)?$',
isAmex: true,
gaps: [4, 10],
lengths: [15],
code: {
name: 'CID',
size: 4
}
},
{
niceType: 'Diners Club',
type: 'diners-club',
pattern: '^3((0([0-5]\\d*)?)|[689]\\d*)?$',
gaps: [4, 10],
lengths: [14],
code: {
name: 'CVV',
size: 3
}
},
{
niceType: 'Discover',
type: 'discover',
pattern: '^6(0|01|011\\d*|5\\d*|4|4[4-9]\\d*)?$',
gaps: [4, 8, 12],
lengths: [16, 19],
code: {
name: 'CID',
size: 3
}
},
{
niceType: 'JCB',
type: 'jcb',
pattern: '^((2|21|213|2131\\d*)|(1|18|180|1800\\d*)|(3|35\\d*))$',
gaps: [4, 8, 12],
lengths: [16],
code: {
name: 'CVV',
size: 3
}
},
{
niceType: 'UnionPay',
type: 'unionpay',
pattern: '^6(2\\d*)?$',
gaps: [4, 8, 12],
lengths: [16, 17, 18, 19],
code: {
name: 'CVN',
size: 3
}
},
{
niceType: 'Maestro',
type: 'maestro',
pattern: '^((5((0|[6-9])\\d*)?)|(6|6[37]\\d*))$',
gaps: [4, 8, 12],
lengths: [12, 13, 14, 15, 16, 17, 18, 19],
code: {
name: 'CVC',
size: 3
}
var types = {};
var VISA = 'visa';
var MASTERCARD = 'master-card';
var AMERICAN_EXPRESS = 'american-express';
var DINERS_CLUB = 'diners-club';
var DISCOVER = 'discover';
var JCB = 'jcb';
var UNIONPAY = 'unionpay';
var MAESTRO = 'maestro';
var CVV = 'CVV';
var CID = 'CID';
var CVC = 'CVC';
var CVN = 'CVN';
function clone(x) {
var pattern, dupe;
if (!x) { return null; }
pattern = x.pattern.source;
dupe = JSON.parse(JSON.stringify(x));
dupe.pattern = pattern;
return dupe;
}
types[VISA] = {
niceType: 'Visa',
type: VISA,
pattern: /^4\d*$/,
gaps: [4, 8, 12],
lengths: [16],
code: {
name: CVV,
size: 3
}
];
};
module.exports = function getCardTypes(cardNumber) {
var i, value;
types[MASTERCARD] = {
niceType: 'MasterCard',
type: MASTERCARD,
pattern: /^(5|5[1-5]\d*|2|22|222|222[1-9]\d*|2[3-6]\d*|27[0-1]\d*|2720\d*)$/,
gaps: [4, 8, 12],
lengths: [16],
code: {
name: CVC,
size: 3
}
};
types[AMERICAN_EXPRESS] = {
niceType: 'American Express',
type: AMERICAN_EXPRESS,
pattern: /^3([47]\d*)?$/,
isAmex: true,
gaps: [4, 10],
lengths: [15],
code: {
name: CID,
size: 4
}
};
types[DINERS_CLUB] = {
niceType: 'Diners Club',
type: DINERS_CLUB,
pattern: /^3((0([0-5]\d*)?)|[689]\d*)?$/,
gaps: [4, 10],
lengths: [14],
code: {
name: CVV,
size: 3
}
};
types[DISCOVER] = {
niceType: 'Discover',
type: DISCOVER,
pattern: /^6(0|01|011\d*|5\d*|4|4[4-9]\d*)?$/,
gaps: [4, 8, 12],
lengths: [16, 19],
code: {
name: CID,
size: 3
}
};
types[JCB] = {
niceType: 'JCB',
type: JCB,
pattern: /^((2|21|213|2131\d*)|(1|18|180|1800\d*)|(3|35\d*))$/,
gaps: [4, 8, 12],
lengths: [16],
code: {
name: CVV,
size: 3
}
};
types[UNIONPAY] = {
niceType: 'UnionPay',
type: UNIONPAY,
pattern: /^6(2\d*)?$/,
gaps: [4, 8, 12],
lengths: [16, 17, 18, 19],
code: {
name: CVN,
size: 3
}
};
types[MAESTRO] = {
niceType: 'Maestro',
type: MAESTRO,
pattern: /^((5((0|[6-9])\d*)?)|(6|6[37]\d*))$/,
gaps: [4, 8, 12],
lengths: [12, 13, 14, 15, 16, 17, 18, 19],
code: {
name: CVC,
size: 3
}
};
function creditCardType(cardNumber) {
var type, value;
var result = [];

@@ -103,8 +134,8 @@

if (cardNumber === '') { return clone(types); }
for (type in types) {
if (!types.hasOwnProperty(type)) { continue; }
for (i = 0; i < types.length; i++) {
value = types[i];
value = types[type];
if (RegExp(value.pattern).test(cardNumber)) {
if (cardNumber.length === 0 || value.pattern.test(cardNumber)) {
result.push(clone(value));

@@ -115,6 +146,19 @@ }

return result;
}
creditCardType.getTypeInfo = function (type) {
return clone(types[type]);
};
function clone(x) {
return JSON.parse(JSON.stringify(x));
}
creditCardType.types = {
VISA: VISA,
MASTERCARD: MASTERCARD,
AMERICAN_EXPRESS: AMERICAN_EXPRESS,
DINERS_CLUB: DINERS_CLUB,
DISCOVER: DISCOVER,
JCB: JCB,
UNIONPAY: UNIONPAY,
MAESTRO: MAESTRO
};
module.exports = creditCardType;
{
"name": "credit-card-type",
"version": "4.0.3",
"version": "4.1.0",
"description": "A library for determining credit card type",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -24,8 +24,8 @@ Credit Card Type [![Build Status](https://travis-ci.org/braintree/credit-card-type.svg)](https://travis-ci.org/braintree/credit-card-type) [![npm version](https://badge.fury.io/js/credit-card-type.svg)](http://badge.fury.io/js/credit-card-type) [![Bower](https://badge.fury.io/bo/credit-card-type.svg)](http://badge.fury.io/bo/credit-card-type)

```javascript
var getCardTypes = require('credit-card-type');
var creditCardType = require('credit-card-type');
var visaCards = getCardTypes('4111');
var visaCards = creditCardType('4111');
console.log(visaCards[0].type); // 'visa'
var ambiguousCards = getCardTypes('6');
var ambiguousCards = creditCardType('6');
console.log(ambiguousCards.length); // 3

@@ -39,5 +39,5 @@ console.log(ambiguousCards[0].niceType); // 'Discover'

### `getCardTypes(number: String)`
### `creditCardType(number: String)`
`getCardTypes` will return an array of objects, each with the following data:
`creditCardType` will return an array of objects, each with the following data:

@@ -47,3 +47,3 @@ | Key | Type | Description |

| `niceType` | `String` | A pretty printed representation of the card brand.<br/>- `Visa`<br />- `MasterCard`<br />- `American Express`<br />- `Diners Club`<br />- `Discover`<br />- `JCB`<br />- `UnionPay`<br />- `Maestro` |
| `type` | `String` | A code-friendly presentation of the card brand (useful to class names in CSS).<br/>- `visa`<br />- `master-card`<br />- `american-express`<br />- `diners-club`<br />- `discover`<br />- `jcb`<br />- `unionpay`<br />- `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.<br/>- `visa`<br />- `master-card`<br />- `american-express`<br />- `diners-club`<br />- `discover`<br />- `jcb`<br />- `unionpay`<br />- `maestro` |
| `pattern` | `RegExp` | The regular expression used to determine the card type. |

@@ -56,2 +56,19 @@ | `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. |

### `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`

@@ -85,2 +102,51 @@

### 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

@@ -87,0 +153,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc