This repository has been archived by the owner on Jan 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrowserHelper.js
62 lines (51 loc) · 2.07 KB
/
browserHelper.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
const chalk = require('chalk');
const caniuse = require('caniuse-api');
const matched = x => ({
on: () => matched(x),
otherwise: () => x,
})
const match = x => ({
on: (pred, fn) => (pred(x) ? matched(fn(x)) : match(x)),
otherwise: fn => fn(x),
})
const allowedBrowsers = ['chrome', 'firefox', 'safari', 'edge', 'ie', 'and_chr', 'ios_saf'];
const browserNameMap = {
'chrome': 'Chrome',
'firefox': 'Firebox',
'safari': 'Safari',
'edge': 'Edge',
'ie': 'Internet Explorer',
'and_chr': 'Android Chrome',
'ios_saf': 'iOS Safari',
}
const featureNameMap = {
'web-app-manifest': 'Web Application Manifest',
'serviceworkers': 'Service Workers',
'offline-apps': 'Offline Web Application',
'push-api': 'Push Notifications'
}
const getNameForBrowser = (key) => browserNameMap[key] || key;
const getNameForFeature = (key) => featureNameMap[key] || key;
const hasKey = (key) => (obj) => obj[key];
const isSupported = hasKey('y');
const isPartiallySupported = hasKey('a');
const isSupportedWithPrefix = hasKey('x');
const isNotSupported = hasKey('n');
const logSupport = (browser, color, message, key) => (version) => console.log(getNameForBrowser(browser) + ' ' + chalk[color](message + ' ' + version[key]));
const getFeatureSupportFor = (allowedBrowsers) => (featureName) => {
const feature = caniuse.getSupport(featureName);
const mappedFeatureName = getNameForFeature(featureName);
console.log(`
${mappedFeatureName}
${chalk.yellow('=======================================')}
`)
Object.keys(feature).filter(browser => allowedBrowsers.indexOf(browser) !== -1).map(browser => {
match(feature[browser])
.on(isSupported, logSupport(browser, 'green', 'supported ≥', 'y'))
.on(isPartiallySupported, logSupport(browser, 'yellow', 'partially supported ≥', 'a'))
.on(isSupportedWithPrefix, logSupport(browser, 'magenta', 'supported with prefix ≥', 'x'))
.on(isNotSupported, logSupport(browser, 'red', 'not supported ≤', 'n'))
.otherwise(() => console.log('no value'))
});
}
module.exports = getFeatureSupportFor(allowedBrowsers);