forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Break out URLSearchParams, require typedef for unparsable files
Differential Revision: D60376327
- Loading branch information
1 parent
03c0e5e
commit 2ffde5c
Showing
5 changed files
with
154 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* @flow | ||
*/ | ||
|
||
// Small subset from whatwg-url: https://github.com/jsdom/whatwg-url/tree/master/src | ||
// The reference code bloat comes from Unicode issues with URLs, so those won't work here. | ||
export class URLSearchParams { | ||
_searchParams: Array<Array<string>> = []; | ||
|
||
constructor(params: any) { | ||
if (typeof params === 'object') { | ||
Object.keys(params).forEach(key => this.append(key, params[key])); | ||
} | ||
} | ||
|
||
append(key: string, value: string): void { | ||
this._searchParams.push([key, value]); | ||
} | ||
|
||
delete(name: string): void { | ||
throw new Error('URLSearchParams.delete is not implemented'); | ||
} | ||
|
||
get(name: string): void { | ||
throw new Error('URLSearchParams.get is not implemented'); | ||
} | ||
|
||
getAll(name: string): void { | ||
throw new Error('URLSearchParams.getAll is not implemented'); | ||
} | ||
|
||
has(name: string): void { | ||
throw new Error('URLSearchParams.has is not implemented'); | ||
} | ||
|
||
set(name: string, value: string): void { | ||
throw new Error('URLSearchParams.set is not implemented'); | ||
} | ||
|
||
sort(): void { | ||
throw new Error('URLSearchParams.sort is not implemented'); | ||
} | ||
|
||
// $FlowFixMe[unsupported-syntax] | ||
// $FlowFixMe[missing-local-annot] | ||
[Symbol.iterator]() { | ||
return this._searchParams[Symbol.iterator](); | ||
} | ||
|
||
toString(): string { | ||
if (this._searchParams.length === 0) { | ||
return ''; | ||
} | ||
const last = this._searchParams.length - 1; | ||
return this._searchParams.reduce((acc, curr, index) => { | ||
return ( | ||
acc + | ||
encodeURIComponent(curr[0]) + | ||
'=' + | ||
encodeURIComponent(curr[1]) + | ||
(index === last ? '' : '&') | ||
); | ||
}, ''); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
packages/react-native/Libraries/Blob/URLSearchParams.js.flow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* @flow | ||
*/ | ||
|
||
declare export class URLSearchParams { | ||
_searchParams: Array<Array<string>>; | ||
constructor(params: any): void; | ||
append(key: string, value: string): void; | ||
delete(name: string): void; | ||
get(name: string): void; | ||
getAll(name: string): void; | ||
has(name: string): void; | ||
set(name: string, value: string): void; | ||
sort(): void; | ||
@@iterator: Iterator<Array<string>>; | ||
toString(): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters