Skip to content

Commit

Permalink
fix: replace URL.canParse()
Browse files Browse the repository at this point in the history
[URL.canParse][1] was only pretty recently created, and is not
available in many environments.

We can instead wrap a `URL` creation in a `try..catch`, as listed in
MDN. Unfortunately, this is a bit slower, but at least it works almost
everywhere.

[1]: https://developer.mozilla.org/en-US/docs/Web/API/URL/canParse_static

Fixes: braintree#78
  • Loading branch information
aloisklink committed Sep 23, 2024
1 parent cdd33eb commit 64689dc
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ function decodeHtmlCharacters(str: string) {
}

function isValidUrl(url: string): boolean {
return URL.canParse(url);
try {
// We can replace this with URL.canParse() once it's more widely available
new URL(url); // eslint-disable-line no-new -- We're only checking for exceptions
return true;
} catch (error) {
return false;
}
}

function decodeURI(uri: string): string {
Expand Down

0 comments on commit 64689dc

Please sign in to comment.