Skip to content

Commit

Permalink
perf: only parse filter expression at the boot time
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Nov 13, 2019
1 parent b244837 commit 11f13d9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
7 changes: 5 additions & 2 deletions src/bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import yargs from 'yargs';
import split from 'split2';
import JSON5 from 'json5';
import {
Instance as Chalk,
} from 'chalk';
Expand Down Expand Up @@ -67,10 +68,12 @@ let stream = process.stdin
return line + '\n';
}));

if (argv.filterExpression) {
if (argv.filterExpression.length > 0) {
const filterExpressions = JSON5.parse(argv.filterExpression);

stream = stream.pipe(createLogFilter({
chalk,
filterExpression: argv.filterExpression,
filterExpression: filterExpressions,
head: argv.head,
lag: argv.lag,
}));
Expand Down
7 changes: 2 additions & 5 deletions src/factories/createLogFilter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// @flow

import JSON5 from 'json5';
import split from 'split2';
import {
matchObject,
Expand All @@ -18,8 +17,6 @@ export default (configuration: LogFilterConfigurationType) => {
let printNextLines = 0;
let buffer = [];

const filterExpression = configuration.filterExpression && JSON5.parse(configuration.filterExpression);

const filterLog = (line: string) => {
buffer.push(line);

Expand All @@ -29,7 +26,7 @@ export default (configuration: LogFilterConfigurationType) => {

let result;

if (matchObject(subject, filterExpression)) {
if (matchObject(subject, configuration.filterExpression)) {
result = buffer.slice(-1 * lastLinePrinterLinesAgo - 1, -1).join('\n') + '\n' + line.trim();

lastLinePrinterLinesAgo = 0;
Expand All @@ -46,7 +43,7 @@ export default (configuration: LogFilterConfigurationType) => {
lastLinePrinterLinesAgo++;
}

return result.trim() + '\n';
return result ? result.trim() + '\n' : '';
};

return split((line) => {
Expand Down

0 comments on commit 11f13d9

Please sign in to comment.