Generate Node 0.10-friendly transform streams to manipulate other streams.
A collection of Node.js transform stream utilities for simple data manipulation.
npm install sculpt --save.
All of Sculpt's streams operate in
objectMode, so be careful that you know what data types are
going in and coming out of your streams. Normally Node.js streams are guaranteed to be strings or
buffers, but that is not the case when streams operate in object mode.
var stream = sculptmapreturn chunk + chunkstreampipeprocessstdoutstreamwrite'hello'// hellohello
Map can also operate asynchronously. To make the stream async, pass a second argument
(a done callback) and call
var stream = sculptmaprequestRemoteDatachunkdoneerr chunk + dataasyncstreampipeprocessstdoutstreamwrite'hello'// 'hello some remote data...'
Map streams can also operate in multi mode, which lets them push multiple unique values
in a single callback. Callbacks in multi mode must return arrays, and each item
will be pushed individually. To create a map steam in multi mode call
This is most useful when you're consuming the output with another stream that depends on meaningful items in each push. This is how the split stream is implemented.
var i = 0var stream = sculptmapi++return itoString chunkmultistreampipeprocessstdoutstreamwrite'hello'// 1hello
Map streams can be set to ignore values that are
undefined. Ordinarily Node.js treats
undefined) as signaling the end of a stream. In some cases it's useful to be
able to avoid pushing data for some inputs without having a separate stream to filter the data — for
example, cases where deciding whether you want to push data requires expensive computation. In
those cases, you can set the stream to ignore
var stream = sculptmapif chunk === 'hello' returnreturn chunkignoreUndefinedstreampipeprocessstdoutstreamwrite'hello'streawrite'world'// world
var stream = sculptfilterreturn chunktoStringlength >= 5streamon'data' console.logbindconsolestreamwrite'hi'streamwrite'hello'streamwrite'goodbye'// 'hellogoodbye'
Filter can also operate asynchronously. To make the stream async, pass a second argument
(a done callback) and call
var stream = sculptfilterrequestRemoteValidationchunkdoneerr !! validasyncstreamon'data' console.logbindconsolestreamwrite'hi'streamwrite'hello'streamwrite'goodbye'// 'hellogoodbye'
var stream = sculptappend'!!'streamon'data' console.logbindconsolestreamwrite'hello'streamwrite'world'// 'hello!!world!!'
var stream = sculptprepend'> 'streampipeprocessstdoutstreamwrite'hello\n'streamwrite'world'// > hello// > world
var stream = sculptreplace'!' '?'streampipeprocessstdoutstreamwrite'hello! 'streamwrite'world 'streamwrite'goodbye!'// 'hello? world goodbye?'
This is intended to be used on arrays, but could work on any data type that has a
var stream = sculptjoin'|'streampipeprocessstdoutstreamwrite1 2 3streamwrite'foo' 'bar'// '1|2|3foo|bar'
var stream = sculptinvoke'toString'streampipeprocessstdoutstreamend123// '123'
This is intended to be used on strings (and create arrays), but could work on any data type that
var stream = sculptsplit'|'var partNumber = 0streamon'data'partNumber++console.logpartNumber partstreamwrite'hi|bye|foo|bar'// '1 hi'// '2 bye'// '3 foo'// '4 bar'
Each output chunk will be a buffer of
length bytes, except the last chunk, which will be however many bytes are left over.
var stream = sculptbyteLength5streamon'data'console.logchunktoStringstreamend'abcdefghijk'// 'abcde'// 'fghij'// 'k'
Errors from the forked stream are bubbled up to this transform stream.
var stream = sculptforkprocessstderrstreampipeprocessstderrstreamwrite'hello world'// 'hello world' is output to stdout and stderr
var count = 0var stream = tapif item === 'bump'count++streamon'end'console.log'Count is %d' countstreamwrite'bump'streamwrite'bump'streamwrite'hello'streamwrite'bump'// 'Count is 3'
Transform streams can be piped together. Let's say you have a file with song lyrics and you want to clean it up.
fscreateReadStream'./lyrics.txt'// Split into individual lines// The following streams will operate on one line at a time.pipesculptsplit'\n'// Remove trailing whitespace from each linepipesculptreplace/\s+$/ ''// Remove empty linespipesculptfilterreturn linelength > 0// Bring back line breakspipesculptappend'\n'// Print the resultpipeprocessstdout