Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[test][api] Added expiry validation and tests, also introduced brower…

… support
  • Loading branch information...
commit 93dc1b24f8db56718005b6be260263cf6ae109d6 1 parent 67d70b0
@3rd-Eden 3rd-Eden authored
Showing with 40 additions and 0 deletions.
  1. +26 −0 lib/creditcard.js
  2. +14 −0 test/creditcard.test.js
View
26 lib/creditcard.js
@@ -1,3 +1,4 @@
+(function (exports) {
'use strict';
/**
@@ -136,6 +137,30 @@ exports.validate = function validate(number) {
};
/**
+ * 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
* from appearing on printed receipts.
@@ -179,3 +204,4 @@ exports.parse = function parse(number) {
, validates: exports.validate(number) // Does the creditcard validate
};
};
+}(typeof exports !== 'undefined' ? exports : (creditcard = {})));
View
14 test/creditcard.test.js
@@ -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);
+ });
+});
Please sign in to comment.
Something went wrong with that request. Please try again.