-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
148 lines (119 loc) · 2.39 KB
/
index.js
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* Dependencies
*/
var instrument = require('instrument');
var map = require('cover-map');
var domify = require('domify');
var query = require('query');
var each = require('each');
var ui = require('./ui');
/**
* Export `Coverage`
*/
module.exports = Coverage;
/**
* Initializae `Coverage`.
*
* @param {String} name
* @api public
*/
function Coverage(name){
if (!(this instanceof Coverage)) return new Coverage(name);
this.cov = instrument(name);
this.name = name;
}
/**
* Render.
*
* @api public
*/
Coverage.prototype.render = function(){
var el = query('#coverage');
if (!el) throw new Error('"#coverage" was not found');
var obj = map(this.cov);
var self = this;
// ui stuff
obj.level = level(obj);
obj.name = this.name;
// normalize
obj.mods = obj.mods.map(function(mod){
return self.normalize(mod);
});
// hide mocha
var mocha = query('#mocha');
mocha.style.display = 'none';
// render
el.appendChild(domify(ui(obj)));
};
/**
* Normalize `mod`.
*
* @param {Object} mod
* @return {Object}
* @api private
*/
Coverage.prototype.normalize = function(mod){
var lines = mod.source.split(/\r?\n/g);
var ranges = mod.ranges;
var chars = 0;
// sort
ranges = ranges.sort(function(a, b){
return a[0] - b[0];
});
// mod lines
mod.lines = [];
// level
mod.level = level(mod);
// normalize ranges
each(lines, function(line, i){
var range = ranges[0] || [0, 0];
var start = range[0];
var end = range[1];
var len = line.length;
// ignore @sourceURL
if (0 == line.indexOf('//@ sourceURL=')) return;
// total
chars += len + 1;
// skip
if (start > chars - 2) {
return mod.lines.push({
src: line + '\n',
n: i
});
}
// range
var range = ranges.shift();
// skip
if (!range) {
return mod.lines.push({
src: line + '\n',
n: i
});
}
// hits
var mark = range.join(':');
var hits = mod.covered[mark] || 0;
// empty
if (!line.trim()) hits = null;
// sloc
mod.lines.push({
src: line + '\n',
hits: hits,
n: i
});
});
return mod;
};
/**
* Get level of coverage with `obj`.
*
* @param {Object} obj
* @return {String}
*/
function level(obj){
var p = obj.percent / 10;
if (7 <= p) return 'high';
if (5 <= p) return 'medium';
if (3 <= p) return 'low';
return 'terrible';
}