diff --git a/constructor.go b/constructor.go index 2453407..b50927b 100644 --- a/constructor.go +++ b/constructor.go @@ -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] { diff --git a/constructor_test.go b/constructor_test.go index b20a144..fe6ff86 100644 --- a/constructor_test.go +++ b/constructor_test.go @@ -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) diff --git a/itertool/itertool.go b/itertool/itertool.go index 5e9426e..5be8468 100644 --- a/itertool/itertool.go +++ b/itertool/itertool.go @@ -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