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 <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>
  • Loading branch information
jasnell authored and danielleadams committed Apr 5, 2023
1 parent 8b636c3 commit 1ece4a7
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 @@ -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 Expand Up @@ -525,6 +531,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 @@ -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 1ece4a7

Please sign in to comment.