Created
December 1, 2020 22:53
-
-
Save gesielrosa/2a4efc42de68016c4b16f9118e0cbf21 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
/** | |
* 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