Skip to content

Commit

Permalink
refactor: Improve textutil.PrefixString
Browse files Browse the repository at this point in the history
Signed-off-by: Oleksandr Redko <[email protected]>
  • Loading branch information
alexandear committed Nov 5, 2024
1 parent 0d058b0 commit e26d192
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 e26d192

Please sign in to comment.