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

Make Parser.parse-anything methods public

This commit is contained in:
Denis Demchenko 2018-06-30 14:26:39 +03:00
parent f202c1cd30
commit 98007768b4
2 changed files with 34 additions and 25 deletions

View File

@ -6,17 +6,27 @@ import enginesParsersList from './parser-engines';
class Parser { class Parser {
/** /**
* Create instance of Parser * Create instance of Parser
* @param UA *
* @throw * @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
*
* @constructor * @constructor
*/ */
constructor(UA) { constructor(UA, skipParsing = false) {
if (UA === void (0) || UA === null || UA === '') { if (UA === void (0) || UA === null || UA === '') {
throw new Error("UserAgent parameter can't be empty"); throw new Error("UserAgent parameter can't be empty");
} }
this._ua = UA; this._ua = UA;
this.parsedResult = {}; this.parsedResult = {};
if (skipParsing !== true) {
this.parse();
}
} }
/** /**
@ -41,10 +51,8 @@ class Parser {
/** /**
* Get parsed browser object * Get parsed browser object
* @return {Object} * @return {Object}
*
* @private
*/ */
_parseBrowser() { parseBrowser() {
this.parsedResult.browser = {}; this.parsedResult.browser = {};
const browserDescriptor = browserParsersList.find((_browser) => { const browserDescriptor = browserParsersList.find((_browser) => {
@ -77,7 +85,7 @@ class Parser {
return this.parsedResult.browser; return this.parsedResult.browser;
} }
return this._parseBrowser(); return this.parseBrowser();
} }
/** /**
@ -120,15 +128,14 @@ class Parser {
return this.parsedResult.os; return this.parsedResult.os;
} }
return this._parseOS(); return this.parseOS();
} }
/** /**
* Parse OS and save it to this.parsedResult.os * Parse OS and save it to this.parsedResult.os
* @return {*|{}} * @return {*|{}}
* @private
*/ */
_parseOS() { parseOS() {
this.parsedResult.os = {}; this.parsedResult.os = {};
const os = osParsersList.find((_os) => { const os = osParsersList.find((_os) => {
@ -182,15 +189,14 @@ class Parser {
return this.parsedResult.platform; return this.parsedResult.platform;
} }
return this._parsePlatform(); return this.parsePlatform();
} }
/** /**
* Get parsed platform * Get parsed platform
* @return {{}} * @return {{}}
* @private
*/ */
_parsePlatform() { parsePlatform() {
this.parsedResult.platform = {}; this.parsedResult.platform = {};
const platform = platformParsersList.find((_platform) => { const platform = platformParsersList.find((_platform) => {
@ -221,15 +227,14 @@ class Parser {
return this.parsedResult.engine; return this.parsedResult.engine;
} }
return this._parseEngine(); return this.parseEngine();
} }
/** /**
* Get parsed platform * Get parsed platform
* @return {{}} * @return {{}}
* @private
*/ */
_parseEngine() { parseEngine() {
this.parsedResult.engine = {}; this.parsedResult.engine = {};
const engine = enginesParsersList.find((_engine) => { const engine = enginesParsersList.find((_engine) => {
@ -255,10 +260,10 @@ class Parser {
* Parse full information about the browser * Parse full information about the browser
*/ */
parse() { parse() {
this._parseBrowser(); this.parseBrowser();
this._parseOS(); this.parseOS();
this._parsePlatform(); this.parsePlatform();
this._parseEngine(); this.parseEngine();
return this; return this;
} }

View File

@ -3,7 +3,7 @@ import sinon from 'sinon';
import Parser from '../../src/parser'; import Parser from '../../src/parser';
const UA = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36 OPR/43.0.2442.1165'; const UA = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36 OPR/43.0.2442.1165';
const parser = new Parser(UA); const parser = new Parser(UA, true);
test('constructor', (t) => { test('constructor', (t) => {
t.truthy(parser instanceof Parser); t.truthy(parser instanceof Parser);
@ -18,12 +18,12 @@ test('Parser.test', (t) => {
}); });
test('Parser._parseBrowser is being called when the Parser.getBrowser() is called', (t) => { test('Parser._parseBrowser is being called when the Parser.getBrowser() is called', (t) => {
const spy = sinon.spy(parser, '_parseBrowser'); const spy = sinon.spy(parser, 'parseBrowser');
const b = parser.getBrowser(); const b = parser.getBrowser();
t.truthy(spy.called); t.truthy(spy.called);
t.is(b.name, 'Opera'); t.is(b.name, 'Opera');
t.is(b.version, '43.0.2442.1165'); t.is(b.version, '43.0.2442.1165');
parser._parseBrowser.restore(); parser.parseBrowser.restore();
}); });
test('Parser.getBrowserName returns a correct result', (t) => { test('Parser.getBrowserName returns a correct result', (t) => {
@ -35,10 +35,10 @@ test('Parser.getBrowserVersion returns a correct result', (t) => {
}); });
test('Parser._parseOS is being called when getOS() called', (t) => { test('Parser._parseOS is being called when getOS() called', (t) => {
const spy = sinon.spy(parser, '_parseOS'); const spy = sinon.spy(parser, 'parseOS');
parser.getOS(); parser.getOS();
t.truthy(spy.called); t.truthy(spy.called);
parser._parseOS.restore(); parser.parseOS.restore();
}); });
test('Parser.getOSName gives a name of the browser', (t) => { test('Parser.getOSName gives a name of the browser', (t) => {
@ -52,3 +52,7 @@ test('Parser.getOSName gives a lower-cased name of the browser', (t) => {
test('Parser.getOSVersion returns a correct result', (t) => { test('Parser.getOSVersion returns a correct result', (t) => {
t.is(parser.getOSVersion(), '10.12.4'); t.is(parser.getOSVersion(), '10.12.4');
}); });
test('Skip parsing shouldn\'t parse', (t) => {
t.deepEqual((new Parser(UA, true)).getResult(), {});
});