Skip to content

Commit 0e8dc06

Browse files
authored
Improv sorting of selectors to be more human-like (#68)
1 parent 305aea0 commit 0e8dc06

File tree

6 files changed

+49
-49
lines changed

6 files changed

+49
-49
lines changed

package-lock.json

Lines changed: 5 additions & 0 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"postcss": "^7.0.2",
3838
"postcss-values-parser": "^2.0.0",
3939
"specificity": "^0.4.1",
40+
"string-natural-compare": "^2.0.3",
4041
"tinycolor2": "^1.4.1"
4142
},
4243
"devDependencies": {

src/utils/uniquer.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
function stringCompare(a, b) {
2-
return a
3-
.toLowerCase()
4-
.localeCompare(
5-
b.toLowerCase()
6-
)
7-
}
1+
const stringSortFn = require('string-natural-compare')
82

93
module.exports = (values, sortFn) => {
10-
sortFn = sortFn || stringCompare
4+
sortFn = sortFn || stringSortFn.caseInsensitive
115

126
const reduced = [...values.reduce((map, value) => {
137
// Create a Map of unique values and their counts

test/analyzer/atrules/fontfaces/output.json

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
"total": 6,
33
"totalUnique": 5,
44
"unique": [
5+
{
6+
"count": 1,
7+
"value": {
8+
"font-family": "MyHelvetica",
9+
"src": "local(\"Helvetica Neue Bold\"), local(\"HelveticaNeue-Bold\"), url(MgOpenModernaBold.ttf)",
10+
"font-weight": "bold"
11+
}
12+
},
513
{
614
"count": 1,
715
"value": {
@@ -17,21 +25,6 @@
1725
"unicode-range": "U+0025-00FF"
1826
}
1927
},
20-
{
21-
"count": 1,
22-
"value": {
23-
"font-family": "MyHelvetica",
24-
"src": "local(\"Helvetica Neue Bold\"), local(\"HelveticaNeue-Bold\"), url(MgOpenModernaBold.ttf)",
25-
"font-weight": "bold"
26-
}
27-
},
28-
{
29-
"count": 2,
30-
"value": {
31-
"src": "url('https://example.com')",
32-
"font-family": "'Example Duplicate'"
33-
}
34-
},
3528
{
3629
"count": 1,
3730
"value": {
@@ -45,6 +38,13 @@
4538
"font-family": "\"Bitstream Vera Serif Bold\"",
4639
"src": "url(\"https://mdn.mozillademos.org/files/2468/VeraSeBd.ttf\")"
4740
}
41+
},
42+
{
43+
"count": 2,
44+
"value": {
45+
"src": "url('https://example.com')",
46+
"font-family": "'Example Duplicate'"
47+
}
4848
}
4949
]
5050
}

test/analyzer/selectors/output.json

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
"total": 6,
66
"totalUnique": 6,
77
"unique": [
8+
{
9+
"value": "#jsSelector",
10+
"count": 1
11+
},
812
{
913
"value": ".adjecent.js-selector",
1014
"count": 1
@@ -24,21 +28,13 @@
2428
{
2529
"value": "[id='js-selector']",
2630
"count": 1
27-
},
28-
{
29-
"value": "#jsSelector",
30-
"count": 1
3131
}
3232
]
3333
},
3434
"id": {
3535
"total": 4,
3636
"totalUnique": 4,
3737
"unique": [
38-
{
39-
"value": ".Foo > .Bar ~ .Baz [type=\"text\"] + span:before #bazz #fizz #buzz #drank #drugs",
40-
"count": 1
41-
},
4238
{
4339
"value": "#foo",
4440
"count": 1
@@ -50,6 +46,10 @@
5046
{
5147
"value": "#multipe #ids #counted #as #one",
5248
"count": 1
49+
},
50+
{
51+
"value": ".Foo > .Bar ~ .Baz [type=\"text\"] + span:before #bazz #fizz #buzz #drank #drugs",
52+
"count": 1
5353
}
5454
]
5555
},
@@ -58,43 +58,43 @@
5858
"totalUnique": 11,
5959
"unique": [
6060
{
61-
"value": ".foo *",
61+
"value": "*",
6262
"count": 1
6363
},
6464
{
65-
"value": ".foo * .bar",
65+
"value": "* + html .selector",
6666
"count": 1
6767
},
6868
{
69-
"value": ".selector:not(*:root)",
70-
"count": 1
69+
"value": "* html .selector",
70+
"count": 2
7171
},
7272
{
73-
"value": "*",
73+
"value": "*+html .selector",
7474
"count": 1
7575
},
7676
{
77-
"value": "* + html .selector",
77+
"value": "*:first-child + html .selector",
7878
"count": 1
7979
},
8080
{
81-
"value": "* html .selector",
82-
"count": 2
81+
"value": "*:first-child+html .selector",
82+
"count": 1
8383
},
8484
{
85-
"value": "*:first-child + html .selector",
85+
"value": "*|html[xmlns*=\"\"] .selector",
8686
"count": 1
8787
},
8888
{
89-
"value": "*:first-child+html .selector",
89+
"value": ".foo *",
9090
"count": 1
9191
},
9292
{
93-
"value": "*+html .selector",
93+
"value": ".foo * .bar",
9494
"count": 1
9595
},
9696
{
97-
"value": "*|html[xmlns*=\"\"] .selector",
97+
"value": ".selector:not(*:root)",
9898
"count": 1
9999
},
100100
{

test/analyzer/values/output.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
"total": 18,
55
"totalUnique": 12,
66
"unique": [
7-
{
8-
"value": "-apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, Helvetica, Arial, sans-serif, \"Apple Color Emoji\", \"Segoe UI Emoji\", \"Segoe UI Symbol\"",
9-
"count": 1
10-
},
117
{
128
"value": "\"-apple-system\", \"BlinkMacSystemFont\", \"Segoe UI\", \"Roboto\", \"Helvetica\", \"Arial\", sans-serif, \"Apple Color Emoji\", \"Segoe UI Emoji\", \"Segoe UI Symbol\"",
139
"count": 1
@@ -36,6 +32,10 @@
3632
"value": "\"Source Sans Pro\", serif",
3733
"count": 1
3834
},
35+
{
36+
"value": "-apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, Helvetica, Arial, sans-serif, \"Apple Color Emoji\", \"Segoe UI Emoji\", \"Segoe UI Symbol\"",
37+
"count": 1
38+
},
3939
{
4040
"value": "Consolas, \"Liberation Mono\", Menlo, Courier, monospace",
4141
"count": 1
@@ -140,11 +140,11 @@
140140
"count": 1
141141
},
142142
{
143-
"value": "rgb(100, 200, 10)",
143+
"value": "rgba(100, 200, 10, .5)",
144144
"count": 1
145145
},
146146
{
147-
"value": "rgba(100, 200, 10, .5)",
147+
"value": "rgb(100, 200, 10)",
148148
"count": 1
149149
},
150150
{

0 commit comments

Comments
 (0)