123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- 'use strict';
- const { FileCoverage } = require('./file-coverage');
- const { CoverageSummary } = require('./coverage-summary');
- function maybeConstruct(obj, klass) {
- if (obj instanceof klass) {
- return obj;
- }
- return new klass(obj);
- }
- function loadMap(source) {
- const data = Object.create(null);
- if (!source) {
- return data;
- }
- Object.entries(source).forEach(([k, cov]) => {
- data[k] = maybeConstruct(cov, FileCoverage);
- });
- return data;
- }
- class CoverageMap {
-
- constructor(obj) {
- if (obj instanceof CoverageMap) {
- this.data = obj.data;
- } else {
- this.data = loadMap(obj);
- }
- }
-
- merge(obj) {
- const other = maybeConstruct(obj, CoverageMap);
- Object.values(other.data).forEach(fc => {
- this.addFileCoverage(fc);
- });
- }
-
- filter(callback) {
- Object.keys(this.data).forEach(k => {
- if (!callback(k)) {
- delete this.data[k];
- }
- });
- }
-
- toJSON() {
- return this.data;
- }
-
- files() {
- return Object.keys(this.data);
- }
-
- fileCoverageFor(file) {
- const fc = this.data[file];
- if (!fc) {
- throw new Error(`No file coverage available for: ${file}`);
- }
- return fc;
- }
-
- addFileCoverage(fc) {
- const cov = new FileCoverage(fc);
- const { path } = cov;
- if (this.data[path]) {
- this.data[path].merge(cov);
- } else {
- this.data[path] = cov;
- }
- }
-
- getCoverageSummary() {
- const ret = new CoverageSummary();
- Object.values(this.data).forEach(fc => {
- ret.merge(fc.toSummary());
- });
- return ret;
- }
- }
- module.exports = {
- CoverageMap
- };
|