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: nodejs#47883
  • Loading branch information
sankalp1999 committed May 8, 2023
1 parent 0b3fcfc commit 7d99ffc
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 @@ -435,7 +435,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 @@ -445,12 +445,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 @@ -495,7 +511,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 @@ -505,8 +521,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 7d99ffc

Please sign in to comment.