Skip to content

Commit 5e025c7

Browse files
JinhyeokFangtargos
authored andcommitted
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 <matteo.collina@gmail.com> Reviewed-By: Mattias Buelens <mattias@buelens.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 1a93df8 commit 5e025c7

1 file changed

Lines changed: 6 additions & 21 deletions

File tree

‎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
functionmap(fn,options){
69-
if(typeoffn!=='function'){
70-
thrownewERR_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
asyncfunctionevery(fn,options=undefined){
226-
if(typeoffn!=='function'){
227-
thrownewERR_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!(awaitsome.call(this,async(...args)=>{
232226
return!(awaitfn(...args));
@@ -241,10 +235,7 @@ async function find(fn, options) {
241235
}
242236

243237
asyncfunctionforEach(fn,options){
244-
if(typeoffn!=='function'){
245-
thrownewERR_INVALID_ARG_TYPE(
246-
'fn',['Function','AsyncFunction'],fn);
247-
}
238+
validateFunction(fn,'fn');
248239
asyncfunctionforEachFn(value,options){
249240
awaitfn(value,options);
250241
returnkEmpty;
@@ -254,10 +245,7 @@ async function forEach(fn, options) {
254245
}
255246

256247
functionfilter(fn,options){
257-
if(typeoffn!=='function'){
258-
thrownewERR_INVALID_ARG_TYPE(
259-
'fn',['Function','AsyncFunction'],fn);
260-
}
248+
validateFunction(fn,'fn');
261249
asyncfunctionfilterFn(value,options){
262250
if(awaitfn(value,options)){
263251
returnvalue;
@@ -277,10 +265,7 @@ class ReduceAwareErrMissingArgs extends ERR_MISSING_ARGS {
277265
}
278266

279267
asyncfunctionreduce(reducer,initialValue,options){
280-
if(typeofreducer!=='function'){
281-
thrownewERR_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)