Skip to content

Commit eefe3b1

Browse files
authored
stream: replace manual function validation with validateFunction
Replace repetitive manual function type checking with the existing validateFunction in multiple stream operator functions. PR-URL: #59529 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Mattias Buelens <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 196f546 commit eefe3b1

File tree

1 file changed

+6
-21
lines changed

1 file changed

+6
-21
lines changed

lib/internal/streams/operators.js

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ const { AbortController, AbortSignal } = require('internal/abort_controller');
1818
const {
1919
AbortError,
2020
codes: {
21-
ERR_INVALID_ARG_TYPE,
2221
ERR_INVALID_ARG_VALUE,
2322
ERR_MISSING_ARGS,
2423
ERR_OUT_OF_RANGE,
@@ -28,6 +27,7 @@ const {
2827
validateAbortSignal,
2928
validateInteger,
3029
validateObject,
30+
validateFunction,
3131
} = require('internal/validators');
3232
const { kWeakHandler, kResistStopPropagation } = require('internal/event_target');
3333
const { finished } = require('internal/streams/end-of-stream');
@@ -66,10 +66,7 @@ function compose(stream, options) {
6666
}
6767

6868
function map(fn, options) {
69-
if (typeof fn !== 'function') {
70-
throw new ERR_INVALID_ARG_TYPE(
71-
'fn', ['Function', 'AsyncFunction'], fn);
72-
}
69+
validateFunction(fn, 'fn');
7370
if (options != null) {
7471
validateObject(options, 'options');
7572
}
@@ -223,10 +220,7 @@ async function some(fn, options = undefined) {
223220
}
224221

225222
async function every(fn, options = undefined) {
226-
if (typeof fn !== 'function') {
227-
throw new ERR_INVALID_ARG_TYPE(
228-
'fn', ['Function', 'AsyncFunction'], fn);
229-
}
223+
validateFunction(fn, 'fn');
230224
// https://en.wikipedia.org/wiki/De_Morgan%27s_laws
231225
return !(await some.call(this, async (...args) => {
232226
return !(await fn(...args));
@@ -241,10 +235,7 @@ async function find(fn, options) {
241235
}
242236

243237
async function forEach(fn, options) {
244-
if (typeof fn !== 'function') {
245-
throw new ERR_INVALID_ARG_TYPE(
246-
'fn', ['Function', 'AsyncFunction'], fn);
247-
}
238+
validateFunction(fn, 'fn');
248239
async function forEachFn(value, options) {
249240
await fn(value, options);
250241
return kEmpty;
@@ -254,10 +245,7 @@ async function forEach(fn, options) {
254245
}
255246

256247
function filter(fn, options) {
257-
if (typeof fn !== 'function') {
258-
throw new ERR_INVALID_ARG_TYPE(
259-
'fn', ['Function', 'AsyncFunction'], fn);
260-
}
248+
validateFunction(fn, 'fn');
261249
async function filterFn(value, options) {
262250
if (await fn(value, options)) {
263251
return value;
@@ -277,10 +265,7 @@ class ReduceAwareErrMissingArgs extends ERR_MISSING_ARGS {
277265
}
278266

279267
async function reduce(reducer, initialValue, options) {
280-
if (typeof reducer !== 'function') {
281-
throw new ERR_INVALID_ARG_TYPE(
282-
'reducer', ['Function', 'AsyncFunction'], reducer);
283-
}
268+
validateFunction(reducer, 'reducer');
284269
if (options != null) {
285270
validateObject(options, 'options');
286271
}

0 commit comments

Comments
 (0)