From 00611cfb3df601a578e0baf487d94e053787a7ea Mon Sep 17 00:00:00 2001 From: Christopher Rogers Date: Thu, 5 Mar 2015 15:26:53 -0800 Subject: [PATCH] Allows cardType to run detections on partial numbers - Adds a second boolean parameter to the method Signed-off-by: Christopher Rogers --- lib/recurly/validate.js | 5 +++-- test/unit/validate.test.js | 16 +++++++++++++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/recurly/validate.js b/lib/recurly/validate.js index db9031fac..b02590eaf 100644 --- a/lib/recurly/validate.js +++ b/lib/recurly/validate.js @@ -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'; }, diff --git a/test/unit/validate.test.js b/test/unit/validate.test.js index e1c783fd6..7175cfacf 100644 --- a/test/unit/validate.test.js +++ b/test/unit/validate.test.js @@ -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'); }); });