From 7d99ffc3f6f893bcee0deae22f804578fa85cb27 Mon Sep 17 00:00:00 2001 From: sankalp1999 Date: Sat, 6 May 2023 00:28:04 +0530 Subject: [PATCH] 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: https://github.com/nodejs/node/issues/47883 --- lib/internal/url.js | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/lib/internal/url.js b/lib/internal/url.js index 441a02f0455df4..924168f1e86f34 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -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; } }