Permalink
Browse files

[test][fix] The PAN formatting was wrong for creditcards that didn't …

…have 4 chars
  • Loading branch information...
1 parent 595e6f4 commit 67bf098c063503196fe044b980a3df47a3156301 @3rd-Eden 3rd-Eden committed Dec 1, 2012
Showing with 23 additions and 5 deletions.
  1. +9 −5 lib/creditcard.js
  2. +14 −0 test/creditcard.test.js
View
@@ -144,12 +144,16 @@ exports.validate = function validate(number) {
* @return {String} pan
*/
exports.pan = exports.PAN = function pan(number) {
- var pattern = exports.format(number)
- , keep = pattern.slice(-4);
+ number = (''+ number).replace(/\D/g, '');
+
+ var length = number.length - 4
+ , pattern = exports.format(number);
- return pattern.slice(0, -4).replace(/\d/g, function replace() {
- return 'X';
- }) + keep;
+ return pattern.replace(/\d/g, function replace(char) {
+ return length-- > 0
+ ? 'X'
+ : char;
+ });
};
/**
@@ -53,3 +53,17 @@ describe('creditcard#format', function () {
expect(creditcard.format('4222222222222')).to.equal('4222 2222 2222 2');
});
});
+
+describe('creditcard#pan', function () {
+ it('should handle Amerian Express', function () {
+ expect(creditcard.pan('378282246310005')).to.equal('XXXX XXXXXX X0005');
+ });
+
+ it('should formot all other credit card formats', function () {
+ expect(creditcard.pan('4111111111111111')).to.equal('XXXX XXXX XXXX 1111');
+ });
+
+ it('should also truncate credit card numbers with less chars correctly', function () {
+ expect(creditcard.pan('4222222222222')).to.equal('XXXX XXXX X222 2');
+ });
+});

0 comments on commit 67bf098

Please sign in to comment.