Skip to content

Instantly share code, notes, and snippets.

@Josh68
Last active February 24, 2021 05:32
Show Gist options
  • Save Josh68/665087e479ebf9ff2d4befd61081be33 to your computer and use it in GitHub Desktop.
Save Josh68/665087e479ebf9ff2d4befd61081be33 to your computer and use it in GitHub Desktop.
Test API with caniuse and a given browserslist
>0.2%
not dead
not ie < 11
not op_mini all
not safari 5.1
  • npm i
  • npm run caniuse
  • Modify .browserslistrc as desired
  • Example pass: "What API do you want to test support for?" history
  • Example fail: "What API do you want to test support for?" es6
import caniuse from "caniuse-api";
import browserslist from "browserslist";
import readline from "readline";
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const prompt = () =>
new Promise((resolve) => {
rl.question("What API do you want to test support for? ", function (resp) {
resolve(resp);
});
});
const test = (api) => {
let pass = true;
// Create a support matrix object
const list = {};
// For each browserslist key (UA type) set the lowest version that needs to be supported
browserslist().forEach((entry) => {
const keyVal = entry.split(" ");
if (typeof list[keyVal[0]] === "undefined" || list[keyVal[0]] > keyVal[1]) {
list[keyVal[0]] = parseFloat(keyVal[1]);
}
});
// Get caniuse support details for passed in API
const supportList = caniuse.getSupport(api, true);
// Create an object to match the structure of the support matrix
const supported = {};
// Find minimum versions for each UA that support the passed in API
Object.keys(supportList).forEach((ua) => {
// May need to check `supportList[ua]` and `supportList[ua]["y"] exist, not sure
supported[ua] = supportList[ua]["y"]; // "y" is full support
});
Object.keys(supported).forEach((ua) => {
if (supported[ua] > list[ua]) {
pass = false;
}
});
console.info("Your browserslist with minimum versions:");
console.info(list);
console.info(`List of minimum versions that support ${api}:`);
console.info(supported);
console.info(`Pass? ${pass}`);
};
prompt()
.then((response) => {
test(response);
process.exit(0);
})
.catch((error) => {
console.error(error);
process.exit(1);
});
{
"name": "caniuse",
"version": "1.0.0",
"description": "Test feature/API support according to browserslist",
"main": "index.js",
"dependencies": {
"caniuse-lite": "^1.0.30001191",
"colorette": "^1.2.1",
"electron-to-chromium": "^1.3.672",
"escalade": "^3.1.1",
"lodash.memoize": "^4.1.2",
"lodash.uniq": "^4.5.0",
"node-releases": "^1.1.70"
},
"devDependencies": {
"browserslist": "^4.16.3",
"caniuse-api": "^3.0.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"caniuse": "node caniuse.mjs"
},
"keywords": [],
"author": "",
"license": "ISC"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment