btrz-mathematics
Advanced tools
Comparing version 1.1.0 to 1.2.0
{ | ||
"name": "btrz-mathematics", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Betterez Mathematics", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "NODE_ENV=test multi='xunit=test-results/xunit.xml spec=-' node_modules/istanbul/lib/cli.js cover node_modules/mocha/bin/_mocha -- --ui bdd -R mocha-multi -t 5000", | ||
"test-w": "NODE_ENV=test node_modules/mocha/bin/mocha -w", | ||
"publish": "publish" | ||
"test": "NODE_ENV=test node_modules/mocha/bin/mocha --ui bdd -t 5000", | ||
"test-w": "NODE_ENV=test node_modules/mocha/bin/mocha -w" | ||
}, | ||
@@ -37,7 +36,4 @@ "repository": { | ||
"chai": "^3.0.0", | ||
"istanbul": "^0.4.1", | ||
"mocha": "latest", | ||
"mocha-multi": "latest", | ||
"publish": "^0.5.0" | ||
"mocha": "latest" | ||
} | ||
} |
# btrz-mathematics | ||
A mathematics library for node and crazy numbers |
@@ -29,2 +29,21 @@ "use strict"; | ||
function isNegativeZero(x) { | ||
return ( 1 / x ) === -Infinity; | ||
} | ||
function addOne(whole) { | ||
const numWhole = Number(whole); | ||
if (numWhole !== 0) { | ||
return numWhole > 0 ? numWhole + 1 : numWhole - 1; | ||
} | ||
return isNegativeZero(whole) ? numWhole - 1 : numWhole + 1; | ||
} | ||
function wholePlusDigits(whole, digits = '00') { | ||
if (Number(whole) !== 0) { | ||
return `${whole}.${digits}` | ||
} | ||
return isNegativeZero(whole) && digits != 0 ? `-0.${digits}` : `0.${digits}`; | ||
} | ||
class Mathematics { | ||
@@ -73,6 +92,6 @@ | ||
flip = cents.substr((digits), 1); | ||
if ((flip < 5) || roundingPolicy === 'down') { return whole + '.00'; } | ||
if ((flip < 5) || roundingPolicy === 'down') { return wholePlusDigits(whole); } | ||
if ((flip > 4) || roundingPolicy === 'up') { | ||
whole = Number(whole) + 1; | ||
return whole + '.00'; | ||
whole = addOne(whole); | ||
return wholePlusDigits(whole); | ||
} | ||
@@ -95,4 +114,4 @@ } | ||
if (roundedCents === 100){ | ||
whole = Number(whole) + 1; | ||
return whole + '.00'; | ||
whole = addOne(whole); | ||
return wholePlusDigits(whole); | ||
} | ||
@@ -107,3 +126,3 @@ } | ||
return whole + '.' + roundedCents; | ||
return wholePlusDigits(whole, roundedCents); | ||
} | ||
@@ -110,0 +129,0 @@ } |
@@ -112,2 +112,41 @@ "use strict"; | ||
}); | ||
describe("boder cases for positive and negative zeros", () => { | ||
it("should round negative zero to 0.00 with precision 0", function () { | ||
expect(mathematics.round(0.33, 0)).to.be.eql("0.00"); | ||
}); | ||
it("should round negative zero to 0.00 with precision 0", function () { | ||
expect(mathematics.round(-0.33, 0)).to.be.eql("0.00"); | ||
}); | ||
it("should round to -1.00 with precision 2", function () { | ||
expect(mathematics.round(-0.9974999999999999, 2)).to.be.eql("-1.00"); | ||
}); | ||
it("should round to -2.00 with precision 2", function () { | ||
expect(mathematics.round(-1.9974999999999999, 2)).to.be.eql("-2.00"); | ||
}); | ||
it("should round to -1.10 with precision 2", function () { | ||
expect(mathematics.round(-1.09725, 2)).to.be.eql("-1.10"); | ||
}); | ||
it("should round to -1.01 with precision 2", function () { | ||
expect(mathematics.round(-1.009725, 2)).to.be.eql("-1.01"); | ||
}); | ||
it("should round to 0.00 with precision 2", function () { | ||
expect(mathematics.round(0.00333, 2)).to.be.eql("0.00"); | ||
}); | ||
it("should round negative zero to 0.00 with precision 2", function () { | ||
expect(mathematics.round(-0.00333, 2)).to.be.eql("0.00"); | ||
}); | ||
it("should round to same negative number with 5 digits", function () { | ||
expect(mathematics.round(-0.02, 5)).to.be.eql("-0.02000"); | ||
}); | ||
}); | ||
}); |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
79702
2
18
640
4
2