refactor: centralize safe decode helpers

This commit is contained in:
thePR0M3TH3AN committed 2025-10-02 12:48:24 -04:00
1 parent fb0c2a31ea
commit c2c7f6445d
5 files changed
+115 -48

No files matched your search

+32
View File
@@ -0,0 +1,32 @@
export function safeDecodeURIComponent(value) {
if (typeof value !== "string") {
return "";
}
if (value === "") {
return "";
}
try {
return decodeURIComponent(value);
} catch (err) {
return value;
}
}
export function safeDecodeURIComponentLoose(value, { trim = true } = {}) {
if (typeof value !== "string") {
return "";
}
const working = trim ? value.trim() : value;
if (trim && !working) {
return "";
}
try {
return decodeURIComponent(working);
} catch (err) {
return working;
}
}