Reduce function calls in a string, using a callback
Professionally supported reduce-function-call is now available
npm install reduce-function-callvarreduceFunctionCall=require("reduce-function-call")reduceFunctionCall("foo(1)","foo",function(body){// body === "1"returnparseInt(body,10)+1})// "2"varnothingOrUpper=function(body,functionIdentifier){// ignore empty valueif(body===""){returnfunctionIdentifier+"()"}returnbody.toUpperCase()}reduceFunctionCall("bar()","bar",nothingOrUpper)// "bar()"reduceFunctionCall("upper(baz)","upper",nothingOrUpper)// "BAZ"reduceFunctionCall("math(math(2 + 2) * 4 + math(2 + 2)) and other things","math",function(body,functionIdentifier,call){try{returneval(body)}catch(e){returncall}})// "20 and other things"reduceFunctionCall("sha bla blah() blaa bla() abla() aabla() blaaa()",/\b([a-z]?bla[a-z]?)\(/,function(body,functionIdentifier){if(functionIdentifier==="bla"){return"ABRACADABRA"}returnfunctionIdentifier.replace("bla","!")}// "sha bla !h blaa ABRACADABRA a! aabla() blaaa()"See unit tests for others examples.
Work on a branch, install dev-dependencies, respect coding style & run tests before submitting a bug fix or a feature.
git clone https://github.com/MoOx/reduce-function-call.gitgit checkout -b patch-1npm installnpm testTo report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.