Skip to content

Commit

Permalink
Create 26-property-pairs.js
Browse files Browse the repository at this point in the history
  • Loading branch information
LeaVerou committed Oct 27, 2020
1 parent 05dba5a commit dbef879
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions js/26-property-pairs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
export default function compute() {

let usedTogether = {};

walkRules(ast.stylesheet.rules, rule => {
let props = new Set();

if (!rule.declarations) {
return;
}

let ruleProps = rule.declarations
// Ignore custom properties and prefixed ones. CP because they don't generalize
// and prefixed ones because they will trivially correlate with each other and the non-prefixed version
.filter(d => !d.property.startsWith("-"))
.map(d => d.property);

for (let prop of ruleProps) {
props.add(prop);
}

for (let prop of props) {
usedTogether[prop] = usedTogether[prop] || {};

for (let prop2 of props) {
if (prop === prop2) {
continue;
}

usedTogether[prop][prop2] = usedTogether[prop][prop2] || 0;
usedTogether[prop][prop2]++;
}
}
}, {
// rules: r => Boolean(r.declarations),
not: {
type: "font-face"
}
});

let ret = {};

// Now sort by usage count
for (let prop in usedTogether) {
let obj = usedTogether[prop];

// Remove properties that are only used together once
for (let p in obj) {
if (obj[p] === 1) {
delete obj[p];
}
else {
let key = [prop, p].sort();
ret[key] = ret[key] || 0;
ret[key] += obj[p];
}
}

// let sortedEntries = Object.entries(obj).sort((a, b) => b[1] - a[1]);
//
// if (sortedEntries.length > 0) {
// usedTogether[prop] = Object.fromEntries(sortedEntries);
// }
// else {
// delete usedTogether[prop];
// }
}

return sortObject(ret);

}

0 comments on commit dbef879

Please sign in to comment.