From 6ce3293cc4da5a75bd7c094ef26af7da69c19f72 Mon Sep 17 00:00:00 2001 From: Denys Otrishko Date: Sat, 30 May 2020 21:22:04 +0300 Subject: [PATCH] events: use internal/validators in event_target.js PR-URL: https://github.com/nodejs/node/pull/34015 Reviewed-By: Denys Otrishko Reviewed-By: Benjamin Gruenbaum --- lib/internal/event_target.js | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index 8c4ea11a14ed8b..bccb2e0edc6a0d 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -7,7 +7,6 @@ const { Object, Set, Symbol, - NumberIsNaN, SymbolToStringTag, } = primordials; @@ -15,10 +14,10 @@ const { codes: { ERR_INVALID_ARG_TYPE, ERR_EVENT_RECURSION, - ERR_OUT_OF_RANGE, ERR_MISSING_ARGS } } = require('internal/errors'); +const { validateInteger, validateObject } = require('internal/validators'); const { customInspectSymbol } = require('internal/util'); const { inspect } = require('util'); @@ -54,11 +53,10 @@ class Event { constructor(type, options) { - if (arguments.length === 0) { + if (arguments.length === 0) throw new ERR_MISSING_ARGS('type'); - } - if (options != null && typeof options !== 'object') - throw new ERR_INVALID_ARG_TYPE('options', 'object', options); + if (options != null) + validateObject(options, 'options'); const { cancelable, bubbles, composed } = { ...options }; this.#cancelable = !!cancelable; this.#bubbles = !!bubbles; @@ -350,9 +348,7 @@ class NodeEventTarget extends EventTarget { } setMaxListeners(n) { - if (typeof n !== 'number' || n < 0 || NumberIsNaN(n)) { - throw new ERR_OUT_OF_RANGE('n', 'a non-negative number', n); - } + validateInteger(n, 'n', 0); this.#maxListeners = n; return this; } @@ -430,11 +426,9 @@ function validateListener(listener) { } function validateEventListenerOptions(options) { - if (typeof options === 'boolean') { - options = { capture: options }; - } - if (options == null || typeof options !== 'object') - throw new ERR_INVALID_ARG_TYPE('options', 'object', options); + if (typeof options === 'boolean') + return { capture: options }; + validateObject(options, 'options'); const { once = false, capture = false,