From 88d71dc301028735decf6f12cc1174f13c5cacff Mon Sep 17 00:00:00 2001 From: Deokjin Kim Date: Sat, 4 Feb 2023 06:12:05 +0900 Subject: [PATCH] lib: refactor to use min/max of `validateNumber` Fix NumberIsNaN is called two times and change error code to ERR_OUT_OF_RANGE when RangeError is occurred in test. PR-URL: https://github.com/nodejs/node/pull/45772 Reviewed-By: James M Snell Reviewed-By: Luigi Pinca Reviewed-By: Darshan Sen --- lib/internal/histogram.js | 13 +++---------- test/sequential/test-performance-eventloopdelay.js | 2 +- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/lib/internal/histogram.js b/lib/internal/histogram.js index 92a679e8f3a634..df091e9c2e9805 100644 --- a/lib/internal/histogram.js +++ b/lib/internal/histogram.js @@ -3,7 +3,6 @@ const { MapPrototypeEntries, NumberIsNaN, - NumberIsInteger, NumberMAX_SAFE_INTEGER, ObjectFromEntries, ReflectConstruct, @@ -186,9 +185,8 @@ class Histogram { if (!isHistogram(this)) throw new ERR_INVALID_THIS('Histogram'); validateNumber(percentile, 'percentile'); - if (NumberIsNaN(percentile) || percentile <= 0 || percentile > 100) - throw new ERR_INVALID_ARG_VALUE.RangeError('percentile', percentile); + throw new ERR_OUT_OF_RANGE('percentile', '> 0 && <= 100', percentile); return this[kHandle]?.percentile(percentile); } @@ -201,9 +199,8 @@ class Histogram { if (!isHistogram(this)) throw new ERR_INVALID_THIS('Histogram'); validateNumber(percentile, 'percentile'); - if (NumberIsNaN(percentile) || percentile <= 0 || percentile > 100) - throw new ERR_INVALID_ARG_VALUE.RangeError('percentile', percentile); + throw new ERR_OUT_OF_RANGE('percentile', '> 0 && <= 100', percentile); return this[kHandle]?.percentileBigInt(percentile); } @@ -283,11 +280,7 @@ class RecordableHistogram extends Histogram { return; } - if (!NumberIsInteger(val)) - throw new ERR_INVALID_ARG_TYPE('val', ['integer', 'bigint'], val); - - if (val < 1 || val > NumberMAX_SAFE_INTEGER) - throw new ERR_OUT_OF_RANGE('val', 'a safe integer greater than 0', val); + validateInteger(val, 'val', 1); this[kHandle]?.record(val); } diff --git a/test/sequential/test-performance-eventloopdelay.js b/test/sequential/test-performance-eventloopdelay.js index f262e9de3fe6cb..24e5744118cc38 100644 --- a/test/sequential/test-performance-eventloopdelay.js +++ b/test/sequential/test-performance-eventloopdelay.js @@ -91,7 +91,7 @@ const { sleep } = require('internal/util'); () => histogram.percentile(i), { name: 'RangeError', - code: 'ERR_INVALID_ARG_VALUE' + code: 'ERR_OUT_OF_RANGE' } ); });