Skip to content

Commit 2f0c87f

Browse files
committed
add URLSearchParams.prototype.size
1 parent 685d750 commit 2f0c87f

15 files changed

+93
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## Changelog
22
##### Unreleased
3+
- Added `URLSearchParams.prototype.size` getter, [url/734](https://github.com/whatwg/url/pull/734)
34
- Allowed cloning resizable `ArrayBuffer`s in the `structuredClone` polyfill
45
- Fixed wrong export in `/(stable|actual|full)/instance/unshift` entries, [#1207](https://github.com/zloirock/core-js/issues/1207)
56
- Compat data improvements:

ORIGINAL_README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -3213,7 +3213,7 @@ queueMicrotask(() => console.log('called as microtask'));
32133213
```
32143214
32153215
#### `URL` and `URLSearchParams`[⬆](#index)
3216-
[`URL` standard](https://url.spec.whatwg.org/) implementation. Modules [`web.url`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/web.url.js), [`web.url.to-json`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/web.url.to-json.js), [`web.url-search-params`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/web.url-search-params.js).
3216+
[`URL` standard](https://url.spec.whatwg.org/) implementation. Modules [`web.url`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/web.url.js), [`web.url.to-json`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/web.url.to-json.js), [`web.url-search-params`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/web.url-search-params.js), [`web.url-search-params.size`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/web.url-search-params.size.js).
32173217
```js
32183218
class URL {
32193219
constructor(url: string, base?: string);
@@ -3248,6 +3248,7 @@ class URLSearchParams {
32483248
keys(): Iterator<key>;
32493249
values(): Iterator<value>;
32503250
@@iterator(): Iterator<[key, value]>;
3251+
readonly attribute size: number;
32513252
}
32523253
```
32533254
[*CommonJS entry points:*](#commonjs-api)
@@ -3257,7 +3258,7 @@ core-js(-pure)/stable|actual|full/url
32573258
core-js/stable|actual|full/url/to-json
32583259
core-js(-pure)/stable|actual|full/url-search-params
32593260
```
3260-
[*Examples*](https://is.gd/AfIwve):
3261+
[*Examples*](https://tinyurl.com/2fccy7sb):
32613262
```js
32623263
const url = new URL('https://login:[email protected]:8080/foo/bar?a=1&b=2&a=3#fragment');
32633264

@@ -3292,6 +3293,8 @@ params.append('c', 4);
32923293
params.append('a', 2);
32933294
params.sort();
32943295

3296+
console.log(params.size); // => 5
3297+
32953298
for (let [key, value] of params) {
32963299
console.log(key); // => 'a', 'a', 'a', 'b', 'c'
32973300
console.log(value); // => '1', '3', '2', '2', '4'

packages/core-js-compat/src/data.mjs

+2
Original file line numberDiff line numberDiff line change
@@ -2466,6 +2466,8 @@ export const data = {
24662466
node: '10.0',
24672467
safari: '14.0',
24682468
},
2469+
'web.url-search-params.size': {
2470+
},
24692471
};
24702472

24712473
export const renamed = new Map([

packages/core-js-compat/src/modules-by-versions.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -195,5 +195,6 @@ export default {
195195
'esnext.json.raw-json',
196196
'esnext.symbol.is-registered',
197197
'esnext.symbol.is-well-known',
198+
'web.url-search-params.size',
198199
],
199200
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// empty

packages/core-js/internals/url-constructor-detection.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var fails = require('../internals/fails');
22
var wellKnownSymbol = require('../internals/well-known-symbol');
3+
var DESCRIPTORS = require('../internals/descriptors');
34
var IS_PURE = require('../internals/is-pure');
45

56
var ITERATOR = wellKnownSymbol('iterator');
@@ -15,6 +16,7 @@ module.exports = !fails(function () {
1516
result += key + value;
1617
});
1718
return (IS_PURE && !url.toJSON)
19+
|| (!searchParams.size && (IS_PURE || !DESCRIPTORS))
1820
|| !searchParams.sort
1921
|| url.href !== 'http://a/c%20d?a=1&c=3'
2022
|| searchParams.get('c') !== '3'

packages/core-js/modules/web.url-search-params.constructor.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var uncurryThis = require('../internals/function-uncurry-this');
88
var DESCRIPTORS = require('../internals/descriptors');
99
var USE_NATIVE_URL = require('../internals/url-constructor-detection');
1010
var defineBuiltIn = require('../internals/define-built-in');
11+
var defineBuiltInAccessor = require('../internals/define-built-in-accessor');
1112
var defineBuiltIns = require('../internals/define-built-ins');
1213
var setToStringTag = require('../internals/set-to-string-tag');
1314
var createIteratorConstructor = require('../internals/iterator-create-constructor');
@@ -203,7 +204,8 @@ URLSearchParamsState.prototype = {
203204
var URLSearchParamsConstructor = function URLSearchParams(/* init */) {
204205
anInstance(this, URLSearchParamsPrototype);
205206
var init = arguments.length > 0 ? arguments[0] : undefined;
206-
setInternalState(this, new URLSearchParamsState(init));
207+
var state = setInternalState(this, new URLSearchParamsState(init));
208+
if (!DESCRIPTORS) this.length = state.entries.length;
207209
};
208210

209211
var URLSearchParamsPrototype = URLSearchParamsConstructor.prototype;
@@ -215,6 +217,7 @@ defineBuiltIns(URLSearchParamsPrototype, {
215217
validateArgumentsLength(arguments.length, 2);
216218
var state = getInternalParamsState(this);
217219
push(state.entries, { key: $toString(name), value: $toString(value) });
220+
if (!DESCRIPTORS) this.length++;
218221
state.updateURL();
219222
},
220223
// `URLSearchParams.prototype.delete` method
@@ -229,6 +232,7 @@ defineBuiltIns(URLSearchParamsPrototype, {
229232
if (entries[index].key === key) splice(entries, index, 1);
230233
else index++;
231234
}
235+
if (!DESCRIPTORS) this.length = entries.length;
232236
state.updateURL();
233237
},
234238
// `URLSearchParams.prototype.get` method
@@ -290,6 +294,7 @@ defineBuiltIns(URLSearchParamsPrototype, {
290294
}
291295
}
292296
if (!found) push(entries, { key: key, value: val });
297+
if (!DESCRIPTORS) this.length = entries.length;
293298
state.updateURL();
294299
},
295300
// `URLSearchParams.prototype.sort` method
@@ -335,6 +340,16 @@ defineBuiltIn(URLSearchParamsPrototype, 'toString', function toString() {
335340
return getInternalParamsState(this).serialize();
336341
}, { enumerable: true });
337342

343+
// `URLSearchParams.prototype.size` getter
344+
// https://github.com/whatwg/url/pull/734
345+
if (DESCRIPTORS) defineBuiltInAccessor(URLSearchParamsPrototype, 'size', {
346+
get: function size() {
347+
return getInternalParamsState(this).entries.length;
348+
},
349+
configurable: true,
350+
enumerable: true
351+
});
352+
338353
setToStringTag(URLSearchParamsConstructor, URL_SEARCH_PARAMS);
339354

340355
$({ global: true, constructor: true, forced: !USE_NATIVE_URL }, {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
var DESCRIPTORS = require('../internals/descriptors');
3+
var uncurryThis = require('../internals/function-uncurry-this');
4+
var defineBuiltInAccessor = require('../internals/define-built-in-accessor');
5+
6+
var URLSearchParamsPrototype = URLSearchParams.prototype;
7+
var forEach = uncurryThis(URLSearchParamsPrototype.forEach);
8+
9+
// `URLSearchParams.prototype.size` getter
10+
// https://github.com/whatwg/url/pull/734
11+
if (DESCRIPTORS && !('size' in URLSearchParamsPrototype)) {
12+
defineBuiltInAccessor(URLSearchParamsPrototype, 'size', {
13+
get: function size() {
14+
var count = 0;
15+
forEach(this, function () { count++; });
16+
return count;
17+
},
18+
configurable: true,
19+
enumerable: true
20+
});
21+
}

packages/core-js/proposals/url.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
require('../modules/web.url');
33
require('../modules/web.url.to-json');
44
require('../modules/web.url-search-params');
5+
require('../modules/web.url-search-params.size');

packages/core-js/web/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require('../modules/web.timers');
1313
require('../modules/web.url');
1414
require('../modules/web.url.to-json');
1515
require('../modules/web.url-search-params');
16+
require('../modules/web.url-search-params.size');
1617
var path = require('../internals/path');
1718

1819
module.exports = path;
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require('../modules/web.url-search-params');
2+
require('../modules/web.url-search-params.size');
23
var path = require('../internals/path');
34

45
module.exports = path.URLSearchParams;

packages/core-js/web/url.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require('../modules/web.url');
22
require('../modules/web.url.to-json');
33
require('../modules/web.url-search-params');
4+
require('../modules/web.url-search-params.size');
45
var path = require('../internals/path');
56

67
module.exports = path.URL;

tests/compat/tests.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1945,5 +1945,8 @@ GLOBAL.tests = {
19451945
'web.url.to-json': [URL_AND_URL_SEARCH_PARAMS_SUPPORT, function () {
19461946
return URL.prototype.toJSON;
19471947
}],
1948-
'web.url-search-params.constructor': URL_AND_URL_SEARCH_PARAMS_SUPPORT
1948+
'web.url-search-params.constructor': URL_AND_URL_SEARCH_PARAMS_SUPPORT,
1949+
'web.url-search-params.size': [URL_AND_URL_SEARCH_PARAMS_SUPPORT, function () {
1950+
return 'size' in URLSearchParams.prototype;
1951+
}]
19491952
};

tests/unit-global/web.url-search-params.js

+19
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,25 @@ QUnit.test('URLSearchParams#@@iterator', assert => {
850850
if (DESCRIPTORS) assert.true(getOwnPropertyDescriptor(getPrototypeOf(new URLSearchParams()[Symbol.iterator]()), 'next').enumerable, 'enumerable .next');
851851
});
852852

853+
QUnit.test('URLSearchParams#size', assert => {
854+
const params = new URLSearchParams('a=1&b=2&b=3');
855+
assert.true('size' in params);
856+
assert.same(params.size, 3);
857+
858+
if (DESCRIPTORS) {
859+
assert.true('size' in URLSearchParams.prototype);
860+
861+
const { enumerable, configurable, get } = getOwnPropertyDescriptor(URLSearchParams.prototype, 'size');
862+
863+
assert.true(enumerable, 'enumerable');
864+
assert.true(configurable, 'configurable');
865+
866+
if (!NODE) assert.looksNative(get);
867+
868+
assert.throws(() => get.call([]));
869+
}
870+
});
871+
853872
QUnit.test('URLSearchParams#@@toStringTag', assert => {
854873
const params = new URLSearchParams('a=b');
855874
assert.same(({}).toString.call(params), '[object URLSearchParams]');

tests/unit-pure/web.url-search-params.js

+17
Original file line numberDiff line numberDiff line change
@@ -826,3 +826,20 @@ QUnit.test('URLSearchParams#@@iterator', assert => {
826826

827827
if (DESCRIPTORS) assert.true(getOwnPropertyDescriptor(getPrototypeOf(new URLSearchParams()[Symbol.iterator]()), 'next').enumerable, 'enumerable .next');
828828
});
829+
830+
QUnit.test('URLSearchParams#size', assert => {
831+
const params = new URLSearchParams('a=1&b=2&b=3');
832+
assert.true('size' in params);
833+
assert.same(params.size, 3);
834+
835+
if (DESCRIPTORS) {
836+
assert.true('size' in URLSearchParams.prototype);
837+
838+
const { enumerable, configurable, get } = getOwnPropertyDescriptor(URLSearchParams.prototype, 'size');
839+
840+
assert.true(enumerable, 'enumerable');
841+
assert.true(configurable, 'configurable');
842+
843+
assert.throws(() => get.call([]));
844+
}
845+
});

0 commit comments

Comments
 (0)