Skip to content

Commit

Permalink
Merge pull request #2846 from alexandear/refactor/prefix-string
Browse files Browse the repository at this point in the history
refactor: Improve textutil.PrefixString
  • Loading branch information
jandubois authored Nov 5, 2024
2 parents efaec2c + e26d192 commit dab750e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
12 changes: 5 additions & 7 deletions pkg/textutil/textutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,13 @@ func ExecuteTemplate(tmpl string, args interface{}) ([]byte, error) {

// PrefixString adds prefix to beginning of each line.
func PrefixString(prefix, text string) string {
result := []string{}
for _, line := range strings.Split(text, "\n") {
if line == "" {
result = append(result, "")
continue
lines := strings.Split(text, "\n")
for i, line := range lines {
if line != "" {
lines[i] = prefix + line
}
result = append(result, prefix+line)
}
return strings.Join(result, "\n")
return strings.Join(lines, "\n")
}

// IndentString add spaces to beginning of each line.
Expand Down
3 changes: 3 additions & 0 deletions pkg/textutil/textutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import (
)

func TestPrefixString(t *testing.T) {
assert.Equal(t, "", PrefixString("- ", ""))
assert.Equal(t, "\n", PrefixString("- ", "\n"))
assert.Equal(t, "- foo", PrefixString("- ", "foo"))
assert.Equal(t, "- foo\n- bar\n", PrefixString("- ", "foo\nbar\n"))
assert.Equal(t, "- foo\n\n- bar\n", PrefixString("- ", "foo\n\nbar\n"))
}

func TestIndentString(t *testing.T) {
Expand Down

0 comments on commit dab750e

Please sign in to comment.