chore: improve trim empty lines
This commit is contained in:
parent
c0d76a7918
commit
892057bb7f
2 changed files with 25 additions and 0 deletions
|
|
@ -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")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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`)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue