-
-
Notifications
You must be signed in to change notification settings - Fork 47
Filter
Filter
is a token item filter based on FilterBase. It selects objects from a stream ignoring the rest. Its filter is called for all value tokens. A shape of incoming stream is preserved: when selecting a subobject, all parent objects will be recreated to keep a stream valid.
In order to recreate parent objects correctly this filter requires that an upstream produced packed keys (keyValue
tokens).
const Filter = require('stream-json/filters/Filter');
const fs = require('fs');
// our data stream:
// {total: 123456789, meta: {...}, data: [...]}
// we want to remove all properties but 'data':
// {data: [...]}
const pipeline = fs.createReadStream('sample.json')
.pipe(Filter.withParser({filter: 'data'}));
pipeline.on('data', data => console.log(data));
Filter
has no special API. Based on FilterBase
it uses the following options:
-
filter
- If it returns a truthy value, the current object is streamed out.
- If we have any unselected parent objects, they will be recreated.
- Any subobject will have a chance to be filtered.
- It is called only for the following tokens:
startObject
,startArray
,startString
,startNumber
,stringValue
,numberValue
,nullValue
,trueValue
,falseValue
.
- If it returns a truthy value, the current object is streamed out.
pathSeparator
once
See their definitions in FilterBase.
make()
and filter()
are two aliases of the factory function. It takes options described above, and return a new instance of Filter
. filter()
helps to reduce a boilerplate when creating data processing pipelines:
const {chain} = require('stream-chain');
const {parser} = require('stream-json/Parser');
const {filter} = require('stream-json/filters/Filter');
const fs = require('fs');
const pipeline = chain([
fs.createReadStream('sample.json'),
parser(),
filter({filter: 'data'})
]);
let objectCounter = 0;
pipeline.on('data', data => data.name === 'startObject' && ++objectCounter);
pipeline.on('end', () => console.log(`Found ${objectCounter} objects.`));
Constructor property of make()
(and filter()
) is set to Filter
. It can be used for indirect creating of filters or metaprogramming if needed.
withParser()
takes one argument:
-
options
is an object described in Parser's options. It is used to initialize both streams (aParser
instance and a stream returned bymake()
).
It returns a stream produced by stream-chain, which wraps the pipeline. The most important utility of withParser()
is that it correctly sets object modes of the returned stream: object mode for the Readable part and text mode for the Writable part.
This static method is created using withParser() utility. It simplifies a case when a stream should be immediately preceded by a parser.
const Filter = require('stream-json/filters/Filter');
const fs = require('fs');
const pipeline = fs.createReadStream('sample.json')
.pipe(Filter.withParser({filter: 'data'}));
let objectCounter = 0;
pipeline.on('data', data => data.name === 'startObject' && ++objectCounter);
pipeline.on('end', () => console.log(`Found ${objectCounter} objects.`));