From 39063a139a34cd76d7977fa9e1f35838913a86b9 Mon Sep 17 00:00:00 2001 From: Voltrex Date: Wed, 4 Aug 2021 14:04:51 +0430 Subject: [PATCH] lib: cleanup instance validation Cleaned up the `URLSearchParams`'s `this` validation to increase readability by moving them to an extra helper function. --- lib/internal/url.js | 47 ++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/lib/internal/url.js b/lib/internal/url.js index 0749e07d6e7677..070d0d1a4f5009 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -153,6 +153,10 @@ class URLContext { } } +function isURLSearchParams(self) { + return self && self[searchParams] && !self[searchParams][searchParams]; +} + class URLSearchParams { // URL Standard says the default value is '', but as undefined and '' have // the same result, undefined is used to prevent unnecessary parsing. @@ -222,9 +226,8 @@ class URLSearchParams { } [inspect.custom](recurseTimes, ctx) { - if (!this || !this[searchParams] || this[searchParams][searchParams]) { + if (!isURLSearchParams(this)) throw new ERR_INVALID_THIS('URLSearchParams'); - } if (typeof recurseTimes === 'number' && recurseTimes < 0) return ctx.stylize('[Object]', 'special'); @@ -259,9 +262,9 @@ class URLSearchParams { } append(name, value) { - if (!this || !this[searchParams] || this[searchParams][searchParams]) { + if (!isURLSearchParams(this)) throw new ERR_INVALID_THIS('URLSearchParams'); - } + if (arguments.length < 2) { throw new ERR_MISSING_ARGS('name', 'value'); } @@ -273,9 +276,9 @@ class URLSearchParams { } delete(name) { - if (!this || !this[searchParams] || this[searchParams][searchParams]) { + if (!isURLSearchParams(this)) throw new ERR_INVALID_THIS('URLSearchParams'); - } + if (arguments.length < 1) { throw new ERR_MISSING_ARGS('name'); } @@ -294,9 +297,9 @@ class URLSearchParams { } get(name) { - if (!this || !this[searchParams] || this[searchParams][searchParams]) { + if (!isURLSearchParams(this)) throw new ERR_INVALID_THIS('URLSearchParams'); - } + if (arguments.length < 1) { throw new ERR_MISSING_ARGS('name'); } @@ -312,9 +315,9 @@ class URLSearchParams { } getAll(name) { - if (!this || !this[searchParams] || this[searchParams][searchParams]) { + if (!isURLSearchParams(this)) throw new ERR_INVALID_THIS('URLSearchParams'); - } + if (arguments.length < 1) { throw new ERR_MISSING_ARGS('name'); } @@ -331,9 +334,9 @@ class URLSearchParams { } has(name) { - if (!this || !this[searchParams] || this[searchParams][searchParams]) { + if (!isURLSearchParams(this)) throw new ERR_INVALID_THIS('URLSearchParams'); - } + if (arguments.length < 1) { throw new ERR_MISSING_ARGS('name'); } @@ -349,9 +352,9 @@ class URLSearchParams { } set(name, value) { - if (!this || !this[searchParams] || this[searchParams][searchParams]) { + if (!isURLSearchParams(this)) throw new ERR_INVALID_THIS('URLSearchParams'); - } + if (arguments.length < 2) { throw new ERR_MISSING_ARGS('name', 'value'); } @@ -436,17 +439,16 @@ class URLSearchParams { // Define entries here rather than [Symbol.iterator] as the function name // must be set to `entries`. entries() { - if (!this || !this[searchParams] || this[searchParams][searchParams]) { + if (!isURLSearchParams(this)) throw new ERR_INVALID_THIS('URLSearchParams'); - } return createSearchParamsIterator(this, 'key+value'); } forEach(callback, thisArg = undefined) { - if (!this || !this[searchParams] || this[searchParams][searchParams]) { + if (!isURLSearchParams(this)) throw new ERR_INVALID_THIS('URLSearchParams'); - } + validateCallback(callback); let list = this[searchParams]; @@ -464,17 +466,15 @@ class URLSearchParams { // https://heycam.github.io/webidl/#es-iterable keys() { - if (!this || !this[searchParams] || this[searchParams][searchParams]) { + if (!isURLSearchParams(this)) throw new ERR_INVALID_THIS('URLSearchParams'); - } return createSearchParamsIterator(this, 'key'); } values() { - if (!this || !this[searchParams] || this[searchParams][searchParams]) { + if (!isURLSearchParams(this)) throw new ERR_INVALID_THIS('URLSearchParams'); - } return createSearchParamsIterator(this, 'value'); } @@ -482,9 +482,8 @@ class URLSearchParams { // https://heycam.github.io/webidl/#es-stringifier // https://url.spec.whatwg.org/#urlsearchparams-stringification-behavior toString() { - if (!this || !this[searchParams] || this[searchParams][searchParams]) { + if (!isURLSearchParams(this)) throw new ERR_INVALID_THIS('URLSearchParams'); - } return serializeParams(this[searchParams]); }