npm.io
3.0.2 • Published 1 year ago

delimiter-stream

Licence
MIT
Version
3.0.2
Deps
0
Size
13 kB
Vulns
0
Weekly
0
Stars
4

delimiter-stream

Build Status

Takes a stream and transforms it into chunks matching the content between delimiters. Useful for analyzing big data sets and file streams on the fly.

Installation

$ npm install delimiter-stream

Usage

Use the stream to split chunks into chunks separated by the delimter either by using the data event or by piping the stream into other streams.

The stream constructor accepts these parameters

  • delimiter delimiter used to split stream (default is \n)
  • encoding encoding of the stream (default is utf8)
Using the data event
var DelimiterStream = require('delimiter-stream');
var StringDecoder = require('string_decoder').StringDecoder;
var decoder = new StringDecoder('utf8');

var delimiterstream = new DelimiterStream({
  delimiter: '|'
});

delimiterstream.on('data', function(chunk) {
  console.log(decoder.write(chunk));
});

delimiterstream.write('one|tw');
delimiterstream.write('o|three');
delimiterstream.write('|four|fiv');
delimiterstream.write('e');
delimiterstream.end();

This results in

one
two
three
four
five
Piping
var fs = require('fs');
var DelimiterStream = require('delimiter-stream');
var StringDecoder = require('string_decoder').StringDecoder;
var decoder = new StringDecoder('utf8');

var linestream = new DelimiterStream();

var input = fs.createReadStream('somefile.txt');

linestream.on('data', function(chunk) {
  console.log(decoder.write(chunk));
});

input.pipe(linestream);

This will print the contents of somefile.txt line by line.

The test suite also shows examples on how to use the stream.