diff --git a/dist/js/shared/str.d.ts b/dist/js/shared/str.d.ts index 1c52214..d3f91f9 100644 --- a/dist/js/shared/str.d.ts +++ b/dist/js/shared/str.d.ts @@ -7,7 +7,7 @@ export function removeNewLinesAndExtraSpaces(str: any): any; export function randomAlphanumeric(length: number): string; export function toFixedLocale(number: any, precision: any): any; /** - * @summary Removes lines started with # character. Shebang (#!) is excluded. + * @summary Removes comments from a given source code text based on the specified programming language. * @param text {String} text to remove comments from. * @param language {String} programming language of the text. * @return {String} diff --git a/dist/js/shared/str.js b/dist/js/shared/str.js index f2075a6..fdf859a 100644 --- a/dist/js/shared/str.js +++ b/dist/js/shared/str.js @@ -40,16 +40,19 @@ function toFixedLocale(number, precision) { } exports.toFixedLocale = toFixedLocale; /** - * @summary Removes lines started with # character. Shebang (#!) is excluded. + * @summary Removes comments from a given source code text based on the specified programming language. * @param text {String} text to remove comments from. * @param language {String} programming language of the text. * @return {String} */ function removeCommentsFromSourceCode(text, language = "shell") { + var _a; const regexList = { - shell: /^(\s+)?#(?!!).*$/gm, + shell: /#(?!!).*$/gm, + fortran: /!.*$/gm, + python: /#.*$/gm, }; - return text.replace(regexList[language], ""); + return text.replace((_a = regexList[language]) !== null && _a !== void 0 ? _a : regexList.shell, ""); } exports.removeCommentsFromSourceCode = removeCommentsFromSourceCode; /** diff --git a/src/js/shared/str.js b/src/js/shared/str.js index ab8c707..bff0800 100644 --- a/src/js/shared/str.js +++ b/src/js/shared/str.js @@ -37,16 +37,18 @@ export function toFixedLocale(number, precision) { } /** - * @summary Removes lines started with # character. Shebang (#!) is excluded. + * @summary Removes comments from a given source code text based on the specified programming language. * @param text {String} text to remove comments from. * @param language {String} programming language of the text. * @return {String} */ export function removeCommentsFromSourceCode(text, language = "shell") { const regexList = { - shell: /^(\s+)?#(?!!).*$/gm, + shell: /#(?!!).*$/gm, + fortran: /!.*$/gm, + python: /#.*$/gm, }; - return text.replace(regexList[language], ""); + return text.replace(regexList[language] ?? regexList.shell, ""); } /** diff --git a/src/py/mat3ra/utils/string.py b/src/py/mat3ra/utils/string.py index 4d75756..6bb4ccd 100644 --- a/src/py/mat3ra/utils/string.py +++ b/src/py/mat3ra/utils/string.py @@ -60,8 +60,14 @@ def snake_to_camel(snake_case_str: str) -> str: def remove_comments_from_source_code(text: str, language: str = "shell") -> str: - """Removes lines starting with # (except shebang).""" - return re.sub(r"^(\s+)?#(?!!).*$", "", text, flags=re.MULTILINE) + """Removes comments from source code based on the language. + TODO: consider preserving values enclosed in quotes + """ + if language == "fortran": + return re.sub(r"!.*$", "", text, flags=re.MULTILINE) + if language == "python": + return re.sub(r"#.*$", "", text, flags=re.MULTILINE) + return re.sub(r"#(?!!).*$", "", text, flags=re.MULTILINE) def remove_empty_lines_from_string(text: str) -> str: diff --git a/tests/py/unit/test_hash.py b/tests/py/unit/test_hash.py index f8430cf..79661d4 100644 --- a/tests/py/unit/test_hash.py +++ b/tests/py/unit/test_hash.py @@ -39,16 +39,13 @@ class Model(BaseModel): def test_remove_timestampable_keys(): - assert remove_timestampable_keys( - {"a": 1, "createdAt": "x", "updatedAt": "y", "removedAt": "z"} - ) == {"a": 1} + assert remove_timestampable_keys({"a": 1, "createdAt": "x", "updatedAt": "y", "removedAt": "z"}) == {"a": 1} def test_comment_and_empty_line_stripping_matches_js(): text = "# comment\n\nx=1\n # indented\n#!/bin/bash\n echo hi # inline\n" without_comments = remove_comments_from_source_code(text) assert "#!/" in without_comments # shebang preserved - assert "echo hi # inline" in without_comments # inline comment preserved assert "comment" not in without_comments - assert remove_empty_lines_from_string(without_comments) == "x=1\n#!/bin/bash\n echo hi # inline" + assert remove_empty_lines_from_string(without_comments) == "x=1\n#!/bin/bash\n echo hi"