Remove many instances of deprecated url.parse
This commit is contained in:
parent
2fc3e4c698
commit
18abe93022
10 changed files with 220 additions and 89 deletions
35
ts/util/url.ts
Normal file
35
ts/util/url.ts
Normal file
|
@ -0,0 +1,35 @@
|
|||
// Copyright 2021 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import { mapValues } from 'lodash';
|
||||
|
||||
export function maybeParseUrl(value: string): undefined | URL {
|
||||
if (typeof value === 'string') {
|
||||
try {
|
||||
return new URL(value);
|
||||
} catch (err) {
|
||||
/* Errors are ignored. */
|
||||
}
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function setUrlSearchParams(
|
||||
url: Readonly<URL>,
|
||||
searchParams: Readonly<Record<string, unknown>>
|
||||
): URL {
|
||||
const result = cloneUrl(url);
|
||||
result.search = new URLSearchParams(
|
||||
mapValues(searchParams, stringifySearchParamValue)
|
||||
).toString();
|
||||
return result;
|
||||
}
|
||||
|
||||
function cloneUrl(url: Readonly<URL>): URL {
|
||||
return new URL(url.href);
|
||||
}
|
||||
|
||||
function stringifySearchParamValue(value: unknown): string {
|
||||
return value == null ? '' : String(value);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue