Skip to content

Commit

Permalink
lib: refactor to use validateInteger
Browse files Browse the repository at this point in the history
Use validateInteger for record and revise error code of percentile.
  • Loading branch information
deokjinkim committed Dec 7, 2022
1 parent 89113b8 commit 9d44414
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions lib/internal/histogram.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const {
MapPrototypeEntries,
NumberIsNaN,
NumberIsInteger,
NumberMAX_SAFE_INTEGER,
ObjectFromEntries,
Expand Down Expand Up @@ -185,7 +186,9 @@ class Histogram {
percentile(percentile) {
if (!isHistogram(this))
throw new ERR_INVALID_THIS('Histogram');
validateNumber(percentile, 'percentile', 1, 100);
validateNumber(percentile, 'percentile');
if (NumberIsNaN(percentile) || percentile <= 0 || percentile > 100)
throw new ERR_OUT_OF_RANGE('percentile', '> 0 && <= 100', percentile);

return this[kHandle]?.percentile(percentile);
}
Expand All @@ -197,7 +200,9 @@ class Histogram {
percentileBigInt(percentile) {
if (!isHistogram(this))
throw new ERR_INVALID_THIS('Histogram');
validateNumber(percentile, 'percentile', 1, 100);
validateNumber(percentile, 'percentile');
if (NumberIsNaN(percentile) || percentile <= 0 || percentile > 100)
throw new ERR_OUT_OF_RANGE('percentile', '> 0 && <= 100', percentile);

return this[kHandle]?.percentileBigInt(percentile);
}
Expand Down Expand Up @@ -277,11 +282,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);
}
Expand Down

0 comments on commit 9d44414

Please sign in to comment.