Skip to content

Commit

Permalink
number test
Browse files Browse the repository at this point in the history
  • Loading branch information
Takashi Mizohata committed Dec 2, 2011
1 parent b018efe commit d05f572
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/typecast.js
Expand Up @@ -43,7 +43,7 @@ TypeCast.Boolean = function (arg) {

TypeCast.Number = function (arg, raddix) {
raddix = raddix || 10;
var t = typeof arg, result;
var t = typeof arg, result, tmp;
switch (true) {
case (t === 'boolean'):
result = arg ? 1 : 0;
Expand Down Expand Up @@ -72,7 +72,13 @@ TypeCast.Number = function (arg, raddix) {
}
}
else {
result = 0;
tmp = parseInt(arg, raddix);
if (isNaN(tmp)) {
result = 0;
}
else {
result = tmp;
}
}
}
break;
Expand Down
47 changes: 47 additions & 0 deletions test/number.test.js
@@ -0,0 +1,47 @@
var cast = require('../lib/typecast.js'),
assert = require('assert');

module.exports = {
'test number:true' : function () {
assert.equal(1, cast.number(true));
}
,'test number:false' : function () {
assert.equal(0, cast.number(false));
}
,'test number:function' : function () {
assert.equal(0, cast.number(function(){}));
}
,'test number:number' : function () {
assert.equal(1, cast.number(1));
}
,'test number:object number contains' : function () {
assert.equal(8, cast.number(new Number(8)));
}
,'test number:object not number' : function () {
assert.equal(0, cast.number({}));
}
,'test number:object number contains with raddix' : function () {
assert.equal(8, cast.number(new String(010), 8));
}
,'test number:string number contains' : function () {
assert.equal(1, cast.number('1'));
}
,'test number:string number contains with raddix' : function () {
assert.equal(10, cast.number('a', 16));
}
,'test number:string not number' : function () {
assert.equal(0, cast.number('a'));
}
,'test number:string ieee notation' : function () {
assert.equal(-1e+110, cast.number('-0.1e111'));
}
,'test number:string float' : function () {
assert.equal(0.15, cast.number('0.15'));
}
,'test number:string in the middle of the string' : function () {
assert.equal(0.5, cast.number('the profit of this quarter is expected 0.5 percent'));
}
,'test number:undefined' : function () {
assert.equal(0, cast.number(undefined));
}
};

0 comments on commit d05f572

Please sign in to comment.