diff --git a/dist/creditcard.js b/dist/creditcard.js index 5af10e0..578932e 100644 --- a/dist/creditcard.js +++ b/dist/creditcard.js @@ -1,5 +1,5 @@ /*! - * creditcard.js v2.1.0 + * creditcard.js v2.1.1 * Created by @ContaAzul. * * Licensed MIT. @@ -36,7 +36,6 @@ var CreditCard = function () { key: 'getCreditCardNameByNumber', value: function getCreditCardNameByNumber(number) { var INVALID_CARD_MESSAGE = 'Credit card is invalid!'; - if (!this.isValid(number)) return INVALID_CARD_MESSAGE; var CREDIT_CARD_LIST = this.retrieveCreditCardList(); @@ -62,18 +61,20 @@ var CreditCard = function () { } }, { key: 'isExpirationDateValid', - value: function isExpirationDateValid(month, year) { - var m = month; - var y = year; + value: function isExpirationDateValid(paramMonth, paramYearn) { + var month = parseInt(paramMonth, 10); + var year = parseInt(paramYearn, 10); - m = parseInt(m, 10); - y = parseInt(y, 10); + var currentYear = new Date().getFullYear(); + var currentMonth = new Date().getMonth() + 1; - if (isNaN(m) || isNaN(y)) return false; + if (isNaN(month) || isNaN(year)) return false; - if (m < 1 || m > 12) return false; + if (year === currentYear && month < currentMonth) return false; - return !(y < 1000 || y >= 3000); + if (month < 1 || month > 12) return false; + + return !(year < 1000 || year >= 3000); } }]); diff --git a/dist/creditcard.min.js b/dist/creditcard.min.js index 9ecaff3..a025e6e 100644 --- a/dist/creditcard.min.js +++ b/dist/creditcard.min.js @@ -1,7 +1,7 @@ /*! - * creditcard.js v2.1.0 + * creditcard.js v2.1.1 * Created by @ContaAzul. * * Licensed MIT. */ -!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var r;r="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,r.CreditCard=e()}}(function(){var e,r,n;return function e(r,n,t){function i(a,o){if(!n[a]){if(!r[a]){var l="function"==typeof require&&require;if(!o&&l)return l(a,!0);if(u)return u(a,!0);var f=new Error("Cannot find module '"+a+"'");throw f.code="MODULE_NOT_FOUND",f}var s=n[a]={exports:{}};r[a][0].call(s.exports,function(e){var n=r[a][1][e];return i(n?n:e)},s,s.exports,e,r,n,t)}return n[a].exports}for(var u="function"==typeof require&&require,a=0;a12)&&!(i<1e3||i>=3e3))}}]),e}();r.exports=o},{"./creditCardList":2,"./helpers/luhn":3}],2:[function(e,r,n){"use strict";function t(e,r){if(!(e instanceof r))throw new TypeError("Cannot call a class as a function")}var i=function(){function e(e,r){for(var n=0;n0&&i%10===0}}]),e}();r.exports=u},{}]},{},[1])(1)}); \ No newline at end of file +!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var r;r="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,r.CreditCard=e()}}(function(){var e,r,n;return function e(r,n,t){function i(a,o){if(!n[a]){if(!r[a]){var l="function"==typeof require&&require;if(!o&&l)return l(a,!0);if(u)return u(a,!0);var f=new Error("Cannot find module '"+a+"'");throw f.code="MODULE_NOT_FOUND",f}var s=n[a]={exports:{}};r[a][0].call(s.exports,function(e){var n=r[a][1][e];return i(n?n:e)},s,s.exports,e,r,n,t)}return n[a].exports}for(var u="function"==typeof require&&require,a=0;a12)&&!(i<1e3||i>=3e3)))}}]),e}();r.exports=o},{"./creditCardList":2,"./helpers/luhn":3}],2:[function(e,r,n){"use strict";function t(e,r){if(!(e instanceof r))throw new TypeError("Cannot call a class as a function")}var i=function(){function e(e,r){for(var n=0;n0&&i%10===0}}]),e}();r.exports=u},{}]},{},[1])(1)}); \ No newline at end of file diff --git a/package.json b/package.json index e67edca..937e529 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "creditcard.js", - "version": "2.1.0", + "version": "2.1.1", "description": "A simple library for credit-card validation in JavaScript", "main": "dist/creditcard.min.js", "repository": "ContaAzul/creditcard.js", diff --git a/specs/creditCard.spec.js b/specs/creditCard.spec.js index cf8e7b7..1344494 100644 --- a/specs/creditCard.spec.js +++ b/specs/creditCard.spec.js @@ -11,11 +11,19 @@ describe('CreditCard', () => { elo: '5041756758046020', amex: '373257135458763', aura: '5078601870000127985', - hiper: '6062825303833679' + hiper: '6062825303833679', + visaMask: '4532000000000000' }; }); describe('#validadeExpiryDate', () => { + beforeEach(() => { + jasmine.clock().install(); + jasmine.clock().mockDate(new Date(2017, 6, 10)); + }); + + afterEach(() => jasmine.clock().uninstall()); + it('should return true with its a VALID date', () => { expect(creditcard.isExpirationDateValid('10', '2020')).toBeTruthy(); }); @@ -31,6 +39,10 @@ describe('CreditCard', () => { it('should return false with empty month or year', () => { expect(creditcard.isExpirationDateValid('', '')).toBeFalsy(); }); + + it('should return false when month is past', () => { + expect(creditcard.isExpirationDateValid('01', '2017')).toBeFalsy(); + }); }); describe('#validadeCreditCard', () => { @@ -96,6 +108,11 @@ describe('CreditCard', () => { expect(creditCardName).toBe('Hipercard'); }); + it('should return valid name for mask number', () => { + let creditCardName = creditcard.getCreditCardNameByNumber(CREDIT_CARDS.visaMask); + expect(creditCardName).toBe('Visa'); + }); + it('should return false', () => { let creditCardName = creditcard.getCreditCardNameByNumber(INVALID_CREDIT_CARD); expect(creditCardName).toBe('Credit card is invalid!'); diff --git a/src/creditCard.js b/src/creditCard.js index 7e13ee5..42d9ae2 100644 --- a/src/creditCard.js +++ b/src/creditCard.js @@ -17,8 +17,6 @@ class CreditCard { getCreditCardNameByNumber(number) { const INVALID_CARD_MESSAGE = 'Credit card is invalid!'; - if (!this.isValid(number)) - return INVALID_CARD_MESSAGE; let CREDIT_CARD_LIST = this.retrieveCreditCardList(); @@ -43,20 +41,23 @@ class CreditCard { return regex.test(code); } - isExpirationDateValid(month, year) { - let m = month; - let y = year; + isExpirationDateValid(paramMonth, paramYearn) { + const month = parseInt(paramMonth, 10); + const year = parseInt(paramYearn, 10); - m = parseInt(m, 10); - y = parseInt(y, 10); + const currentYear = new Date().getFullYear(); + const currentMonth = new Date().getMonth() + 1; - if (isNaN(m) || isNaN(y)) + if (isNaN(month) || isNaN(year)) return false; - if (m < 1 || m > 12) + if (year === currentYear && month < currentMonth) return false; - return !(y < 1000 || y >= 3000); + if (month < 1 || month > 12) + return false; + + return !(year < 1000 || year >= 3000); } }