Skip to content

Commit

Permalink
url: implement URLSearchParams size getter
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell committed Jan 22, 2023
1 parent e487638 commit 5101af7
Show file tree
Hide file tree
Showing 3 changed files with 22 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
6 changes: 6 additions & 0 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,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
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 @@ -54,6 +54,14 @@ const { URL, URLSearchParams } = 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 5101af7

Please sign in to comment.