Skip to content

Commit

Permalink
url: add value argument to has and delete methods
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sankalp1999 committed May 5, 2023
1 parent 0a3f6a9 commit 7cc66e4
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ class URLSearchParams {
}
}

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

Expand All @@ -446,12 +446,28 @@ class URLSearchParams {

const list = this.#searchParams;
name = toUSVString(name);

if (value !== undefined) {
value = toUSVString(value);
}

for (let i = 0; i < list.length;) {
const cur = list[i];
if (cur === name) {
list.splice(i, 2);
if (value !== undefined) {
const key = list[i]
const val = list[i + 1]
if (key === name && val === value) {
list.splice(i, 2);
}
else {
i += 2;
}
} else {
i += 2;
const cur = list[i];
if (cur === name) {
list.splice(i, 2);
} else {
i += 2;
}
}
}
if (this.#context) {
Expand Down Expand Up @@ -496,7 +512,7 @@ class URLSearchParams {
return values;
}

has(name) {
has(name, value) {
if (typeof this !== 'object' || this === null || !(#searchParams in this))
throw new ERR_INVALID_THIS('URLSearchParams');

Expand All @@ -506,8 +522,16 @@ class URLSearchParams {

const list = this.#searchParams;
name = toUSVString(name);

if (value !== undefined) {
value = toUSVString(value);
}

for (let i = 0; i < list.length; i += 2) {
if (list[i] === name) {
if(value !== undefined && list[i] === name && list[i + 1] === value) {
return true;
}
if (value === undefined && list[i] === name) {
return true;
}
}
Expand Down

0 comments on commit 7cc66e4

Please sign in to comment.