Skip to content

Commit f1842fb

Browse files
Fix URLSearchParams.keys() returning only unique keys
The WHATWG URL spec states that URLSearchParams.keys() must yield one entry per name-value pair, meaning duplicate names appear multiple times. The previous implementation returned Map.keys() directly, which yields each key only once regardless of how many values are stored under it. This caused keys() to be inconsistent with values() and entries(), both of which already handle duplicates correctly. Updated keys() to use a generator that yields each key once per associated value, matching the approach already used by values() and entries(). Test plan: Added a test asserting that keys() on URLSearchParams with duplicate key entries ('key1', 'key1', 'key2') returns all three names. Changelog: [General] [Fixed] - Fix URLSearchParams.keys() returning only unique keys instead of all name-value pair names
1 parent 8bac1df commit f1842fb

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,14 @@ export class URLSearchParams {
7777
}
7878

7979
keys(): Iterator<string> {
80-
return this._searchParams.keys();
80+
function* generateKeys(params: Map<string, string[]>): Iterator<string> {
81+
for (const [key, values] of params) {
82+
for (let i = 0; i < values.length; i++) {
83+
yield key;
84+
}
85+
}
86+
}
87+
return generateKeys(this._searchParams);
8188
}
8289

8390
values(): Iterator<string> {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ describe('URL', function () {
114114
expect(paramsFromArray.getAll('key1')).toEqual(['value1', 'value2']);
115115
expect(paramsFromArray.get('key2')).toBe('value3');
116116

117+
// keys() should yield one entry per name-value pair, including duplicates
118+
const keysArray = Array.from(paramsFromArray.keys());
119+
expect(keysArray).toEqual(['key1', 'key1', 'key2']);
120+
117121
// Manipulating existing search params in the URL
118122
const urlParams = url.searchParams;
119123
expect(urlParams.get('query')).toBe('testQuery');

0 commit comments

Comments
 (0)