2017-05-18 19:57:18 +00:00
|
|
|
import browserParsersList from './parser-browsers';
|
|
|
|
import osParsersList from './parser-os';
|
2017-06-08 22:12:44 +00:00
|
|
|
import platformParsersList from './parser-platforms';
|
2017-06-09 20:04:43 +00:00
|
|
|
import enginesParsersList from './parser-engines';
|
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
|
|
|
*
|
|
|
|
* @param {String} UA — User-Agent string
|
|
|
|
* @param {Boolean} [skipParsing=false] — parser can skip parsing in purpose of performance
|
|
|
|
* improvements if you need to make a more particular parsing
|
|
|
|
* like `.parseBrowser()` or `.parsePlatform()`
|
|
|
|
*
|
|
|
|
* @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;
|
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
|
|
|
|
* @return {String}
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
* @return {String} Browser's name
|
|
|
|
*
|
|
|
|
* @public
|
2017-04-09 14:13:00 +00:00
|
|
|
*/
|
2017-06-09 20:04:43 +00:00
|
|
|
getBrowserName(toLowerCase) {
|
|
|
|
if (toLowerCase) {
|
|
|
|
return String(this.getBrowser().name).toLowerCase();
|
|
|
|
}
|
2017-04-09 19:46:23 +00:00
|
|
|
return this.getBrowser().name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
return String(name).toLowerCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
2017-06-08 22:12:44 +00:00
|
|
|
getResult() {
|
2017-05-18 19:57:18 +00:00
|
|
|
return this.parsedResult;
|
|
|
|
}
|
2017-04-04 20:03:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-09 14:13:00 +00:00
|
|
|
export default Parser;
|