Skip to content

Instantly share code, notes, and snippets.

@ttchengcheng
Last active July 26, 2018 03:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ttchengcheng/ddc01d4245527ca58bef7c31e462bc97 to your computer and use it in GitHub Desktop.
Save ttchengcheng/ddc01d4245527ca58bef7c31e462bc97 to your computer and use it in GitHub Desktop.
Commonly used Javascript snippet
function jsonStr(obj, indent) {
return JSON.stringify(
obj,
// default result of JSON.stringify() for RegExp object is {}
(key, value)=> {
if (value instanceof RegExp) {
return value.toString(); // RegExp.toString() returns string like '/\\.(t|j)sx?$/'
}
return value;
},
// to output a formatted JSON, use the 3rd parameter of JSON.stringify for indenting, usually some spaces or '\t'.
indent ? ' '.repeat(indent) : '');
}
function isRegExp(obj) {
return obj instanceof RegExp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment