|
| 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