Created
August 20, 2024 10:51
-
-
Save pemre/03d06b27ecbc832c527982a2850d8f49 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const safeJsonParse = (str) => { | |
try { | |
return [null, JSON.parse(str)]; | |
} catch (err) { | |
return [err]; | |
} | |
}; | |
// EXAMPLE USAGE | |
const checkEditorSession = (url, widget) => { | |
if (url && typeof url === "string") { | |
const decodedUrl = decodeURIComponent(url); | |
// console.log("HELLO! PAR", JSON.parse(decodedUrl)); | |
const [err, json] = safeJsonParse(decodedUrl); | |
if (!err) { | |
console.log("Successfully parsed JSON:", json); | |
const jsonStr = JSON.stringify(json, null, 2); | |
get().setContents({ contents: jsonStr }); | |
return useJson.setState({ json: jsonStr, loading: false }); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment