All files / src/utils/parsers json.ts

100% Statements 7/7
100% Branches 4/4
100% Functions 2/2
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26            2x     7x 3x   3x       2x               2x  
import { extname } from 'path';
import type { BaseParser, Results } from './base';
import * as is from '../guards';
import type { Summary } from '../guards/json';
 
export class JsonParser implements BaseParser<Summary> {
  globs = [];
 
  public matches(name: string, contents: string) {
    if (extname(name) !== '.json') return false;
    const data = JSON.parse(contents);
 
    return is.json.summary(data) ? data : false;
  }
 
  public parse(data: Summary): Results {
    return {
      lines: data.total.lines.pct / 100,
      functions: data.total.functions.pct / 100,
      branches: data.total.branches.pct / 100,
    };
  }
}
 
export const json = new JsonParser();