browsers.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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, ref1;
  38. return browserslist(requirements, {
  39. stats: this.stats,
  40. path: (ref = this.options) != null ? ref.from : void 0,
  41. env: (ref1 = this.options) != null ? ref1.env : void 0
  42. });
  43. };
  44. Browsers.prototype.browsers = function(criteria) {
  45. var browser, data, ref, selected, versions;
  46. selected = [];
  47. ref = this.data;
  48. for (browser in ref) {
  49. data = ref[browser];
  50. versions = criteria(data).map(function(version) {
  51. return browser + " " + version;
  52. });
  53. selected = selected.concat(versions);
  54. }
  55. return selected;
  56. };
  57. Browsers.prototype.prefix = function(browser) {
  58. var data, name, prefix, ref, version;
  59. ref = browser.split(' '), name = ref[0], version = ref[1];
  60. data = this.data[name];
  61. if (data.prefix_exceptions) {
  62. prefix = data.prefix_exceptions[version];
  63. }
  64. prefix || (prefix = data.prefix);
  65. return '-' + prefix + '-';
  66. };
  67. Browsers.prototype.isSelected = function(browser) {
  68. return this.selected.indexOf(browser) !== -1;
  69. };
  70. return Browsers;
  71. })();
  72. module.exports = Browsers;
  73. }).call(this);