Skip to content

Commit 1ece4a7

Browse files
jasnelldanielleadams
authored andcommitted
url: implement URLSearchParams size getter
Refs: whatwg/url#734 PR-URL: #46308 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
1 parent 8b636c3 commit 1ece4a7

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

doc/api/url.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,14 @@ console.log(params.toString());
940940
// Prints foo=def&abc=def&xyz=opq
941941
```
942942

943+
#### `urlSearchParams.size`
944+
945+
<!-- YAML
946+
added: REPLACEME
947+
-->
948+
949+
The total number of parameter entries.
950+
943951
#### `urlSearchParams.sort()`
944952

945953
<!-- YAML

lib/internal/url.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,12 @@ class URLSearchParams {
290290
return `${this.constructor.name} {}`;
291291
}
292292

293+
get size() {
294+
if (!isURLSearchParams(this))
295+
throw new ERR_INVALID_THIS('URLSearchParams');
296+
return this[searchParams].length / 2;
297+
}
298+
293299
append(name, value) {
294300
if (!isURLSearchParams(this))
295301
throw new ERR_INVALID_THIS('URLSearchParams');
@@ -525,6 +531,7 @@ ObjectDefineProperties(URLSearchParams.prototype, {
525531
getAll: kEnumerableProperty,
526532
has: kEnumerableProperty,
527533
set: kEnumerableProperty,
534+
size: kEnumerableProperty,
528535
sort: kEnumerableProperty,
529536
entries: kEnumerableProperty,
530537
forEach: kEnumerableProperty,

test/parallel/test-whatwg-url-properties.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ const { URL, URLSearchParams } = require('url');
5454
testMethod(URLSearchParams.prototype, name, methodName);
5555
});
5656

57+
{
58+
const params = new URLSearchParams();
59+
params.append('a', 'b');
60+
params.append('a', 'c');
61+
params.append('b', 'c');
62+
assert.strictEqual(params.size, 3);
63+
}
64+
5765
function stringifyName(name) {
5866
if (typeof name === 'symbol') {
5967
const { description } = name;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy