Files
2025-01-04 00:34:03 +01:00

17 lines
390 B
JavaScript

/**
* Attempt to parse a URL without throwing an error.
* @param {string} url The string to parse.
* @returns {URL|null} The parsed URL if successful, otherwise null.
*/
export function parseSafe(url) {
try {
return new URL(url);
} catch (err) {}
return null;
}
// Define properties on the URL environment
Object.defineProperties(URL, {
parseSafe: {value: parseSafe}
});