Last active
August 27, 2021 14:42
-
-
Save johnloy/9398dc2781d3f77bdf99b18ab55b0d68 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
/* tslint:disable-next-line */ | |
let stringifyObj = function (obj: object): any {}; | |
if (!DEBUG) { | |
stringifyObj = (obj: object): string => { | |
const placeholder = "____PLACEHOLDER____"; | |
const fns: Function[] = []; | |
let json = JSON.stringify( | |
obj, | |
function (_, value: any): any { | |
if (typeof value === "function") { | |
fns.push(value); | |
return placeholder; | |
} | |
return value; | |
}, | |
2 | |
); | |
json = json.replace(new RegExp('"' + placeholder + '"', "g"), (_): string => | |
fns.shift()!.toString() | |
); | |
return json; | |
}; | |
} | |
export default stringifyObj; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment