Add util.Lines line splitter

pull/53/head
rwxrob 2 years ago
parent ada752394a
commit 39757a5c2a
No known key found for this signature in database
GPG Key ID: 2B9111F33082AE77

@ -0,0 +1,19 @@
package util
import (
"bufio"
"fmt"
"strings"
)
// Lines transforms the input into a string and then divides that string
// up into lines (\r?\n) suitable for functional map operations.
func Lines[T any](in T) []string {
buf := fmt.Sprintf("%v", in)
lines := []string{}
scan := bufio.NewScanner(strings.NewReader(buf))
for scan.Scan() {
lines = append(lines, scan.Text())
}
return lines
}

@ -0,0 +1,20 @@
package util_test
import (
"github.com/rwxrob/bonzai/each"
"github.com/rwxrob/bonzai/util"
)
func ExampleLines() {
buf := `
some
thing
here
mkay
`
each.Print(util.Lines(buf))
// Output:
// something heremkay
}
Loading…
Cancel
Save