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