Skip to content

Commit

Permalink
lib: cleanup instance validation
Browse files Browse the repository at this point in the history
Cleaned up the `URLSearchParams`'s `this` validation to increase
readability by moving them to an extra helper function.

PR-URL: #39656
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
  • Loading branch information
VoltrexKeyva authored and danielleadams committed Aug 16, 2021
1 parent 2751cdf commit cc08d30
Showing 1 changed file with 23 additions and 24 deletions.
47 changes: 23 additions & 24 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,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.
Expand Down Expand Up @@ -248,9 +252,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');
Expand Down Expand Up @@ -285,9 +288,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');
}
Expand All @@ -299,9 +302,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');
}
Expand All @@ -320,9 +323,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');
}
Expand All @@ -338,9 +341,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');
}
Expand All @@ -357,9 +360,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');
}
Expand All @@ -375,9 +378,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');
}
Expand Down Expand Up @@ -462,17 +465,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];
Expand All @@ -490,27 +492,24 @@ 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');
}

// 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]);
}
Expand Down

0 comments on commit cc08d30

Please sign in to comment.