Files

5 lines
245 B
JavaScript
Raw Permalink Normal View History

// Safe JSON.parse: non-string passthrough; on parse error return caller-chosen `fallback`.
export function safeParseJSON(str, fallback) {
if (typeof str !== "string") return str;
try { return JSON.parse(str); } catch { return fallback; }
}