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 <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Daijiro Wachi <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: Minwoo Jung <[email protected]>
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

+8
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

+7
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

+8
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)