Skip to content

Commit 7cc66e4

Browse files
committed
url: add value argument to has and delete methods
The change aims to add value argument to two methods of URLSearchParams class i.e the has method and the delete method. For has method, if value argument is provided, then use it to check for presence. For delete method, if value argument provided, use it to delete. Fixes: #47883
1 parent 0a3f6a9 commit 7cc66e4

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

lib/internal/url.js

+31-7
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ class URLSearchParams {
436436
}
437437
}
438438

439-
delete(name) {
439+
delete(name, value) {
440440
if (typeof this !== 'object' || this === null || !(#searchParams in this))
441441
throw new ERR_INVALID_THIS('URLSearchParams');
442442

@@ -446,12 +446,28 @@ class URLSearchParams {
446446

447447
const list = this.#searchParams;
448448
name = toUSVString(name);
449+
450+
if (value !== undefined) {
451+
value = toUSVString(value);
452+
}
453+
449454
for (let i = 0; i < list.length;) {
450-
const cur = list[i];
451-
if (cur === name) {
452-
list.splice(i, 2);
455+
if (value !== undefined) {
456+
const key = list[i]
457+
const val = list[i + 1]
458+
if (key === name && val === value) {
459+
list.splice(i, 2);
460+
}
461+
else {
462+
i += 2;
463+
}
453464
} else {
454-
i += 2;
465+
const cur = list[i];
466+
if (cur === name) {
467+
list.splice(i, 2);
468+
} else {
469+
i += 2;
470+
}
455471
}
456472
}
457473
if (this.#context) {
@@ -496,7 +512,7 @@ class URLSearchParams {
496512
return values;
497513
}
498514

499-
has(name) {
515+
has(name, value) {
500516
if (typeof this !== 'object' || this === null || !(#searchParams in this))
501517
throw new ERR_INVALID_THIS('URLSearchParams');
502518

@@ -506,8 +522,16 @@ class URLSearchParams {
506522

507523
const list = this.#searchParams;
508524
name = toUSVString(name);
525+
526+
if (value !== undefined) {
527+
value = toUSVString(value);
528+
}
529+
509530
for (let i = 0; i < list.length; i += 2) {
510-
if (list[i] === name) {
531+
if(value !== undefined && list[i] === name && list[i + 1] === value) {
532+
return true;
533+
}
534+
if (value === undefined && list[i] === name) {
511535
return true;
512536
}
513537
}

0 commit comments

Comments
 (0)