stream: replace manual function validation with validateFunction · nodejs/node@5e025c7
@@ -18,7 +18,6 @@ const { AbortController, AbortSignal } = require('internal/abort_controller');
1818const {
1919 AbortError,
2020codes: {
21-ERR_INVALID_ARG_TYPE,
2221ERR_INVALID_ARG_VALUE,
2322ERR_MISSING_ARGS,
2423ERR_OUT_OF_RANGE,
@@ -28,6 +27,7 @@ const {
2827 validateAbortSignal,
2928 validateInteger,
3029 validateObject,
30+ validateFunction,
3131} = require('internal/validators');
3232const { kWeakHandler, kResistStopPropagation } = require('internal/event_target');
3333const { finished } = require('internal/streams/end-of-stream');
@@ -66,10 +66,7 @@ function compose(stream, options) {
6666}
67676868function 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');
7370if (options != null) {
7471validateObject(options, 'options');
7572}
@@ -223,10 +220,7 @@ async function some(fn, options = undefined) {
223220}
224221225222async 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
231225return !(await some.call(this, async (...args) => {
232226return !(await fn(...args));
@@ -241,10 +235,7 @@ async function find(fn, options) {
241235}
242236243237async 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');
248239async function forEachFn(value, options) {
249240await fn(value, options);
250241return kEmpty;
@@ -254,10 +245,7 @@ async function forEach(fn, options) {
254245}
255246256247function 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');
261249async function filterFn(value, options) {
262250if (await fn(value, options)) {
263251return value;
@@ -277,10 +265,7 @@ class ReduceAwareErrMissingArgs extends ERR_MISSING_ARGS {
277265}
278266279267async 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');
284269if (options != null) {
285270validateObject(options, 'options');
286271}