Skip to content

Commit 3c6ab18

Browse files
committed
Added ignore() + tests.
1 parent ba6d616 commit 3c6ab18

File tree

2 files changed

+175
-18
lines changed

2 files changed

+175
-18
lines changed

src/filters/ignore.js

+20-18
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,27 @@
22

33
'use strict';
44

5-
const Replace = require('./replace');
6-
const withParser = require('../utils/with-parser');
5+
const {none} = require('stream-chain');
76

8-
class Ignore extends Replace {
9-
static make(options) {
10-
return new Ignore(options);
11-
}
7+
const {filterBase, makeStackDiffer} = require('./filter-base.js');
8+
const withParser = require('../utils/with-parser.js');
129

13-
static withParser(options) {
14-
return withParser(Ignore.make, options);
15-
}
10+
const defaultEmptyArrayItem = {name: 'nullValue', value: null};
1611

17-
constructor(options) {
18-
super(options);
19-
this._replacement = Replace.arrayReplacement([]);
20-
this._allowEmptyReplacement = true;
21-
}
22-
}
23-
Ignore.ignore = Ignore.make;
24-
Ignore.make.Constructor = Ignore;
12+
const ignore = options => {
13+
const stackDiffer = makeStackDiffer();
14+
return filterBase({
15+
specialAction: 'reject',
16+
defaultAction: 'accept-token',
17+
transition(stack, chunk, action, options) {
18+
if (action === 'reject') return none;
19+
return stackDiffer(stack, chunk, options);
20+
}
21+
})(options);
22+
};
2523

26-
module.exports = Ignore;
24+
module.exports = ignore;
25+
module.exports.ignore = ignore;
26+
27+
module.exports.withParser = options => withParser(ignore, Object.assign({packKeys: true}, options));
28+
module.exports.withParserAsStream = options => withParser.asStream(ignore, Object.assign({packKeys: true}, options));

tests/test-ignore.mjs

+155
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
'use strict';
2+
3+
import test from 'tape-six';
4+
import chain from 'stream-chain';
5+
6+
import ignore from '../src/filters/ignore.js';
7+
import streamArray from '../src/streamers/stream-array.js';
8+
9+
import readString from './read-string.mjs';
10+
11+
test.asPromise('ignore', (t, resolve, reject) => {
12+
const input = [{a: {}}, {b: []}, {c: null}, {d: 1}, {e: 'e'}],
13+
pipeline = chain([readString(JSON.stringify(input)), ignore.withParser({packKeys: true, packValues: false, filter: stack => stack[0] % 2})]),
14+
expected = [
15+
'startArray',
16+
'startObject',
17+
'startKey',
18+
'stringChunk',
19+
'endKey',
20+
'keyValue',
21+
'startObject',
22+
'endObject',
23+
'endObject',
24+
'startObject',
25+
'startKey',
26+
'stringChunk',
27+
'endKey',
28+
'keyValue',
29+
'nullValue',
30+
'endObject',
31+
'startObject',
32+
'startKey',
33+
'stringChunk',
34+
'endKey',
35+
'keyValue',
36+
'startString',
37+
'stringChunk',
38+
'endString',
39+
'endObject',
40+
'endArray'
41+
],
42+
result = [];
43+
44+
pipeline.on('data', chunk => result.push(chunk.name));
45+
pipeline.on('error', reject);
46+
pipeline.on('end', () => {
47+
t.deepEqual(result, expected);
48+
resolve();
49+
});
50+
});
51+
52+
test.asPromise('ignore: no streaming', (t, resolve, reject) => {
53+
const input = [{a: {}}, {b: []}, {c: null}, {d: 1}, {e: 'e'}],
54+
pipeline = chain([
55+
readString(JSON.stringify(input)),
56+
ignore.withParser({packKeys: true, packValues: false, streamValues: false, filter: stack => stack[0] % 2})
57+
]),
58+
expected = [
59+
'startArray',
60+
'startObject',
61+
'keyValue',
62+
'startObject',
63+
'endObject',
64+
'endObject',
65+
'startObject',
66+
'keyValue',
67+
'nullValue',
68+
'endObject',
69+
'startObject',
70+
'keyValue',
71+
'startString',
72+
'stringChunk',
73+
'endString',
74+
'endObject',
75+
'endArray'
76+
],
77+
result = [];
78+
79+
pipeline.on('data', chunk => result.push(chunk.name));
80+
pipeline.on('error', reject);
81+
pipeline.on('end', () => {
82+
t.deepEqual(result, expected);
83+
resolve();
84+
});
85+
});
86+
87+
test.asPromise('ignore: objects', (t, resolve, reject) => {
88+
const input = [{a: {}}, {b: []}, {c: null}, {d: 1}, {e: 'e'}],
89+
pipeline = chain([readString(JSON.stringify(input)), ignore.withParser({filter: stack => stack[0] % 2}), streamArray()]),
90+
expected = [{a: {}}, {c: null}, {e: 'e'}],
91+
result = [];
92+
93+
pipeline.on('data', chunk => result.push(chunk.value));
94+
pipeline.on('error', reject);
95+
pipeline.on('end', () => {
96+
t.deepEqual(result, expected);
97+
resolve();
98+
});
99+
});
100+
101+
test.asPromise('ignore: objects with a string filter', (t, resolve, reject) => {
102+
const input = [{a: {}}, {b: []}, {c: null}, {d: 1}, {e: 'e'}],
103+
pipeline = chain([readString(JSON.stringify(input)), ignore.withParser({filter: '1'}), streamArray()]),
104+
expected = [{a: {}}, {c: null}, {d: 1}, {e: 'e'}],
105+
result = [];
106+
107+
pipeline.on('data', chunk => result.push(chunk.value));
108+
pipeline.on('error', reject);
109+
pipeline.on('end', () => {
110+
t.deepEqual(result, expected);
111+
resolve();
112+
});
113+
});
114+
115+
test.asPromise('ignore: objects with a RegExp filter', (t, resolve, reject) => {
116+
const input = [{a: {}}, {b: []}, {c: null}, {d: 1}, {e: 'e'}],
117+
pipeline = chain([readString(JSON.stringify(input)), ignore.withParser({filter: /\b[1-5]\.[a-d]\b/}), streamArray()]),
118+
expected = [{a: {}}, {}, {}, {}, {e: 'e'}],
119+
result = [];
120+
121+
pipeline.on('data', chunk => result.push(chunk.value));
122+
pipeline.on('error', reject);
123+
pipeline.on('end', () => {
124+
t.deepEqual(result, expected);
125+
resolve();
126+
});
127+
});
128+
129+
test.asPromise('ignore: empty', (t, resolve, reject) => {
130+
const input = [{a: {}}, {b: []}, {c: null}, {d: 1}, {e: 'e'}],
131+
pipeline = chain([readString(JSON.stringify(input)), ignore.withParser({filter: stack => stack.length}), streamArray()]),
132+
expected = [],
133+
result = [];
134+
135+
pipeline.on('data', chunk => result.push(chunk.value));
136+
pipeline.on('error', reject);
137+
pipeline.on('end', () => {
138+
t.deepEqual(result, expected);
139+
resolve();
140+
});
141+
});
142+
143+
test.asPromise('ignore: once', (t, resolve, reject) => {
144+
const input = [{a: {}}, {b: []}, {c: null}, {d: 1}, {e: 'e'}],
145+
pipeline = chain([readString(JSON.stringify(input)), ignore.withParser({filter: /\b[1-5]\.[a-d]\b/, once: true}), streamArray()]),
146+
expected = [{a: {}}, {}, {c: null}, {d: 1}, {e: 'e'}],
147+
result = [];
148+
149+
pipeline.on('data', chunk => result.push(chunk.value));
150+
pipeline.on('error', reject);
151+
pipeline.on('end', () => {
152+
t.deepEqual(result, expected);
153+
resolve();
154+
});
155+
});

0 commit comments

Comments
 (0)