Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

url: add value argument to has and delete methods #47885

Merged
merged 13 commits into from
May 14, 2023
Merged
Next Next commit
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
sankalp1999 committed May 8, 2023
commit 7d99ffc3f6f893bcee0deae22f804578fa85cb27
38 changes: 31 additions & 7 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
@@ -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');

@@ -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) {
@@ -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');

@@ -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;
}
}