From 51954799196a8e9312b48757a1ee4967676b43cd Mon Sep 17 00:00:00 2001 From: Mohammed Keyvanzadeh Date: Mon, 7 Feb 2022 01:11:13 +0330 Subject: [PATCH] stream: refactor to use more validators Use more validators where appropriate for consistency. PR-URL: https://github.com/nodejs/node/pull/41871 Reviewed-By: Matteo Collina Reviewed-By: Benjamin Gruenbaum Reviewed-By: Robert Nagy Reviewed-By: Darshan Sen Reviewed-By: James M Snell Reviewed-By: Antoine du Hamel Reviewed-By: Mestery Reviewed-By: Minwoo Jung --- lib/internal/streams/operators.js | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/internal/streams/operators.js b/lib/internal/streams/operators.js index fe77bf91e94885..c48a058303bb9a 100644 --- a/lib/internal/streams/operators.js +++ b/lib/internal/streams/operators.js @@ -13,6 +13,7 @@ const { const { validateAbortSignal, validateInteger, + validateObject, } = require('internal/validators'); const { kWeakHandler } = require('internal/event_target'); const { finished } = require('internal/streams/end-of-stream'); @@ -36,8 +37,8 @@ function map(fn, options) { throw new ERR_INVALID_ARG_TYPE( 'fn', ['Function', 'AsyncFunction'], fn); } - if (options != null && typeof options !== 'object') { - throw new ERR_INVALID_ARG_TYPE('options', ['Object']); + if (options != null) { + validateObject(options, 'options'); } if (options?.signal != null) { validateAbortSignal(options.signal, 'options.signal'); @@ -167,8 +168,8 @@ function map(fn, options) { } function asIndexedPairs(options = undefined) { - if (options != null && typeof options !== 'object') { - throw new ERR_INVALID_ARG_TYPE('options', ['Object']); + if (options != null) { + validateObject(options, 'options'); } if (options?.signal != null) { validateAbortSignal(options.signal, 'options.signal'); @@ -252,8 +253,8 @@ async function reduce(reducer, initialValue, options) { throw new ERR_INVALID_ARG_TYPE( 'reducer', ['Function', 'AsyncFunction'], reducer); } - if (options != null && typeof options !== 'object') { - throw new ERR_INVALID_ARG_TYPE('options', ['Object']); + if (options != null) { + validateObject(options, 'options'); } if (options?.signal != null) { validateAbortSignal(options.signal, 'options.signal'); @@ -296,8 +297,8 @@ async function reduce(reducer, initialValue, options) { } async function toArray(options) { - if (options != null && typeof options !== 'object') { - throw new ERR_INVALID_ARG_TYPE('options', ['Object']); + if (options != null) { + validateObject(options, 'options'); } if (options?.signal != null) { validateAbortSignal(options.signal, 'options.signal'); @@ -336,8 +337,8 @@ function toIntegerOrInfinity(number) { } function drop(number, options = undefined) { - if (options != null && typeof options !== 'object') { - throw new ERR_INVALID_ARG_TYPE('options', ['Object']); + if (options != null) { + validateObject(options, 'options'); } if (options?.signal != null) { validateAbortSignal(options.signal, 'options.signal'); @@ -360,8 +361,8 @@ function drop(number, options = undefined) { } function take(number, options = undefined) { - if (options != null && typeof options !== 'object') { - throw new ERR_INVALID_ARG_TYPE('options', ['Object']); + if (options != null) { + validateObject(options, 'options'); } if (options?.signal != null) { validateAbortSignal(options.signal, 'options.signal');