chai-almost
Advanced tools
Comparing version 1.0.0 to 1.0.1
33
index.js
@@ -8,2 +8,5 @@ 'use strict' | ||
/** | ||
* small utility functions | ||
*/ | ||
function isNumber (val) { | ||
@@ -21,2 +24,7 @@ return type(val) === 'number' | ||
/** | ||
* Makes a comparator function to be passed to deepEqual. | ||
* The returned function will return null if both arguments are not numbers, | ||
* indicating that deepEqual should proceed with other equality checks | ||
*/ | ||
function comparator (tolerance) { | ||
@@ -31,2 +39,6 @@ return function (left, right) { | ||
/** | ||
* Sets global tolerance and returns a function to be passed to chai.use | ||
* @see http://chaijs.com/guide/plugins/ | ||
*/ | ||
function chaiAlmost (customTolerance) { | ||
@@ -39,2 +51,8 @@ var standardTolerance = customTolerance || DEFAULT_TOLERANCE | ||
/** | ||
* Returns a new shallow equality function to override | ||
* .equal, .equals, .eq that tests 'almost' equality | ||
* if both values are numbers and a 'tolerance' flag is set. | ||
* Sends to deep equality check if deep flag is set | ||
*/ | ||
function overrideAssertEqual (_super) { | ||
@@ -63,2 +81,7 @@ return function assertEqual (val, msg) { | ||
/** | ||
* Returns a new deep equality function to override | ||
* .eql, .eqls that tests 'almost' equality if both corresponding | ||
* values are numbers and tolerance flag is set | ||
*/ | ||
function overrideAssertEql (_super) { | ||
@@ -85,2 +108,7 @@ return function assertEql (val, msg) { | ||
/** | ||
* .almost() method. To be used at the end of the chain like: | ||
* expect(4).to.not.be.almost(5, 1.5). Simply adds tolerance flag then calls | ||
* .equal. This will redirect to .eql if deep flag set | ||
*/ | ||
function method (val, toleranceOverride) { | ||
@@ -94,2 +122,7 @@ var tolerance = toleranceOverride || standardTolerance | ||
/** | ||
* .almost chainable property to be used like: | ||
* expect(3.99999999).to.almost.equal(4). Simply adds | ||
* tolerance flag to be read by equality checking methods | ||
*/ | ||
function chainingBehavior () { | ||
@@ -96,0 +129,0 @@ flag(this, 'tolerance', standardTolerance) |
{ | ||
"name": "chai-almost", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Extends chai with assertions that allow for floating point rounding errors", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,2 +5,3 @@ # chai-almost | ||
[![npm](https://img.shields.io/npm/v/chai-almost.svg)](https://www.npmjs.com/package/chai-almost) | ||
[![node](https://img.shields.io/badge/node-%3E=0.10-green.svg)](https://www.npmjs.com/package/chai-almost) | ||
@@ -25,6 +26,6 @@ Extends [chai](https://github.com/chaijs/chai) with assertions that allow numbers to be "almost" equal (as in, within some tolerance of one another). This is useful in particular when accounting for floating point rounding errors or other approximations. | ||
By default, `chai-almost` allows a tolerance of 1 x 10<sup>-6</sup>. This will let most rounding errors to pass safely but will still reject most true inequalities. You may override this value by passing your desired tolerance as an argument to the module on invocation: | ||
By default, `chai-almost` allows a tolerance of 1 x 10<sup>-6</sup>. This will let most rounding errors pass safely but will still reject most true inequalities. You may override this value by passing your desired tolerance as an argument to the module on invocation: | ||
``` | ||
chai.use(chaiAlmost(0.1)) | ||
chai.use(chaiAlmost(0.1)); | ||
``` | ||
@@ -34,15 +35,15 @@ | ||
The 'almost' assertion may be used chainably with deep or shallow equality: | ||
The `almost` assertion may be used chainably with deep or shallow equality: | ||
``` | ||
// shallow | ||
expect(3.9999999).to.almost.equal(4) // passes | ||
expect(3.9).to.almost.equal(4) // fails | ||
expect(4.0000001).to.be.almost(4) // passes | ||
expect(4.1).to.not.be.almost(4) // passes | ||
expect(3.9999999).to.almost.equal(4); // passes | ||
expect(3.9).to.almost.equal(4); // fails | ||
expect(4.0000001).to.be.almost(4); // passes | ||
expect(4.1).to.not.be.almost(4); // passes | ||
// deep | ||
expect({ taco: 'pastor', num: 3.9999999 }).to.almost.eql({ taco: 'pastor', num: 4 }) // passes | ||
expect([[1, 2, 2.9999999], 1.0000001]).to.be.deep.almost([[1, 2, 3], 1]) // passes | ||
expect({ taco: 'pastor', num: 3.9 }).to.not.almost.eql({ taco: 'pastor', num: 4 }) // passes | ||
expect({ taco: 'pastor', num: 3.9999999 }).to.almost.eql({ taco: 'pastor', num: 4 }); // passes | ||
expect([[1, 2, 2.9999999], 1.0000001]).to.be.deep.almost([[1, 2, 3], 1]); // passes | ||
expect({ taco: 'pastor', num: 3.9 }).to.not.almost.eql({ taco: 'pastor', num: 4 }); // passes | ||
``` | ||
@@ -53,5 +54,5 @@ | ||
``` | ||
expect('taco').to.almost.equal('taco') // still passes | ||
expect({ x: 5 }).to.be.almost({ x: 5 }) // still fails (shallow equality) | ||
expect(['tacos', 2, 3]).to.be.deep.almost(['burritos', 2, 2.9999999]) // still fails | ||
expect('taco').to.almost.equal('taco'); // still passes | ||
expect({ x: 5 }).to.be.almost({ x: 5 }); // still fails (shallow equality) | ||
expect(['tacos', 2, 3]).to.be.deep.almost(['burritos', 2, 2.9999999]); // still fails | ||
``` | ||
@@ -61,3 +62,3 @@ | ||
A one-time custom tolerance for shallow or deep equality may be passed in as the second argument to `almost`: | ||
A single-instance custom tolerance for shallow or deep equality may be passed in as the second argument to `almost`: | ||
@@ -64,0 +65,0 @@ ``` |
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
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
44198
114
64
0