-
-
Notifications
You must be signed in to change notification settings - Fork 51
FilterBase
Eugene Lazutkin edited this page Jun 18, 2018
·
4 revisions
All provided filters use FilterBase as its foundation. It is a base class meant to be extended, which provides common facilities used by filters.
This document describes the user-facing interface only. If you want to build your own filter, feel free to inspect the code to gain more insights.
Internally, FilterBase keeps track of objects by building a stack. Items of a stack can be:
- Number. In this case, a corresponding object is an array, and the number is the current index.
- String. In this case, a corresponding object is an object, and the string is the current property key.
-
null. In this case, a corresponding object is an object, but keys are not tracked.FilterBasekeeps track of keys only if a previous stream returns packed keys. In this case, a filter assumes that only object's shape will be used for filtering.
The stack is used to make filtering.
options is an optional object described in details in node.js' Stream documentation. Additionally, the following optional custom properties are recognized:
-
pathSeparatoris a string that separates stack values when it is converted to a string. The algorithm is straightforward:stack.join(pathSeparator). The default:'.'.const obj = [{a: 1}, {b: 2}]; // stack when filtering 1: [0, 'a'] // converted to a string: '0.a' // stack when filtering 2: [1, 'b'] // converted to a string: '1.b'
-
filteris a way to accept or reject a data item. The interpretation of its returned value is up to concrete filter objects. Its value can be one of the following types:- String. The stack is converted to a string using
pathSeparator, then it should be equal tofiltervalue, or it should be longer and thefiltervalue should be on a boundary of thepathSeparatorvalue.const obj = {a: [1, 2], ab: null}; const filter = 'a'; // it fits ['a'], ['a', 0], and ['a', 1], but not ['ab']
-
RegExp. The stack is converted to a string usingpathSeparator, then the filter is applied usingfilter.test(path).const obj = {a: [1, 2], ab: null}; const filter = /^a\b/; // it fits ['a'], ['a', 0], and ['a', 1], but not ['ab'] const filter = /^a/; // it fits ['a'], ['a', 0], ['a', 1], and ['ab']
- Function. The filter is applied using
filter(stack, chunk), wherechunkis a data item being filtered. The function is called in the context of current filter object. It should return a truthy/falsy value. - The default:
() => true.
- String. The stack is converted to a string using
-
onceis a flag. When it is truthy, a filter object will make a selection (depending on its definition of selection) only once. Otherwise, all selections are included. The default:false. -
replacementis what should be used instead of skipped objects. Not all filters use this option. Its value can be one of the following types:- Function. The filter is applied using
replacement(stack, chunk), wherechunkis a data item being filtered. The function is called in the context of current filter object. It should return an array of semantically valid data items. - Otherwise, it is assumed to be a static array of semantically valid data items.
- The default: a
nulldata item —[{name: 'nullValue', value: null}].
- Function. The filter is applied using
-
allowEmptyReplacementis a flag. It explicitly allows or disallows to replace removed values with an empty array.- The problem is that when streaming an object, a key will be already streamed, then a filter may want to remove a corresponding value (replace it with an empty array), which produces an invalid JSON stream. In order to avoid it, when
allowEmptyReplacementis falsy, a filter checks the length of a replacement array and replaces it with the default (usually thenulldata item) if it is empty. - If a source stream packs keys, the problem can be avoided by delaying streaming keys. When
allowEmptyReplacementistrue, a filter will use this algorithm to stream keys.
- The problem is that when streaming an object, a key will be already streamed, then a filter may want to remove a corresponding value (replace it with an empty array), which produces an invalid JSON stream. In order to avoid it, when
- Streaming flags. They are used only when a filter stream delayed keys (
allowEmptyReplacementistrue). Both of them are here for compatibility withParser. See details in Parser's options.-
streamValuesis assigned first. -
streamKeysis assigned next. When it is effective value is falsy, nostartKey,stringChunk, norendKeyare produced. OnlykeyValueis issued. - The default:
true.
-