Skip to content

Commit

Permalink
add bigint support to within
Browse files Browse the repository at this point in the history
  • Loading branch information
koddsson committed Oct 6, 2024
1 parent 90e3ae7 commit 3587410
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
12 changes: 7 additions & 5 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1572,9 +1572,9 @@ Assertion.addMethod('lessThanOrEqual', assertMost);
* expect(4, 'nooo why fail??').to.be.within(1, 3);
*
* @name within
* @param {Number} start lower bound inclusive
* @param {Number} finish upper bound inclusive
* @param {String} msg _optional_
* @param {unknown} start lower bound inclusive
* @param {unknown} finish upper bound inclusive
* @param {string} msg _optional_
* @namespace BDD
* @api public
*/
Expand All @@ -1598,12 +1598,14 @@ Assertion.addMethod('within', function (start, finish, msg) {
if (doLength && objType !== 'map' && objType !== 'set') {
new Assertion(obj, flagMsg, ssfi, true).to.have.property('length');
}

const isNumeric = x => ['Number', 'BigInt'].includes(_.type(x))

if (!doLength && (objType === 'date' && (startType !== 'date' || finishType !== 'date'))) {
errorMessage = msgPrefix + 'the arguments to within must be dates';
} else if ((startType !== 'number' || finishType !== 'number') && (doLength || objType === 'number')) {
} else if ((!isNumeric(start) || !isNumeric(finish)) && (doLength || isNumeric(obj))) {
errorMessage = msgPrefix + 'the arguments to within must be numbers';
} else if (!doLength && (objType !== 'date' && objType !== 'number')) {
} else if (!doLength && (objType !== 'date' && !isNumeric(obj))) {
var printObj = (objType === 'string') ? "'" + obj + "'" : obj;
errorMessage = msgPrefix + 'expected ' + printObj + ' to be a number or a date';
} else {
Expand Down
3 changes: 3 additions & 0 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,9 @@ describe('expect', function () {
expect('foo').to.have.lengthOf.within(2, 4);
expect([ 1, 2, 3 ]).to.have.length.within(2, 4);
expect([ 1, 2, 3 ]).to.have.lengthOf.within(2, 4);
expect(5n).to.be.within(5, 10);
expect(5).to.be.within(3n, 6);
expect(5).to.be.within(3, 5n);

err(function(){
expect(5).to.not.be.within(4, 6, 'blah');
Expand Down

0 comments on commit 3587410

Please sign in to comment.