Skip to content

Instantly share code, notes, and snippets.

@johnloy
Last active August 27, 2021 14:42
Show Gist options
  • Save johnloy/9398dc2781d3f77bdf99b18ab55b0d68 to your computer and use it in GitHub Desktop.
Save johnloy/9398dc2781d3f77bdf99b18ab55b0d68 to your computer and use it in GitHub Desktop.
/* 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