From d953553cb76cac5ae4d46893c74dfbbd01a61a04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Oddsson?= Date: Wed, 9 Oct 2024 13:26:06 +0000 Subject: [PATCH] extract `isNumeric` into `utils` --- lib/chai/core/assertions.js | 30 ++++++++++-------------------- lib/chai/utils/index.js | 7 ++++++- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/lib/chai/core/assertions.js b/lib/chai/core/assertions.js index e87789b0..e2971726 100644 --- a/lib/chai/core/assertions.js +++ b/lib/chai/core/assertions.js @@ -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); } @@ -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 { @@ -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 { @@ -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 { @@ -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 { diff --git a/lib/chai/utils/index.js b/lib/chai/utils/index.js index 80306f79..fd4e6358 100644 --- a/lib/chai/utils/index.js +++ b/lib/chai/utils/index.js @@ -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'; @@ -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)) +}