Permalink
Browse files

[test] Added tests, caught some small issues

  • Loading branch information...
1 parent 4d04c50 commit 01889802f147810178840c8756bd3536bae768ea @3rd-Eden 3rd-Eden committed Dec 1, 2012
Showing with 92 additions and 23 deletions.
  1. +22 −14 lib/creditcard.js
  2. +13 −9 package.json
  3. +55 −0 test/creditcard.test.js
  4. +2 −0 test/mocha.opts
View
@@ -47,7 +47,6 @@ exports.testnumbers = [
, 341111111111111 // american express
, 5431111111111111 // mastercard
, 5610591081018250 // australian bank
- , 76009244561 // dankort pbs
, 5019717010103742 // dankort pbs
, 6331101999990016 // switch/solo paymentech
];
@@ -59,7 +58,7 @@ exports.testnumbers = [
* @returns {String|Undefined}
*/
exports.cardscheme = function cardscheme(number) {
- number = number.replace(/\D/g, '');
+ number = (''+ number).replace(/\D/g, '');
if (/^(5610|560221|560222|560223|560224|560225)/.test(number)) {
return 'Australian Bank Card';
@@ -100,14 +99,15 @@ exports.cardscheme = function cardscheme(number) {
* @returns {String} formatted version
*/
exports.format = function format(number) {
- number = number.replace(/\D/g, '');
+ number = (''+ number).replace(/\D/g, '');
- var pattern = /^(34|37)/.test(number)
- ? 'XXXX XXXXXX XXXXX' // American express has a different pattern
- : 'XXXX XXXX XXXX XXXX'; // All other credit cards
+ var index = 0
+ , pattern = /^(34|37)/.test(number)
+ ? 'XXXX XXXXXX XXXXX' // American express has a different pattern
+ : 'XXXX XXXX XXXX XXXX'; // All other credit cards
- return pattern.replace(/X/g, function replace(char, index) {
- return number.charAt(index) || '';
+ return pattern.replace(/X/g, function replace(char) {
+ return number.charAt(index++) || '';
}).trim();
};
@@ -118,13 +118,21 @@ exports.format = function format(number) {
* @param {String} number
* @returns {Boolean}
*/
-exports.validate = function validate(number, b, c, d, e) {
- for (d = +number[b = number.length - 1], e = 0; b--;) {
- c = +number[b];
- d += ++e % 2 ? 2 * c % 10 + (c > 4) : c;
+exports.validate = function validate(number) {
+ number = (''+ number).replace(/\D/g, '');
+
+ var i = number.length
+ , sum = 0
+ , mul = 1
+ , ca;
+
+ while (i--) {
+ ca = number.charAt(i) * mul;
+ sum += ca - (ca > 9) * 9;
+ mul ^= 3;
}
- return !(d % 10);
+ return (sum % 10 === 0) && (sum > 0);
};
/**
@@ -151,7 +159,7 @@ exports.pan = exports.PAN = function pan(number) {
* @returns {String}
*/
exports.parse = function parse(number) {
- number = number.replace(/\D/g, '');
+ number = (''+ number).replace(/\D/g, '');
var scheme = exports.cardscheme(number);
View
@@ -4,28 +4,32 @@
"description": "Creditcard number parsing, validation and information extraction",
"main": "lib/index.js",
"scripts": {
- "test": "echo \"Error: no test specified\" && exit 1"
+ "test": "./node_modules/.bin/mocha $(shell find test -name '*.test.js')"
},
"repository": {
"type": "git",
"url": "git@github.com:observing/creditcard.git"
},
"keywords": [
+ "IEC",
+ "ISO/IEC 7812",
+ "bic",
"card",
- "credit-card",
- "credit",
"card",
+ "credit",
+ "credit-card",
"creditcard",
- "parsing",
- "validation",
"extraction",
"iin",
- "bic",
- "mii",
"iso",
- "IEC",
- "ISO/IEC 7812"
+ "mii",
+ "parsing",
+ "validation"
],
+ "devDependencies": {
+ "chai": "1.4.0",
+ "mocha": "1.7.3"
+ },
"author": "Arnout Kazemier",
"license": "MIT"
}
View
@@ -0,0 +1,55 @@
+'use strict';
+
+/**
+ * Configure our assertations library.
+ */
+var chai = require('chai')
+ , expect = chai.expect;
+
+chai.Assertion.includeStack = true;
+
+/**
+ * The actual library that we test..
+ */
+var creditcard = require('../lib/creditcard');
+
+describe('creditcard#validate', function () {
+ var valid = 4222222222222
+ , invalid = 242424242424;
+
+ it('should validate the creditcard number', function () {
+ expect(creditcard.validate(valid)).to.equal(true);
+ });
+
+ it('should not validate the creditcard number', function () {
+ expect(creditcard.validate(invalid)).to.equal(false);
+ });
+});
+
+describe('creditcard#testnumbers', function () {
+ it('should have an array of test numbers', function () {
+ expect(creditcard.testnumbers).to.be.a('array');
+
+ // also ensure that they validate
+ creditcard.testnumbers.forEach(function (number) {
+ var valid = creditcard.validate(number);
+
+ if (!valid) console.log('failed to process', number);
+ expect(valid).to.equal(true);
+ });
+ });
+});
+
+describe('creditcard#format', function () {
+ it('should format American Express using a 4/6/5 pattern', function () {
+ expect(creditcard.format('378282246310005')).to.equal('3782 822463 10005');
+ });
+
+ it('should formot all other credit card numbers in a 4/4/4/4 pattern', function () {
+ expect(creditcard.format('4111111111111111')).to.equal('4111 1111 1111 1111');
+ });
+
+ it('should also format credit card numbers with less chars correctly', function () {
+ expect(creditcard.format('4222222222222')).to.equal('4222 2222 2222 2');
+ });
+});
View
@@ -0,0 +1,2 @@
+--reporter spec
+--ui bdd

0 comments on commit 0188980

Please sign in to comment.