UNPKG

2.17 kBJavaScriptView Raw
1"use strict";
2
3var browserslist = require('browserslist');
4
5var agents = require('caniuse-lite').agents;
6
7var utils = require('./utils');
8
9var Browsers = /*#__PURE__*/function () {
10 /**
11 * Return all prefixes for default browser data
12 */
13 Browsers.prefixes = function prefixes() {
14 if (this.prefixesCache) {
15 return this.prefixesCache;
16 }
17
18 this.prefixesCache = [];
19
20 for (var name in agents) {
21 this.prefixesCache.push("-" + agents[name].prefix + "-");
22 }
23
24 this.prefixesCache = utils.uniq(this.prefixesCache).sort(function (a, b) {
25 return b.length - a.length;
26 });
27 return this.prefixesCache;
28 }
29 /**
30 * Check is value contain any possible prefix
31 */
32 ;
33
34 Browsers.withPrefix = function withPrefix(value) {
35 if (!this.prefixesRegexp) {
36 this.prefixesRegexp = new RegExp(this.prefixes().join('|'));
37 }
38
39 return this.prefixesRegexp.test(value);
40 };
41
42 function Browsers(data, requirements, options, browserslistOpts) {
43 this.data = data;
44 this.options = options || {};
45 this.browserslistOpts = browserslistOpts || {};
46 this.selected = this.parse(requirements);
47 }
48 /**
49 * Return browsers selected by requirements
50 */
51
52
53 var _proto = Browsers.prototype;
54
55 _proto.parse = function parse(requirements) {
56 var opts = {};
57
58 for (var i in this.browserslistOpts) {
59 opts[i] = this.browserslistOpts[i];
60 }
61
62 opts.path = this.options.from;
63 opts.env = this.options.env;
64 return browserslist(requirements, opts);
65 }
66 /**
67 * Return prefix for selected browser
68 */
69 ;
70
71 _proto.prefix = function prefix(browser) {
72 var _browser$split = browser.split(' '),
73 name = _browser$split[0],
74 version = _browser$split[1];
75
76 var data = this.data[name];
77 var prefix = data.prefix_exceptions && data.prefix_exceptions[version];
78
79 if (!prefix) {
80 prefix = data.prefix;
81 }
82
83 return "-" + prefix + "-";
84 }
85 /**
86 * Is browser is selected by requirements
87 */
88 ;
89
90 _proto.isSelected = function isSelected(browser) {
91 return this.selected.includes(browser);
92 };
93
94 return Browsers;
95}();
96
97module.exports = Browsers;
\No newline at end of file