Last active
June 20, 2019 15:48
-
-
Save jonathantneal/fb625b55e94709a8763802b0baea075b to your computer and use it in GitHub Desktop.
Return a list (`[]`) of all unsupported browsers (`"${browser} ${version}"`) by a caniuse id (`"${id}"`)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const caniuse = require('caniuse-lite'); | |
// return a list of all unsupported browsers by a caniuse id | |
function getUnsupportedBrowsersByCaniuseId (caniuseId) { | |
// get compressed feature details from caniuse-lite by a caniuse id | |
const featureDetails = caniuse.features[caniuseId]; | |
// if the feature exists | |
if (featureDetails) { | |
// get browsers stats for the feature | |
const { stats: statsByBrowser } = caniuse.feature(featureDetails); | |
// get a list of all unsupported browsers from the browsers stats | |
const unsupportedBrowsers = Object.keys(statsByBrowser).reduce( | |
(unsupportedBrowsers, browser) => { | |
// get the browser stats organized by version | |
const statsByBrowserVersion = statsByBrowser[browser]; | |
// get a list of all unsupported browser versions | |
const unsupportedBrowserVersions = Object.keys(statsByBrowserVersion).reduce( | |
(unsupportedBrowserVersions, version) => { | |
// get the statistic for the specific browser version | |
const statForBrowserVersion = statsByBrowserVersion[version]; | |
// if the feature is not fully supported by the specific browser version | |
if (statForBrowserVersion[0] !== 'y') { | |
// if the version is "all" | |
if (version === 'all') { | |
// if the agent has known versions then push every browser version query | |
if ('versions' in Object(caniuse.agents[browser])) { | |
caniuse.agents[browser].versions.filter(version => version !== null).forEach(version => { | |
unsupportedBrowserVersions.push(`${browser} ${version}`); | |
}); | |
} | |
// otherwise, if the agent has no known versiosn, push a generic query | |
else { | |
unsupportedBrowserVersions.push(`${browser} > 0`); | |
} | |
} | |
// otherwise, if the version is not "all", push the specific browser version query | |
else { | |
unsupportedBrowserVersions.push(`${browser} ${version}`); | |
} | |
} | |
// return the list of all unsupported browser versions | |
return unsupportedBrowserVersions; | |
}, | |
[] | |
); | |
// push the unsupported browser versions to the list of all unsupported browsers | |
unsupportedBrowsers.push(...unsupportedBrowserVersions); | |
// return the list of all unsupported browsers | |
return unsupportedBrowsers; | |
}, | |
[] | |
); | |
// return the list of all unsupported browsers | |
return unsupportedBrowsers; | |
} | |
// otherwise, return a generic query for all browsers | |
return [ '> 0%' ]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment