caniuse-support
Version:
Query the caniuse offline database for feature support.
87 lines (85 loc) • 2.85 kB
JavaScript
;
/**
* @license
* Copyright (C) 2016 Chi Vinh Le and contributors.
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var lite = require("caniuse-lite");
/**
* Query the caniuse database for feature support.
*
* @param {String} feature see full list https://github.com/Fyrd/caniuse/tree/master/features-json.
* @param {{id: string; version: string}} browser
*/
function getSupport(feature, browser) {
var support = { level: "unknown", needPrefix: false, notes: [] };
var stats = lite.feature(require("caniuse-lite/data/features/" + feature + ".js")).stats[browser.id];
if (!stats) {
return support;
}
var versionIdx = getVersionIndex(browser);
if (versionIdx) {
var value = stats[versionIdx];
if (value.indexOf("y") !== -1) {
support.level = "full";
}
else if (value.indexOf("a") !== -1) {
support.level = "partial";
}
else {
support.level = "none";
}
support.needPrefix = value.indexOf("x") !== -1;
value.split(" ").forEach(function (s) {
if (s === "y") {
support.level = "full";
}
else if (s === "a") {
support.level = "partial";
}
else if (s[0] === "#") {
support.notes.push(parseInt(s.substr(1), 10));
}
});
}
return support;
}
exports.getSupport = getSupport;
/**
* Get matching browser version index of the caniuse db.
* Will return last known version index if target version > any known versions.
* Returns an empty string if target version < any known versions.
*
* @param {{id: string; version: string}} browser
*/
function getVersionIndex(browser) {
var targetVersion = parseFloat(browser.version);
var stats = lite.feature(require("caniuse-lite/data/features/css-appearance.js")).stats[browser.id];
if (!stats) {
return "";
}
// Sorted contains a list with sorted version numbers.
var sorted = Object.keys(stats).sort(function (a, b) { return parseFloat(a) - parseFloat(b); });
// At the end of this loop, match contains the closest matching version.
var match = "";
for (var _i = 0, sorted_1 = sorted; _i < sorted_1.length; _i++) {
var v = sorted_1[_i];
var from = v.split("-").map(function (x) { return parseFloat(x); })[0];
if (isNaN(from)) {
match = v.split("-")[0];
break;
}
if (targetVersion >= from) {
match = v;
}
else {
break;
}
}
return match;
}
exports.getVersionIndex = getVersionIndex;
//# sourceMappingURL=support.js.map