mirror of
https://github.com/PR0M3TH3AN/bitvid.git
synced 2026-11-06 12:52:26 +00:00
refactor: centralize safe decode helpers
This commit is contained in:
1 parent
fb0c2a31ea
commit
c2c7f6445d
5 files changed
+115
-48
No files matched your search
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user