split streaming buffers into neat, decoded lines of text
npm install intertext-splitlines
Table of Contents generated with DocToc
- What It Does
- How to Use It
- One-Off Call
- Iterators
- Settings
- Revisions
InterText SplitLines facilitates splitting and assembling buffers into neat, decoded lines of text.
In case you have one or more buffers with textual content, the simplest way to use InterText SplitLines
is to use the splitlines() method which will return a list of strings, each representing one line:
``coffeeFor demonstration, let's assemble a number of buffers with lines
randomly spread all over the place:
buffers = [
"helo"
" there!\nHere "
"come\na few lines\n"
"of text that are\nquite unevenly "
"spread over several\n"
"buffers.", ]
buffers = ( Buffer.from d for d in buffers )
[ 'helo there!',
'Here come',
'a few lines',
'of text that are',
'quite unevenly spread over several',
'buffers.', ]
`
Observe that newline characters will be removed from the output so there's no way to determine whether
the last line did or did not end with a newline; this should be the desired result most of the time. In
the event that a trailing newline should be detectable, pass in an explicit setting:
`coffee`
lines = SL.splitlines { skip_empty_last: false, }, buffers
* whenever you receive a buffer from a stream or other source (such as a NodeJS stream's data event),SL.walk_lines ctx, buffer
call with that data; this returns an iterator over the decoded complete linesSL.flush ctx
in the buffer, if any
* when the stream has ended, there may still be buffered data with any number of lines, so don't forget to
call to receive another iterator over the last line, if any
In JavaScript:
`js`
// for each buffer received, do:
for ( line of SL.walk_lines( ctx, buffer ) )
{ do_something_with( line ) };
// after the last buffer has been received, do:
for ( line of SL.flush( ctx ) )
{ do_something_with( line ) };
In CoffeeScript:
`coffee`for each buffer received, do:
for line from SL.walk_lines ctx, buffer
do_something_with lineafter the last buffer has been received, do:
for line from SL.flush ctx
do_something_with line
* ?splitter
* ?decode
encoding.?skip_empty_last
* —whether to emit an emtpy string as last item when the source endedsplitter
in .?keep_newlines
* —whether to return strings or buffers that end in whateversplitter
is set to. That is abc/def with settings { splitter: '/', keep_newlines: false, } would[ 'abc', 'def', ]
split into , wheras with { keep_newlines: true, }, the result would be [ 'abc/',
'def', ]
* [X] throw out find_first_match(), replace by buffer.indexOf()splitlines()
* [X] do not return lists but iterators
* publish v1.0.0
---------------------------------------------------------------------
* [X] implement skip_empty_last
* [X] implement setting
* publish v1.1.0
* [X] fix treatment of last line when emitting buffers
* publish v1.1.1
---------------------------------------------------------------------
* [X] implement setting keep_newlinesencoding`
* publish v1.2.0
---------------------------------------------------------------------
* [ ] implement
* [ ] make keeping of newlines configurable
* [ ] make sure all relevant line ending conventions are properly honored
* [ ] allow custom line splitter