diff options
author | Denys Otrishko <shishugi@gmail.com> | 2020-05-30 21:22:04 +0300 |
---|---|---|
committer | James M Snell <jasnell@gmail.com> | 2020-06-24 03:08:07 +0300 |
commit | 5e6ae9aa5d228eb9b54f3ebde6a3d791dff3d0ed (patch) | |
tree | 03440319a91bbc21b24cd23582bb89498c557e46 /lib/internal/event_target.js | |
parent | 91d9cdfffc61be3c537d220f6b58e7087873a964 (diff) |
events: use internal/validators in event_target.js
PR-URL: https://github.com/nodejs/node/pull/34015
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Diffstat (limited to 'lib/internal/event_target.js')
-rw-r--r-- | lib/internal/event_target.js | 22 |
1 files changed, 8 insertions, 14 deletions
diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index 8c4ea11a14e..bccb2e0edc6 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, |