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

92 lines
1.6 KiB

7 years ago
import browsersList from './parser-browsers';
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;
}
/**
* Get parsed browser object
* @return {Object}
*
* @private
7 years ago
*/
_parseBrowser() {
7 years ago
this.parsedResult.browser = {};
const browser = browsersList.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 (browser) {
this.parsedResult.browser = browser.detect(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
/**
* Test a UA string for a regexp
* @param {RegExp} regex
* @return {Boolean}
*/
test(regex) {
return regex.test(this._ua);
7 years ago
}
parseBrowserName() {}
parseBrowserVersion() {}
parsePlatform(){}
parseOS(){}
parseOSName(){}
parseOSVersion(){}
parseFullInfo(){}
}
7 years ago
export default Parser;