Skip to content

Instantly share code, notes, and snippets.

@osamaishtiaq
Created January 15, 2024 14:08
Show Gist options
  • Save osamaishtiaq/481f09c982cabcfbfc1ed1b9ccceb51d to your computer and use it in GitHub Desktop.
Save osamaishtiaq/481f09c982cabcfbfc1ed1b9ccceb51d to your computer and use it in GitHub Desktop.
Removes a function call foo'(' along with its closing bracket ')' from a text
function buildFunctionRemoverFunction(functionName) {
return function (text) {
let replacedTxt = text.replaceAll(functionName+"(", "").split("");
let isBracketClosed = true;
for (let i = 0; i < replacedTxt.length-1; i+=1) {
if (replacedTxt[i] === "(") {
isBracketClosed = false;
} else if (replacedTxt[i] === ")" && isBracketClosed === false) {
isBracketClosed = true;
} else if (replacedTxt[i] === ")" && isBracketClosed === true) {
// If i encounter a closing bracket but bracket is already closed then I have to remove it.
replacedTxt = [...replacedTxt.slice(0,i),...replacedTxt.slice(i+1)]
}
}
return replacedTxt.join('');
}
}
/*** Usage
const removeAfterDistinct = buildFunctionRemoverFunction('afterDistinct')
removeAfterDistinct('afterDistinct(field1), afterDistinct(field2)')
// result 'field1, field2'
***/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment