browsers.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. (function() {
  2. var Browsers, browserslist, utils;
  3. browserslist = require('browserslist');
  4. utils = require('./utils');
  5. Browsers = (function() {
  6. Browsers.prefixes = function() {
  7. var data, i, name;
  8. if (this.prefixesCache) {
  9. return this.prefixesCache;
  10. }
  11. data = require('caniuse-db/data.json').agents;
  12. return this.prefixesCache = utils.uniq((function() {
  13. var results;
  14. results = [];
  15. for (name in data) {
  16. i = data[name];
  17. results.push("-" + i.prefix + "-");
  18. }
  19. return results;
  20. })()).sort(function(a, b) {
  21. return b.length - a.length;
  22. });
  23. };
  24. Browsers.withPrefix = function(value) {
  25. if (!this.prefixesRegexp) {
  26. this.prefixesRegexp = RegExp("" + (this.prefixes().join('|')));
  27. }
  28. return this.prefixesRegexp.test(value);
  29. };
  30. function Browsers(data1, requirements, options, stats) {
  31. this.data = data1;
  32. this.options = options;
  33. this.stats = stats;
  34. this.selected = this.parse(requirements);
  35. }
  36. Browsers.prototype.parse = function(requirements) {
  37. var ref;
  38. return browserslist(requirements, {
  39. path: (ref = this.options) != null ? ref.from : void 0,
  40. stats: this.stats
  41. });
  42. };
  43. Browsers.prototype.browsers = function(criteria) {
  44. var browser, data, ref, selected, versions;
  45. selected = [];
  46. ref = this.data;
  47. for (browser in ref) {
  48. data = ref[browser];
  49. versions = criteria(data).map(function(version) {
  50. return browser + " " + version;
  51. });
  52. selected = selected.concat(versions);
  53. }
  54. return selected;
  55. };
  56. Browsers.prototype.prefix = function(browser) {
  57. var data, name, prefix, ref, version;
  58. ref = browser.split(' '), name = ref[0], version = ref[1];
  59. data = this.data[name];
  60. if (data.prefix_exceptions) {
  61. prefix = data.prefix_exceptions[version];
  62. }
  63. prefix || (prefix = data.prefix);
  64. return '-' + prefix + '-';
  65. };
  66. Browsers.prototype.isSelected = function(browser) {
  67. return this.selected.indexOf(browser) !== -1;
  68. };
  69. return Browsers;
  70. })();
  71. module.exports = Browsers;
  72. }).call(this);