credit-card-type
Advanced tools
Comparing version 3.0.0 to 4.0.0
{ | ||
"name": "credit-card-type", | ||
"main": "index.js", | ||
"version": "3.0.0", | ||
"version": "4.0.0", | ||
"homepage": "https://github.com/braintree/credit-card-type", | ||
@@ -6,0 +6,0 @@ "authors": [ |
@@ -0,1 +1,6 @@ | ||
4.0.0 | ||
===== | ||
- Further resolve ambiguity issues with various cards; return an array of potential card types instead of a single type | ||
3.0.0 | ||
@@ -2,0 +7,0 @@ ===== |
34
index.js
@@ -19,3 +19,3 @@ var isString = require('lodash.isstring'); | ||
type: 'master-card', | ||
pattern: '^5[1-5]\\d*$', | ||
pattern: '^5([1-5]\\d*)?$', | ||
gaps: [4, 8, 12], | ||
@@ -31,3 +31,3 @@ lengths: [16], | ||
type: 'american-express', | ||
pattern: '^3[47]\\d*$', | ||
pattern: '^3([47]\\d*)?$', | ||
isAmex: true, | ||
@@ -44,3 +44,3 @@ gaps: [4, 10], | ||
type: 'diners-club', | ||
pattern: '^3(0[0-5]|[689])\\d*$', | ||
pattern: '^3((0([0-5]\\d*)?)|[689]\\d*)?$', | ||
gaps: [4, 10], | ||
@@ -56,3 +56,3 @@ lengths: [14], | ||
type: 'discover', | ||
pattern: '^6(011|5|4[4-9])\\d*$', | ||
pattern: '^6(0|01|011\\d*|5\\d*|4|4[4-9]\\d*)?$', | ||
gaps: [4, 8, 12], | ||
@@ -68,3 +68,3 @@ lengths: [16], | ||
type: 'jcb', | ||
pattern: '^(2131|1800|35)\\d*$', | ||
pattern: '^((2|21|213|2131\\d*)|(1|18|180|1800\\d*)|(3|35\\d*))$', | ||
gaps: [4, 8, 12], | ||
@@ -80,3 +80,3 @@ lengths: [16], | ||
type: 'unionpay', | ||
pattern: '^62\\d*$', | ||
pattern: '^6(2\\d*)?$', | ||
gaps: [4, 8, 12], | ||
@@ -92,3 +92,3 @@ lengths: [16, 17, 18, 19], | ||
type: 'maestro', | ||
pattern: '^(50|5[6-9]|60\\d{2}|61|63|64\\d|6[6-9])\\d*$', | ||
pattern: '^((5((0|[6-9])\\d*)?)|(6|6[37]\\d*))$', | ||
gaps: [4, 8, 12], | ||
@@ -103,17 +103,19 @@ lengths: [12, 13, 14, 15, 16, 17, 18, 19], | ||
module.exports = function getCardType(cardNumber) { | ||
var key, value; | ||
var noMatch = {}; | ||
module.exports = function getCardTypes(cardNumber) { | ||
var i, value; | ||
var result = []; | ||
if (!isString(cardNumber)) { return noMatch; } | ||
if (!isString(cardNumber)) { return result; } | ||
for (key in types) { | ||
if (!types.hasOwnProperty(key)) { continue; } | ||
if (cardNumber === '') { return clone(types); } | ||
value = types[key]; | ||
for (i = 0; i < types.length; i++) { | ||
value = types[i]; | ||
if (RegExp(value.pattern).test(cardNumber)) { return clone(value); } | ||
if (RegExp(value.pattern).test(cardNumber)) { | ||
result.push(clone(value)); | ||
} | ||
} | ||
return noMatch; | ||
return result; | ||
}; |
{ | ||
"name": "credit-card-type", | ||
"version": "3.0.0", | ||
"version": "4.0.0", | ||
"description": "A library for determining credit card type", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -11,7 +11,12 @@ 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) | ||
```javascript | ||
var getCardType = require('credit-card-type'); | ||
var getCardTypes = require('credit-card-type'); | ||
var card = getCardType('4111'); | ||
var visaCards = getCardTypes('4111'); | ||
console.log(visaCards[0].type); // 'visa' | ||
console.log(card.type); // 'visa' | ||
var ambiguousCards = getCardTypes('6'); | ||
console.log(ambiguousCards.length); // 3 | ||
console.log(ambiguousCards[0].niceType); // 'Discover' | ||
console.log(ambiguousCards[1].niceType); // 'UnionPay' | ||
console.log(ambiguousCards[2].niceType); // 'Maestro' | ||
``` | ||
@@ -21,5 +26,5 @@ | ||
### `getCardType(number: String)` | ||
### `getCardTypes(number: String)` | ||
`getCardType` will return an Object with the following data: | ||
`getCardTypes` will return an array of objects, each with the following data: | ||
@@ -35,2 +40,4 @@ | Key | Type | Description | | ||
If no card types are found, this returns an empty array. | ||
#### `code` | ||
@@ -37,0 +44,0 @@ |
@@ -5,14 +5,13 @@ var expect = require('chai').expect; | ||
describe('getCardType', function () { | ||
it('returns an empty object if passed nothing', function () { | ||
expect(getCardType()).to.deep.equal({}); | ||
it('returns an empty array if passed non-strings', function () { | ||
expect(getCardType()).to.deep.equal([]); | ||
expect(getCardType(null)).to.deep.equal([]); | ||
expect(getCardType(true)).to.deep.equal([]); | ||
expect(getCardType(false)).to.deep.equal([]); | ||
expect(getCardType('ren hoek')).to.deep.equal([]); | ||
expect(getCardType(3920342)).to.deep.equal([]); | ||
expect(getCardType([])).to.deep.equal([]); | ||
expect(getCardType({})).to.deep.equal([]); | ||
}); | ||
it('returns an empty object if passed garbage', function () { | ||
expect(getCardType('ren hoek')).to.deep.equal({}); | ||
expect(getCardType(3920342)).to.deep.equal({}); | ||
expect(getCardType([])).to.deep.equal({}); | ||
expect(getCardType({})).to.deep.equal({}); | ||
}); | ||
describe('matches card numbers to brand', function () { | ||
@@ -38,2 +37,3 @@ var tests = [ | ||
['37', 'american-express'], | ||
['341', 'american-express'], | ||
['34343434343434', 'american-express'], | ||
@@ -44,3 +44,7 @@ ['378282246310005', 'american-express'], | ||
['30', 'diners-club'], | ||
['300', 'diners-club'], | ||
['36', 'diners-club'], | ||
['38', 'diners-club'], | ||
['39', 'diners-club'], | ||
['30569309025904', 'diners-club'], | ||
@@ -51,3 +55,7 @@ ['38520000023237', 'diners-club'], | ||
['60', 'discover'], | ||
['601', 'discover'], | ||
['6011', 'discover'], | ||
['64', 'discover'], | ||
['644', 'discover'], | ||
['65', 'discover'], | ||
@@ -74,13 +82,9 @@ ['644', 'discover'], | ||
['59', 'maestro'], | ||
['6012', 'maestro'], | ||
['6019', 'maestro'], | ||
['61', 'maestro'], | ||
['63', 'maestro'], | ||
['66', 'maestro'], | ||
['67', 'maestro'], | ||
['68', 'maestro'], | ||
['69', 'maestro'], | ||
['6304000000000000', 'maestro'], | ||
['6799990100000000019', 'maestro'], | ||
['1', 'jcb'], | ||
['2', 'jcb'], | ||
['35', 'jcb'], | ||
@@ -99,6 +103,6 @@ ['2131', 'jcb'], | ||
it('returns type ' + type + ' for ' + number + '', function () { | ||
var expected = {type: type}; | ||
it('returns type ' + type + ' for ' + number, function () { | ||
var actual = getCardType(number); | ||
expect(actual.type).to.equal(expected.type); | ||
expect(actual).to.have.lengthOf(1); | ||
expect(actual[0].type).to.equal(type); | ||
}); | ||
@@ -108,15 +112,63 @@ }); | ||
describe('ambiguous card types', function () { | ||
var ambiguous = [ | ||
['', ['visa', 'master-card', 'american-express', 'diners-club', 'discover', 'jcb', 'unionpay', 'maestro']], | ||
['3', ['american-express', 'diners-club', 'jcb']], | ||
['5', ['master-card', 'maestro']], | ||
['6', ['discover', 'maestro', 'unionpay']] | ||
]; | ||
ambiguous.forEach(function (group) { | ||
var number = group[0]; | ||
var expectedNames = group[1].sort(); | ||
it('returns ' + expectedNames.join(' and ') + ' for ' + number, function () { | ||
var actualNames = getCardType(number).map(function (type) { | ||
return type.type; | ||
}).sort(); | ||
expect(expectedNames).to.deep.equal(actualNames); | ||
}); | ||
}); | ||
}); | ||
describe('unknown card types', function () { | ||
var unknowns = [ | ||
'0', | ||
'1', | ||
'2', | ||
'3', | ||
'30', | ||
'12', | ||
'123', | ||
'181', | ||
'1802', | ||
'22', | ||
'212', | ||
'2132', | ||
'306', | ||
'5', | ||
'6', | ||
'60', | ||
'601', | ||
'64', | ||
'31', | ||
'32', | ||
'33', | ||
'600', | ||
'602', | ||
'603', | ||
'604', | ||
'605', | ||
'606', | ||
'607', | ||
'608', | ||
'609', | ||
'6010', | ||
'6012', | ||
'6013', | ||
'6014', | ||
'6015', | ||
'6016', | ||
'6017', | ||
'6018', | ||
'6019', | ||
'61', | ||
'640', | ||
'641', | ||
'642', | ||
'643', | ||
'66', | ||
'68', | ||
'69', | ||
'7', | ||
@@ -128,4 +180,4 @@ '8', | ||
unknowns.forEach(function (unknown) { | ||
it('returns an empty object for ' + unknown, function () { | ||
expect(getCardType(unknown)).to.deep.equal({}); | ||
it('returns an empty array for ' + unknown, function () { | ||
expect(getCardType(unknown)).to.have.lengthOf(0); | ||
}); | ||
@@ -137,32 +189,40 @@ }); | ||
it('MasterCard', function () { | ||
expect(getCardType('5454545454545454').code.size).to.equal(3); | ||
expect(getCardType('5454545454545454').code.name).to.equal('CVC'); | ||
var code = getCardType('5454545454545454')[0].code; | ||
expect(code.size).to.equal(3); | ||
expect(code.name).to.equal('CVC'); | ||
}); | ||
it('Visa', function () { | ||
expect(getCardType('4111111111111111').code.size).to.equal(3); | ||
expect(getCardType('4111111111111111').code.name).to.equal('CVV'); | ||
var code = getCardType('4111111111111111')[0].code; | ||
expect(code.size).to.equal(3); | ||
expect(code.name).to.equal('CVV'); | ||
}); | ||
it('American Express', function () { | ||
expect(getCardType('378734493671000').code.size).to.equal(4); | ||
expect(getCardType('378734493671000').code.name).to.equal('CID'); | ||
var code = getCardType('378734493671000')[0].code; | ||
expect(code.size).to.equal(4); | ||
expect(code.name).to.equal('CID'); | ||
}); | ||
it('Discover', function () { | ||
expect(getCardType('6011000990139424').code.size).to.equal(3); | ||
expect(getCardType('6011000990139424').code.name).to.equal('CID'); | ||
var code = getCardType('6011000990139424')[0].code; | ||
expect(code.size).to.equal(3); | ||
expect(code.name).to.equal('CID'); | ||
}); | ||
it('JCB', function () { | ||
expect(getCardType('30569309025904').code.size).to.equal(3); | ||
expect(getCardType('30569309025904').code.name).to.equal('CVV'); | ||
var code = getCardType('30569309025904')[0].code; | ||
expect(code.size).to.equal(3); | ||
expect(code.name).to.equal('CVV'); | ||
}); | ||
it('DinersClub', function () { | ||
expect(getCardType('30569309025904').code.size).to.equal(3); | ||
expect(getCardType('30569309025904').code.name).to.equal('CVV'); | ||
var code = getCardType('30569309025904')[0].code; | ||
expect(code.size).to.equal(3); | ||
expect(code.name).to.equal('CVV'); | ||
}); | ||
it('UnionPay', function () { | ||
expect(getCardType('6221558812340000').code.size).to.equal(3); | ||
expect(getCardType('6221558812340000').code.name).to.equal('CVN'); | ||
var code = getCardType('6221558812340000')[0].code; | ||
expect(code.size).to.equal(3); | ||
expect(code.name).to.equal('CVN'); | ||
}); | ||
it('Maestro', function () { | ||
expect(getCardType('6304000000000000').code.size).to.equal(3); | ||
expect(getCardType('6304000000000000').code.name).to.equal('CVC'); | ||
var code = getCardType('6304000000000000')[0].code; | ||
expect(code.size).to.equal(3); | ||
expect(code.name).to.equal('CVC'); | ||
}); | ||
@@ -172,28 +232,22 @@ }); | ||
describe('returns lengths for', function () { | ||
it('maestro', function () { | ||
expect(getCardType('6304000000000000').lengths).to.deep.equal([12,13,14,15,16,17,18,19]); | ||
it('Maestro', function () { | ||
expect(getCardType('6304000000000000')[0].lengths).to.deep.equal([12,13,14,15,16,17,18,19]); | ||
}); | ||
it('diners club', function () { | ||
expect(getCardType('305').lengths).to.deep.equal([14]); | ||
it('DinersClub', function () { | ||
expect(getCardType('305')[0].lengths).to.deep.equal([14]); | ||
}); | ||
it('discover', function () { | ||
expect(getCardType('6011').lengths).to.deep.equal([16]); | ||
it('Discover', function () { | ||
expect(getCardType('6011')[0].lengths).to.deep.equal([16]); | ||
}); | ||
it('visa', function () { | ||
expect(getCardType('4').lengths).to.deep.equal([16]); | ||
it('Visa', function () { | ||
expect(getCardType('4')[0].lengths).to.deep.equal([16]); | ||
}); | ||
it('mastercard', function () { | ||
expect(getCardType('54').lengths).to.deep.equal([16]); | ||
it('MasterCard', function () { | ||
expect(getCardType('54')[0].lengths).to.deep.equal([16]); | ||
}); | ||
}); | ||
describe('returns empty for', function () { | ||
it('bad card', function () { | ||
expect(getCardType('Foo')).to.deep.equal({}); | ||
}); | ||
}); | ||
it('works for String objects', function () { | ||
var number = new String('4111111111111111'); | ||
expect(getCardType(number).type).to.equal('visa'); | ||
expect(getCardType(number)[0].type).to.equal('visa'); | ||
}); | ||
@@ -204,4 +258,4 @@ | ||
result.type = 'whaaaaaat'; | ||
expect(getCardType('4111111111111111').type).to.equal('visa'); | ||
expect(getCardType('4111111111111111')[0].type).to.equal('visa'); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21148
413
78