Skip to content

Commit

Permalink
Added skippedArrayValue, packKeys is true by default.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhop committed Dec 21, 2024
1 parent 633eebb commit d1526bd
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 14 deletions.
34 changes: 24 additions & 10 deletions src/filters/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,22 @@ const filter = options => {
for (let i = previousStack.length - 1; i > commonLength; --i) {
returnTokens.push({name: typeof previousStack[i] == 'number' ? 'endArray' : 'endObject'});
}

// update the index
if (commonLength < previousStack.length) {
if (commonLength < stack.length) {
const key = stack[commonLength];
if (typeof key == 'string') {
if (options?.streamKeys) {
returnTokens.push({name: 'startKey'}, {name: 'stringChunk', value: key}, {name: 'endKey'}, {name: 'keyValue', value: key});
} else {
returnTokens.push({name: 'startKey'}, {name: 'stringChunk', value: key}, {name: 'endKey'});
}
if (options?.packKeys || !options?.streamKeys) {
returnTokens.push({name: 'keyValue', value: key});
}
} else if (typeof key == 'number' && options?.skippedArrayValue) {
for (let i = Math.max(0, previousStack[commonLength] + 1); i < key; ++i) {
returnTokens.push(...options.skippedArrayValue);
}
}
previousStack[commonLength] = key;
++commonLength;
Expand All @@ -49,19 +56,26 @@ const filter = options => {
const key = stack[i];
previousStack.push(key);
if (typeof key == 'number') {
if (key >= 0) returnTokens.push({name: 'startArray'});
if (key >= 0) {
returnTokens.push({name: 'startArray'});
if (options?.skippedArrayValue) {
for (let j = 0; j < key; ++j) {
returnTokens.push(...options.skippedArrayValue);
}
}
}
} else if (typeof key == 'string') {
returnTokens.push({name: 'startObject'});
if (options?.streamKeys) {
returnTokens.push({name: 'startObject'}, {name: 'startKey'}, {name: 'stringChunk', value: key}, {name: 'endKey'}, {name: 'keyValue', value: key});
} else {
returnTokens.push({name: 'startObject'}, {name: 'keyValue', value: key});
returnTokens.push({name: 'startKey'}, {name: 'stringChunk', value: key}, {name: 'endKey'});
}
if (options?.packKeys || !options?.streamKeys) {
returnTokens.push({name: 'keyValue', value: key});
}
}
}

// save the stack
// previousStack = stack.slice();
// previousStack.splice(commonLength, previousStack.length - commonLength, ...stack.slice(commonLength));
switch (chunk?.name) {
case 'startObject':
previousStack.push(null);
Expand All @@ -79,5 +93,5 @@ const filter = options => {
module.exports = filter;
module.exports.filter = filter;

module.exports.withParser = options => withParser(filter, options);
module.exports.withParserAsStream = options => withParser.asStream(filter, options);
module.exports.withParser = options => withParser(filter, Object.assign({packKeys: true}, options));
module.exports.withParserAsStream = options => withParser.asStream(filter, Object.assign({packKeys: true}, options));
4 changes: 2 additions & 2 deletions src/filters/pick.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ const pick = filterBase();
module.exports = pick;
module.exports.pick = pick;

module.exports.withParser = options => withParser(pick, options);
module.exports.withParserAsStream = options => withParser.asStream(pick, options);
module.exports.withParser = options => withParser(pick, Object.assign({packKeys: true}, options));
module.exports.withParserAsStream = options => withParser.asStream(pick, Object.assign({packKeys: true}, options));
25 changes: 23 additions & 2 deletions tests/test-filter.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import readString from './read-string.mjs';

test.asPromise('filter', (t, resolve, reject) => {
const input = '{"a": 1, "b": true, "c": ["d"]}',
pipeline = chain([readString(input), filter.withParser({packKeys: true, packValues: false, filter: /^(|a|c)$/})]),
pipeline = chain([readString(input), filter.withParser({packValues: false, filter: /^(|a|c)$/})]),
result = [];

pipeline.on('data', chunk => result.push(chunk));
Expand Down Expand Up @@ -39,7 +39,7 @@ test.asPromise('filter', (t, resolve, reject) => {

test.asPromise('filter: no streaming', (t, resolve, reject) => {
const input = '{"a": 1, "b": true, "c": ["d"]}',
pipeline = chain([readString(input), filter.withParser({packKeys: true, packValues: false, streamValues: false, filter: /^(a|c)$/})]),
pipeline = chain([readString(input), filter.withParser({packValues: false, streamValues: false, filter: /^(a|c)$/})]),
result = [];

pipeline.on('data', chunk => result.push(chunk));
Expand Down Expand Up @@ -94,6 +94,27 @@ test.asPromise('filter: array', (t, resolve, reject) => {
pipeline.resume();
});

test.asPromise('filter: array with skipped values', (t, resolve, reject) => {
const data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
asm = assembler(),
pipeline = chain([
readString(JSON.stringify(data)),
filter.withParser({
filter: stack => stack.length == 1 && typeof stack[0] == 'number' && stack[0] % 2,
skippedArrayValue: [{name: 'nullValue', value: null}]
}),
asm.tapChain
]);

pipeline.on('error', reject);
pipeline.on('end', () => {
t.deepEqual(asm.current, [null, 2, null, 4, null, 6, null, 8, null, 10]);
resolve();
});

pipeline.resume();
});

test.asPromise('filter: bug46', (t, resolve, reject) => {
const data = [
{data: {a: 1, b: 2}, x: 1},
Expand Down
1 change: 1 addition & 0 deletions tests/test-pick.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ test.asPromise('parser: pick events', (t, resolve, reject) => {
'startKey',
'stringChunk',
'endKey',
'keyValue',
'trueValue',
'endObject'
]);
Expand Down

0 comments on commit d1526bd

Please sign in to comment.