2.0.0 • Published 2 years ago
output-buffer v2.0.0
output-buffer
Buffers your outputs and calls a function when a linebreak is found
Table of contents
Install
$ npm i output-buffer
Buffers your output. When line separators are detected, it calls the function passed to the constructor with the line of data.
Usage
import { OutputBuffer } from 'output-buffer'
const buffer = new OutputBuffer(console.info)
buffer.append('foo')
buffer.append('bar')
buffer.append('\n') // prints 'foobar'
buffer.append('foo')
buffer.append('ba\nr') // prints fooba
buffer.flush() // prints 'r'
Specifying a line separator
The default line separator is the regex /\r\n|\r|\n/
- this will handle CRLF, CR or LF.
A custom separator (string or regex) can be supplied as a second argument to the constructor, e.g.:
const buffer = new OutputBuffer(console.info, '\r\n')
or:
const buffer = new OutputBuffer(console.info, /\r\n/)
Finding the size of the buffer
buffer.size()
will return how many characters have yet to be passed to the callback.
const buffer = new OutputBuffer(console.info, '\r\n')
console.info(buffer.size()) // prints 0
buffer.append('foo')
console.info(buffer.size()) // prints 3
buffer.append('bar/n')
console.info(buffer.size()) // prints 0
License
Licensed under either of
- Apache 2.0, (LICENSE-APACHE / http://www.apache.org/licenses/LICENSE-2.0)
- MIT (LICENSE-MIT / http://opensource.org/licenses/MIT)
Contribute
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.