-
Notifications
You must be signed in to change notification settings - Fork 322
/
feature-support.js
65 lines (61 loc) · 2.05 KB
/
feature-support.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
function describeSupport(input) {
switch (input) {
case 'no': {
return {
className: 'no-support',
description: `<span class="support">no support</span>`,
};
}
case 'yes': {
return {
className: 'has-support',
description: `<span class="support">supported</span>`,
};
}
case 'partial': {
return {
className: 'partial-support',
description: `<span class="support">partially supported</span>`,
};
}
default: {
return {
className: 'has-support',
description: `<span class="support">supported since version <span class="version">${input}</span></span>`,
};
}
}
}
const mapFromEnvironmentIdsToNames = new Map([
['chrome', 'Chrome'],
['firefox', 'Firefox'],
['safari', 'Safari'],
['nodejs', 'Node.js'],
['babel', 'Babel'],
]);
function environmentIdToName(input) {
return mapFromEnvironmentIdsToNames.get(input);
}
function expandFeatureSupport(input) {
// https://stackoverflow.com/a/1732454/96656
const re = /<feature-support\s+chrome="(?<chrome>[^"]+)"\s+firefox="(?<firefox>[^"]+)"\s+safari="(?<safari>[^"]+)"\s+nodejs="(?<nodejs>[^"]+)"\s+babel="(?<babel>[^"]+)"><\/feature-support>/g;
return input.replace(re, (...args) => {
const groups = args[args.length - 1];
const buf = ['<ul class="feature-support">'];
for (const [key, value] of Object.entries(groups)) {
const [version, url] = value.split(' ');
const {className, description} = describeSupport(version);
buf.push(`
<li class="environment ${ className }${ url ? ' has-link' : ''}">
${ url ? `<a href="${ encodeURI(url) }">` : '' }
<span class="icon ${ key }">${ environmentIdToName(key) }:</span>
${ description }
${ url ? '</a>' : '' }
</li>
`);
}
buf.push('</ul><div class="feature-support-info"><a href="/features/support">about this feature support listing</a></div>');
return buf.join('\n').replace(/\s+/g, ' ');
});
}
module.exports = expandFeatureSupport;