Skip to content

Commit

Permalink
here.Doc will remove trailing empty lines
Browse files Browse the repository at this point in the history
  • Loading branch information
samlown committed Sep 5, 2023
1 parent 2a58b55 commit 9cb9c85
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion build/schemas/cal/date-time.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"type": "string",
"format": "date-time",
"title": "Date Time",
"description": "Civil date time in simplified ISO format with no time zone\ninformation, for example: 2021-05-26T13:45:00\n"
"description": "Civil date time in simplified ISO format with no time zone\ninformation, for example: 2021-05-26T13:45:00"
}
},
"$comment": "Generated with GOBL v0.55.0"
Expand Down
12 changes: 12 additions & 0 deletions pkg/here/here.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func Doc(raw string) string {

minIndentSize := getMinIndent(lines, skipFirstLine)
lines = removeIndentation(lines, minIndentSize, skipFirstLine)
lines = removeEmptyTailLines(lines)

return strings.Join(lines, "\n")
}
Expand Down Expand Up @@ -104,6 +105,17 @@ func removeIndentation(lines []string, n int, skipFirstLine bool) []string {
return lines
}

// removeEmptyTailLines removes empty lines from the end of the lines array.
func removeEmptyTailLines(lines []string) []string {
for i := len(lines) - 1; i >= 0; i-- {
if lines[i] != "" {
break
}
lines = lines[:i]
}
return lines
}

// Docf returns unindented and formatted string as here-document.
// Formatting is done as for fmt.Printf().
func Docf(raw string, args ...interface{}) string {
Expand Down
8 changes: 4 additions & 4 deletions pkg/here/here_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ var tests = []testCase{
Foo
Bar
`,
"Foo\nBar\n"},
"Foo\nBar"},
{`Foo
Bar`,
"Foo\nBar"},
{`Foo
Bar
`,
"Foo\n\t\nBar\n"}, // Second line contains two tabs.
"Foo\n\t\nBar"}, // Second line contains two tabs.
{`
Foo
Bar
Expand All @@ -42,7 +42,7 @@ var tests = []testCase{
`
Foo
Bar
`, "Foo\nBar\n"},
`, "Foo\nBar"},
{"\n\u3000zenkaku space", "\x80\x80zenkaku space"},
}

Expand Down Expand Up @@ -71,7 +71,7 @@ func TestDocf(t *testing.T) {
`
i := 42
s := "Hello"
expect := "int: 42\nstring: Hello\n"
expect := "int: 42\nstring: Hello"

result := here.Docf(tc, i, s)
if result != expect {
Expand Down

0 comments on commit 9cb9c85

Please sign in to comment.