Skip to content

Commit d986f31

Browse files
committed
url: implement URLSearchParams size getter
Refs: whatwg/url#734
1 parent 225c578 commit d986f31

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
@@ -271,6 +271,12 @@ class URLSearchParams {
271271
return `${this.constructor.name} {}`;
272272
}
273273

274+
get size() {
275+
if (!isURLSearchParams(this))
276+
throw new ERR_INVALID_THIS('URLSearchParams');
277+
return this[searchParams].length / 2;
278+
}
279+
274280
append(name, value) {
275281
if (!isURLSearchParams(this))
276282
throw new ERR_INVALID_THIS('URLSearchParams');
@@ -506,6 +512,7 @@ ObjectDefineProperties(URLSearchParams.prototype, {
506512
getAll: kEnumerableProperty,
507513
has: kEnumerableProperty,
508514
set: kEnumerableProperty,
515+
size: kEnumerableProperty,
509516
sort: kEnumerableProperty,
510517
entries: kEnumerableProperty,
511518
forEach: kEnumerableProperty,

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

+8
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ const { URL, URLSearchParams, format } = require('url');
6565
testMethod(URLSearchParams.prototype, name, methodName);
6666
});
6767

68+
{
69+
const params = new URLSearchParams();
70+
params.append('a', 'b');
71+
params.append('a', 'c');
72+
params.append('b', 'c');
73+
assert.strictEqual(params.size, 3);
74+
}
75+
6876
function stringifyName(name) {
6977
if (typeof name === 'symbol') {
7078
const { description } = name;

0 commit comments

Comments
 (0)