lib: use validateArray for array arguments · nodejs/node@02f6fee · GitHub
Skip to content

Commit 02f6fee

Browse files
DevJunzaduh95
authored andcommitted
lib: use validateArray for array arguments
Three call sites still duplicated the ArrayIsArray check that validateArray already performs. Both files were already importing other validators next to these checks. The error code, argument name and expected type are unchanged, so the thrown error stays identical. The ArrayIsArray primordial is no longer used in histogram.js and is dropped from its destructuring; blocklist.js still uses it elsewhere. The existing tests only asserted the error code, so assertions covering the full error message are added for all three call sites. They pass both before and after this change. Refs: #64959 Assisted-by: claude:fable-5 Signed-off-by: JunHwan Choi <devjunsday@gmail.com> PR-URL: #65344 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent b6b5764 commit 02f6fee

4 files changed

Lines changed: 51 additions & 11 deletions

File tree

‎lib/internal/blocklist.js‎

Lines changed: 3 additions & 7 deletions

‎lib/internal/histogram.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const {
4-
ArrayIsArray,
54
Float64Array,
65
Map,
76
MapPrototypeEntries,
@@ -33,6 +32,7 @@ const {
3332
} = require('internal/errors');
3433

3534
const {
35+
validateArray,
3636
validateInteger,
3737
validateNumber,
3838
validateObject,
@@ -539,8 +539,7 @@ class Histogram {
539539
percentilesAt(percentiles) {
540540
if (!isHistogram(this))
541541
throw new ERR_INVALID_THIS('Histogram');
542-
if (!ArrayIsArray(percentiles))
543-
throw new ERR_INVALID_ARG_TYPE('percentiles', 'Array', percentiles);
542+
validateArray(percentiles, 'percentiles');
544543
for (let i = 0; i < percentiles.length; i++) {
545544
validateNumber(percentiles[i], `percentiles[${i}]`);
546545
if (NumberIsNaN(percentiles[i]) ||

‎test/parallel/test-blocklist.js‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,34 @@ const util = require('util');
339339
});
340340
}
341341

342+
{
343+
// addAddresses() and addCIDRs() must throw the same errors for non-array
344+
// input regardless of how the checks are implemented internally.
345+
const blockList = new BlockList();
346+
for (const [value, received] of [
347+
['x', "type string ('x')"],
348+
[123, 'type number (123)'],
349+
[{}, 'an instance of Object'],
350+
[null, 'null'],
351+
[undefined, 'undefined'],
352+
[1n, 'type bigint (1n)'],
353+
[true, 'type boolean (true)'],
354+
]) {
355+
assert.throws(() => blockList.addAddresses(value), {
356+
code: 'ERR_INVALID_ARG_TYPE',
357+
name: 'TypeError',
358+
message: 'The "addresses" argument must be an instance of Array. ' +
359+
`Received ${received}`,
360+
});
361+
assert.throws(() => blockList.addCIDRs(value), {
362+
code: 'ERR_INVALID_ARG_TYPE',
363+
name: 'TypeError',
364+
message: 'The "cidrs" argument must be an instance of Array. ' +
365+
`Received ${received}`,
366+
});
367+
}
368+
}
369+
342370
{
343371
// Test addAddresses() batch insert.
344372
const blockList = new BlockList();

‎test/parallel/test-perf-hooks-histogram-analysis.js‎

Lines changed: 18 additions & 1 deletion

0 commit comments

Comments
 (0)