2019-02-14 16:06:14 +00:00
|
|
|
import browserParsersList from './parser-browsers.js';
|
|
|
|
import osParsersList from './parser-os.js';
|
|
|
|
import platformParsersList from './parser-platforms.js';
|
|
|
|
import enginesParsersList from './parser-engines.js';
|
|
|
|
import Utils from './utils.js';
|
2017-04-04 20:03:47 +00:00
|
|
|
|
2018-07-05 19:44:43 +00:00
|
|
|
/**
|
|
|
|
* The main class that arranges the whole parsing process.
|
|
|
|
*/
|
2017-04-04 20:03:47 +00:00
|
|
|
class Parser {
|
2017-04-09 14:13:00 +00:00
|
|
|
/**
|
|
|
|
* Create instance of Parser
|
2018-06-30 11:26:39 +00:00
|
|
|
*
|
2018-07-05 19:44:43 +00:00
|
|
|
* @param {String} UA User-Agent string
|
|
|
|
* @param {Boolean} [skipParsing=false] parser can skip parsing in purpose of performance
|
2018-06-30 11:26:39 +00:00
|
|
|
* improvements if you need to make a more particular parsing
|
2018-07-05 19:44:43 +00:00
|
|
|
* like {@link Parser#parseBrowser} or {@link Parser#parsePlatform}
|
2018-06-30 11:26:39 +00:00
|
|
|
*
|
|
|
|
* @throw {Error} in case of empty UA String
|
|
|
|
*
|
2017-04-09 14:13:00 +00:00
|
|
|
* @constructor
|
|
|
|
*/
|
2018-06-30 11:26:39 +00:00
|
|
|
constructor(UA, skipParsing = false) {
|
2017-12-20 21:29:06 +00:00
|
|
|
if (UA === void (0) || UA === null || UA === '') {
|
2017-04-09 14:13:00 +00:00
|
|
|
throw new Error("UserAgent parameter can't be empty");
|
|
|
|
}
|
|
|
|
|
2017-04-04 20:03:47 +00:00
|
|
|
this._ua = UA;
|
2018-07-05 19:44:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef ParsedResult
|
|
|
|
* @property {Object} browser
|
2018-07-08 09:31:23 +00:00
|
|
|
* @property {String|undefined} [browser.name]
|
|
|
|
* Browser name, like `"Chrome"` or `"Internet Explorer"`
|
|
|
|
* @property {String|undefined} [browser.version] Browser version as a String `"12.01.45334.10"`
|
2018-07-05 19:44:43 +00:00
|
|
|
* @property {Object} os
|
2018-07-08 09:31:23 +00:00
|
|
|
* @property {String|undefined} [os.name] OS name, like `"Windows"` or `"macOS"`
|
|
|
|
* @property {String|undefined} [os.version] OS version, like `"NT 5.1"` or `"10.11.1"`
|
|
|
|
* @property {String|undefined} [os.versionName] OS name, like `"XP"` or `"High Sierra"`
|
2018-07-05 19:44:43 +00:00
|
|
|
* @property {Object} platform
|
2018-07-08 09:31:23 +00:00
|
|
|
* @property {String|undefined} [platform.type]
|
|
|
|
* platform type, can be either `"desktop"`, `"tablet"` or `"mobile"`
|
|
|
|
* @property {String|undefined} [platform.vendor] Vendor of the device,
|
|
|
|
* like `"Apple"` or `"Samsung"`
|
|
|
|
* @property {String|undefined} [platform.model] Device model,
|
|
|
|
* like `"iPhone"` or `"Kindle Fire HD 7"`
|
2018-07-05 19:44:43 +00:00
|
|
|
* @property {Object} engine
|
2018-07-08 09:31:23 +00:00
|
|
|
* @property {String|undefined} [engine.name]
|
|
|
|
* Can be any of this: `WebKit`, `Blink`, `Gecko`, `Trident`, `Presto`, `EdgeHTML`
|
|
|
|
* @property {String|undefined} [engine.version] String version of the engine
|
2018-07-05 19:44:43 +00:00
|
|
|
*/
|
2017-04-09 14:13:00 +00:00
|
|
|
this.parsedResult = {};
|
2018-06-30 11:26:39 +00:00
|
|
|
|
|
|
|
if (skipParsing !== true) {
|
|
|
|
this.parse();
|
|
|
|
}
|
2017-04-04 20:03:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-09 14:13:00 +00:00
|
|
|
/**
|
|
|
|
* Get UserAgent string of current Parser instance
|
2018-07-05 19:44:43 +00:00
|
|
|
* @return {String} User-Agent String of the current <Parser> object
|
2017-04-09 14:13:00 +00:00
|
|
|
*
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
getUA() {
|
|
|
|
return this._ua;
|
|
|
|
}
|
|
|
|
|
2017-04-09 19:46:23 +00:00
|
|
|
/**
|
|
|
|
* Test a UA string for a regexp
|
|
|
|
* @param {RegExp} regex
|
|
|
|
* @return {Boolean}
|
|
|
|
*/
|
|
|
|
test(regex) {
|
|
|
|
return regex.test(this._ua);
|
|
|
|
}
|
|
|
|
|
2017-04-09 14:13:00 +00:00
|
|
|
/**
|
|
|
|
* Get parsed browser object
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
2018-06-30 11:26:39 +00:00
|
|
|
parseBrowser() {
|
2017-04-09 14:13:00 +00:00
|
|
|
this.parsedResult.browser = {};
|
|
|
|
|
2017-12-20 21:29:06 +00:00
|
|
|
const browserDescriptor = browserParsersList.find((_browser) => {
|
2017-04-09 19:09:47 +00:00
|
|
|
if (typeof _browser.test === 'function') {
|
|
|
|
return _browser.test(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_browser.test instanceof Array) {
|
2017-12-20 21:29:06 +00:00
|
|
|
return _browser.test.some(condition => this.test(condition));
|
2017-04-09 19:09:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error("Browser's test function is not valid");
|
2017-04-04 20:03:47 +00:00
|
|
|
});
|
2017-04-09 14:13:00 +00:00
|
|
|
|
2017-05-18 19:57:18 +00:00
|
|
|
if (browserDescriptor) {
|
|
|
|
this.parsedResult.browser = browserDescriptor.describe(this.getUA());
|
2017-04-09 14:13:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.parsedResult.browser;
|
|
|
|
}
|
|
|
|
|
2017-04-09 16:16:38 +00:00
|
|
|
/**
|
|
|
|
* Get parsed browser object
|
|
|
|
* @return {Object}
|
|
|
|
*
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
getBrowser() {
|
|
|
|
if (this.parsedResult.browser) {
|
|
|
|
return this.parsedResult.browser;
|
|
|
|
}
|
|
|
|
|
2018-06-30 11:26:39 +00:00
|
|
|
return this.parseBrowser();
|
2017-04-09 16:16:38 +00:00
|
|
|
}
|
|
|
|
|
2017-04-09 14:13:00 +00:00
|
|
|
/**
|
2017-04-09 19:46:23 +00:00
|
|
|
* Get browser's name
|
2018-07-02 19:24:02 +00:00
|
|
|
* @return {String} Browser's name or an empty string
|
2017-04-09 19:46:23 +00:00
|
|
|
*
|
|
|
|
* @public
|
2017-04-09 14:13:00 +00:00
|
|
|
*/
|
2017-06-09 20:04:43 +00:00
|
|
|
getBrowserName(toLowerCase) {
|
|
|
|
if (toLowerCase) {
|
2018-07-02 19:24:02 +00:00
|
|
|
return String(this.getBrowser().name).toLowerCase() || '';
|
2017-06-09 20:04:43 +00:00
|
|
|
}
|
2018-07-02 19:24:02 +00:00
|
|
|
return this.getBrowser().name || '';
|
2017-04-09 19:46:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-08 17:51:04 +00:00
|
|
|
/**
|
|
|
|
* Get browser's version
|
|
|
|
* @return {String} version of browser
|
|
|
|
*
|
|
|
|
* @public
|
|
|
|
*/
|
2017-04-09 19:46:23 +00:00
|
|
|
getBrowserVersion() {
|
|
|
|
return this.getBrowser().version;
|
2017-04-04 20:03:47 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 17:51:04 +00:00
|
|
|
/**
|
|
|
|
* Get OS
|
|
|
|
* @return {Object}
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* this.getOS();
|
|
|
|
* {
|
|
|
|
* name: 'macOS',
|
|
|
|
* version: '10.11.12'
|
|
|
|
* }
|
|
|
|
*/
|
2017-04-15 19:46:18 +00:00
|
|
|
getOS() {
|
|
|
|
if (this.parsedResult.os) {
|
|
|
|
return this.parsedResult.os;
|
|
|
|
}
|
|
|
|
|
2018-06-30 11:26:39 +00:00
|
|
|
return this.parseOS();
|
2017-04-15 19:46:18 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 17:51:04 +00:00
|
|
|
/**
|
|
|
|
* Parse OS and save it to this.parsedResult.os
|
|
|
|
* @return {*|{}}
|
|
|
|
*/
|
2018-06-30 11:26:39 +00:00
|
|
|
parseOS() {
|
2017-04-15 19:46:18 +00:00
|
|
|
this.parsedResult.os = {};
|
|
|
|
|
2017-12-20 21:29:06 +00:00
|
|
|
const os = osParsersList.find((_os) => {
|
2017-04-15 19:46:18 +00:00
|
|
|
if (typeof _os.test === 'function') {
|
|
|
|
return _os.test(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_os.test instanceof Array) {
|
2017-12-20 21:29:06 +00:00
|
|
|
return _os.test.some(condition => this.test(condition));
|
2017-04-15 19:46:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error("Browser's test function is not valid");
|
|
|
|
});
|
|
|
|
|
|
|
|
if (os) {
|
2017-05-18 19:57:18 +00:00
|
|
|
this.parsedResult.os = os.describe(this.getUA());
|
2017-04-15 19:46:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.parsedResult.os;
|
|
|
|
}
|
|
|
|
|
2017-06-08 17:51:04 +00:00
|
|
|
/**
|
|
|
|
* Get OS name
|
2017-06-08 22:12:44 +00:00
|
|
|
* @param {Boolean} [toLowerCase] return lower-cased value
|
2017-06-08 17:51:04 +00:00
|
|
|
* @return {String} name of the OS — macOS, Windows, Linux, etc.
|
|
|
|
*/
|
2017-06-08 22:12:44 +00:00
|
|
|
getOSName(toLowerCase) {
|
2017-12-20 21:29:06 +00:00
|
|
|
const { name } = this.getOS();
|
2017-06-08 22:12:44 +00:00
|
|
|
|
|
|
|
if (toLowerCase) {
|
2018-07-02 19:24:02 +00:00
|
|
|
return String(name).toLowerCase() || '';
|
2017-06-08 22:12:44 +00:00
|
|
|
}
|
|
|
|
|
2018-07-02 19:24:02 +00:00
|
|
|
return name || '';
|
2017-04-15 19:46:18 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 17:51:04 +00:00
|
|
|
/**
|
|
|
|
* Get OS version
|
|
|
|
* @return {String} full version with dots ('10.11.12', '5.6', etc)
|
|
|
|
*/
|
2017-05-18 19:57:18 +00:00
|
|
|
getOSVersion() {
|
2017-04-15 19:46:18 +00:00
|
|
|
return this.getOS().version;
|
|
|
|
}
|
|
|
|
|
2017-06-09 19:15:19 +00:00
|
|
|
/**
|
|
|
|
* Get parsed platform
|
|
|
|
* @return {{}}
|
|
|
|
*/
|
2017-06-08 22:12:44 +00:00
|
|
|
getPlatform() {
|
|
|
|
if (this.parsedResult.platform) {
|
|
|
|
return this.parsedResult.platform;
|
|
|
|
}
|
|
|
|
|
2018-06-30 11:26:39 +00:00
|
|
|
return this.parsePlatform();
|
2017-06-08 22:12:44 +00:00
|
|
|
}
|
|
|
|
|
2018-07-02 19:24:02 +00:00
|
|
|
/**
|
|
|
|
* Get platform name
|
2018-07-08 09:08:49 +00:00
|
|
|
* @param {Boolean} [toLowerCase=false]
|
2018-07-02 19:24:02 +00:00
|
|
|
* @return {*}
|
|
|
|
*/
|
2018-07-08 09:08:49 +00:00
|
|
|
getPlatformType(toLowerCase = false) {
|
2018-07-04 20:09:33 +00:00
|
|
|
const { type } = this.getPlatform();
|
2018-07-02 19:24:02 +00:00
|
|
|
|
|
|
|
if (toLowerCase) {
|
2018-07-04 20:09:33 +00:00
|
|
|
return String(type).toLowerCase() || '';
|
2018-07-02 19:24:02 +00:00
|
|
|
}
|
|
|
|
|
2018-07-04 20:09:33 +00:00
|
|
|
return type || '';
|
2018-07-02 19:24:02 +00:00
|
|
|
}
|
|
|
|
|
2017-06-09 19:15:19 +00:00
|
|
|
/**
|
|
|
|
* Get parsed platform
|
|
|
|
* @return {{}}
|
|
|
|
*/
|
2018-06-30 11:26:39 +00:00
|
|
|
parsePlatform() {
|
2017-06-08 22:12:44 +00:00
|
|
|
this.parsedResult.platform = {};
|
|
|
|
|
2017-12-20 21:29:06 +00:00
|
|
|
const platform = platformParsersList.find((_platform) => {
|
2017-06-08 22:12:44 +00:00
|
|
|
if (typeof _platform.test === 'function') {
|
|
|
|
return _platform.test(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_platform.test instanceof Array) {
|
2017-12-20 21:29:06 +00:00
|
|
|
return _platform.test.some(condition => this.test(condition));
|
2017-06-08 22:12:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error("Browser's test function is not valid");
|
|
|
|
});
|
|
|
|
|
|
|
|
if (platform) {
|
|
|
|
this.parsedResult.platform = platform.describe(this.getUA());
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.parsedResult.platform;
|
|
|
|
}
|
|
|
|
|
2017-06-09 20:04:43 +00:00
|
|
|
/**
|
|
|
|
* Get parsed engine
|
|
|
|
* @return {{}}
|
|
|
|
*/
|
|
|
|
getEngine() {
|
|
|
|
if (this.parsedResult.engine) {
|
|
|
|
return this.parsedResult.engine;
|
|
|
|
}
|
|
|
|
|
2018-06-30 11:26:39 +00:00
|
|
|
return this.parseEngine();
|
2017-06-09 20:04:43 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 14:04:37 +00:00
|
|
|
/**
|
|
|
|
* Get engines's name
|
|
|
|
* @return {String} Engines's name or an empty string
|
|
|
|
*
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
getEngineName(toLowerCase) {
|
|
|
|
if (toLowerCase) {
|
|
|
|
return String(this.getEngine().name).toLowerCase() || '';
|
|
|
|
}
|
|
|
|
return this.getEngine().name || '';
|
|
|
|
}
|
|
|
|
|
2017-06-09 20:04:43 +00:00
|
|
|
/**
|
|
|
|
* Get parsed platform
|
|
|
|
* @return {{}}
|
|
|
|
*/
|
2018-06-30 11:26:39 +00:00
|
|
|
parseEngine() {
|
2017-06-09 20:04:43 +00:00
|
|
|
this.parsedResult.engine = {};
|
|
|
|
|
2017-12-20 21:29:06 +00:00
|
|
|
const engine = enginesParsersList.find((_engine) => {
|
2017-06-09 20:04:43 +00:00
|
|
|
if (typeof _engine.test === 'function') {
|
|
|
|
return _engine.test(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_engine.test instanceof Array) {
|
2017-12-20 21:29:06 +00:00
|
|
|
return _engine.test.some(condition => this.test(condition));
|
2017-06-09 20:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error("Browser's test function is not valid");
|
|
|
|
});
|
|
|
|
|
|
|
|
if (engine) {
|
|
|
|
this.parsedResult.engine = engine.describe(this.getUA());
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.parsedResult.engine;
|
|
|
|
}
|
|
|
|
|
2017-05-18 19:57:18 +00:00
|
|
|
/**
|
|
|
|
* Parse full information about the browser
|
|
|
|
*/
|
2017-06-08 22:12:44 +00:00
|
|
|
parse() {
|
2018-06-30 11:26:39 +00:00
|
|
|
this.parseBrowser();
|
|
|
|
this.parseOS();
|
|
|
|
this.parsePlatform();
|
|
|
|
this.parseEngine();
|
2017-06-08 22:12:44 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2017-05-18 19:57:18 +00:00
|
|
|
|
2018-07-05 19:44:43 +00:00
|
|
|
/**
|
|
|
|
* Get parsed result
|
|
|
|
* @return {ParsedResult}
|
|
|
|
*/
|
2017-06-08 22:12:44 +00:00
|
|
|
getResult() {
|
2019-01-19 13:43:29 +00:00
|
|
|
return Object.assign({}, this.parsedResult);
|
2017-05-18 19:57:18 +00:00
|
|
|
}
|
2018-07-02 13:51:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if parsed browser matches certain conditions
|
|
|
|
*
|
2018-07-05 19:44:43 +00:00
|
|
|
* @param {Object} checkTree It's one or two layered object,
|
2018-07-02 13:51:40 +00:00
|
|
|
* which can include a platform or an OS on the first layer
|
|
|
|
* and should have browsers specs on the bottom-laying layer
|
|
|
|
*
|
2018-07-08 09:08:49 +00:00
|
|
|
* @returns {Boolean|undefined} Whether the browser satisfies the set conditions or not.
|
|
|
|
* Returns `undefined` when the browser is no described in the checkTree object.
|
2018-07-05 19:44:43 +00:00
|
|
|
*
|
2018-07-02 13:51:40 +00:00
|
|
|
* @example
|
|
|
|
* const browser = new Bowser(UA);
|
2019-04-12 18:43:09 +00:00
|
|
|
* if (browser.satisfies({chrome: '>118.01.1322' }))
|
2018-07-02 13:51:40 +00:00
|
|
|
* // or with os
|
2019-04-12 18:43:09 +00:00
|
|
|
* if (browser.satisfies({windows: { chrome: '>118.01.1322' } }))
|
2018-07-02 13:51:40 +00:00
|
|
|
* // or with platforms
|
2019-04-12 18:43:09 +00:00
|
|
|
* if (browser.satisfies({desktop: { chrome: '>118.01.1322' } }))
|
2018-07-02 13:51:40 +00:00
|
|
|
*/
|
2018-07-07 15:35:18 +00:00
|
|
|
satisfies(checkTree) {
|
2018-07-08 09:08:49 +00:00
|
|
|
const platformsAndOSes = {};
|
|
|
|
let platformsAndOSCounter = 0;
|
|
|
|
const browsers = {};
|
|
|
|
let browsersCounter = 0;
|
|
|
|
|
|
|
|
const allDefinitions = Object.keys(checkTree);
|
|
|
|
|
|
|
|
allDefinitions.forEach((key) => {
|
|
|
|
const currentDefinition = checkTree[key];
|
|
|
|
if (typeof currentDefinition === 'string') {
|
|
|
|
browsers[key] = currentDefinition;
|
|
|
|
browsersCounter += 1;
|
|
|
|
} else if (typeof currentDefinition === 'object') {
|
|
|
|
platformsAndOSes[key] = currentDefinition;
|
|
|
|
platformsAndOSCounter += 1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (platformsAndOSCounter > 0) {
|
|
|
|
const platformsAndOSNames = Object.keys(platformsAndOSes);
|
|
|
|
const OSMatchingDefinition = platformsAndOSNames.find(name => (this.isOS(name)));
|
|
|
|
|
|
|
|
if (OSMatchingDefinition) {
|
|
|
|
const osResult = this.satisfies(platformsAndOSes[OSMatchingDefinition]);
|
2018-07-02 13:51:40 +00:00
|
|
|
|
2018-07-08 09:08:49 +00:00
|
|
|
if (osResult !== void 0) {
|
|
|
|
return osResult;
|
|
|
|
}
|
2018-07-02 13:51:40 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 09:08:49 +00:00
|
|
|
const platformMatchingDefinition = platformsAndOSNames.find(name => (this.isPlatform(name)));
|
|
|
|
if (platformMatchingDefinition) {
|
|
|
|
const platformResult = this.satisfies(platformsAndOSes[platformMatchingDefinition]);
|
|
|
|
|
|
|
|
if (platformResult !== void 0) {
|
|
|
|
return platformResult;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (browsersCounter > 0) {
|
|
|
|
const browserNames = Object.keys(browsers);
|
2019-02-18 01:05:40 +00:00
|
|
|
const matchingDefinition = browserNames.find(name => (this.isBrowser(name, true)));
|
2018-07-08 09:08:49 +00:00
|
|
|
|
|
|
|
if (matchingDefinition !== void 0) {
|
|
|
|
return this.compareVersion(browsers[matchingDefinition]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined;
|
2018-07-02 13:51:40 +00:00
|
|
|
}
|
|
|
|
|
2019-04-12 18:06:09 +00:00
|
|
|
/**
|
|
|
|
* Check if the browser name equals the passed string
|
|
|
|
* @param browserName The string to compare with the browser name
|
|
|
|
* @param [includingAlias=false] The flag showing whether alias will be included into comparison
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2019-04-12 18:04:58 +00:00
|
|
|
isBrowser(browserName, includingAlias = false) {
|
2019-02-18 01:05:40 +00:00
|
|
|
const defaultBrowserName = this.getBrowserName();
|
|
|
|
const possibleNames = [defaultBrowserName.toLowerCase()];
|
2019-04-12 18:04:58 +00:00
|
|
|
const alias = Utils.getBrowserAlias(defaultBrowserName);
|
2019-02-18 01:05:40 +00:00
|
|
|
|
2019-04-12 18:04:58 +00:00
|
|
|
if (includingAlias && typeof alias !== 'undefined') {
|
|
|
|
possibleNames.push(alias.toLowerCase());
|
2019-02-18 01:05:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return possibleNames.indexOf(browserName.toLowerCase()) !== -1;
|
2018-07-02 13:51:40 +00:00
|
|
|
}
|
|
|
|
|
2018-07-07 15:35:18 +00:00
|
|
|
compareVersion(version) {
|
2018-12-27 16:49:58 +00:00
|
|
|
let expectedResults = [0];
|
2018-07-02 20:30:48 +00:00
|
|
|
let comparableVersion = version;
|
2018-08-16 16:04:10 +00:00
|
|
|
let isLoose = false;
|
2018-07-02 20:30:48 +00:00
|
|
|
|
2018-09-09 12:02:29 +00:00
|
|
|
const currentBrowserVersion = this.getBrowserVersion();
|
|
|
|
|
|
|
|
if (typeof currentBrowserVersion !== 'string') {
|
|
|
|
return void 0;
|
|
|
|
}
|
|
|
|
|
2018-12-27 16:49:58 +00:00
|
|
|
if (version[0] === '>' || version[0] === '<') {
|
2018-07-02 20:30:48 +00:00
|
|
|
comparableVersion = version.substr(1);
|
2018-12-27 16:49:58 +00:00
|
|
|
if (version[1] === '=') {
|
|
|
|
isLoose = true;
|
|
|
|
comparableVersion = version.substr(2);
|
|
|
|
} else {
|
|
|
|
expectedResults = [];
|
|
|
|
}
|
|
|
|
if (version[0] === '>') {
|
|
|
|
expectedResults.push(1);
|
|
|
|
} else {
|
|
|
|
expectedResults.push(-1);
|
|
|
|
}
|
2018-07-02 20:30:48 +00:00
|
|
|
} else if (version[0] === '=') {
|
|
|
|
comparableVersion = version.substr(1);
|
2018-08-16 16:04:10 +00:00
|
|
|
} else if (version[0] === '~') {
|
|
|
|
isLoose = true;
|
|
|
|
comparableVersion = version.substr(1);
|
2018-07-02 20:30:48 +00:00
|
|
|
}
|
2018-08-16 16:04:10 +00:00
|
|
|
|
2018-12-27 16:49:58 +00:00
|
|
|
return expectedResults.indexOf(
|
2019-02-14 16:06:14 +00:00
|
|
|
Utils.compareVersions(currentBrowserVersion, comparableVersion, isLoose),
|
2018-12-27 16:49:58 +00:00
|
|
|
) > -1;
|
2018-07-02 19:24:02 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 09:08:49 +00:00
|
|
|
isOS(osName) {
|
2018-07-02 19:24:02 +00:00
|
|
|
return this.getOSName(true) === String(osName).toLowerCase();
|
|
|
|
}
|
|
|
|
|
2018-07-04 20:09:33 +00:00
|
|
|
isPlatform(platformType) {
|
|
|
|
return this.getPlatformType(true) === String(platformType).toLowerCase();
|
|
|
|
}
|
|
|
|
|
2019-01-24 14:04:37 +00:00
|
|
|
isEngine(engineName) {
|
|
|
|
return this.getEngineName(true) === String(engineName).toLowerCase();
|
|
|
|
}
|
|
|
|
|
2018-07-04 20:09:33 +00:00
|
|
|
/**
|
|
|
|
* Is anything? Check if the browser is called "anything",
|
|
|
|
* the OS called "anything" or the platform called "anything"
|
|
|
|
* @param {String} anything
|
|
|
|
* @returns {Boolean}
|
|
|
|
*/
|
|
|
|
is(anything) {
|
2018-07-08 09:08:49 +00:00
|
|
|
return this.isBrowser(anything) || this.isOS(anything) || this.isPlatform(anything);
|
2018-07-02 13:51:40 +00:00
|
|
|
}
|
2018-07-17 20:43:54 +00:00
|
|
|
|
|
|
|
/**
|
2019-01-19 13:44:08 +00:00
|
|
|
* Check if any of the given values satisfies this.is(anything)
|
2018-07-17 20:43:54 +00:00
|
|
|
* @param {String[]} anythings
|
|
|
|
* @returns {Boolean}
|
|
|
|
*/
|
|
|
|
some(anythings = []) {
|
|
|
|
return anythings.some(anything => this.is(anything));
|
|
|
|
}
|
2017-04-04 20:03:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-09 14:13:00 +00:00
|
|
|
export default Parser;
|