Skip to content

Commit

Permalink
Merge pull request #187 from recurly/partial-card-type-detection
Browse files Browse the repository at this point in the history
Allows cardType to run detections on partial numbers
  • Loading branch information
bhelx committed Mar 10, 2015
2 parents 7f03c87 + 00611cf commit 5e771ea
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
5 changes: 3 additions & 2 deletions lib/recurly/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,14 @@ module.exports = {
* TODO(chrissrogers): Maybe undefined instread of "unknown"?
*
* @param {Number|String} number The card number
* @param {Boolean} partial detect card type on a partial (incomplete) number
* @return {String} card type
*/

cardType: function (number) {
cardType: function (number, partial) {
var str = parseCard(number);
var card = find(types, function (card) {
return card.pattern.test(str) && ~index(card.lengths, str.length);
return card.pattern.test(str) && (partial || ~index(card.lengths, str.length));
});
return card && card.type || 'unknown';
},
Expand Down
16 changes: 13 additions & 3 deletions test/unit/validate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,27 @@ describe('Recurly.validate', function () {
describe('cardType', function() {
it('should parse visa', function() {
var type = recurly.validate.cardType('4111-1111-1111-1111');
assert('visa' === type);
assert(type === 'visa');
});

it('should parse american_express', function() {
var type = recurly.validate.cardType('372546612345678');
assert('american_express' === type);
assert(type === 'american_express');
});

it('should parse unknown', function() {
var type = recurly.validate.cardType('867-5309-jenny');
assert('unknown' === type);
assert(type === 'unknown');
});

it('should not parse partial numbers', function () {
var type = recurly.validate.cardType('3725');
assert(type === 'unknown');
});

it('should parse partial numbers if instructed', function () {
var type = recurly.validate.cardType('3725', true);
assert(type === 'american_express');
});
});

Expand Down

0 comments on commit 5e771ea

Please sign in to comment.