Skip to content

Commit ae37bab

Browse files
authored
Fixes false positives on colors that are almost duplicates
1 parent b4b91ef commit ae37bab

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

src/analyzer/values/colors.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,11 @@ const addShortestNotation = color => {
6969
const addAliases = (acc, curr) => {
7070
if (!acc[curr.key]) {
7171
acc[curr.key] = {
72-
key: curr.key,
7372
aliases: []
7473
}
7574
}
7675

7776
acc[curr.key] = {
78-
...acc[curr.key],
7977
aliases: [...acc[curr.key].aliases, curr]
8078
}
8179

@@ -93,17 +91,21 @@ const validateColor = color => {
9391

9492
const normalizeColors = color => {
9593
// Add a normalized value
94+
95+
// Avoid using TinyColor's toHslString() because it rounds
96+
// the numbers and incorrectly reports aliases
97+
const {h, s, l, a} = tinycolor(color.value).toHsl()
98+
9699
return {
97100
...color,
98-
key: tinycolor(color.value).toHslString()
101+
key: `h${h}s${s}l${l}a${a}`
99102
}
100103
}
101104

102105
const rmTmpProps = color => {
103106
// Remove temporary props that were needed for analysis
104-
const {key, ...restColor} = color
105107
return {
106-
...restColor,
108+
...color,
107109
aliases: color.aliases.map(alias => {
108110
const {key, ...restAlias} = alias
109111
return restAlias

test/analyzer/values/input.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@
6868
color: rgba(0,0,0,1);
6969
color: hsl(0,0,0);
7070
color: hsla(0,0,0,1);
71+
72+
/* Almost duplicates */
73+
color: #d9d9d9;
74+
color: #dadada;
7175
}
7276
.color-keyword {
7377
outline: 1px solid tomato;

test/analyzer/values/output.json

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"total": 91,
2+
"total": 93,
33
"fontfamilies": {
44
"total": 18,
55
"totalUnique": 12,
@@ -121,11 +121,11 @@
121121
"count": 1
122122
}
123123
],
124-
"share": 0.04395604395604396
124+
"share": 0.043010752688172046
125125
},
126126
"colors": {
127-
"total": 37,
128-
"totalUnique": 35,
127+
"total": 39,
128+
"totalUnique": 37,
129129
"unique": [
130130
{
131131
"value": "#000",
@@ -147,6 +147,14 @@
147147
"value": "#aff034",
148148
"count": 1
149149
},
150+
{
151+
"value": "#d9d9d9",
152+
"count": 1
153+
},
154+
{
155+
"value": "#dadada",
156+
"count": 1
157+
},
150158
{
151159
"value": "#fff",
152160
"count": 1

0 commit comments

Comments
 (0)