mirror of https://github.com/rwxrob/bonzai
Add util.Lines line splitter
parent
ada752394a
commit
39757a5c2a
@ -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…
Reference in New Issue