Skip to content

Commit

Permalink
Create 05-specificity.js
Browse files Browse the repository at this point in the history
Rel #5
  • Loading branch information
LeaVerou committed Sep 14, 2020
1 parent f60b314 commit 3b042e9
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions js/05-specificity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export default function compute() {

let ret = {
selectorCount: 0,
ruleCount: 0,
specificityCount: {},
maxSpecifity: [0, 0, 0]
};

let ss = [0, 0, 0];

walkRules(ast, rule => {
ret.ruleCount++;

for (let selector of rule.selectors) {
ret.selectorCount++;
let s = parsel.specificity(selector);
ss = ss.map((a, i) => a + s[i]);
let max = Math.max(...s);

incrementByKey(ret.specificityCount, max <= 5? s + "" : "higher");

let base = Math.max(...ret.maxSpecifity, ...s);
if (parsel.specificityToNumber(s, base) > parsel.specificityToNumber(ret.maxSpecifity, base)) {
ret.maxSpecifity = s;
ret.maxSpecifitySelector = selector;
}
}
}, {type: "rule"});

ret.selectorsPerRule = ret.selectorCount / ret.ruleCount;
ret.avgSpecificity = ss.map(s => s / ret.selectorCount);

return ret;

}

0 comments on commit 3b042e9

Please sign in to comment.