Created
June 2, 2024 19:30
-
-
Save Akronae/a66dfc56102646f32d50ad5c06e1160c to your computer and use it in GitHub Desktop.
Clean/sanitize a JSON file by removing comments and trailing commas.
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 cleanTsConfig(content: string) { | |
return ( | |
content | |
// Remove comments | |
.replace( | |
/(?:\r\n|\n|^)(?:[^'"])*?(?:'(?:[^\r\n\\']|\\'|[\\]{2})*'|"(?:[^\r\n\\"]|\\"|[\\]{2})*")*?(?:[^'"])*?(\/\*(?:[\s\S]*?)\*\/|\/\/.*)/g, | |
"" | |
) | |
// Remove trailing commas | |
.replace(/,(\s+\])/gm, "$1") | |
.replace(/,(\s+\})/gm, "$1") | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment