forked from cssstats/cssstats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.js
187 lines (165 loc) · 4.95 KB
/
stats.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
var cssstats = require('css-statistics');
var beautify = require('cssbeautify');
function parseDeclarations(declarations, indexes) {
var array = [];
if (!indexes) return;
indexes.forEach(function(i) {
array.push(declarations[i]);
});
return array;
}
function parseUniques(stats) {
if (!stats) return false;
var uniques = {};
uniques.fontSize = parseDeclarations(stats.declarations.all, stats.declarations.unique.fontSize);
uniques.fontFamily = parseDeclarations(stats.declarations.all, stats.declarations.unique.fontFamily);
uniques.width = parseDeclarations(stats.declarations.all, stats.declarations.unique.width);
uniques.height = parseDeclarations(stats.declarations.all, stats.declarations.unique.height);
uniques.color = parseDeclarations(stats.declarations.all, stats.declarations.unique.color);
uniques.backgroundColor = parseDeclarations(stats.declarations.all, stats.declarations.unique.backgroundColor);
uniques.margin = parseDeclarations(stats.declarations.all, stats.declarations.unique.margin);
uniques.padding = parseDeclarations(stats.declarations.all, stats.declarations.unique.padding);
uniques.borderRadius = parseDeclarations(stats.declarations.all, stats.declarations.unique.borderRadius);
uniques.fontSizeSorted = sortFontSizes(stats);
return uniques;
}
function parseSpecificity(selectors) {
if (!selectors.length) return;
var array = [];
selectors.forEach(function(selector) {
var values = selector.specificity.split(',');
var a = parseInt(values[0], 10) * 1000;
var b = parseInt(values[1], 10) * 100;
var c = parseInt(values[2], 10) * 10;
var d = parseInt(values[3], 10);
if (a > 10000) a = 10000;
if (b > 1000) b = 1000;
if (c > 100) c = 100;
if (d > 10) d = 10;
array.push(a + b + c + d);
});
return array;
}
function fontSizeToPx(value) {
var raw;
raw = parseFloat(value, 10);
if (value.match(/px$/)) {
return raw;
}
if (value.match(/em$/)) {
return raw * 16;
}
if (value.match(/%$/)) {
return raw * .16;
}
switch (value) {
case 'inherit':
return 16;
case 'xx-small':
return 9;
case 'x-small':
return 10;
case 'small':
return 13;
case 'medium':
return 16;
case 'large':
return 18;
case 'x-large':
return 24;
case 'xx-large':
return 32;
case 'small':
return 13;
case 'larger':
return 19;
default:
return 1024;
}
}
function sortFontSizes(stats) {
var sortBy = function(a, b) {
c = fontSizeToPx(a.value);
d = fontSizeToPx(b.value);
if (c > d) {
return -1;
} else {
return 1;
}
}
var sorted = parseDeclarations(stats.declarations.all, stats.declarations.unique.fontSize);
if (!sorted) return false;
return sorted.sort(sortBy);
}
function parsePropertiesBreakdown(stats) {
if (!stats) return false;
var result = [];
var total = stats.declarations.all.length;
var properties = stats.aggregates.properties;
var otherSum = 0;
if (!properties.length) return false;
properties.forEach(function(property) {
var obj = {};
obj.property = property;
obj.percentage = (stats.declarations.byProperty[property].length / total * 100);
if (obj.percentage < 2) {
otherSum += obj.percentage;
} else {
result.push(obj);
}
});
if (!result.length) return false;
result = result.sort(function(a,b) { return b.percentage - a.percentage });
result.push({ property: 'other', percentage: otherSum });
result.forEach(function(property) {
property.percentagePretty = property.percentage.toFixed(2);
});
return result;
}
function uniquesGraph(stats) {
var obj = {};
obj.max = 0;
var keys = ['margin', 'padding', 'width', 'height', 'color', 'backgroundColor', 'float', 'display', 'position'];
keys.forEach(function(key) {
obj[key] = {};
if (!stats.declarations.byProperty[key]) {
obj[key].total = 0;
obj[key].unique = 0;
} else {
obj[key].total = stats.declarations.byProperty[key].length;
obj[key].unique = stats.declarations.unique[key].length;
if (obj[key].total > obj.max) {
obj.max = obj[key].total;
}
}
});
keys.forEach(function(key) {
if (!obj[key]) return false;
obj[key].percentTotal = obj[key].total / obj.max;
obj[key].percentUnique = obj[key].unique / obj.max;
});
return obj;
}
function rulesizeGraph(rules) {
var array = [];
rules.forEach(function(rule) {
if (!rule.declarations) return false;
array.push(rule.declarations.length);
});
return array;
}
module.exports = function(obj) {
var model = obj;
model.cssPretty = beautify(obj.css);
model.stats = cssstats(obj.css, {
silent: true
});
if (!model.stats) {
console.log('no stats');
}
model.uniques = parseUniques(model.stats);
model.specificityGraph = parseSpecificity(model.stats.selectors);
model.rulesizeGraph = rulesizeGraph(model.stats.rules);
model.uniquesGraph = uniquesGraph(model.stats);
return model;
};