Open RafalSkolasinski opened 1 year ago
Yeah related to #7, #14 and #17. I just haven't decided on the matter since it's technically a breaking change.
Could be handled via optional argument? Or maybe another function dedent.DedentN
(or sth with a better ring to it)
Yeah I guess there's a few options 🤔
FWIW, I found this package when I needed a way to write multiline literal strings with leading tabs for unit tests in a readable manner. That an initial newline isn't removed makes this implementation unpleasant to use since I have to write something like this:
Prints(dedent.Dedent(` [
&abc= y
&def= z
&xyz= x
]
`)),
Rather than this (the only difference being the placement of the opening bracket:
Prints(dedent.Dedent(`
[
&abc= y
&def= z
&xyz= x
]
`)),
For the record, the solution I settled on is the obvious one. At the top of the Dedent
function replace
text = whitespaceOnly.ReplaceAllString(text, "")
with
if text[0] == '\n' {
text = whitespaceOnly.ReplaceAllString(text[1:], "")
} else {
text = whitespaceOnly.ReplaceAllString(text, "")
}
Thanks @krader1961 - this seems to work perfectly. I applied that in my fork to keep handy around until fix lands in main repo.
I just append [1:]
after Dedent()
if I don't want the first newline. Like beginning string with \
in Python herestring.
query := dedent.Dedent(`
SELECT *
FROM t1, t2
WHERE t1.col > 2000;
`)[1:]
I just append [1:] after Dedent() if I don't want the first newline.
@bersace Yes, that works but is less efficient and requires the user to explicitly remove the newline prefix. Which, in the context of this package, shouldn't be necessary.
I just append [1:] after Dedent() if I don't want the first newline.
@bersace Yes, that works but is less efficient and requires the user to explicitly remove the newline prefix. Which, in the context of this package, shouldn't be necessary.
Yes, this is a workaround. But I prefer to handle it when producing a string with Dedent rather than presuming any string starting with a \n
is dedented. The latter is an inversion of control. It depends on your use case.
I have the feeling that adding a new function that skips initial new line would be a good compromise preserving behaviour / explicitness to folks that prefer it and nice short-cut for these who want to skip new line.
@lithammer I am happy to put in PR with the change if you think this is a right approach?
Hit this as well. I ended up wrapping it as strings.Trim(dedent.Dedent(s)))
running code from readme does not seem to remove the initial empty line