Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const decodeURL = str => {
if (parse(str).protocol) {
const parsed = new URL(str);
// Exit if input is a data url
if (parsed.origin === 'null') return str;
// TODO: refactor to `url.format()` once Node 8 is dropped
const url = parsed.toString().replace(parsed.hostname, toUnicode(parsed.hostname));
return safeDecodeURI(url);
}
return safeDecodeURI(str);
};
const encodeURL = str => {
if (parse(str).protocol) {
const parsed = new URL(str);
// Exit if input is a data url
if (parsed.origin === 'null') return str;
parsed.search = encodeURI(safeDecodeURI(parsed.search));
// preserve IDN
// TODO: refactor to url.format() once Node 8 EOL
return parsed.toString().replace(parsed.hostname, toUnicode(parsed.hostname));
}
return encodeURI(safeDecodeURI(str));
};