Skip to content

Commit 404f442

Browse files
authored
lib: cleanup instance validation
Cleaned up the `URLSearchParams`'s `this` validation to increase readability by moving them to an extra helper function.
1 parent fe3c5a7 commit 404f442

File tree

1 file changed

+23
-36
lines changed

1 file changed

+23
-36
lines changed

lib/internal/url.js

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ class URLContext {
153153
}
154154
}
155155

156+
function isURLSearchParams(self) {
157+
if (!self || !self[searchParams] || self[searchParams][searchParams])
158+
throw new ERR_INVALID_THIS('URLSearchParams');
159+
}
160+
156161
class URLSearchParams {
157162
// URL Standard says the default value is '', but as undefined and '' have
158163
// the same result, undefined is used to prevent unnecessary parsing.
@@ -222,9 +227,7 @@ class URLSearchParams {
222227
}
223228

224229
[inspect.custom](recurseTimes, ctx) {
225-
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
226-
throw new ERR_INVALID_THIS('URLSearchParams');
227-
}
230+
isURLSearchParams(this);
228231

229232
if (typeof recurseTimes === 'number' && recurseTimes < 0)
230233
return ctx.stylize('[Object]', 'special');
@@ -259,9 +262,8 @@ class URLSearchParams {
259262
}
260263

261264
append(name, value) {
262-
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
263-
throw new ERR_INVALID_THIS('URLSearchParams');
264-
}
265+
isURLSearchParams(this);
266+
265267
if (arguments.length < 2) {
266268
throw new ERR_MISSING_ARGS('name', 'value');
267269
}
@@ -273,9 +275,8 @@ class URLSearchParams {
273275
}
274276

275277
delete(name) {
276-
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
277-
throw new ERR_INVALID_THIS('URLSearchParams');
278-
}
278+
isURLSearchParams(this);
279+
279280
if (arguments.length < 1) {
280281
throw new ERR_MISSING_ARGS('name');
281282
}
@@ -294,9 +295,8 @@ class URLSearchParams {
294295
}
295296

296297
get(name) {
297-
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
298-
throw new ERR_INVALID_THIS('URLSearchParams');
299-
}
298+
isURLSearchParams(this);
299+
300300
if (arguments.length < 1) {
301301
throw new ERR_MISSING_ARGS('name');
302302
}
@@ -312,9 +312,8 @@ class URLSearchParams {
312312
}
313313

314314
getAll(name) {
315-
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
316-
throw new ERR_INVALID_THIS('URLSearchParams');
317-
}
315+
isURLSearchParams(this);
316+
318317
if (arguments.length < 1) {
319318
throw new ERR_MISSING_ARGS('name');
320319
}
@@ -331,9 +330,8 @@ class URLSearchParams {
331330
}
332331

333332
has(name) {
334-
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
335-
throw new ERR_INVALID_THIS('URLSearchParams');
336-
}
333+
isURLSearchParams(this);
334+
337335
if (arguments.length < 1) {
338336
throw new ERR_MISSING_ARGS('name');
339337
}
@@ -349,9 +347,8 @@ class URLSearchParams {
349347
}
350348

351349
set(name, value) {
352-
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
353-
throw new ERR_INVALID_THIS('URLSearchParams');
354-
}
350+
isURLSearchParams(this);
351+
355352
if (arguments.length < 2) {
356353
throw new ERR_MISSING_ARGS('name', 'value');
357354
}
@@ -436,17 +433,13 @@ class URLSearchParams {
436433
// Define entries here rather than [Symbol.iterator] as the function name
437434
// must be set to `entries`.
438435
entries() {
439-
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
440-
throw new ERR_INVALID_THIS('URLSearchParams');
441-
}
436+
isURLSearchParams(this);
442437

443438
return createSearchParamsIterator(this, 'key+value');
444439
}
445440

446441
forEach(callback, thisArg = undefined) {
447-
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
448-
throw new ERR_INVALID_THIS('URLSearchParams');
449-
}
442+
isURLSearchParams(this);
450443
validateCallback(callback);
451444

452445
let list = this[searchParams];
@@ -464,27 +457,21 @@ class URLSearchParams {
464457

465458
// https://heycam.github.io/webidl/#es-iterable
466459
keys() {
467-
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
468-
throw new ERR_INVALID_THIS('URLSearchParams');
469-
}
460+
isURLSearchParams(this);
470461

471462
return createSearchParamsIterator(this, 'key');
472463
}
473464

474465
values() {
475-
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
476-
throw new ERR_INVALID_THIS('URLSearchParams');
477-
}
466+
isURLSearchParams(this);
478467

479468
return createSearchParamsIterator(this, 'value');
480469
}
481470

482471
// https://heycam.github.io/webidl/#es-stringifier
483472
// https://url.spec.whatwg.org/#urlsearchparams-stringification-behavior
484473
toString() {
485-
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
486-
throw new ERR_INVALID_THIS('URLSearchParams');
487-
}
474+
isURLSearchParams(this);
488475

489476
return serializeParams(this[searchParams]);
490477
}

0 commit comments

Comments
 (0)