Skip to content

Commit b2632c2

Browse files
authored
add max identifier count analysis (#84)
it's easier to get the max identifier count from a simple object than havingto get it from an array
1 parent d332d62 commit b2632c2

File tree

4 files changed

+60
-52
lines changed

4 files changed

+60
-52
lines changed
Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,42 @@
11
const specificity = require('specificity')
22
const {caseInsensitive: stringCompare} = require('string-natural-compare')
33

4+
// Sort by identifiers count, then by alphabet
5+
function sortByIdentifiersCount(a, b) {
6+
if (a.count === b.count) {
7+
return stringCompare(a, b)
8+
}
9+
10+
return b.count - a.count
11+
}
12+
13+
function getSelectorSpecificity(selector) {
14+
return specificity.calculate(selector).shift()
15+
}
16+
417
module.exports = selectors => {
5-
const totalSelectors = selectors.length
618
const identifiersPerSelector = selectors
7-
.map(selector => specificity.calculate(selector).shift())
8-
.map(selector => {
19+
.map(getSelectorSpecificity)
20+
.map(specificity => {
921
return {
10-
selector: selector.selector,
11-
identifiers: selector.parts.length
22+
value: specificity.selector,
23+
count: specificity.parts.length
1224
}
1325
})
1426

1527
const totalIdentifiers = identifiersPerSelector
16-
.map(selector => selector.identifiers)
28+
.map(selector => selector.count)
1729
.reduce((prev, curr) => prev + curr, 0)
18-
const average = totalIdentifiers / totalSelectors
19-
20-
const top = count => {
21-
// Sort by identifiers count, then by alphabet
22-
const sorter = (a, b) => {
23-
if (a.identifiers === b.identifiers) {
24-
return stringCompare(a, b)
25-
}
2630

27-
return b.identifiers - a.identifiers
28-
}
31+
const totalSelectors = selectors.length
32+
const average = totalIdentifiers / totalSelectors
2933

30-
return identifiersPerSelector.sort(sorter).slice(0, count)
31-
}
34+
const sorted = identifiersPerSelector.sort(sortByIdentifiersCount)
35+
const [max] = sorted
3236

3337
return {
3438
average,
35-
top: top(5)
39+
max,
40+
top: sorted.slice(0, 5)
3641
}
3742
}

src/analyzer/selectors/specificity.js

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,21 @@ module.exports = selectors => {
77
.reverse()
88

99
const top = count => {
10-
return [...all]
11-
.slice(0, count)
12-
.map(selector => {
13-
const spec = specificity
14-
.calculate(selector)
15-
.shift()
16-
.specificityArray
10+
return [...all].slice(0, count).map(selector => {
11+
const [a, b, c, d] = specificity
12+
.calculate(selector)
13+
.shift().specificityArray
1714

18-
return {
19-
selector,
20-
specificity: {
21-
a: spec[0],
22-
b: spec[1],
23-
c: spec[2],
24-
d: spec[3]
25-
}
15+
return {
16+
value: selector,
17+
specificity: {
18+
a,
19+
b,
20+
c,
21+
d
2622
}
27-
})
23+
}
24+
})
2825
}
2926

3027
return {

test/analyzer/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,14 @@ test('Returns the correct analysis object structure', async t => {
6868
'selectors.id.totalUnique': 0,
6969
'selectors.id.unique': [],
7070
'selectors.identifiers.average': 1,
71-
'selectors.identifiers.top': [{identifiers: 1, selector: 'foo'}],
71+
'selectors.identifiers.top': [{count: 1, value: 'foo'}],
72+
'selectors.identifiers.max.count': 1,
73+
'selectors.identifiers.max.value': 'foo',
7274
'selectors.js.total': 0,
7375
'selectors.js.totalUnique': 0,
7476
'selectors.js.unique': [],
7577
'selectors.specificity.top': [
76-
{selector: 'foo', specificity: {a: 0, b: 0, c: 0, d: 1}}
78+
{value: 'foo', specificity: {a: 0, b: 0, c: 0, d: 1}}
7779
],
7880
'selectors.total': 1,
7981
'selectors.totalUnique': 1,

test/analyzer/selectors/output.json

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108
"specificity": {
109109
"top": [
110110
{
111-
"selector": ".Foo > .Bar ~ .Baz [type=\"text\"] + span:before #bazz #fizz #buzz #drank #drugs",
111+
"value": ".Foo > .Bar ~ .Baz [type=\"text\"] + span:before #bazz #fizz #buzz #drank #drugs",
112112
"specificity": {
113113
"a": 0,
114114
"b": 5,
@@ -117,7 +117,7 @@
117117
}
118118
},
119119
{
120-
"selector": "#multipe #ids #counted #as #one",
120+
"value": "#multipe #ids #counted #as #one",
121121
"specificity": {
122122
"a": 0,
123123
"b": 5,
@@ -126,7 +126,7 @@
126126
}
127127
},
128128
{
129-
"selector": "#foo",
129+
"value": "#foo",
130130
"specificity": {
131131
"a": 0,
132132
"b": 1,
@@ -135,7 +135,7 @@
135135
}
136136
},
137137
{
138-
"selector": "#jsSelector",
138+
"value": "#jsSelector",
139139
"specificity": {
140140
"a": 0,
141141
"b": 1,
@@ -144,7 +144,7 @@
144144
}
145145
},
146146
{
147-
"selector": ".a .b .c .d .e .f .g .h .i .j .k .l .m .n .o .p .q .r .s .t .u .v .w .x .y .z",
147+
"value": ".a .b .c .d .e .f .g .h .i .j .k .l .m .n .o .p .q .r .s .t .u .v .w .x .y .z",
148148
"specificity": {
149149
"a": 0,
150150
"b": 0,
@@ -156,26 +156,30 @@
156156
},
157157
"identifiers": {
158158
"average": 2.8461538461538463,
159+
"max": {
160+
"count": 26,
161+
"value": ".a .b .c .d .e .f .g .h .i .j .k .l .m .n .o .p .q .r .s .t .u .v .w .x .y .z"
162+
},
159163
"top": [
160164
{
161-
"identifiers": 26,
162-
"selector": ".a .b .c .d .e .f .g .h .i .j .k .l .m .n .o .p .q .r .s .t .u .v .w .x .y .z"
165+
"count": 26,
166+
"value": ".a .b .c .d .e .f .g .h .i .j .k .l .m .n .o .p .q .r .s .t .u .v .w .x .y .z"
163167
},
164168
{
165-
"identifiers": 11,
166-
"selector": ".Foo > .Bar ~ .Baz [type=\"text\"] + span:before #bazz #fizz #buzz #drank #drugs"
169+
"count": 11,
170+
"value": ".Foo > .Bar ~ .Baz [type=\"text\"] + span:before #bazz #fizz #buzz #drank #drugs"
167171
},
168172
{
169-
"identifiers": 5,
170-
"selector": "#multipe #ids #counted #as #one"
173+
"count": 5,
174+
"value": "#multipe #ids #counted #as #one"
171175
},
172176
{
173-
"identifiers": 3,
174-
"selector": "[role=\"menuitem\"][aria-checked=\"true\"]::before"
177+
"count": 3,
178+
"value": "[role=\"menuitem\"][aria-checked=\"true\"]::before"
175179
},
176180
{
177-
"identifiers": 2,
178-
"selector": "* html .selector"
181+
"count": 2,
182+
"value": "* html .selector"
179183
}
180184
]
181185
},

0 commit comments

Comments
 (0)