credit-card
Advanced tools
Comparing version 1.1.3 to 1.2.0
@@ -9,2 +9,3 @@ var Hoek = require('hoek'); | ||
cardPattern: /^4[0-9]{12}(?:[0-9]{3})?$/, | ||
partialPattern: /^4/, | ||
cvvPattern: /^\d{3}$/ | ||
@@ -15,2 +16,3 @@ }, | ||
cardPattern: /^5[1-5][0-9]{14}$/, | ||
partialPattern: /^5[1-5]/, | ||
cvvPattern: /^\d{3}$/ | ||
@@ -21,2 +23,3 @@ }, | ||
cardPattern: /^3[47][0-9]{13}$/, | ||
partialPattern: /^3[47]/, | ||
cvvPattern: /^\d{4}$/ | ||
@@ -27,2 +30,3 @@ }, | ||
cardPattern: /^3(?:0[0-5]|[68][0-9])[0-9]{11}$/, | ||
partialPattern: /^3(0[0-5]|[68])/, | ||
cvvPattern: /^\d{3}$/ | ||
@@ -33,2 +37,3 @@ }, | ||
cardPattern: /^6(?:011|5[0-9]{2})[0-9]{12}$/, | ||
partialPattern: /^6(011|5[0-9])/, | ||
cvvPattern: /^\d{3}$/ | ||
@@ -39,2 +44,3 @@ }, | ||
cardPattern: /^(?:2131|1800|35\d{3})\d{11}$/, | ||
partialPattern: /^(2131|1800|35)/, | ||
cvvPattern: /^\d{3}$/ | ||
@@ -102,2 +108,23 @@ } | ||
function determineCardType(number, options) { | ||
var settings = Hoek.applyToDefaults(_defaults, options || {}); | ||
var cardTypes = settings.cardTypes; | ||
var keys = Object.keys(cardTypes); | ||
number = sanitizeNumberString(number); | ||
for (var i = 0, il = keys.length; i < il; ++i) { | ||
var key = keys[i]; | ||
var type = cardTypes[key]; | ||
if (type.cardPattern.test(number) || | ||
(settings.allowPartial === true && type.partialPattern.test(number))) { | ||
return type.cardType; | ||
} | ||
} | ||
return null; | ||
} | ||
function isValidCardNumber(number, type, options) { | ||
@@ -178,6 +205,6 @@ return doesNumberMatchType(number, type, options) && luhn(number); | ||
var nDigit; | ||
for (var i = number.length - 1; i >= 0; --i) { | ||
nDigit = ~~number.charAt(i); | ||
if (bEven) { | ||
@@ -234,2 +261,3 @@ if ((nDigit *= 2) > 9) { | ||
validate: validate, | ||
determineCardType: determineCardType, | ||
isValidCardNumber: isValidCardNumber, | ||
@@ -236,0 +264,0 @@ isValidExpiryMonth: isValidExpiryMonth, |
{ | ||
"name": "credit-card", | ||
"version": "1.1.3", | ||
"version": "1.2.0", | ||
"description": "credit card validation", | ||
@@ -21,3 +21,3 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "node node_modules/.bin/lab -t 100 -v" | ||
"test": "node node_modules/.bin/lab -v -t 100 -a code" | ||
}, | ||
@@ -24,0 +24,0 @@ "keywords": [ |
@@ -71,4 +71,15 @@ # credit-card | ||
Performs several validation checks on credit card data. | ||
Performs several validation checks on credit card data. | ||
### `determineCardType(number [, options])` | ||
- Arguments | ||
- `number` (string) - Credit card number string. This value is passed to `sanitizeNumberString()` prior to classification. | ||
- `options` (object) - An optional object used to pass in additional options. The following options are supported. | ||
- `allowPartial` (boolean) - If `true`, then partial matches are accepted. Otherwise, only full length credit card numbers are accepted. Defaults to `false`. | ||
- Returns | ||
- string - a string representing the card type. If no type matches the `number`, then `null` is returned. | ||
Given a credit card number, this function attempts to determine the card type. | ||
### `isValidCardNumber(number, type [, options])` | ||
@@ -183,3 +194,3 @@ | ||
The following example defines and validates a new card type known as `GIFT_CARD`. Notice that the `card` variable has its `cardType` set to `GIFT_CARD`, and a new `pin` field has been defined. The `options` variable is passed as the second argument to `validate()`. These options define the new `GIFT_CARD` type. The `cardPattern` is a regular expression that all gift card numbers should match. Similarly, `cvvPattern` is a regular expression that the CVV should match. | ||
The following example defines and validates a new card type known as `GIFT_CARD`. Notice that the `card` variable has its `cardType` set to `GIFT_CARD`, and a new `pin` field has been defined. The `options` variable is passed as the second argument to `validate()`. These options define the new `GIFT_CARD` type. The `cardPattern` is a regular expression that all complete gift card numbers should match. `partialPattern` is the minimal regular expression that can be used to detect a gift card. `cvvPattern` is a regular expression that the complete CVV should match. | ||
@@ -203,2 +214,3 @@ Also notice the `customValidation` function. This function is used when normal validation is not quite enough. This function is passed the `card` object and `settings` object used by `validate()`. This function can return any data, which will be added directly to the response. | ||
cardPattern: /^4[0-9]{12}(?:[0-9]{3})?$/, | ||
partialPattern: /^4/, | ||
cvvPattern: /.*/ | ||
@@ -205,0 +217,0 @@ } |
@@ -205,2 +205,48 @@ var Code = require('code'); | ||
describe('#determineCardType()', function() { | ||
it('successfully detects full numbers', function(done) { | ||
expect(CreditCard.determineCardType('378282246310005')).to.equal('AMERICANEXPRESS'); | ||
expect(CreditCard.determineCardType('371449635398431')).to.equal('AMERICANEXPRESS'); | ||
expect(CreditCard.determineCardType('378734493671000')).to.equal('AMERICANEXPRESS'); | ||
expect(CreditCard.determineCardType('30569309025904')).to.equal('DINERSCLUB'); | ||
expect(CreditCard.determineCardType('38520000023237')).to.equal('DINERSCLUB'); | ||
expect(CreditCard.determineCardType('6011111111111117')).to.equal('DISCOVER'); | ||
expect(CreditCard.determineCardType('6011000990139424')).to.equal('DISCOVER'); | ||
expect(CreditCard.determineCardType('3530111333300000')).to.equal('JCB'); | ||
expect(CreditCard.determineCardType('3566002020360505')).to.equal('JCB'); | ||
expect(CreditCard.determineCardType('5555555555554444')).to.equal('MASTERCARD'); | ||
expect(CreditCard.determineCardType('5105105105105100')).to.equal('MASTERCARD'); | ||
expect(CreditCard.determineCardType('4111111111111111')).to.equal('VISA'); | ||
expect(CreditCard.determineCardType('4012888888881881')).to.equal('VISA'); | ||
expect(CreditCard.determineCardType('4222222222222')).to.equal('VISA'); | ||
expect(CreditCard.determineCardType('0000000000000000')).to.equal(null); | ||
done(); | ||
}); | ||
it('successfully detects partial numbers if allowPartial is true', function(done) { | ||
expect(CreditCard.determineCardType('37', {allowPartial: true})).to.equal('AMERICANEXPRESS'); | ||
expect(CreditCard.determineCardType('34', {allowPartial: true})).to.equal('AMERICANEXPRESS'); | ||
expect(CreditCard.determineCardType('3787344', {allowPartial: true})).to.equal('AMERICANEXPRESS'); | ||
expect(CreditCard.determineCardType('305', {allowPartial: true})).to.equal('DINERSCLUB'); | ||
expect(CreditCard.determineCardType('38', {allowPartial: true})).to.equal('DINERSCLUB'); | ||
expect(CreditCard.determineCardType('6011', {allowPartial: true})).to.equal('DISCOVER'); | ||
expect(CreditCard.determineCardType('601100099013', {allowPartial: true})).to.equal('DISCOVER'); | ||
expect(CreditCard.determineCardType('35', {allowPartial: true})).to.equal('JCB'); | ||
expect(CreditCard.determineCardType('3566002020360505', {allowPartial: true})).to.equal('JCB'); | ||
expect(CreditCard.determineCardType('5555555', {allowPartial: true})).to.equal('MASTERCARD'); | ||
expect(CreditCard.determineCardType('51', {allowPartial: true})).to.equal('MASTERCARD'); | ||
expect(CreditCard.determineCardType('411', {allowPartial: true})).to.equal('VISA'); | ||
expect(CreditCard.determineCardType('4', {allowPartial: true})).to.equal('VISA'); | ||
expect(CreditCard.determineCardType('42222222222', {allowPartial: true})).to.equal('VISA'); | ||
done(); | ||
}); | ||
it('does not allow partial matches if allowPartial is false', function(done) { | ||
expect(CreditCard.determineCardType('5555555')).to.equal(null); | ||
expect(CreditCard.determineCardType('4', {allowPartial: false})).to.equal(null); | ||
done(); | ||
}); | ||
}); | ||
describe('#isValidCardNumber()', function() { | ||
@@ -271,3 +317,3 @@ it('returns true for valid cards', function(done) { | ||
cardPattern: /^91*$/, | ||
cvvPattern: /.*/, | ||
cvvPattern: /.*/ | ||
} | ||
@@ -274,0 +320,0 @@ })).to.equal(true); |
Sorry, the diff of this file is not supported yet
43959
733
272