@@ -4,15 +4,16 @@ const { AbortController } = require('internal/abort_controller');
44
55const {
66 codes : {
7- ERR_INVALID_ARG_TYPE ,
87 ERR_MISSING_ARGS ,
98 ERR_OUT_OF_RANGE ,
109 } ,
1110 AbortError,
1211} = require ( 'internal/errors' ) ;
1312const {
1413 validateAbortSignal,
14+ validateFunction,
1515 validateInteger,
16+ validateObject,
1617} = require ( 'internal/validators' ) ;
1718const { kWeakHandler } = require ( 'internal/event_target' ) ;
1819const { finished } = require ( 'internal/streams/end-of-stream' ) ;
@@ -32,12 +33,9 @@ const kEmpty = Symbol('kEmpty');
3233const kEof = Symbol ( 'kEof' ) ;
3334
3435function map ( fn , options ) {
35- if ( typeof fn !== 'function' ) {
36- throw new ERR_INVALID_ARG_TYPE (
37- 'fn' , [ 'Function' , 'AsyncFunction' ] , fn ) ;
38- }
39- if ( options != null && typeof options !== 'object' ) {
40- throw new ERR_INVALID_ARG_TYPE ( 'options' , [ 'Object' ] ) ;
36+ validateFunction ( fn , 'fn' ) ;
37+ if ( options != null ) {
38+ validateObject ( options , 'options' ) ;
4139 }
4240 if ( options ?. signal != null ) {
4341 validateAbortSignal ( options . signal , 'options.signal' ) ;
@@ -167,8 +165,8 @@ function map(fn, options) {
167165}
168166
169167function asIndexedPairs ( options = undefined ) {
170- if ( options != null && typeof options !== 'object' ) {
171- throw new ERR_INVALID_ARG_TYPE ( ' options' , [ 'Object' ] ) ;
168+ if ( options != null ) {
169+ validateObject ( options , 'options' ) ;
172170 }
173171 if ( options ?. signal != null ) {
174172 validateAbortSignal ( options . signal , 'options.signal' ) ;
@@ -186,8 +184,8 @@ function asIndexedPairs(options = undefined) {
186184}
187185
188186async function some ( fn , options ) {
189- if ( options != null && typeof options !== 'object' ) {
190- throw new ERR_INVALID_ARG_TYPE ( ' options' , [ 'Object' ] ) ;
187+ if ( options != null ) {
188+ validateObject ( options , 'options' ) ;
191189 }
192190 if ( options ?. signal != null ) {
193191 validateAbortSignal ( options . signal , 'options.signal' ) ;
@@ -216,21 +214,15 @@ async function some(fn, options) {
216214}
217215
218216async function every ( fn , options ) {
219- if ( typeof fn !== 'function' ) {
220- throw new ERR_INVALID_ARG_TYPE (
221- 'fn' , [ 'Function' , 'AsyncFunction' ] , fn ) ;
222- }
217+ validateFunction ( fn , 'fn' ) ;
223218 // https://en.wikipedia.org/wiki/De_Morgan%27s_laws
224219 return ! ( await some . call ( this , async ( ...args ) => {
225220 return ! ( await fn ( ...args ) ) ;
226221 } , options ) ) ;
227222}
228223
229224async function forEach ( fn , options ) {
230- if ( typeof fn !== 'function' ) {
231- throw new ERR_INVALID_ARG_TYPE (
232- 'fn' , [ 'Function' , 'AsyncFunction' ] , fn ) ;
233- }
225+ validateFunction ( fn , 'fn' ) ;
234226 async function forEachFn ( value , options ) {
235227 await fn ( value , options ) ;
236228 return kEmpty ;
@@ -240,10 +232,7 @@ async function forEach(fn, options) {
240232}
241233
242234function filter ( fn , options ) {
243- if ( typeof fn !== 'function' ) {
244- throw new ERR_INVALID_ARG_TYPE (
245- 'fn' , [ 'Function' , 'AsyncFunction' ] , fn ) ;
246- }
235+ validateFunction ( fn , 'fn' ) ;
247236 async function filterFn ( value , options ) {
248237 if ( await fn ( value , options ) ) {
249238 return value ;
@@ -263,12 +252,9 @@ class ReduceAwareErrMissingArgs extends ERR_MISSING_ARGS {
263252}
264253
265254async function reduce ( reducer , initialValue , options ) {
266- if ( typeof reducer !== 'function' ) {
267- throw new ERR_INVALID_ARG_TYPE (
268- 'reducer' , [ 'Function' , 'AsyncFunction' ] , reducer ) ;
269- }
270- if ( options != null && typeof options !== 'object' ) {
271- throw new ERR_INVALID_ARG_TYPE ( 'options' , [ 'Object' ] ) ;
255+ validateFunction ( reducer , 'reducer' ) ;
256+ if ( options != null ) {
257+ validateObject ( options , 'options' ) ;
272258 }
273259 if ( options ?. signal != null ) {
274260 validateAbortSignal ( options . signal , 'options.signal' ) ;
@@ -311,8 +297,8 @@ async function reduce(reducer, initialValue, options) {
311297}
312298
313299async function toArray ( options ) {
314- if ( options != null && typeof options !== 'object' ) {
315- throw new ERR_INVALID_ARG_TYPE ( ' options' , [ 'Object' ] ) ;
300+ if ( options != null ) {
301+ validateObject ( options , 'options' ) ;
316302 }
317303 if ( options ?. signal != null ) {
318304 validateAbortSignal ( options . signal , 'options.signal' ) ;
@@ -351,8 +337,8 @@ function toIntegerOrInfinity(number) {
351337}
352338
353339function drop ( number , options = undefined ) {
354- if ( options != null && typeof options !== 'object' ) {
355- throw new ERR_INVALID_ARG_TYPE ( ' options' , [ 'Object' ] ) ;
340+ if ( options != null ) {
341+ validateObject ( options , 'options' ) ;
356342 }
357343 if ( options ?. signal != null ) {
358344 validateAbortSignal ( options . signal , 'options.signal' ) ;
@@ -375,8 +361,8 @@ function drop(number, options = undefined) {
375361}
376362
377363function take ( number , options = undefined ) {
378- if ( options != null && typeof options !== 'object' ) {
379- throw new ERR_INVALID_ARG_TYPE ( ' options' , [ 'Object' ] ) ;
364+ if ( options != null ) {
365+ validateObject ( options , 'options' ) ;
380366 }
381367 if ( options ?. signal != null ) {
382368 validateAbortSignal ( options . signal , 'options.signal' ) ;
0 commit comments