Created
January 5, 2024 14:31
-
-
Save treytomes/905936947b0863879d6637735f6f7ef3 to your computer and use it in GitHub Desktop.
Split a quoted string around a delimiter.
This file contains 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
import "qa" | |
// Split a quoted string on a delimiter. | |
string.splitQuote = function(delim) | |
a = self.split("""") | |
b = [] | |
for n in a.indexes | |
if n % 2 == 0 then | |
b += a[n].trim.split(delim) | |
else | |
b.push """" + a[n] + """" | |
end if | |
end for | |
return b | |
end function | |
runUnitTests = function | |
delim = " " | |
text = """But what if I spoke before thinking?"" he thought to himself." | |
qa.assertEqual text.splitQuote(delim), [ | |
"""But what if I spoke before thinking?""", | |
"he", | |
"thought", | |
"to", | |
"himself.", | |
] | |
text = "He said, ""But what did you think?"", but no one was listening." | |
qa.assertEqual text.splitQuote(delim), [ | |
"He", | |
"said,", | |
"""But what did you think?""", | |
",", | |
"but", | |
"no", | |
"one", | |
"was", | |
"listening.", | |
] | |
end function | |
runUnitTests |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment