I am trying to write a UDF to fix grammatical errors in the output of other macros. It's a simple find and replace function that is basically just a wrapper for the replace() function. When I feed it a string (a whole sentence including spaces), it finds and replaces errors as expected. When I feed it a string list with a " " separator (the same sentence, but produced by another UDF), the grammatical errors go unchanged.
I don't know if this is a bug or if I'm just doing something wrong. I just want to be able to feed it a sentence, whatever the source, and have it find and replace substrings within that sentence.
When I pass a sentence directly to my edit() function in chat:
Code: Select all
[r: edit("I want a iron sword .")]
Code: Select all
I want a iron sword . // string passed to edit() before replace()
{"a iron":"an iron"} // json object {"error":"correct"}
I want an iron sword . // string after "error" has been replaced by "correct"
Username: // (no output)
When I pass a sentence to a test() function that in turn passes it to edit() as a space-separated string list:
Code: Select all
[r: test("I want a iron sword .")]
Code: Select all
I want a iron sword . // string passed to test() before transformation
I want a iron sword . // string passed to test() after transformation
I want a iron sword . // string passed to edit() before replace()
{"a iron":"an iron"} // json object {"error":"correct"}
I want a iron sword . // string after "error" should have been replaced by "correct"
Username: // (no output)
Code: Select all
[h: line = arg(0)]
[broadcast(line)]
[h: tweaks = getLibProperty("tweaks")]
[broadcast(tweaks)]
[h,forEach(fix, tweaks): line = replace(line, "\\Q" + fix + "\\E", json.get(tweaks,fix))]
[broadcast(line)]
[h: macro.return = line]
My test() function that transforms the string with spaces into a space-separated string list and passes it to edit():
Code: Select all
[h: res = ""]
[broadcast(arg(0))]
[h,forEach(word,arg(0)," "," "): res = listAppend(res,word," ")]
[broadcast(res)]
[h: macro.return = edit(res)]
Also, passing the string list through string() first did not work, nor did passing it through replace() ahead of time to replace all whitespace with regular spaces. I don't know what the difference is under the hood between a sting with spaces vs a space-separated string list, but the two are identical to the end user so I don't even know the exact nature of what I am trying to fix here.