Skip to content

Commit

Permalink
feat: Custom colors and spacings stats
Browse files Browse the repository at this point in the history
  • Loading branch information
sapegin committed Jul 31, 2020
1 parent de4689a commit ec2a150
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 2 deletions.
4 changes: 4 additions & 0 deletions bin/antbear.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const {
getComponentsStats,
getPropsStats,
getValuesStats,
getColorsStats,
getSpacingStats,
} = require('../src');

function getBinaryName() {
Expand Down Expand Up @@ -79,6 +81,8 @@ if (patterns.length > 0) {
printObject(getComponentsStats(instances), 'Overridden components');
printObject(getPropsStats(instances), 'Properties');
printObject(getValuesStats(instances), 'Values');
printObject(getColorsStats(instances), 'Custom colors');
printObject(getSpacingStats(instances), 'Custom spacing');

if (isVerbose) {
printInstances(instances);
Expand Down
25 changes: 25 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
"css-shorthand-expand": "^1.2.0",
"estree-walker": "^2.0.1",
"glob": "^7.1.6",
"is-color": "^1.0.2",
"is-css-length": "^0.2.0",
"kleur": "^4.0.2",
"lodash": "^4.17.19",
"longest": "^2.0.1",
Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const {
getComponentsStats,
getPropsStats,
getValuesStats,
getColorsStats,
getSpacingStats,
} = require('../src/util/calculateStats');

function getInstances(files) {
Expand All @@ -21,4 +23,6 @@ module.exports = {
getComponentsStats,
getPropsStats,
getValuesStats,
getColorsStats,
getSpacingStats,
};
49 changes: 47 additions & 2 deletions src/util/calculateStats.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
const { countBy, flatMap } = require('lodash');
const isColor = require('is-color');
const isLength = require('is-css-length');

// TODO: Custom colors
// TODO: Custom spacing
// TODO: Better expanding of background, border
// TODO: box-shadow, text-shadow, outline
const COLOR_PROPS = [
'color',
'background-color',
'border-color',
'outline-color',
];

const SPACING_PROPS = [
'margin-top',
'margin-right',
'margin-bottom',
'margin-left',
'padding-top',
'padding-right',
'padding-bottom',
'padding-left',
];

const isComponent = (name) => name.match(/^[A-Z]/);

Expand Down Expand Up @@ -31,3 +50,29 @@ module.exports.getValuesStats = function (instances) {
const styles = getAllStyles(instances);
return countBy(styles, (style) => style.value);
};

module.exports.getColorsStats = function (instances) {
const styles = getAllStyles(instances);
return styles.reduce((colors, { name, value }) => {
if (COLOR_PROPS.includes(name) && isColor(value)) {
if (!colors[value]) {
colors[value] = 0;
}
colors[value]++;
}
return colors;
}, {});
};

module.exports.getSpacingStats = function (instances) {
const styles = getAllStyles(instances);
return styles.reduce((spacings, { name, value }) => {
if (SPACING_PROPS.includes(name) && isLength(value)) {
if (!spacings[value]) {
spacings[value] = 0;
}
spacings[value]++;
}
return spacings;
}, {});
};

0 comments on commit ec2a150

Please sign in to comment.