1
0
Fork 0

fix(reporter): avoid repeat copies in table report

This commit is contained in:
Sean Sube 2022-03-07 04:45:16 +00:00
parent 2941a1cd9d
commit fe4d13d51c
1 changed files with 15 additions and 13 deletions

View File

@ -1,31 +1,33 @@
import { getOrDefault, leftPad, setOrPush } from '@apextoaster/js-utils';
import { RuleResult } from '../rule/index.js';
import { Rule, RuleResult } from '../rule/index.js';
import { Reporter } from './index.js';
const MARGIN = ' ';
const COL_DELIMITER = '|';
const ROW_DELIMITER = '\n';
interface RuleCounts {
changes: number;
errors: number;
rule: string;
}
export class TableReporter implements Reporter {
public async report(results: ReadonlyArray<RuleResult>): Promise<string> {
const rules = new Map<string, {changes: number; errors: number}>();
const rules = new Map<Rule, RuleCounts>();
for (const result of results) {
const prev = getOrDefault(rules, result.rule.name, {changes: 0, errors: 0});
const prev = getOrDefault(rules, result.rule, {
changes: 0,
errors: 0,
rule: result.rule.name,
});
prev.changes += result.changes.length;
prev.errors += result.errors.length;
rules.set(result.rule.name, prev);
}
const rows: Array<{rule: string; changes: number; errors: number}> = [];
for (const [rule, counts] of rules) {
rows.push({
rule,
changes: counts.changes,
errors: counts.errors,
});
rules.set(result.rule, prev);
}
const rows = Array.from(rules.values());
return printTable(rows, ['rule', 'errors', 'changes'], {
delimiter: {
column: COL_DELIMITER,