Skip to content

Commit

Permalink
[test][api] Added expiry validation and tests, also introduced brower…
Browse files Browse the repository at this point in the history
… support
  • Loading branch information
3rd-Eden committed Dec 2, 2012
1 parent 67d70b0 commit 93dc1b2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/creditcard.js
@@ -1,3 +1,4 @@
(function (exports) {
'use strict';

/**
Expand Down Expand Up @@ -135,6 +136,30 @@ exports.validate = function validate(number) {
return (sum % 10 === 0) && (sum > 0);
};

/**
* Validates the expiry number.
*
* @param {String|Number} month
* @param {String|Number} year
* @return {Boolean}
*/
exports.expiry = function expiry(month, year) {
// number conversion
month = +month;
year = +year;

// incorrect numbers should fail fast
if (!month || year) return false;

var date = new Date()
, now = +date;

date.setFullYear(year);
date.setMonth(--month);

return +date >= now;
};

/**
* Applies PAN truncation to the given creditcard. PAN (primary account number)
* trunction is a "technology" that prevents most of the digits of a creditcard
Expand Down Expand Up @@ -179,3 +204,4 @@ exports.parse = function parse(number) {
, validates: exports.validate(number) // Does the creditcard validate
};
};
}(typeof exports !== 'undefined' ? exports : (creditcard = {})));
14 changes: 14 additions & 0 deletions test/creditcard.test.js
Expand Up @@ -93,3 +93,17 @@ describe('creditcard#parse', function () {
expect(data.validates).to.equal(true);
});
});

describe('creditcard#expiry', function () {
it('should validate the expiry', function () {
var today = new Date();

expect(creditcard.expiry((today.getMonth() + 1), today.getFullYear()));
});

it('should not validate the expiry', function () {
expect(creditcard.expiry('06', '1990')).to.equal(false);
expect(creditcard.expiry('06', '12')).to.equal(false);
expect(creditcard.expiry(6, 12)).to.equal(false);
});
});

0 comments on commit 93dc1b2

Please sign in to comment.