Skip to content

Commit dbffda2

Browse files
authored
add z-index analysis (#95)
Analyze z-indexes, while ignoring CSS keywords and globals. Return a list of z-indexes sorted from low to high.
1 parent b2e3660 commit dbffda2

File tree

6 files changed

+153
-1
lines changed

6 files changed

+153
-1
lines changed

readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ analyze('foo{}')
128128
// 'values.fontsizes.total': 0,
129129
// 'values.fontsizes.totalUnique': 0,
130130
// 'values.fontsizes.unique': [],
131+
// 'values.zindexes.total': 0,
132+
// 'values.zindexes.unique': [],
133+
// 'values.zindexes.totalUnique': 0,
131134
// 'values.prefixed.share': 0,
132135
// 'values.prefixed.total': 0,
133136
// 'values.prefixed.totalUnique': 0,

src/analyzer/values/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module.exports = declarations => {
77
const colors = require('./colors.js')(declarations)
88
const browserhacks = require('./browserhacks.js')(declarations)
99
const boxshadows = require('./box-shadows.js')(declarations)
10+
const zindexes = require('./z-indexes.js')(declarations)
1011

1112
return {
1213
total: all.length,
@@ -15,6 +16,7 @@ module.exports = declarations => {
1516
fontfamilies,
1617
colors,
1718
browserhacks,
18-
boxshadows
19+
boxshadows,
20+
zindexes
1921
}
2022
}

src/analyzer/values/z-indexes.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const uniquer = require('../../utils/uniquer')
2+
const {KEYWORDS} = require('../../utils/css')
3+
4+
module.exports = declarations => {
5+
const all = declarations
6+
// Ignore all declarations that are not z-index
7+
.filter(({property}) => property === 'z-index')
8+
// Ignore all CSS keywords and globals
9+
.filter(({value}) => !KEYWORDS.includes(value))
10+
// Create a list of integers
11+
.map(({value}) => parseInt(value, 10))
12+
13+
const {unique, totalUnique} = uniquer(all, (a, b) => a - b)
14+
15+
return {
16+
total: all.length,
17+
unique,
18+
totalUnique
19+
}
20+
}

test/analyzer/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ test('Returns the correct analysis object structure', async t => {
108108
'values.fontsizes.total': 0,
109109
'values.fontsizes.totalUnique': 0,
110110
'values.fontsizes.unique': [],
111+
'values.zindexes.total': 0,
112+
'values.zindexes.totalUnique': 0,
113+
'values.zindexes.unique': [],
111114
'values.prefixed.share': 0,
112115
'values.prefixed.total': 0,
113116
'values.prefixed.totalUnique': 0,

test/analyzer/values/output.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,5 +417,10 @@
417417
"total": 0,
418418
"unique": [],
419419
"totalUnique": 0
420+
},
421+
"zindexes": {
422+
"total": 0,
423+
"unique": [],
424+
"totalUnique": 0
420425
}
421426
}

test/analyzer/values/z-indexes.js

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
const test = require('ava')
2+
const analyze = require('../../../src/analyzer/values/z-indexes.js')
3+
4+
test('It responds with the correct structure', t => {
5+
const actual = analyze([])
6+
const expected = {
7+
total: 0,
8+
unique: [],
9+
totalUnique: 0
10+
}
11+
12+
t.deepEqual(actual, expected)
13+
})
14+
15+
test('It recognizes a z-index correctly', t => {
16+
const actual = analyze([
17+
{
18+
property: 'z-index',
19+
value: '1'
20+
}
21+
])
22+
const expected = {
23+
total: 1,
24+
unique: [
25+
{
26+
value: 1,
27+
count: 1
28+
}
29+
],
30+
totalUnique: 1
31+
}
32+
33+
t.deepEqual(actual, expected)
34+
})
35+
36+
test('It ignores keywords, and global values', t => {
37+
const expected = {
38+
total: 0,
39+
totalUnique: 0,
40+
unique: []
41+
}
42+
const actual = analyze([
43+
{
44+
property: 'z-index',
45+
value: 'auto'
46+
},
47+
{
48+
property: 'z-index',
49+
value: 'unset'
50+
}
51+
])
52+
53+
t.deepEqual(actual, expected)
54+
})
55+
56+
test('It sorts multiple z-indexes correctly, from small to large', t => {
57+
const expected = {
58+
total: 4,
59+
unique: [
60+
{
61+
value: -1,
62+
count: 1
63+
},
64+
{
65+
value: 0,
66+
count: 1
67+
},
68+
{
69+
value: 2,
70+
count: 1
71+
},
72+
{
73+
value: 99999,
74+
count: 1
75+
}
76+
],
77+
totalUnique: 4
78+
}
79+
const actual = analyze([
80+
{
81+
property: 'z-index',
82+
value: '2'
83+
},
84+
{
85+
property: 'z-index',
86+
value: '99999'
87+
},
88+
{
89+
property: 'z-index',
90+
value: '0'
91+
},
92+
{
93+
property: 'z-index',
94+
value: '-1'
95+
}
96+
])
97+
98+
t.deepEqual(actual, expected)
99+
})
100+
101+
test('It ignores properties that are not z-index', t => {
102+
const expected = {
103+
total: 0,
104+
unique: [],
105+
totalUnique: 0
106+
}
107+
const actual = analyze([
108+
{
109+
property: 'line-height',
110+
value: '1'
111+
},
112+
{
113+
property: 'margin',
114+
value: '0'
115+
}
116+
])
117+
118+
t.deepEqual(actual, expected)
119+
})

0 commit comments

Comments
 (0)