Skip to content

Commit 9b1a8ff

Browse files
louixfacebook-github-bot
authored andcommitted
Implement size for URLSearchParams (#51507)
Summary: The current `URLSearchParams` is missing the readonly `size` property defined in the [whatwg spec](https://url.spec.whatwg.org/#interface-urlsearchparams), and attempting to use it returns `undefined`. This PR adds it. ## Changelog: [GENERAL] [FIXED] - Added size property to URLSearchParams implementation <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: [ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests Pull Request resolved: #51507 Test Plan: I've modified the [tests](packages/react-native/Libraries/Blob/__tests__/URL-test.js) to assert the `size` property is correct. Reviewed By: cipolleschi Differential Revision: D75205273 Pulled By: rshest fbshipit-source-id: 4b773dfc95693a5e084663258de50d161d6facff
1 parent 50ec74e commit 9b1a8ff

File tree

4 files changed

+13
-0
lines changed

4 files changed

+13
-0
lines changed

packages/react-native/Libraries/Blob/URLSearchParams.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
export class URLSearchParams {
1414
_searchParams: Map<string, string[]> = new Map();
1515

16+
get size(): number {
17+
return this._searchParams.size;
18+
}
19+
1620
constructor(params?: Record<string, string> | string | [string, string][]) {
1721
if (params === null) {
1822
return;

packages/react-native/Libraries/Blob/URLSearchParams.js.flow

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
declare export class URLSearchParams {
1212
_searchParams: Array<[string, string]>;
13+
+size: number;
1314
constructor(
1415
params?: Record<string, string> | string | Array<[string, string]>,
1516
): void;

packages/react-native/Libraries/Blob/__tests__/URL-test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ describe('URL', function () {
5959

6060
// Test searchParams
6161
const searchParams = url.searchParams;
62+
expect(searchParams.size).toBe(2);
6263
expect(searchParams.get('query')).toBe('testQuery');
6364
expect(searchParams.get('key')).toBe('value');
6465

@@ -69,6 +70,7 @@ describe('URL', function () {
6970
'&param3=value3+with+spaces+legacy',
7071
].join(''),
7172
);
73+
expect(paramsFromString.size).toBe(3);
7274
expect(paramsFromString.get('param1')).toBe('value1');
7375
expect(paramsFromString.get('param2')).toBe('value2 with spaces');
7476
expect(paramsFromString.get('param3')).toBe('value3 with spaces legacy');
@@ -82,6 +84,7 @@ describe('URL', function () {
8284
active: 'true',
8385
});
8486

87+
expect(paramsFromObject.size).toBe(3);
8588
expect(paramsFromObject.get('user')).toBe('john');
8689
expect(paramsFromObject.get('age')).toBe('30');
8790
expect(paramsFromObject.get('active')).toBe('true');
@@ -97,6 +100,7 @@ describe('URL', function () {
97100

98101
// URLSearchParams: Empty
99102
const emptyParams = new URLSearchParams('');
103+
expect(emptyParams.size).toBe(0);
100104
expect([...emptyParams.entries()]).toEqual([]);
101105

102106
// URLSearchParams: Array (for multiple values of the same key)
@@ -105,6 +109,7 @@ describe('URL', function () {
105109
['key1', 'value2'],
106110
['key2', 'value3'],
107111
]);
112+
expect(paramsFromArray.size).toBe(2);
108113
expect(paramsFromArray.getAll('key1')).toEqual(['value1', 'value2']);
109114
expect(paramsFromArray.get('key2')).toBe('value3');
110115

@@ -115,10 +120,12 @@ describe('URL', function () {
115120

116121
// Adding a new param
117122
urlParams.append('newKey', 'newValue');
123+
expect(urlParams.size).toBe(3);
118124
expect(urlParams.get('newKey')).toBe('newValue');
119125

120126
// Deleting a param
121127
urlParams.delete('key');
128+
expect(urlParams.size).toBe(2);
122129
expect(urlParams.get('key')).toBeNull();
123130

124131
// Checking if a param exists

packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,6 +1343,7 @@ declare export class URL {
13431343

13441344
exports[`public API should not change unintentionally Libraries/Blob/URLSearchParams.js.flow 1`] = `
13451345
"declare export class URLSearchParams {
1346+
+size: number;
13461347
constructor(
13471348
params?: Record<string, string> | string | Array<[string, string]>
13481349
): void;

0 commit comments

Comments
 (0)