I have an R function which is similar to this:
logFun <- function(msg1, msg2){
return(sprintf("%s: %s", msg1, msg2))
}
logFun("123", "456")
It's used in a lot of places, and not always entered nicely e.g. these are all example use cases:
var1 <- "aa"
var2 <- "bb"
logFun(var1, var2)
logFun("aa", var2)
logFun("aa", "bb")
logFun(var1, "bb")
logFun(msg1 = "aa", msg2 = "bb")
logFun(msg1 = var1, msg2 = "bb")
...
Say I had a new function e.g.
logFun2 <- function(msg1, msg2, type){
return(sprintf("[%s] %s: %s", type, msg1, msg2))
}
logFun2("123", "456", "bug")
and the function calls have to be preserved (msg1, msg2, type)
, as many have been converted from logFun
to logFun2
. What I am trying to do with Sublime is to replace the list of use cases above with the following:
logFun2(var1, var2, type = "bug")
logFun2("aa", var2, type = "bug")
logFun2("aa", "bb", type = "bug")
logFun2(var1, "bb", type = "bug")
logFun2(msg1 = "aa", msg2 = "bb", type = "bug")
logFun2(msg1 = var1, msg2 = "bb", type = "bug")
I've read some bits around lookbehind matching, but finding it tricky to find out if its a doable job, or whether I should find another solution to the problem. With https://regex101.com/, I've managed to isolate the logFun(
part with (?<=logFun\()
, but not sure where to go from here. Any guidance would be appreciated :)
Thanks, Jonny
Edit 1
Q: Why cannot you define your logFun2
function with default value for argument type = "bug"
and just replace logFun
with logFun2
?
A: In reality, we don't just have logFun
. We say have functions logBug
, logInfo
, logWarning
etc. We are then changing these in to a singular function logGeneral
e.g. logBug
becomes logGeneral(.., .., type = "bug")
.
So, yep you are right we could for one type of log specify the default and not worry about that one.