diff options
author | Sankalp Shubham <sankalp.shubham99@gmail.com> | 2023-05-14 20:05:19 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-14 14:35:19 +0000 |
commit | 9f3aacbc27ac3f377d225ecd51aecb0f3de12566 (patch) | |
tree | 85bc557766df0fcb80d9fbb1cf35fbac60a7eb90 /lib | |
parent | abb1c45af774ae3caaa449ddcbfe44143a98e9c7 (diff) | |
download | node-new-9f3aacbc27ac3f377d225ecd51aecb0f3de12566.tar.gz |
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
PR-URL: https://github.com/nodejs/node/pull/47885
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Debadree Chatterjee <debadree333@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/internal/url.js | 37 |
1 files changed, 28 insertions, 9 deletions
diff --git a/lib/internal/url.js b/lib/internal/url.js index 441a02f045..ccd89830f9 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -435,7 +435,7 @@ class URLSearchParams { } } - delete(name) { + delete(name, value = undefined) { if (typeof this !== 'object' || this === null || !(#searchParams in this)) throw new ERR_INVALID_THIS('URLSearchParams'); @@ -445,12 +445,23 @@ class URLSearchParams { const list = this.#searchParams; name = toUSVString(name); - for (let i = 0; i < list.length;) { - const cur = list[i]; - if (cur === name) { - list.splice(i, 2); - } else { - i += 2; + + if (value !== undefined) { + value = toUSVString(value); + for (let i = 0; i < list.length;) { + if (list[i] === name && list[i + 1] === value) { + list.splice(i, 2); + } else { + i += 2; + } + } + } else { + for (let i = 0; i < list.length;) { + if (list[i] === name) { + list.splice(i, 2); + } else { + i += 2; + } } } if (this.#context) { @@ -495,7 +506,7 @@ class URLSearchParams { return values; } - has(name) { + has(name, value = undefined) { if (typeof this !== 'object' || this === null || !(#searchParams in this)) throw new ERR_INVALID_THIS('URLSearchParams'); @@ -505,11 +516,19 @@ 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) { - return true; + if (value === undefined || list[i + 1] === value) { + return true; + } } } + return false; } |