Skip to content

Commit b3c86d1

Browse files
authored
Rename color aliases to notations (#79)
Notations is a better name for this as that it what they actually are: different notations of the same color value.
1 parent 230bd4e commit b3c86d1

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

src/analyzer/values/colors.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,18 @@ function extractColorsFromDeclaration(declaration) {
5353
const addCount = color => {
5454
return {
5555
...color,
56-
count: color.aliases.reduce((acc, curr) => {
57-
return acc + curr.count
56+
count: color.notations.reduce((total, {count}) => {
57+
return total + count
5858
}, 0)
5959
}
6060
}
6161

62-
const addShortestNotation = color => {
62+
const addMostCommonNotation = color => {
6363
return {
6464
...color,
65-
value: [...color.aliases]
65+
value: [...color.notations]
6666
.sort((a, b) => {
67+
// If counts are the same, get the shortest notation
6768
if (a.count === b.count) {
6869
return a.value.length - b.value.length
6970
}
@@ -74,23 +75,23 @@ const addShortestNotation = color => {
7475
}
7576
}
7677

77-
const addAliases = (acc, curr) => {
78+
const addNotations = (acc, curr) => {
7879
if (!acc[curr.normalized]) {
7980
acc[curr.normalized] = {
80-
aliases: []
81+
notations: []
8182
}
8283
}
8384

8485
acc[curr.normalized] = {
85-
aliases: [...acc[curr.normalized].aliases, curr]
86+
notations: [...acc[curr.normalized].notations, curr]
8687
}
8788

8889
return acc
8990
}
9091

9192
const filterDuplicateColors = color => {
9293
// Filter out the actual duplicate colors
93-
return color.aliases.length > 1
94+
return color.notations.length > 1
9495
}
9596

9697
const validateColor = color => {
@@ -101,7 +102,7 @@ const normalizeColors = color => {
101102
// Add a normalized value
102103

103104
// Avoid using TinyColor's toHslString() because it rounds
104-
// the numbers and incorrectly reports aliases
105+
// the numbers and incorrectly reports duplicates
105106
const {h, s, l, a} = tinycolor(color.value).toHsl()
106107
const normalized = a === 0 ? 0 : `h${h}s${s}l${l}a${a}`
107108

@@ -115,23 +116,23 @@ const rmTmpProps = color => {
115116
// Remove temporary props that were needed for analysis
116117
return {
117118
...color,
118-
aliases: color.aliases.map(alias => {
119-
const {normalized, ...restAlias} = alias
120-
return restAlias
119+
notations: color.notations.map(notation => {
120+
const {normalized, ...colorProps} = notation
121+
return colorProps
121122
})
122123
}
123124
}
124125

125-
const withAliases = colors =>
126+
const withDuplicateNotations = colors =>
126127
Object.values(
127128
colors
128129
.filter(validateColor)
129130
.map(normalizeColors)
130-
.reduce(addAliases, {})
131+
.reduce(addNotations, {})
131132
)
132133
.filter(filterDuplicateColors)
133134
.map(addCount)
134-
.map(addShortestNotation) // @TODO: use most often appearing color here
135+
.map(addMostCommonNotation)
135136
.map(rmTmpProps)
136137

137138
module.exports = declarations => {
@@ -148,6 +149,6 @@ module.exports = declarations => {
148149
total: all.length,
149150
unique,
150151
totalUnique,
151-
duplicates: withAliases(unique)
152+
duplicates: withDuplicateNotations(unique)
152153
}
153154
}

test/analyzer/values/output.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@
276276
{
277277
"count": 2,
278278
"value": "rgba(0,0,0,0)",
279-
"aliases": [
279+
"notations": [
280280
{
281281
"value": "hsla(0,0%,0%,0)",
282282
"count": 1
@@ -290,7 +290,7 @@
290290
{
291291
"value": "rgba(100, 200, 10, .5)",
292292
"count": 2,
293-
"aliases": [
293+
"notations": [
294294
{
295295
"count": 1,
296296
"value": "rgba(100, 200, 10, .5)"
@@ -304,7 +304,7 @@
304304
{
305305
"value": "hsl(270,60%,70%)",
306306
"count": 3,
307-
"aliases": [
307+
"notations": [
308308
{
309309
"count": 1,
310310
"value": "hsl(270,60%,70%)"
@@ -322,7 +322,7 @@
322322
{
323323
"value": "hsl(270 60% 50% / .15)",
324324
"count": 4,
325-
"aliases": [
325+
"notations": [
326326
{
327327
"count": 1,
328328
"value": "hsl(270, 60%, 50%, 15%)"
@@ -344,7 +344,7 @@
344344
{
345345
"value": "#fff",
346346
"count": 4,
347-
"aliases": [
347+
"notations": [
348348
{
349349
"count": 1,
350350
"value": "hsl(360, 100%, 100%)"
@@ -366,7 +366,7 @@
366366
{
367367
"count": 8,
368368
"value": "black",
369-
"aliases": [
369+
"notations": [
370370
{
371371
"value": "hsl(0,0,0)",
372372
"count": 1

0 commit comments

Comments
 (0)