chore: improve trim empty lines

This commit is contained in:
andig 2023-02-27 16:15:26 +01:00
parent c0d76a7918
commit 892057bb7f
2 changed files with 25 additions and 0 deletions

View file

@ -4,6 +4,7 @@ import (
"fmt"
"strings"
"github.com/samber/lo"
"gopkg.in/yaml.v3"
)
@ -31,3 +32,13 @@ func yamlQuote(value string) string {
return value
}
func trimEmptyLines(s string) string {
lines := strings.Split(s, "\n")
for i, line := range lines {
if strings.TrimSpace(line) == "" {
lines[i] = ""
}
}
return strings.Join(lo.Compact(lines), "\n")
}

View file

@ -32,3 +32,17 @@ func TestYamlDecodeLeadingZero(t *testing.T) {
t.Fatalf("expected %s, got %s", exp, res)
}
}
func TestTrimEmptyLines(t *testing.T) {
lines := `
foo
bar
buz
`
assert.Equal(t, trimEmptyLines(lines), `foo
bar
buz`)
}