You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lancedikson_bowser/src/parser.js

182 lines
3.2 KiB

import browserParsersList from './parser-browsers';
import osParsersList from './parser-os';
7 years ago
class Parser {
7 years ago
/**
* Create instance of Parser
* @param UA
* @throw
* @constructor
*/
7 years ago
constructor(UA) {
7 years ago
if (UA === void(0) || UA === null || UA === '') {
throw new Error("UserAgent parameter can't be empty");
}
7 years ago
this._ua = UA;
7 years ago
this.parsedResult = {};
7 years ago
}
7 years ago
/**
* 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);
}
7 years ago
/**
* Get parsed browser object
* @return {Object}
*
* @private
7 years ago
*/
_parseBrowser() {
7 years ago
this.parsedResult.browser = {};
const browserDescriptor = browserParsersList.find(_browser => {
if (typeof _browser.test === 'function') {
return _browser.test(this);
}
if (_browser.test instanceof Array) {
return _browser.test.some((condition) => {
return this.test(condition);
});
}
throw new Error("Browser's test function is not valid");
7 years ago
});
7 years ago
if (browserDescriptor) {
this.parsedResult.browser = browserDescriptor.describe(this.getUA());
7 years ago
}
return this.parsedResult.browser;
}
/**
* Get parsed browser object
* @return {Object}
*
* @public
*/
getBrowser() {
if (this.parsedResult.browser) {
return this.parsedResult.browser;
}
return this._parseBrowser();
}
7 years ago
/**
* Get browser's name
* @return {String} Browser's name
*
* @public
7 years ago
*/
getBrowserName() {
return this.getBrowser().name;
}
/**
* Get browser's version
* @return {String} version of browser
*
* @public
*/
getBrowserVersion() {
return this.getBrowser().version;
7 years ago
}
getPlatform(){}
7 years ago
/**
* Get OS
* @return {Object}
*
* @example
* this.getOS();
* {
* name: 'macOS',
* version: '10.11.12'
* }
*/
7 years ago
getOS() {
if (this.parsedResult.os) {
return this.parsedResult.os;
}
return this._parseOS();
}
/**
* Parse OS and save it to this.parsedResult.os
* @return {*|{}}
* @private
*/
7 years ago
_parseOS() {
this.parsedResult.os = {};
const os = osParsersList.find(_os => {
7 years ago
if (typeof _os.test === 'function') {
return _os.test(this);
}
if (_os.test instanceof Array) {
return _os.test.some((condition) => {
return this.test(condition);
});
}
throw new Error("Browser's test function is not valid");
});
if (os) {
this.parsedResult.os = os.describe(this.getUA());
7 years ago
}
return this.parsedResult.os;
}
/**
* Get OS name
* @return {String} name of the OS macOS, Windows, Linux, etc.
*/
getOSName() {
7 years ago
return this.getOS().name;
}
/**
* Get OS version
* @return {String} full version with dots ('10.11.12', '5.6', etc)
*/
getOSVersion() {
7 years ago
return this.getOS().version;
}
/**
* Parse full information about the browser
*/
parse(){
this._parseBrowser();
this._parseOS();
return this.parsedResult;
}
7 years ago
}
7 years ago
export default Parser;