diff --git a/dist/js/shared/str.d.ts b/dist/js/shared/str.d.ts index bcda43c..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 full-line and inline comments starting with #. 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 a47b9db..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 full-line and inline comments starting with #. 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: /#(?!!).*$/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 b5dd7f9..058b0a1 100644 --- a/src/js/shared/str.js +++ b/src/js/shared/str.js @@ -37,7 +37,7 @@ export function toFixedLocale(number, precision) { } /** - * @summary Removes full-line and inline comments starting with #. 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} @@ -45,10 +45,13 @@ export function toFixedLocale(number, precision) { export function removeCommentsFromSourceCode(text, language = "shell") { const regexList = { shell: /#(?!!).*$/gm, + fortran: /!.*$/gm, + python: /#.*$/gm, }; - return text.replace(regexList[language], ""); + return text.replace(regexList[language] ?? regexList.shell, ""); } + /** * @summary Removes empty lines from a given string. * @param string {String} string to remove empty lines from. diff --git a/src/py/mat3ra/utils/string.py b/src/py/mat3ra/utils/string.py index 7a8b065..6bb4ccd 100644 --- a/src/py/mat3ra/utils/string.py +++ b/src/py/mat3ra/utils/string.py @@ -61,17 +61,13 @@ def snake_to_camel(snake_case_str: str) -> str: def remove_comments_from_source_code(text: str, language: str = "shell") -> str: """Removes comments from source code based on the language. - TODO: consider preserving values enclosed in quotes. support for following cases: - url="https://www.example.com/about#company" - message = "Hello, world!" - var = 2 # it's a comment + TODO: consider preserving values enclosed in quotes """ - patterns = { - "espresso": r"[!#].*$", # ! or # comments - "fortran": r"!.*$", # ! comments only - "shell": r"#(?!!).*$", # # comments (except shebang) - } - return re.sub(patterns.get(language, patterns["shell"]), "", text, flags=re.MULTILINE) + 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: