Skip to content

Commit

Permalink
events: use internal/validators in event_target.js
Browse files Browse the repository at this point in the history
PR-URL: #34015
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
lundibundi authored and codebytere committed Jun 27, 2020
1 parent 5dca04e commit 6ce3293
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@ const {
Object,
Set,
Symbol,
NumberIsNaN,
SymbolToStringTag,
} = primordials;

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');
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 6ce3293

Please sign in to comment.