through2-get
v1.0.0
Published
A through2 to create a lodash.get analog for streams
Downloads
7,504
Readme
through2-get
This is a super thin wrapper around through2 that works like _.get
but for streams.
For when through2 is just too verbose :wink:
IMPORTANT: If you return null
from your function, the stream will end there.
IMPORTANT: _.get can only be applied to object streams.
var get = require("through2-get");
var content = get('content');
// vs. with through2:
var content = through2(function (chunk, encoding, cb) {
this.push(_.get(chunk, 'content'));
return cb();
})
// Then use your get:
source.pipe(content).pipe(sink)
// Works like `_.get` meaning you can specify a path and a default value
var contentEach = get({excludeZBS: true}, 'content', '');
// vs. with through2:
var contentEach = through2(function (chunk, encoding, cb) {
var out = _.get(chunk, 'content', '');
if (out === '') return cb();
this.push(out);
return cb();
});
Differences from _.get
:
- Cannot insert
null
elements into the stream without aborting.
API
require("through2-get")([options,] path, [defaultValue])
Create a stream.Transform
instance with objectMode: true
defaulting to true that will call _.get(path, defaultValue)
on each stream object.
var Tx = require("through2-get").ctor([options,] path, [defaultValue])
Create a reusable stream.Transform
TYPE that can be called via new Tx
or Tx()
to create an instance.
Arguments
options
excludeZBS
(boolean): defaultstrue
.- all other through2 options.
path
(Array|string): The path of the property to get.[defaultValue]
(*): The value returned if the resolved value is undefined.