diff --git a/src/parser.js b/src/parser.js index ca203d8..3fc8a44 100644 --- a/src/parser.js +++ b/src/parser.js @@ -6,17 +6,27 @@ import enginesParsersList from './parser-engines'; class 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(UA) { + constructor(UA, skipParsing = false) { if (UA === void (0) || UA === null || UA === '') { throw new Error("UserAgent parameter can't be empty"); } this._ua = UA; this.parsedResult = {}; + + if (skipParsing !== true) { + this.parse(); + } } /** @@ -41,10 +51,8 @@ class Parser { /** * Get parsed browser object * @return {Object} - * - * @private */ - _parseBrowser() { + parseBrowser() { this.parsedResult.browser = {}; const browserDescriptor = browserParsersList.find((_browser) => { @@ -77,7 +85,7 @@ class Parser { return this.parsedResult.browser; } - return this._parseBrowser(); + return this.parseBrowser(); } /** @@ -120,15 +128,14 @@ class Parser { return this.parsedResult.os; } - return this._parseOS(); + return this.parseOS(); } /** * Parse OS and save it to this.parsedResult.os * @return {*|{}} - * @private */ - _parseOS() { + parseOS() { this.parsedResult.os = {}; const os = osParsersList.find((_os) => { @@ -182,15 +189,14 @@ class Parser { return this.parsedResult.platform; } - return this._parsePlatform(); + return this.parsePlatform(); } /** * Get parsed platform * @return {{}} - * @private */ - _parsePlatform() { + parsePlatform() { this.parsedResult.platform = {}; const platform = platformParsersList.find((_platform) => { @@ -221,15 +227,14 @@ class Parser { return this.parsedResult.engine; } - return this._parseEngine(); + return this.parseEngine(); } /** * Get parsed platform * @return {{}} - * @private */ - _parseEngine() { + parseEngine() { this.parsedResult.engine = {}; const engine = enginesParsersList.find((_engine) => { @@ -255,10 +260,10 @@ class Parser { * Parse full information about the browser */ parse() { - this._parseBrowser(); - this._parseOS(); - this._parsePlatform(); - this._parseEngine(); + this.parseBrowser(); + this.parseOS(); + this.parsePlatform(); + this.parseEngine(); return this; } diff --git a/test/unit/parser.js b/test/unit/parser.js index a25de73..3634980 100644 --- a/test/unit/parser.js +++ b/test/unit/parser.js @@ -3,7 +3,7 @@ import sinon from 'sinon'; 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 parser = new Parser(UA); +const parser = new Parser(UA, true); test('constructor', (t) => { 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) => { - const spy = sinon.spy(parser, '_parseBrowser'); + const spy = sinon.spy(parser, 'parseBrowser'); const b = parser.getBrowser(); t.truthy(spy.called); t.is(b.name, 'Opera'); t.is(b.version, '43.0.2442.1165'); - parser._parseBrowser.restore(); + parser.parseBrowser.restore(); }); 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) => { - const spy = sinon.spy(parser, '_parseOS'); + const spy = sinon.spy(parser, 'parseOS'); parser.getOS(); t.truthy(spy.called); - parser._parseOS.restore(); + parser.parseOS.restore(); }); 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) => { t.is(parser.getOSVersion(), '10.12.4'); }); + +test('Skip parsing shouldn\'t parse', (t) => { + t.deepEqual((new Parser(UA, true)).getResult(), {}); +});