Skip to content

Commit 3b35f6c

Browse files
authored
update color-sorter to v3.0.0 (#77)
1 parent 23f0212 commit 3b35f6c

File tree

5 files changed

+85
-94
lines changed

5 files changed

+85
-94
lines changed

package-lock.json

Lines changed: 4 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
]
4343
},
4444
"dependencies": {
45-
"color-sorter": "^2.2.0",
45+
"color-sorter": "^3.0.0",
4646
"css-at-supports-browser-h4cks-analyzer": "^1.0.0",
4747
"css-color-names": "^1.0.0",
4848
"css-media-query-browser-h4cks-analyzer": "^1.0.0",

src/analyzer/values/colors.js

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
const valueParser = require('postcss-values-parser')
22
const cssColorNames = require('css-color-names')
33
const tinycolor = require('tinycolor2')
4-
const sortColors = require('color-sorter')
4+
const colorSorter = require('color-sorter')
55

66
const uniquer = require('../../utils/uniquer')
77

8-
const CSS_COLOR_KEYWORDS = Object
9-
.keys(cssColorNames)
10-
.map(color => color.toLowerCase())
8+
const CSS_COLOR_KEYWORDS = Object.keys(cssColorNames).map(color =>
9+
color.toLowerCase()
10+
)
1111
const CSS_COLOR_FUNCTIONS = ['hsl', 'hsla', 'rgb', 'rgba']
1212

1313
function prepareValue(value) {
@@ -22,13 +22,14 @@ function nodeIsHexColor(node) {
2222
}
2323

2424
function nodeIsColorFn(node) {
25-
return node.type === 'func' &&
25+
return (
26+
node.type === 'func' &&
2627
CSS_COLOR_FUNCTIONS.includes(prepareValue(node.value))
28+
)
2729
}
2830

2931
function nodeIsKeyword(node) {
30-
return node.type === 'word' &&
31-
CSS_COLOR_KEYWORDS.includes(prepareValue(node))
32+
return node.type === 'word' && CSS_COLOR_KEYWORDS.includes(prepareValue(node))
3233
}
3334

3435
function extractColorsFromDeclaration(declaration) {
@@ -61,9 +62,11 @@ const addCount = color => {
6162
const addShortestNotation = color => {
6263
return {
6364
...color,
64-
value: [...color.aliases].sort((a, b) => {
65-
return a.value.length - b.value.length
66-
}).shift().value
65+
value: [...color.aliases]
66+
.sort((a, b) => {
67+
return a.value.length - b.value.length
68+
})
69+
.shift().value
6770
}
6871
}
6972

@@ -115,17 +118,17 @@ const rmTmpProps = color => {
115118
}
116119
}
117120

118-
const withAliases = colors => Object
119-
.values(
121+
const withAliases = colors =>
122+
Object.values(
120123
colors
121124
.filter(validateColor)
122125
.map(normalizeColors)
123126
.reduce(addAliases, {})
124127
)
125-
.filter(filterDuplicateColors)
126-
.map(addCount)
127-
.map(addShortestNotation)
128-
.map(rmTmpProps)
128+
.filter(filterDuplicateColors)
129+
.map(addCount)
130+
.map(addShortestNotation) // @TODO: use most often appearing color here
131+
.map(rmTmpProps)
129132

130133
module.exports = declarations => {
131134
const all = declarations
@@ -135,16 +138,12 @@ module.exports = declarations => {
135138
.reduce((allColors, declarationColors) => {
136139
return [...allColors, ...declarationColors]
137140
}, [])
138-
const {totalUnique, unique} = uniquer(all)
139-
140-
// Uniquer sorts the colors, so sort them here once more
141-
const sorted = sortColors(unique.map(c => c.value))
142-
const uniqueSorted = sorted.map(c => unique.find(u => u.value === c))
141+
const {totalUnique, unique} = uniquer(all, colorSorter.sortFn)
143142

144143
return {
145144
total: all.length,
146-
unique: uniqueSorted,
145+
unique,
147146
totalUnique,
148-
duplicates: withAliases(uniqueSorted)
147+
duplicates: withAliases(unique)
149148
}
150149
}

src/utils/uniquer.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,22 @@ const stringSortFn = require('string-natural-compare')
33
module.exports = (values, sortFn) => {
44
sortFn = sortFn || stringSortFn.caseInsensitive
55

6-
const reduced = [...values.reduce((map, value) => {
7-
// Create a Map of unique values and their counts
8-
return map.set(value, map.get(value) + 1 || 1)
9-
}, new Map())].map(value => {
6+
// Create a Map of unique values and their counts
7+
const reduced = [
8+
...values.reduce((map, value) => {
9+
return map.set(value, map.get(value) + 1 || 1)
10+
}, new Map())
11+
]
1012
// Create an array of [{value, count}]
11-
return {
12-
value: value[0],
13-
count: value[1]
14-
}
15-
})
13+
.map(value => {
14+
return {
15+
value: value[0],
16+
count: value[1]
17+
}
18+
})
1619

17-
const sorted = reduced
18-
.map(el => el.value)
19-
.sort(sortFn)
20-
21-
const unique = sorted
22-
.map(value => reduced.find(r => r.value === value))
20+
const sorted = reduced.map(el => el.value).sort(sortFn)
21+
const unique = sorted.map(value => reduced.find(r => r.value === value))
2322

2423
return {
2524
unique,

0 commit comments

Comments
 (0)