through2-get

A through2 to create a lodash.get analog for streams

Usage no npm install needed!

<script type="module">
  import through2Get from 'https://cdn.skypack.dev/through2-get';
</script>

README

through2-get

Version License Test Coverage Status

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): defaults true.
    • all other through2 options.
  • path (Array|string): The path of the property to get.
  • [defaultValue] (*): The value returned if the resolved value is undefined.