Comparing version 0.2.0 to 0.3.0
@@ -108,2 +108,54 @@ !function (name, definition) { | ||
function tolerance(precision){ | ||
if(precision == null) precision = 7; | ||
return 0.5 * Math.pow(10, -precision); | ||
} | ||
/** | ||
* | ||
* # almostEqual | ||
* | ||
* Returns true if the two arguments are equal within the given precision. | ||
* | ||
* @param {Number} a | ||
* @param {Number} b | ||
* @param {Number} precision. Optional: defaults to 7. | ||
* @return {Boolean} true if the two arguments are equal with precision decimal places. | ||
* | ||
*/ | ||
exports.almostEqual = function (a,b,precision){ | ||
return Math.abs(a - b) < tolerance(precision); | ||
}; | ||
/** | ||
* | ||
* # deepAlmostEqual | ||
* | ||
* Returns true if all the objects are deeply equal, within the given precision. | ||
* | ||
* @param {Object} a | ||
* @param {Object} b | ||
* @param {Number} precision. Optional: defaults to 7. | ||
* @return {Boolean} true if the two arguments are deeply equal with precision decimal places. | ||
*/ | ||
exports.deepAlmostEqual = function(a,b,precision){ | ||
var tol = tolerance(precision); | ||
function deepEql (act, exp) { | ||
if (Object(act) === act){ | ||
for (var k in act) { | ||
if (!(deepEql(act[k], exp[k]))) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} else { | ||
return Math.abs(act - exp) < tol; | ||
} | ||
} | ||
return deepEql(a,b); | ||
}; | ||
}); // module calc | ||
@@ -143,6 +195,6 @@ | ||
if (flag(this, 'almost')) { | ||
var act = flag(this, 'object') | ||
if (null == precision) precision = 7; | ||
var act = flag(this, 'object'); | ||
if(precision == null) precision = 7; | ||
this.assert( | ||
Math.abs(act - exp) < 0.5 * Math.pow(10, -precision) | ||
calc.almostEqual(exp,act,precision) | ||
, "expected #{this} to equal #{exp} up to " + _.inspect(precision) + " decimal places" | ||
@@ -182,21 +234,6 @@ , "expected #{this} to not equal #{exp} up to " + _.inspect(precision) + " decimal places" | ||
if (flag(this, 'almost')) { | ||
var act = flag(this, 'object') | ||
if (null == precision) precision = 7; | ||
, tol = 0.5 * Math.pow(10, -precision); | ||
function deepEql (act, exp) { | ||
if (Object(act) === act){ | ||
for (var k in act) { | ||
if (!(deepEql(act[k], exp[k]))) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} else { | ||
return Math.abs(act - exp) < tol; | ||
} | ||
}; | ||
var act = flag(this, 'object') ; | ||
if(precision == null) precision = 7; | ||
this.assert( | ||
deepEql(act, exp) | ||
calc.deepAlmostEqual(act,exp,precision) | ||
, "expected #{this} to equal #{exp} up to " + _.inspect(precision) + ' decimal places' | ||
@@ -285,2 +322,7 @@ , "expected #{this} to not equal #{exp} up to " + _.inspect(precision) + ' decimal places' | ||
for(var i in calc){ | ||
if(calc.hasOwnProperty(i)){ | ||
module.exports[i] = calc[i]; | ||
} | ||
} | ||
}); // module stats | ||
@@ -287,0 +329,0 @@ return require('stats'); |
0.3.0 / 2013-09-11 | ||
================== | ||
* Merge pull request #4 from jamestalmage/expose-almost-equal | ||
* Make math and 'almost' functions publicly available. | ||
* Merge pull request #2 from ogom/master | ||
* change the position of the if | ||
0.1.0 / 2012-04-24 | ||
@@ -3,0 +11,0 @@ ================== |
@@ -56,1 +56,53 @@ /*! | ||
}; | ||
function tolerance(precision){ | ||
if(precision == null) precision = 7; | ||
return 0.5 * Math.pow(10, -precision); | ||
} | ||
/** | ||
* | ||
* # almostEqual | ||
* | ||
* Returns true if the two arguments are equal within the given precision. | ||
* | ||
* @param {Number} a | ||
* @param {Number} b | ||
* @param {Number} precision. Optional: defaults to 7. | ||
* @return {Boolean} true if the two arguments are equal with precision decimal places. | ||
* | ||
*/ | ||
exports.almostEqual = function (a,b,precision){ | ||
return Math.abs(a - b) < tolerance(precision); | ||
}; | ||
/** | ||
* | ||
* # deepAlmostEqual | ||
* | ||
* Returns true if all the objects are deeply equal, within the given precision. | ||
* | ||
* @param {Object} a | ||
* @param {Object} b | ||
* @param {Number} precision. Optional: defaults to 7. | ||
* @return {Boolean} true if the two arguments are deeply equal with precision decimal places. | ||
*/ | ||
exports.deepAlmostEqual = function(a,b,precision){ | ||
var tol = tolerance(precision); | ||
function deepEql (act, exp) { | ||
if (Object(act) === act){ | ||
for (var k in act) { | ||
if (!(deepEql(act[k], exp[k]))) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} else { | ||
return Math.abs(act - exp) < tol; | ||
} | ||
} | ||
return deepEql(a,b); | ||
}; |
@@ -31,6 +31,6 @@ var calc = require('./calc'); | ||
if (flag(this, 'almost')) { | ||
var act = flag(this, 'object') | ||
if (null == precision) precision = 7; | ||
var act = flag(this, 'object'); | ||
if(precision == null) precision = 7; | ||
this.assert( | ||
Math.abs(act - exp) < 0.5 * Math.pow(10, -precision) | ||
calc.almostEqual(exp,act,precision) | ||
, "expected #{this} to equal #{exp} up to " + _.inspect(precision) + " decimal places" | ||
@@ -70,21 +70,6 @@ , "expected #{this} to not equal #{exp} up to " + _.inspect(precision) + " decimal places" | ||
if (flag(this, 'almost')) { | ||
var act = flag(this, 'object') | ||
if (null == precision) precision = 7; | ||
, tol = 0.5 * Math.pow(10, -precision); | ||
function deepEql (act, exp) { | ||
if (Object(act) === act){ | ||
for (var k in act) { | ||
if (!(deepEql(act[k], exp[k]))) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} else { | ||
return Math.abs(act - exp) < tol; | ||
} | ||
}; | ||
var act = flag(this, 'object') ; | ||
if(precision == null) precision = 7; | ||
this.assert( | ||
deepEql(act, exp) | ||
calc.deepAlmostEqual(act,exp,precision) | ||
, "expected #{this} to equal #{exp} up to " + _.inspect(precision) + ' decimal places' | ||
@@ -172,1 +157,7 @@ , "expected #{this} to not equal #{exp} up to " + _.inspect(precision) + ' decimal places' | ||
}; | ||
for(var i in calc){ | ||
if(calc.hasOwnProperty(i)){ | ||
module.exports[i] = calc[i]; | ||
} | ||
} |
@@ -5,3 +5,3 @@ { | ||
"description": "Statistical and additional numerical assertions for the Chai Assertion Library.", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"repository": { | ||
@@ -8,0 +8,0 @@ "type": "git", |
@@ -0,1 +1,2 @@ | ||
var tmp; | ||
if (!chai) { | ||
@@ -5,7 +6,12 @@ var chai = require('chai') | ||
chai.use(stats); | ||
tmp = stats; | ||
} | ||
else { | ||
tmp = chai_stats; | ||
} | ||
var should = chai.should() | ||
, assert = chai.assert | ||
, expect = chai.expect; | ||
, expect = chai.expect | ||
, chai_stats = tmp; | ||
@@ -15,3 +21,3 @@ describe('Chai Stats', function () { | ||
describe('Math Basics', function () { | ||
var nums = [ 1, 3, 5, 7, 9, ]; | ||
var nums = [ 1, 3, 5, 7, 9 ]; | ||
@@ -70,2 +76,27 @@ it('should be able to get the sum of a set of numbers', function () { | ||
}); | ||
describe('math functions are publicly available',function(){ | ||
var nums = [ 1, 3, 5, 7, 9 ]; | ||
it('almostEqual',function(){ | ||
expect(chai_stats.almostEqual(3.1415,3,0)).to.be.true; | ||
expect(chai_stats.almostEqual(3.1415,3.1416,3)).to.be.true; | ||
expect(chai_stats.almostEqual(3.1415,3.1416,4)).to.be.false; | ||
}); | ||
it('deepAlmostEqual',function(){ | ||
expect(chai_stats.deepAlmostEqual({ pi: 3.1416 },{ pi: 3 },0)).to.be.true; | ||
expect(chai_stats.deepAlmostEqual({ pi: 3.1416 },{ pi: 3.14159 },4)).to.be.true; | ||
expect(chai_stats.deepAlmostEqual({ pi: 3.1416 },{ pi: 3.14159 },5)).to.be.false; | ||
}); | ||
it('mean',function(){ | ||
expect(chai_stats.mean(nums)).to.equal(5); | ||
}); | ||
it('sum',function(){ | ||
expect(chai_stats.sum(nums)).to.equal(25); | ||
}); | ||
it('sdeviation',function(){ | ||
expect(chai_stats.sdeviation(nums)).to.equal(3.1622776601683795); | ||
}) | ||
}); | ||
}); |
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
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
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
27678
726
0
0