Comparing version 0.0.3 to 0.0.4
@@ -15,3 +15,3 @@ /** | ||
function digit(list) { | ||
function calcDigit(list) { | ||
var result = 0; | ||
@@ -27,6 +27,10 @@ for (var i = 0; i < list.length; i++) { | ||
return result; | ||
return Math.pow(10, result); | ||
} | ||
function toUpperDigit(num, digit) { | ||
return num * digit | 0; | ||
} | ||
function _CalcJs(num) { | ||
function Chain(num) { | ||
this.nums = [num]; | ||
@@ -36,3 +40,3 @@ this.operators = []; | ||
_CalcJs.prototype.add = function(num) { | ||
Chain.prototype.add = function(num) { | ||
this.operators.push('add'); | ||
@@ -43,3 +47,3 @@ this.nums.push(num); | ||
_CalcJs.prototype.sub = function(num) { | ||
Chain.prototype.sub = function(num) { | ||
this.operators.push('sub'); | ||
@@ -50,3 +54,3 @@ this.nums.push(num); | ||
_CalcJs.prototype.multi = function(num) { | ||
Chain.prototype.multi = function(num) { | ||
this.operators.push('multi'); | ||
@@ -57,3 +61,3 @@ this.nums.push(num); | ||
_CalcJs.prototype.div = function(num) { | ||
Chain.prototype.div = function(num) { | ||
this.operators.push('div'); | ||
@@ -64,4 +68,4 @@ this.nums.push(num); | ||
_CalcJs.prototype.end = function() { | ||
var _digit = Math.pow(10, digit(this.nums)); | ||
Chain.prototype.end = function() { | ||
var digit = calcDigit(this.nums); | ||
@@ -74,3 +78,3 @@ var indexMulti = this.operators.indexOf('multi'); | ||
nextIndex = indexMulti + 1; | ||
ret = (this.nums[indexMulti] * _digit) * (this.nums[nextIndex] * _digit) / (_digit * _digit); | ||
ret = toUpperDigit(this.nums[indexMulti], digit) * toUpperDigit(this.nums[nextIndex], digit) / (digit * digit); | ||
this.nums.splice(indexMulti, 2, ret); | ||
@@ -87,10 +91,12 @@ this.operators.splice(indexMulti, 1); | ||
indexDiv = this.operators.indexOf('div'); | ||
digit = calcDigit(this.nums); | ||
} | ||
digit = calcDigit(this.nums); | ||
var result = this.nums[0]; | ||
for (var i = 0; i < this.operators.length; i++) { | ||
if (this.operators[i] === 'add') { | ||
result = (result * _digit + this.nums[i + 1] * _digit) / _digit; | ||
result = (toUpperDigit(result, digit) + toUpperDigit(this.nums[i + 1], digit)) / digit; | ||
} else { | ||
result = (result * _digit - this.nums[i + 1] * _digit) / _digit; | ||
result = (toUpperDigit(result, digit) - toUpperDigit(this.nums[i + 1], digit)) / digit; | ||
} | ||
@@ -110,26 +116,26 @@ } | ||
CalcJs.prototype.add = function() { | ||
var _digit = Math.pow(10, digit(arguments)); | ||
var result = arguments[0] * _digit || 0; | ||
var digit = calcDigit(arguments); | ||
var result = toUpperDigit(arguments[0], digit) || 0; | ||
for (var i = 1; i < arguments.length; i++) { | ||
result += arguments[i] * _digit; | ||
result += toUpperDigit(arguments[i], digit); | ||
} | ||
return result / _digit; | ||
return result / digit; | ||
}; | ||
CalcJs.prototype.sub = function() { | ||
var _digit = Math.pow(10, digit(arguments)); | ||
var result = arguments[0] * _digit || 0; | ||
var digit = calcDigit(arguments); | ||
var result = toUpperDigit(arguments[0], digit) || 0; | ||
for (var i = 1; i < arguments.length; i++) { | ||
result -= arguments[i] * _digit; | ||
result -= toUpperDigit(arguments[i], digit); | ||
} | ||
return result / _digit; | ||
return result / digit; | ||
}; | ||
CalcJs.prototype.multi = function() { | ||
var _digit = Math.pow(10, digit(arguments)); | ||
var result = arguments[0] * _digit || 0; | ||
var digit = calcDigit(arguments); | ||
var result = toUpperDigit(arguments[0], digit) || 0; | ||
for (var i = 1; i < arguments.length; i++) { | ||
result *= arguments[i] * _digit; | ||
result *= toUpperDigit(arguments[i], digit); | ||
} | ||
return result / Math.pow(_digit, arguments.length); | ||
return result / Math.pow(digit, arguments.length); | ||
}; | ||
@@ -146,3 +152,3 @@ | ||
CalcJs.prototype.begin = function(num) { | ||
return new _CalcJs(num); | ||
return new Chain(num); | ||
}; | ||
@@ -149,0 +155,0 @@ |
{ | ||
"name": "calcjs", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "calcjs", | ||
@@ -25,5 +25,6 @@ "main": "calcjs.js", | ||
"devDependencies": { | ||
"should": "~3.3.1", | ||
"mocha": "~1.18.2" | ||
"jshint": "^2.5.10", | ||
"mocha": "^2.0.1", | ||
"should": "^4.3.0" | ||
} | ||
} |
@@ -24,2 +24,6 @@ var should = require('should'); | ||
}); | ||
it('calcjs.multi(0.55, 100) > 55', function() { | ||
var result = calcjs.multi(0.55, 100); | ||
result.should.equal(55); | ||
}); | ||
}); | ||
@@ -26,0 +30,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13721
171
3