Skip to content

Commit

Permalink
add URLSearchParams.prototype.size
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Feb 24, 2023
1 parent 685d750 commit 2f0c87f
Show file tree
Hide file tree
Showing 15 changed files with 93 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Changelog
##### Unreleased
- Added `URLSearchParams.prototype.size` getter, [url/734](https://github.com/whatwg/url/pull/734)
- Allowed cloning resizable `ArrayBuffer`s in the `structuredClone` polyfill
- Fixed wrong export in `/(stable|actual|full)/instance/unshift` entries, [#1207](https://github.com/zloirock/core-js/issues/1207)
- Compat data improvements:
Expand Down
7 changes: 5 additions & 2 deletions ORIGINAL_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3213,7 +3213,7 @@ queueMicrotask(() => console.log('called as microtask'));
```
#### `URL` and `URLSearchParams`[⬆](#index)
[`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).
[`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).
```js
class URL {
constructor(url: string, base?: string);
Expand Down Expand Up @@ -3248,6 +3248,7 @@ class URLSearchParams {
keys(): Iterator<key>;
values(): Iterator<value>;
@@iterator(): Iterator<[key, value]>;
readonly attribute size: number;
}
```
[*CommonJS entry points:*](#commonjs-api)
Expand All @@ -3257,7 +3258,7 @@ core-js(-pure)/stable|actual|full/url
core-js/stable|actual|full/url/to-json
core-js(-pure)/stable|actual|full/url-search-params
```
[*Examples*](https://is.gd/AfIwve):
[*Examples*](https://tinyurl.com/2fccy7sb):
```js
const url = new URL('https://login:[email protected]:8080/foo/bar?a=1&b=2&a=3#fragment');

Expand Down Expand Up @@ -3292,6 +3293,8 @@ params.append('c', 4);
params.append('a', 2);
params.sort();

console.log(params.size); // => 5

for (let [key, value] of params) {
console.log(key); // => 'a', 'a', 'a', 'b', 'c'
console.log(value); // => '1', '3', '2', '2', '4'
Expand Down
2 changes: 2 additions & 0 deletions packages/core-js-compat/src/data.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2466,6 +2466,8 @@ export const data = {
node: '10.0',
safari: '14.0',
},
'web.url-search-params.size': {
},
};

export const renamed = new Map([
Expand Down
1 change: 1 addition & 0 deletions packages/core-js-compat/src/modules-by-versions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,6 @@ export default {
'esnext.json.raw-json',
'esnext.symbol.is-registered',
'esnext.symbol.is-well-known',
'web.url-search-params.size',
],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// empty
2 changes: 2 additions & 0 deletions packages/core-js/internals/url-constructor-detection.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var fails = require('../internals/fails');
var wellKnownSymbol = require('../internals/well-known-symbol');
var DESCRIPTORS = require('../internals/descriptors');
var IS_PURE = require('../internals/is-pure');

var ITERATOR = wellKnownSymbol('iterator');
Expand All @@ -15,6 +16,7 @@ module.exports = !fails(function () {
result += key + value;
});
return (IS_PURE && !url.toJSON)
|| (!searchParams.size && (IS_PURE || !DESCRIPTORS))
|| !searchParams.sort
|| url.href !== 'http://a/c%20d?a=1&c=3'
|| searchParams.get('c') !== '3'
Expand Down
17 changes: 16 additions & 1 deletion packages/core-js/modules/web.url-search-params.constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var uncurryThis = require('../internals/function-uncurry-this');
var DESCRIPTORS = require('../internals/descriptors');
var USE_NATIVE_URL = require('../internals/url-constructor-detection');
var defineBuiltIn = require('../internals/define-built-in');
var defineBuiltInAccessor = require('../internals/define-built-in-accessor');
var defineBuiltIns = require('../internals/define-built-ins');
var setToStringTag = require('../internals/set-to-string-tag');
var createIteratorConstructor = require('../internals/iterator-create-constructor');
Expand Down Expand Up @@ -203,7 +204,8 @@ URLSearchParamsState.prototype = {
var URLSearchParamsConstructor = function URLSearchParams(/* init */) {
anInstance(this, URLSearchParamsPrototype);
var init = arguments.length > 0 ? arguments[0] : undefined;
setInternalState(this, new URLSearchParamsState(init));
var state = setInternalState(this, new URLSearchParamsState(init));
if (!DESCRIPTORS) this.length = state.entries.length;
};

var URLSearchParamsPrototype = URLSearchParamsConstructor.prototype;
Expand All @@ -215,6 +217,7 @@ defineBuiltIns(URLSearchParamsPrototype, {
validateArgumentsLength(arguments.length, 2);
var state = getInternalParamsState(this);
push(state.entries, { key: $toString(name), value: $toString(value) });
if (!DESCRIPTORS) this.length++;
state.updateURL();
},
// `URLSearchParams.prototype.delete` method
Expand All @@ -229,6 +232,7 @@ defineBuiltIns(URLSearchParamsPrototype, {
if (entries[index].key === key) splice(entries, index, 1);
else index++;
}
if (!DESCRIPTORS) this.length = entries.length;
state.updateURL();
},
// `URLSearchParams.prototype.get` method
Expand Down Expand Up @@ -290,6 +294,7 @@ defineBuiltIns(URLSearchParamsPrototype, {
}
}
if (!found) push(entries, { key: key, value: val });
if (!DESCRIPTORS) this.length = entries.length;
state.updateURL();
},
// `URLSearchParams.prototype.sort` method
Expand Down Expand Up @@ -335,6 +340,16 @@ defineBuiltIn(URLSearchParamsPrototype, 'toString', function toString() {
return getInternalParamsState(this).serialize();
}, { enumerable: true });

// `URLSearchParams.prototype.size` getter
// https://github.com/whatwg/url/pull/734
if (DESCRIPTORS) defineBuiltInAccessor(URLSearchParamsPrototype, 'size', {
get: function size() {
return getInternalParamsState(this).entries.length;
},
configurable: true,
enumerable: true
});

setToStringTag(URLSearchParamsConstructor, URL_SEARCH_PARAMS);

$({ global: true, constructor: true, forced: !USE_NATIVE_URL }, {
Expand Down
21 changes: 21 additions & 0 deletions packages/core-js/modules/web.url-search-params.size.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';
var DESCRIPTORS = require('../internals/descriptors');
var uncurryThis = require('../internals/function-uncurry-this');
var defineBuiltInAccessor = require('../internals/define-built-in-accessor');

var URLSearchParamsPrototype = URLSearchParams.prototype;
var forEach = uncurryThis(URLSearchParamsPrototype.forEach);

// `URLSearchParams.prototype.size` getter
// https://github.com/whatwg/url/pull/734
if (DESCRIPTORS && !('size' in URLSearchParamsPrototype)) {
defineBuiltInAccessor(URLSearchParamsPrototype, 'size', {
get: function size() {
var count = 0;
forEach(this, function () { count++; });
return count;
},
configurable: true,
enumerable: true
});
}
1 change: 1 addition & 0 deletions packages/core-js/proposals/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
require('../modules/web.url');
require('../modules/web.url.to-json');
require('../modules/web.url-search-params');
require('../modules/web.url-search-params.size');
1 change: 1 addition & 0 deletions packages/core-js/web/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require('../modules/web.timers');
require('../modules/web.url');
require('../modules/web.url.to-json');
require('../modules/web.url-search-params');
require('../modules/web.url-search-params.size');
var path = require('../internals/path');

module.exports = path;
1 change: 1 addition & 0 deletions packages/core-js/web/url-search-params.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require('../modules/web.url-search-params');
require('../modules/web.url-search-params.size');
var path = require('../internals/path');

module.exports = path.URLSearchParams;
1 change: 1 addition & 0 deletions packages/core-js/web/url.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require('../modules/web.url');
require('../modules/web.url.to-json');
require('../modules/web.url-search-params');
require('../modules/web.url-search-params.size');
var path = require('../internals/path');

module.exports = path.URL;
5 changes: 4 additions & 1 deletion tests/compat/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1945,5 +1945,8 @@ GLOBAL.tests = {
'web.url.to-json': [URL_AND_URL_SEARCH_PARAMS_SUPPORT, function () {
return URL.prototype.toJSON;
}],
'web.url-search-params.constructor': URL_AND_URL_SEARCH_PARAMS_SUPPORT
'web.url-search-params.constructor': URL_AND_URL_SEARCH_PARAMS_SUPPORT,
'web.url-search-params.size': [URL_AND_URL_SEARCH_PARAMS_SUPPORT, function () {
return 'size' in URLSearchParams.prototype;
}]
};
19 changes: 19 additions & 0 deletions tests/unit-global/web.url-search-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,25 @@ QUnit.test('URLSearchParams#@@iterator', assert => {
if (DESCRIPTORS) assert.true(getOwnPropertyDescriptor(getPrototypeOf(new URLSearchParams()[Symbol.iterator]()), 'next').enumerable, 'enumerable .next');
});

QUnit.test('URLSearchParams#size', assert => {
const params = new URLSearchParams('a=1&b=2&b=3');
assert.true('size' in params);
assert.same(params.size, 3);

if (DESCRIPTORS) {
assert.true('size' in URLSearchParams.prototype);

const { enumerable, configurable, get } = getOwnPropertyDescriptor(URLSearchParams.prototype, 'size');

assert.true(enumerable, 'enumerable');
assert.true(configurable, 'configurable');

if (!NODE) assert.looksNative(get);

assert.throws(() => get.call([]));
}
});

QUnit.test('URLSearchParams#@@toStringTag', assert => {
const params = new URLSearchParams('a=b');
assert.same(({}).toString.call(params), '[object URLSearchParams]');
Expand Down
17 changes: 17 additions & 0 deletions tests/unit-pure/web.url-search-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -826,3 +826,20 @@ QUnit.test('URLSearchParams#@@iterator', assert => {

if (DESCRIPTORS) assert.true(getOwnPropertyDescriptor(getPrototypeOf(new URLSearchParams()[Symbol.iterator]()), 'next').enumerable, 'enumerable .next');
});

QUnit.test('URLSearchParams#size', assert => {
const params = new URLSearchParams('a=1&b=2&b=3');
assert.true('size' in params);
assert.same(params.size, 3);

if (DESCRIPTORS) {
assert.true('size' in URLSearchParams.prototype);

const { enumerable, configurable, get } = getOwnPropertyDescriptor(URLSearchParams.prototype, 'size');

assert.true(enumerable, 'enumerable');
assert.true(configurable, 'configurable');

assert.throws(() => get.call([]));
}
});

0 comments on commit 2f0c87f

Please sign in to comment.