Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/js/shared/str.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
7 changes: 5 additions & 2 deletions dist/js/shared/str.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
/**
Expand Down
7 changes: 5 additions & 2 deletions src/js/shared/str.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,21 @@ 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}
*/
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.
Expand Down
16 changes: 6 additions & 10 deletions src/py/mat3ra/utils/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading