11const valueParser = require ( 'postcss-values-parser' )
22const cssColorNames = require ( 'css-color-names' )
33const tinycolor = require ( 'tinycolor2' )
4- const sortColors = require ( 'color-sorter' )
4+ const colorSorter = require ( 'color-sorter' )
55
66const 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+ )
1111const CSS_COLOR_FUNCTIONS = [ 'hsl' , 'hsla' , 'rgb' , 'rgba' ]
1212
1313function prepareValue ( value ) {
@@ -22,13 +22,14 @@ function nodeIsHexColor(node) {
2222}
2323
2424function nodeIsColorFn ( node ) {
25- return node . type === 'func' &&
25+ return (
26+ node . type === 'func' &&
2627 CSS_COLOR_FUNCTIONS . includes ( prepareValue ( node . value ) )
28+ )
2729}
2830
2931function 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
3435function extractColorsFromDeclaration ( declaration ) {
@@ -61,9 +62,11 @@ const addCount = color => {
6162const 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
130133module . 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}
0 commit comments