Skip to content

Commit

Permalink
feat: lines from reader iterator shouldn't strip whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
tychoish committed Jan 14, 2024
1 parent b5a1b56 commit 740c84b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
12 changes: 9 additions & 3 deletions constructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,18 +265,24 @@ func (Handlers) StrSliceConcatinate(input []string) Future[string] {
func (Handlers) Stringer(op fmt.Stringer) Future[string] { return op.String }

// Lines provides a fun.Iterator access over the contexts of a
// (presumably plaintext) io.Reader, using the bufio.Scanner. During
// iteration the leading and trailing space is also trimmed.
// (presumably plaintext) io.Reader, using the bufio.Scanner.
func (Handlers) Lines(reader io.Reader) *Iterator[string] {
scanner := bufio.NewScanner(reader)
return Generator(func(ctx context.Context) (string, error) {
if !scanner.Scan() {
return "", ers.Join(io.EOF, scanner.Err())
}
return strings.TrimSpace(scanner.Text()), nil
return scanner.Text(), nil
})
}

// LinesWithSpaceTrimed provides an iterator with access to the
// line-separated content of an io.Reader, line Lines(), but with the
// leading and trailing space trimmed from each line.
func (Handlers) LinesWithSpaceTrimed(reader io.Reader) *Iterator[string] {
return HF.Lines(reader).Transform(Converter(strings.TrimSpace))
}

// Itoa produces a Transform function that converts integers into
// strings.
func (Handlers) Itoa() Transform[int, string] {
Expand Down
2 changes: 1 addition & 1 deletion constructor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func TestHandlers(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var prev string
check.NotError(t, HF.Lines(buf).Observe(func(line string) {
check.NotError(t, HF.LinesWithSpaceTrimed(buf).Observe(func(line string) {
count++
assert.Equal(t, len(line), 64)
assert.NotEqual(t, prev, line)
Expand Down
2 changes: 1 addition & 1 deletion itertool/itertool.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func Monotonic(max int) *fun.Iterator[int] { return fun.HF.Counter(max) }
// documents into objects in the form of an iterator.
func JSON[T any](in io.Reader) *fun.Iterator[T] {
var zero T
return fun.ConvertIterator(fun.HF.Lines(in), fun.ConverterErr(func(in string) (out T, err error) {
return fun.ConvertIterator(fun.HF.LinesWithSpaceTrimed(in), fun.ConverterErr(func(in string) (out T, err error) {
defer func() { err = ers.Join(err, ers.ParsePanic(recover())) }()
if err = json.Unmarshal([]byte(in), &out); err != nil {
return zero, err
Expand Down

0 comments on commit 740c84b

Please sign in to comment.