Skip to content

Instantly share code, notes, and snippets.

@gaurangrshah
Created April 12, 2021 18:46
Show Gist options
  • Save gaurangrshah/2f51b9ff670e6898adec2781642c44b1 to your computer and use it in GitHub Desktop.
Save gaurangrshah/2f51b9ff670e6898adec2781642c44b1 to your computer and use it in GitHub Desktop.
export function isValidJson(string) {
/**
* @SCOPE: uses json.parse to validate a string as json
* used by:
* - show-json
*
*/
if (typeof string !== "string") return false;
try {
JSON.parse(string);
return true;
} catch (error) {
return false;
}
}
export function jsonCompare(val1, val2) {
// unused
let compare1, compare2;
const values = [val1, val2];
const comparators = values.map((val) =>
isValidJson(val) ? val : JSON.stringify(val)
);
return comparators[0] === comparators[1];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment