Skip to content

Commit 91e0857

Browse files
committed
Simplify browser ranges
Fixes #184
1 parent e06e7c6 commit 91e0857

File tree

4 files changed

+85
-14
lines changed

4 files changed

+85
-14
lines changed

README.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ cat main.css | doiuse --browsers "ie >= 9, > 1%, last 2 versions"
2323

2424
**Sample output:**
2525
```
26-
/projects/website/main.css:5:3: CSS3 Box-sizing not supported by: IE (8,9,10,11), Chrome (36,37,38), Safari (8,7.1), Opera (24,25), iOS Safari (8,7.1,8.1), Android Browser (4.1,4.4,4.4.4), IE Mobile (10,11)
27-
/projects/website/main.css:6:3: CSS3 Box-sizing not supported by: IE (8,9,10,11), Chrome (36,37,38), Safari (8,7.1), Opera (24,25), iOS Safari (8,7.1,8.1), Android Browser (4.1,4.4,4.4.4), IE Mobile (10,11)
28-
/projects/website/main.css:8:3: CSS user-select: none not supported by: IE (8,9)
29-
/projects/website/main.css:9:3: CSS user-select: none not supported by: IE (8,9)
30-
/projects/website/main.css:10:3: CSS user-select: none not supported by: IE (8,9)
31-
/projects/website/main.css:11:3: CSS user-select: none not supported by: IE (8,9)
32-
/projects/website/main.css:12:3: CSS user-select: none not supported by: IE (8,9)
33-
/projects/website/main.css:13:3: Pointer events not supported by: IE (8,9,10), Firefox (32,33), Chrome (36,37,38), Safari (8,7.1), Opera (24,25), iOS Safari (8,7.1,8.1), Android Browser (4.1,4.4,4.4.4), IE Mobile (10)
34-
/projects/website/main.css:14:3: Pointer events not supported by: IE (8,9,10), Firefox (32,33), Chrome (36,37,38), Safari (8,7.1), Opera (24,25), iOS Safari (8,7.1,8.1), Android Browser (4.1,4.4,4.4.4), IE Mobile (10)
26+
/projects/website/main.css:5:3: CSS3 Box-sizing not supported by: IE (8-11), Chrome (36-38), Safari (8,7.1), Opera (24-25), iOS Safari (8,7.1,8.1), Android Browser (4.1,4.4,4.4.4), IE Mobile (10-11)
27+
/projects/website/main.css:6:3: CSS3 Box-sizing not supported by: IE (8-11), Chrome (36-38), Safari (8,7.1), Opera (24-25), iOS Safari (8,7.1,8.1), Android Browser (4.1,4.4,4.4.4), IE Mobile (10-11)
28+
/projects/website/main.css:8:3: CSS user-select: none not supported by: IE (8-9)
29+
/projects/website/main.css:9:3: CSS user-select: none not supported by: IE (8-9)
30+
/projects/website/main.css:10:3: CSS user-select: none not supported by: IE (8-9)
31+
/projects/website/main.css:11:3: CSS user-select: none not supported by: IE (8-9)
32+
/projects/website/main.css:12:3: CSS user-select: none not supported by: IE (8-9)
33+
/projects/website/main.css:13:3: Pointer events not supported by: IE (8-10), Firefox (32-33), Chrome (36-38), Safari (8,7.1), Opera (24-25), iOS Safari (8,7.1,8.1), Android Browser (4.1,4.4,4.4.4), IE Mobile (10)
34+
/projects/website/main.css:14:3: Pointer events not supported by: IE (8-10), Firefox (32-33), Chrome (36-38), Safari (8,7.1), Opera (24-25), iOS Safari (8,7.1,8.1), Android Browser (4.1,4.4,4.4.4), IE Mobile (10)
3535
/projects/website/main.css:32:3: CSS3 Transforms not supported by: IE (8)
3636
```
3737

test/cli.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ const pathToCli = ` node ${joinPath(selfPath, '../bin/cli.js')}`;
1111
const catCss = ` cat ${cssFile} `;
1212

1313
const expectedCssGradients = [
14-
'<streaming css input>:8:1: CSS Gradients not supported by: IE (8,9) (css-gradients)\n',
15-
'<streaming css input>:12:1: CSS Gradients not supported by: IE (8,9) (css-gradients)\n',
14+
'<streaming css input>:8:1: CSS Gradients not supported by: IE (8-9) (css-gradients)\n',
15+
'<streaming css input>:12:1: CSS Gradients not supported by: IE (8-9) (css-gradients)\n',
1616
];
1717

1818
const expectedCssRepeatingGradients = [
19-
'<streaming css input>:16:1: CSS Repeating Gradients not supported by: IE (8,9) (css-repeating-gradients)\n',
20-
'<streaming css input>:20:1: CSS Repeating Gradients not supported by: IE (8,9) (css-repeating-gradients)\n',
19+
'<streaming css input>:16:1: CSS Repeating Gradients not supported by: IE (8-9) (css-repeating-gradients)\n',
20+
'<streaming css input>:20:1: CSS Repeating Gradients not supported by: IE (8-9) (css-repeating-gradients)\n',
2121
];
2222
const expected = [
2323
...expectedCssGradients,

test/util.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { test } from 'tap';
2+
import { formatBrowserName } from '../utils/util.js';
3+
4+
test('works', (t) => {
5+
const cases = [
6+
{
7+
msg: 'consecutive integers',
8+
versions: ['8', '9', '10', '11'],
9+
expected: 'IE (8-11)',
10+
},
11+
{
12+
msg: 'multiple ranges',
13+
versions: ['8', '9', '11', '12', '17', '19'],
14+
expected: 'IE (8-9,11-12,17,19)',
15+
},
16+
{
17+
msg: 'minor version',
18+
versions: ['8', '9', '9.1', '10', '11'],
19+
expected: 'IE (8-9,9.1,10-11)',
20+
},
21+
{
22+
msg: 'non numerical version',
23+
versions: ['8', '9', '9-beta', '10', '11'],
24+
expected: 'IE (8-9,9-beta,10-11)',
25+
},
26+
];
27+
28+
for (const caseItem of cases) {
29+
const actual = formatBrowserName('ie', caseItem.versions);
30+
t.same(actual, caseItem.expected, caseItem.msg);
31+
}
32+
t.end();
33+
});

utils/util.js

+39-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,45 @@ export function formatBrowserName(browserKey, versions) {
2323
if (!versions) {
2424
return browserName || '';
2525
}
26-
return (`${browserName} (${versions.join(',')})`);
26+
27+
const ranges = [];
28+
let rangeStart = -1;
29+
let rangeEnd = -1;
30+
31+
for (const [index, versionString] of versions.entries()) {
32+
const current = +versionString;
33+
const next = +versions[index + 1];
34+
35+
if (Number.isInteger(current)) {
36+
if (rangeStart === -1) {
37+
rangeStart = current;
38+
rangeEnd = current;
39+
} else if (current === rangeEnd + 1) {
40+
rangeEnd = current;
41+
} else {
42+
ranges.push(rangeStart === rangeEnd ? [rangeStart] : [rangeStart, rangeEnd]);
43+
rangeStart = current;
44+
rangeEnd = current;
45+
}
46+
47+
if (!Number.isInteger(next) || current + 1 !== next) {
48+
ranges.push(rangeStart === rangeEnd ? [rangeStart] : [rangeStart, rangeEnd]);
49+
rangeStart = -1;
50+
rangeEnd = -1;
51+
}
52+
} else {
53+
if (rangeStart !== -1) {
54+
ranges.push(rangeStart === rangeEnd ? [rangeStart] : [rangeStart, rangeEnd]);
55+
rangeStart = -1;
56+
rangeEnd = -1;
57+
}
58+
ranges.push([versionString]);
59+
}
60+
}
61+
62+
const versionString = ranges.map((range) => range.join('-')).join(',');
63+
64+
return `${browserName} (${versionString})`;
2765
}
2866

2967
/**

0 commit comments

Comments
 (0)