Comparing version 0.1.0 to 0.2.0
169
lib/stats.js
var calc = require('./calc'); | ||
module.exports = function (chai, _) { | ||
var Assertion = chai.Assertion | ||
, flag = _.flag | ||
_.addProperty(chai.Assertion, 'sum', function () { | ||
var obj = _.flag(this, 'object'); | ||
new chai.Assertion(obj).to.be.instanceOf(Array); | ||
_.flag(this, 'object', calc.sum(obj)); | ||
Assertion.addProperty('almost', function () { | ||
flag(this, 'almost', true); | ||
}); | ||
_.addProperty(chai.Assertion, 'mean', function () { | ||
var obj = _.flag(this, 'object'); | ||
new chai.Assertion(obj).to.be.instanceOf(Array); | ||
_.flag(this, 'object', calc.mean(obj)); | ||
/** | ||
* # .almost.equal(expected, [precision]) | ||
* | ||
* The same as NumPy's assert_almost_equal, for scalars. | ||
* Assert near equality: abs(expected-actual) < 0.5 * 10**(-decimal) | ||
* | ||
* expect(3.1415).to.almost.equal(3.14159, 3); | ||
* assert.almostEqual(3.1416, 3.14159, 3, 'these numbers are almost equal'); | ||
* | ||
* @name equal | ||
* @param {Number} actual | ||
* @param {Number} expected | ||
* @param {Number} decimal | ||
* @param {String} message | ||
* @api public | ||
*/ | ||
Assertion.overwriteMethod('equal', function(_super) { | ||
return function equal (exp, precision) { | ||
if (flag(this, 'almost')) { | ||
var act = flag(this, 'object') | ||
if (null == precision) precision = 7; | ||
this.assert( | ||
Math.abs(act - exp) < 0.5 * Math.pow(10, -precision) | ||
, "expected #{this} to equal #{exp} up to " + _.inspect(precision) + " decimal places" | ||
, "expected #{this} to not equal #{exp} up to " + _.inspect(precision) + " decimal places" | ||
, exp | ||
, act | ||
); | ||
} else { | ||
_super.apply(this, arguments); | ||
} | ||
}; | ||
}); | ||
_.addMethod(chai.assert, 'almostEqual', function (act, exp, precision, msg) { | ||
new Assertion(act, msg).to.almost.equal(exp, precision); | ||
}); | ||
/** | ||
* # .deepAlmostEqual(actual, expected, [decimal, message]) | ||
* | ||
* The same as NumPy's assert_almost_equal, for objects whose leaves are all numbers. | ||
* Assert near equality: abs(expected-actual) < 0.5 * 10**(-decimal) for every leaf. | ||
* | ||
* assert.deepAlmostEqual({pi: 3.1416}, {pi: 3.14159}, 3); | ||
* | ||
* @name equal | ||
* @param {*} actual | ||
* @param {*} expected | ||
* @param {Number} decimal | ||
* @param {String} message | ||
* @api public | ||
*/ | ||
Assertion.overwriteMethod('eql', function (_super) { | ||
return function eql (exp, precision) { | ||
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; | ||
} | ||
}; | ||
this.assert( | ||
deepEql(act, exp) | ||
, "expected #{this} to equal #{exp} up to " + _.inspect(precision) + ' decimal places' | ||
, "expected #{this} to not equal #{exp} up to " + _.inspect(precision) + ' decimal places' | ||
, exp | ||
, act | ||
); | ||
} else { | ||
_super.apply(this, arguments); | ||
} | ||
}; | ||
}); | ||
_.addMethod(chai.assert, 'deepAlmostEqual', function (act, exp, precision, msg) { | ||
new Assertion(act, msg).to.almost.eql(exp, precision); | ||
}); | ||
/** | ||
* ### .sum | ||
* | ||
* Modifies the assertion subject with the sum of an | ||
* array of number so it can be compared using chai's | ||
* core assertions. | ||
* | ||
* expect([ 1, 2, 3 ]).to.have.sum.equal(6); | ||
* expect([ 1, 2, 3 ]).to.have.sum.above(5); | ||
* expect([ 1, 2, 3 ]).to.have.sum.below(7); | ||
* | ||
* @name sum | ||
* @expects {Array} | ||
* @api public | ||
*/ | ||
Assertion.addProperty('sum', function () { | ||
var obj = flag(this, 'object'); | ||
new Assertion(obj).to.be.instanceOf(Array); | ||
flag(this, 'object', calc.sum(obj)); | ||
}); | ||
/** | ||
* ### .mean | ||
* | ||
* Modifies the assertion subject with the mean of an | ||
* array of number so it can be compared using chai's | ||
* core assertions. | ||
* | ||
* expect([ 1, 2, 3 ]).to.have.mean.equal(2); | ||
* expect([ 1, 2, 3 ]).to.have.mean.above(1.5); | ||
* expect([ 1, 2, 3 ]).to.have.mean.below(2.5); | ||
* | ||
* @name mean | ||
* @expects {Array} | ||
* @api public | ||
*/ | ||
Assertion.addProperty('mean', function () { | ||
var obj = flag(this, 'object'); | ||
new Assertion(obj).to.be.instanceOf(Array); | ||
flag(this, 'object', calc.mean(obj)); | ||
}); | ||
function deviation () { | ||
var obj = _.flag(this, 'object'); | ||
new chai.Assertion(obj).to.be.instanceOf(Array); | ||
_.flag(this, 'object', calc.sdeviation(obj)); | ||
var obj = flag(this, 'object'); | ||
new Assertion(obj).to.be.instanceOf(Array); | ||
flag(this, 'object', calc.sdeviation(obj)); | ||
} | ||
_.addProperty(chai.Assertion, 'stdev', deviation); | ||
_.addProperty(chai.Assertion, 'deviation', deviation); | ||
/** | ||
* ### .deviation | ||
* | ||
* Modifies the assertion subject with the standard | ||
* deviations of an array of number so it can be | ||
* compared using chai's core assertions. | ||
* | ||
* expect([ 1, 2, 3, 4 ]).to.have.deviation.almost.equal(1.290, 2); | ||
* | ||
* @name mean | ||
* @expects {Array} | ||
* @api public | ||
*/ | ||
Assertion.addProperty('stdev', deviation); | ||
Assertion.addProperty('deviation', deviation); | ||
}; |
@@ -5,3 +5,3 @@ { | ||
"description": "Statistical and additional numerical assertions for the Chai Assertion Library.", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"repository": { | ||
@@ -18,3 +18,4 @@ "type": "git", | ||
"mocha": "*", | ||
"chai": "*" | ||
"chai": "*", | ||
"folio": "0.2.x" | ||
}, | ||
@@ -21,0 +22,0 @@ "optionalDependencies": {}, |
134
README.md
@@ -1,4 +0,132 @@ | ||
chai-stats | ||
========== | ||
# Chai Stats | ||
Statistical and additional numerical assertions for the Chai Assertion Library. | ||
Statistical and additional numerical assertions for the Chai Assertion Library. | ||
## Installation | ||
#### Node.js | ||
Chai spies are available on npm. | ||
$ npm install chai-stats | ||
#### Browser | ||
Include `chai-stats.js` after including `chai.js`. | ||
```xml | ||
<script src="chai-stats.js"></script> | ||
``` | ||
## Plug In | ||
If you are using `chai-stats` in the browser, there is nothing you need to do. It will detect `chai` in the global | ||
namespace and automagically get used. | ||
If you are using node, here is a useful bit. | ||
```js | ||
var chai = require('chai') | ||
, chaiStats = require('chai-stats'); | ||
chai.use(chaiStats); | ||
var should = chai.should() | ||
, expect = chai.expect; | ||
``` | ||
## API Reference | ||
#### .almost.equal(expected, [precision]) | ||
The same as NumPy's `assert_almost_equal`, for scalars. | ||
Assert near equality: `abs(expected-actual) < 0.5 * 10**(-decimal)` | ||
```javascript | ||
expect(3.1415).to.almost.equal(3.14159, 3); | ||
assert.almostEqual(3.1416, 3.14159, 3, 'these numbers are almost equal'); | ||
``` | ||
#### .deepAlmostEqual(actual, expected, [decimal, message]) | ||
The same as NumPy's `assert_almost_equal`, for objects whose leaves are all numbers. | ||
Assert near equality: `abs(expected-actual) < 0.5 * 10**(-decimal)` for every leaf. | ||
```javascript | ||
expect({ pi: 3.1416 }).to.almost.eql({ pi: 3.14159 }, 3); | ||
assert.deepAlmostEqual({ pi: 3.1416 }, { pi: 3.14159 }, 3); | ||
``` | ||
#### .sum | ||
Modifies the assertion subject with the sum of an | ||
array of number so it can be compared using chai's | ||
core assertions. | ||
```javascript | ||
expect([ 1, 2, 3 ]).to.have.sum.equal(6); | ||
expect([ 1, 2, 3 ]).to.have.sum.above(5); | ||
expect([ 1, 2, 3 ]).to.have.sum.below(7); | ||
``` | ||
#### .mean | ||
Modifies the assertion subject with the mean of an | ||
array of number so it can be compared using chai's | ||
core assertions. | ||
```javascript | ||
expect([ 1, 2, 3 ]).to.have.mean.equal(2); | ||
expect([ 1, 2, 3 ]).to.have.mean.above(1.5); | ||
expect([ 1, 2, 3 ]).to.have.mean.below(2.5); | ||
``` | ||
#### .deviation | ||
Modifies the assertion subject with the standard | ||
deviations of an array of number so it can be | ||
compared using chai's core assertions. | ||
```javascript | ||
expect([ 1, 2, 3, 4 ]).to.have.deviation.almost.equal(1.290, 2); | ||
``` | ||
## Tests | ||
Tests are written using [mocha](http://github.com/visionmedia/mocha) in the BDD interface. | ||
Node tests can be executed using `make test`. Browser tests can be seen by opening `test/browser/index.html`. | ||
## Contributors | ||
repo age : 3 months ago | ||
commits : 21 | ||
active : 5 days | ||
files : 14 | ||
authors : | ||
17 Jake Luer 81.0% | ||
4 josher19 19.0% | ||
## License | ||
(The MIT License) | ||
Copyright (c) 2012 Jake Luer <jake@alogicalparadox.com> | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE |
@@ -7,3 +7,5 @@ if (!chai) { | ||
var should = chai.should(); | ||
var should = chai.should() | ||
, assert = chai.assert | ||
, expect = chai.expect; | ||
@@ -26,4 +28,43 @@ describe('Chai Stats', function () { | ||
nums.should.have.deviation.equal(3.1622776601683795); | ||
[ 1, 2, 3, 4 ].should.have.deviation.almost.equal(1.290, 2); | ||
}); | ||
}); | ||
describe('almost equal / eql', function () { | ||
it('should work: .almost.equal // almostEqual', function() { | ||
(3.1416).should.almost.equal(3.14159, 3); | ||
assert.almostEqual(3.1416, 3.14159, 3); | ||
(function () { | ||
assert.almostEqual(3.1416, 3.14159, 6); | ||
}).should.throw(chai.AssertionError, "expected 3.1416 to equal 3.14159 up to 6 decimal places"); | ||
(function () { | ||
assert.almostEqual(3.1416, 3.14159); | ||
}).should.throw(chai.AssertionError, "expected 3.1416 to equal 3.14159 up to 7 decimal places"); | ||
}); | ||
it('should work: .almost.eql // deepAlmostEqual', function() { | ||
({ pi: 3.1416 }).should.almost.eql({ pi: 3.14159 }, 3); | ||
assert.deepAlmostEqual({pi: 3.1416}, {pi: 3.14159}, 3); | ||
(function () { | ||
assert.deepAlmostEqual({pi: 3.1416}, {pi: 3.14159}, 6); | ||
}).should.throw(chai.AssertionError, "expected { pi: 3.1416 } to equal { pi: 3.14159 } up to 6 decimal places"); | ||
(function () { | ||
assert.deepAlmostEqual({pi: 3.1416}, {pi: 3.14159}); | ||
}).should.throw(chai.AssertionError, "expected { pi: 3.1416 } to equal { pi: 3.14159 } up to 7 decimal places"); | ||
}); | ||
it('should round to nearest number if explicitely given 0 precision', function() { | ||
({ pi: 3.1416 }).should.almost.eql({ pi: 3 }, 0); | ||
assert.deepAlmostEqual({pi: 3.1416}, {pi: 3}, 0); | ||
expect(3.1415).to.almost.equal(3, 0); // precision 0 => rounding | ||
expect(3.1415).to.not.almost.equal(3); // precision undefined => 7 decimal places | ||
expect(5.8).to.almost.equal(6, 0); // precision 0 => rounding | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
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
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
24636
14
623
133
3
1
11
1