1
0
mirror of https://github.com/lancedikson/bowser synced 2024-10-27 20:34:22 +00:00
lancedikson_bowser/src/parser.js

277 lines
5.4 KiB
JavaScript
Raw Normal View History

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
*
* @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
*/
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 = {};
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;
}
/**
* 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}
*/
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
if (browserDescriptor) {
this.parsedResult.browser = browserDescriptor.describe(this.getUA());
2017-04-09 14:13:00 +00:00
}
return this.parsedResult.browser;
}
/**
* Get parsed browser object
* @return {Object}
*
* @public
*/
getBrowser() {
if (this.parsedResult.browser) {
return this.parsedResult.browser;
}
return this.parseBrowser();
}
2017-04-09 14:13:00 +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();
}
return this.getBrowser().name;
}
/**
* Get browser's version
* @return {String} version of browser
*
* @public
*/
getBrowserVersion() {
return this.getBrowser().version;
2017-04-04 20:03:47 +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;
}
return this.parseOS();
2017-04-15 19:46:18 +00:00
}
/**
* Parse OS and save it to this.parsedResult.os
* @return {*|{}}
*/
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) {
this.parsedResult.os = os.describe(this.getUA());
2017-04-15 19:46:18 +00:00
}
return this.parsedResult.os;
}
/**
* Get OS name
2017-06-08 22:12:44 +00:00
* @param {Boolean} [toLowerCase] return lower-cased value
* @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
}
/**
* Get OS version
* @return {String} full version with dots ('10.11.12', '5.6', etc)
*/
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;
}
return this.parsePlatform();
2017-06-08 22:12:44 +00:00
}
2017-06-09 19:15:19 +00:00
/**
* Get parsed platform
* @return {{}}
*/
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;
}
return this.parseEngine();
2017-06-09 20:04:43 +00:00
}
/**
* Get parsed platform
* @return {{}}
*/
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;
}
/**
* Parse full information about the browser
*/
2017-06-08 22:12:44 +00:00
parse() {
this.parseBrowser();
this.parseOS();
this.parsePlatform();
this.parseEngine();
2017-06-08 22:12:44 +00:00
return this;
}
2017-06-08 22:12:44 +00:00
getResult() {
return this.parsedResult;
}
2017-04-04 20:03:47 +00:00
}
2017-04-09 14:13:00 +00:00
export default Parser;