Skip to content

Instantly share code, notes, and snippets.

@trapcodeio
Last active April 26, 2024 04:43
Show Gist options
  • Save trapcodeio/927cf3f382e5ec38518c435817f3ad58 to your computer and use it in GitHub Desktop.
Save trapcodeio/927cf3f382e5ec38518c435817f3ad58 to your computer and use it in GitHub Desktop.
Compare Javascript obj using json
/**
* Compare two objects.
* @param source
* @param compare
*/
export function isSameObjectByJson(source: Record<any, any>, compare: Record<any, any>) {
let s: string, c: string;
try {
s = JSON.stringify(source, null, 0);
} catch (e) {
throw new Error(`isSameObjectByJson cannot convert "source" object to JSON`);
}
try {
c = JSON.stringify(compare, null, 0);
} catch (e) {
throw new Error(`isSameObjectByJson cannot convert "compare" object to JSON`);
}
return s === c;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment