Skip to content

Instantly share code, notes, and snippets.

@gesielrosa
Created December 1, 2020 22:53
Show Gist options
  • Save gesielrosa/2a4efc42de68016c4b16f9118e0cbf21 to your computer and use it in GitHub Desktop.
Save gesielrosa/2a4efc42de68016c4b16f9118e0cbf21 to your computer and use it in GitHub Desktop.
/**
* Used to guarantee an error-free conversion of a string to JSON object.
* @param str: string
* @return any
*/
export function safeJSONParse(str: string): any {
try {
return JSON.parse(str);
} catch (_) {
return null;
}
}
/**
* Used to clone an object
* Note: This does not guarantee a good performance for a large number of conversions!
* @param obj: any
* @return any
*/
export function deepClone(obj: any): any {
return safeJSONParse(JSON.stringify(obj));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment