index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getBrowserScope = exports.setBrowserScope = exports.getLatestStableBrowsers = exports.find = exports.isSupported = exports.getSupport = exports.features = undefined;
  6. var _lodash = require("lodash.memoize");
  7. var _lodash2 = _interopRequireDefault(_lodash);
  8. var _browserslist = require("browserslist");
  9. var _browserslist2 = _interopRequireDefault(_browserslist);
  10. var _caniuseLite = require("caniuse-lite");
  11. var _utils = require("./utils");
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. var featuresList = Object.keys(_caniuseLite.features);
  14. var browsers = void 0;
  15. function setBrowserScope(browserList) {
  16. browsers = (0, _utils.cleanBrowsersList)(browserList);
  17. }
  18. function getBrowserScope() {
  19. return browsers;
  20. }
  21. var parse = (0, _lodash2.default)(_utils.parseCaniuseData, function (feat, browsers) {
  22. return feat.title + browsers;
  23. });
  24. function getSupport(query) {
  25. var feature = void 0;
  26. try {
  27. feature = (0, _caniuseLite.feature)(_caniuseLite.features[query]);
  28. } catch (e) {
  29. var res = find(query);
  30. if (res.length === 1) return getSupport(res[0]);
  31. throw new ReferenceError("Please provide a proper feature name. Cannot find " + query);
  32. }
  33. return parse(feature, browsers);
  34. }
  35. function isSupported(feature, browsers) {
  36. var data = void 0;
  37. try {
  38. data = (0, _caniuseLite.feature)(_caniuseLite.features[feature]);
  39. } catch (e) {
  40. var res = find(feature);
  41. if (res.length === 1) {
  42. data = _caniuseLite.features[res[0]];
  43. } else {
  44. throw new ReferenceError("Please provide a proper feature name. Cannot find " + feature);
  45. }
  46. }
  47. return (0, _browserslist2.default)(browsers, { ignoreUnknownVersions: true }).map(function (browser) {
  48. return browser.split(" ");
  49. }).every(function (browser) {
  50. return data.stats[browser[0]] && data.stats[browser[0]][browser[1]] === "y";
  51. });
  52. }
  53. function find(query) {
  54. if (typeof query !== "string") {
  55. throw new TypeError("The `query` parameter should be a string.");
  56. }
  57. if (~featuresList.indexOf(query)) {
  58. // exact match
  59. return query;
  60. }
  61. return featuresList.filter(function (file) {
  62. return (0, _utils.contains)(file, query);
  63. });
  64. }
  65. function getLatestStableBrowsers() {
  66. return (0, _browserslist2.default)("last 1 version");
  67. }
  68. setBrowserScope();
  69. exports.features = featuresList;
  70. exports.getSupport = getSupport;
  71. exports.isSupported = isSupported;
  72. exports.find = find;
  73. exports.getLatestStableBrowsers = getLatestStableBrowsers;
  74. exports.setBrowserScope = setBrowserScope;
  75. exports.getBrowserScope = getBrowserScope;