Created
January 15, 2024 14:08
-
-
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
This file contains hidden or 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 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