-
-
Notifications
You must be signed in to change notification settings - Fork 53
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.js');
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.withParserAsStream({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.
pathSeparatoronce
See their definitions in FilterBase.
filter() is the factory function. It takes options described above and returns a filter function for use in chain():
const {chain} = require('stream-chain');
const {parser} = require('stream-json/parser.js');
const {filter} = require('stream-json/filters/filter.js');
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.`));Returns a Duplex stream (object-mode both sides) wrapping filter() for .pipe() usage.
withParser() takes one argument:
-
options— combined Parser and filter options. Passed to both the parser andfilter().
Returns a Duplex stream (text-mode writable, object-mode readable) wrapping a parser() + filter() pipeline via stream-chain.
Built with the withParser() utility.
const {filter} = require('stream-json/filters/filter.js');
const fs = require('fs');
const pipeline = fs.createReadStream('sample.json').pipe(filter.withParserAsStream({filter: 'data'}));
let objectCounter = 0;
pipeline.on('data', data => data.name === 'startObject' && ++objectCounter);
pipeline.on('end', () => console.log(`Found ${objectCounter} objects.`));filter ships in two substrate-specific entries with the same factory shape:
-
Node —
stream-json/filters/filter.js. HasasStream,asWebStream,withParser,withParserAsStream,withParserAsWebStream. -
Web —
stream-json/web/filters/filter.js. HasasWebStream,withParser,withParserAsWebStream. Pulls in no Node-stream imports.
Both factories return the same flushable, so chain on either substrate auto-wraps it. Use chain from stream-chain on Node and from stream-chain/web on Web.
// Web
import {chain} from 'stream-chain/web';
import {parser} from 'stream-json/web/parser.js';
import {filter} from 'stream-json/web/filters/filter.js';
const pipeline = chain([source, parser.asWebStream(), filter({filter: 'data'})]);
for await (const tok of pipeline.readable) console.log(tok);Or use the parser-included shortcut:
import {filter} from 'stream-json/web/filters/filter.js';
const {readable, writable} = filter.withParserAsWebStream({filter: 'data'});
sourceReadable.pipeTo(writable);
for await (const tok of readable) console.log(tok);Start here
Core
Filters
Streamers
Essentials
Utilities
File I/O (Node-only)
JSONC
JSONL (use stream-chain)
Reference
Built on stream-chain