Skip to content

Commit

Permalink
extract isNumeric into utils
Browse files Browse the repository at this point in the history
  • Loading branch information
koddsson committed Oct 9, 2024
1 parent 0005660 commit d953553
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
30 changes: 10 additions & 20 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1224,13 +1224,11 @@ function assertAbove (n, msg) {
new Assertion(obj, flagMsg, ssfi, true).to.have.property('length');
}

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

if (!doLength && (objType === 'date' && nType !== 'date')) {
throw new AssertionError(msgPrefix + 'the argument to above must be a date', undefined, ssfi);
} else if (!isNumeric(n) && (doLength || isNumeric(obj))) {
} else if (!_.isNumeric(n) && (doLength || _.isNumeric(obj))) {
throw new AssertionError(msgPrefix + 'the argument to above must be a number', undefined, ssfi);
} else if (!doLength && (objType !== 'date' && !isNumeric(obj))) {
} else if (!doLength && (objType !== 'date' && !_.isNumeric(obj))) {
var printObj = (objType === 'string') ? "'" + obj + "'" : obj;
throw new AssertionError(msgPrefix + 'expected ' + printObj + ' to be a number or a date', undefined, ssfi);
}
Expand Down Expand Up @@ -1324,13 +1322,11 @@ function assertLeast (n, msg) {
new Assertion(obj, flagMsg, ssfi, true).to.have.property('length');
}

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

if (!doLength && (objType === 'date' && nType !== 'date')) {
errorMessage = msgPrefix + 'the argument to least must be a date';
} else if (!isNumeric(n) && (doLength || isNumeric(obj))) {
} else if (!_.isNumeric(n) && (doLength || _.isNumeric(obj))) {
errorMessage = msgPrefix + 'the argument to least must be a number';
} else if (!doLength && (objType !== 'date' && !isNumeric(obj))) {
} 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 Expand Up @@ -1429,13 +1425,11 @@ function assertBelow (n, msg) {
new Assertion(obj, flagMsg, ssfi, true).to.have.property('length');
}

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

if (!doLength && (objType === 'date' && nType !== 'date')) {
errorMessage = msgPrefix + 'the argument to below must be a date';
} else if (!isNumeric(n) && (doLength || isNumeric(obj))) {
} else if (!_.isNumeric(n) && (doLength || _.isNumeric(obj))) {
errorMessage = msgPrefix + 'the argument to below must be a number';
} else if (!doLength && (objType !== 'date' && !isNumeric(obj))) {
} 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 Expand Up @@ -1535,13 +1529,11 @@ function assertMost (n, msg) {
new Assertion(obj, flagMsg, ssfi, true).to.have.property('length');
}

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

if (!doLength && (objType === 'date' && nType !== 'date')) {
errorMessage = msgPrefix + 'the argument to most must be a date';
} else if (!isNumeric(n) && (doLength || isNumeric(obj))) {
} else if (!_.isNumeric(n) && (doLength || _.isNumeric(obj))) {
errorMessage = msgPrefix + 'the argument to most must be a number';
} else if (!doLength && (objType !== 'date' && !isNumeric(obj))) {
} 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 Expand Up @@ -1643,14 +1635,12 @@ 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 ((!isNumeric(start) || !isNumeric(finish)) && (doLength || isNumeric(obj))) {
} else if ((!_.isNumeric(start) || !_.isNumeric(finish)) && (doLength || _.isNumeric(obj))) {
errorMessage = msgPrefix + 'the arguments to within must be numbers';
} else if (!doLength && (objType !== 'date' && !isNumeric(obj))) {
} 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
7 changes: 6 additions & 1 deletion lib/chai/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import * as checkError from 'check-error';
export {test} from './test.js';

// type utility
export {type} from './type-detect.js';
import {type} from './type-detect.js';
export {type};

// expectTypes utility
export {expectTypes} from './expectTypes.js';
Expand Down Expand Up @@ -105,3 +106,7 @@ export {getOperator} from './getOperator.js';
export function isRegExp(obj) {
return Object.prototype.toString.call(obj) === '[object RegExp]';
}

export function isNumeric(obj) {
return ['Number', 'BigInt'].includes(type(obj))
}

0 comments on commit d953553

Please sign in to comment.