-
-
Notifications
You must be signed in to change notification settings - Fork 47
Disassembler
Disassembler
receives a stream of JavaScript objects and produces a token stream.
const {chain} = require('stream-chain');
const {disassembler} = require('stream-json/Disassembler');
const {pick} = require('stream-json/filters/Pick');
const {ignore} = require('stream-json/filters/Ignore');
const {streamValues} = require('stream-json/streamers/StreamValues');
const pipeline = chain([
readObjectFromDatabase(),
disassembler(),
pick({filter: 'content'}),
ignore({filter: /\b_/}),
streamValues()
]);
In this example, we read objects from a database, convert them to a stream of tokens, pick only values of top-level property content
, remove all properties that start with _
(underscore), and assemble them back into a stream of objects.
(Since 1.5.0) Disassembler
supports logic defined by JSON.stringify(): skipping non-JSON properties or replacing them with null
when appropriate, supporting toJSON()
method, and replacer
.
Disassembler
is a Transform stream, which operates in object mode. It doesn't have any special public properties nor methods. In general, it was modeled on Parser.
As Parser
it supports all packing and streaming options described in Parser's constructor options.
Additionally, it supports the following options:
-
(since 1.5.0)
replacer
can be one of two optional objects:- It can be a function, which takes two parameters and returns a value.
- It can be an array of strings or numbers to define a whitelist for object properties.
- See JSON.stringify() for more details.
make()
and disassembler()
are two aliases of the factory function. It takes options
described above, and return a new instance of Disassembler
. disassembler()
helps to reduce a boilerplate when creating data processing pipelines:
const {disassembler} = require('stream-json/Disassembler');
const {Readable} = require('stream');
const {chain} = require('stream-chain');
const pipeline = chain([
new Readable({
objectMode: true,
read(size) {
// let readNext() to return an object or null when we are done
database.readNext().then(data => this.push(data));
}
}),
disassembler()
// editing streams
]);
Constructor
property of make()
(and disassembler()
) is set to Disassembler
. It can be used for indirect creating of disassemblers or metaprogramming if needed.
Disassembler
cannot accept top-level null
due to restrictions of Node's stream system.