Skip to content

Commit

Permalink
url: implement URLSearchParams size getter
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
jasnell committed Feb 25, 2023
1 parent 68b4092 commit c1178c5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
8 changes: 8 additions & 0 deletions doc/api/url.md
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,14 @@ console.log(params.toString());
// Prints foo=def&abc=def&xyz=opq
```

#### `urlSearchParams.size`

<!-- YAML
added: REPLACEME
-->

The total number of parameter entries.

#### `urlSearchParams.sort()`

<!-- YAML
Expand Down
7 changes: 7 additions & 0 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,12 @@ class URLSearchParams {
return `${this.constructor.name} {}`;
}

get size() {
if (!isURLSearchParams(this))
throw new ERR_INVALID_THIS('URLSearchParams');
return this[searchParams].length / 2;
}

append(name, value) {
if (!isURLSearchParams(this))
throw new ERR_INVALID_THIS('URLSearchParams');
Expand Down Expand Up @@ -511,6 +517,7 @@ ObjectDefineProperties(URLSearchParams.prototype, {
getAll: kEnumerableProperty,
has: kEnumerableProperty,
set: kEnumerableProperty,
size: kEnumerableProperty,
sort: kEnumerableProperty,
entries: kEnumerableProperty,
forEach: kEnumerableProperty,
Expand Down
8 changes: 8 additions & 0 deletions test/parallel/test-whatwg-url-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ const { URL, URLSearchParams, format } = require('url');
testMethod(URLSearchParams.prototype, name, methodName);
});

{
const params = new URLSearchParams();
params.append('a', 'b');
params.append('a', 'c');
params.append('b', 'c');
assert.strictEqual(params.size, 3);
}

function stringifyName(name) {
if (typeof name === 'symbol') {
const { description } = name;
Expand Down

0 comments on commit c1178c5

Please sign in to comment.