Last active
July 26, 2018 03:19
-
-
Save ttchengcheng/ddc01d4245527ca58bef7c31e462bc97 to your computer and use it in GitHub Desktop.
Commonly used Javascript snippet
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
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