diff --git a/src/parser-browsers.js b/src/parser-browsers.js index ac71368..9bc990e 100644 --- a/src/parser-browsers.js +++ b/src/parser-browsers.js @@ -1,5 +1,6 @@ import { - getFirstMatch + getFirstMatch, + getSecondMatch } from './utils'; const commonVersionIdentifier = /version\/(\d+(\.\d+)?)/i; @@ -213,7 +214,7 @@ const browsersList = [ { test: [/blackberry|\bbb\d+/i, /rim\stablet/i], detect(ua) { - const version = commonVersionIdentifier || getFirstMatch(/blackberry[\d]+\/(\d+(\.\d+)?)/i, ua); + const version = getFirstMatch(commonVersionIdentifier, ua) || getFirstMatch(/blackberry[\d]+\/(\d+(\.\d+)?)/i, ua); return { name: 'BlackBerry', @@ -224,7 +225,7 @@ const browsersList = [ { test: [/(web|hpw)os/i], detect(ua) { - const version = commonVersionIdentifier || getFirstMatch(/w(?:eb)?osbrowser\/(\d+(\.\d+)?)/i, ua); + const version = getFirstMatch(commonVersionIdentifier, ua) || getFirstMatch(/w(?:eb)?osbrowser\/(\d+(\.\d+)?)/i, ua); return { name: 'WebOS Browser', @@ -246,7 +247,7 @@ const browsersList = [ { test: [/tizen/i], detect(ua) { - const version = getFirstMatch(/(?:tizen\s?)?browser\/(\d+(\.\d+)?)/i, ua) || commonVersionIdentifier; + const version = getFirstMatch(/(?:tizen\s?)?browser\/(\d+(\.\d+)?)/i, ua) || getFirstMatch(commonVersionIdentifier, ua); return { name: 'Tizen', @@ -257,7 +258,7 @@ const browsersList = [ { test: [/qupzilla/i], detect(ua) { - const version = getFirstMatch(/(?:qupzilla)[\s\/](\d+(?:\.\d+)+)/i, ua) || commonVersionIdentifier; + const version = getFirstMatch(/(?:qupzilla)[\s\/](\d+(?:\.\d+)+)/i, ua) || getFirstMatch(commonVersionIdentifier, ua); return { name: 'QupZilla', @@ -268,7 +269,7 @@ const browsersList = [ { test: [/chromium/i], detect(ua) { - const version = getFirstMatch(/(?:chromium)[\s\/](\d+(?:\.\d+)?)/i, ua) || commonVersionIdentifier; + const version = getFirstMatch(/(?:chromium)[\s\/](\d+(?:\.\d+)?)/i, ua) || getFirstMatch(commonVersionIdentifier, ua); return { name: 'Chromium', @@ -287,31 +288,60 @@ const browsersList = [ } } }, + + /* Android Browser */ { test(parser) { - const UA = parser.getUA(); - return UA.test(/^((?!like android).)*$/i); + const notLikeAndroid = !parser.test(/^((?!like android).)*$/i); + const butAndroid = parser.test(/android/i); + return notLikeAndroid && butAndroid; }, detect(ua) { - const version = /phantomjs\/(\d+(\.\d+)?)/i; + const version = getFirstMatch(commonVersionIdentifier, ua); return { - name: 'PhantomJS', + name: 'Android Browser', version } } }, + + /* Safari */ { - test: [/phantom/i], + test: [/safari|applewebkit/i], detect(ua) { - const version = /phantomjs\/(\d+(\.\d+)?)/i; + const version = getFirstMatch(commonVersionIdentifier, ua); return { - name: 'PhantomJS', + name: 'Safari', + version + } + } + }, + + /* Googlebot */ + { + test: [/googlebot/i], + detect(ua) { + const version = getFirstMatch(/googlebot\/(\d+(\.\d+))/i, ua) || getFirstMatch(commonVersionIdentifier, ua); + + return { + name: 'Googlebot', version } } }, + + /* Something else */ + { + test: [/.*/i], + detect(ua) { + return { + name: getFirstMatch(/^(.*)\/(.*) /, ua), + version: getSecondMatch(/^(.*)\/(.*) /, ua) + }; + } + } ]; export default browsersList; diff --git a/src/parser.js b/src/parser.js index 3ac4901..9f665cb 100644 --- a/src/parser.js +++ b/src/parser.js @@ -35,12 +35,22 @@ class Parser { _parseBrowser() { this.parsedResult.browser = {}; - const browser = browsersList.find((browser) => { - return browser.test(this); + 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"); }); if (browser) { - this.parsedResult.browser = browser.parse(this.getUA()); + this.parsedResult.browser = browser.detect(this.getUA()); } return this.parsedResult.browser; diff --git a/test/unit/parser.js b/test/unit/parser.js index 7f2bd2d..efa1d74 100644 --- a/test/unit/parser.js +++ b/test/unit/parser.js @@ -15,3 +15,8 @@ test('getUA', t => { test('test', t => { t.truthy(parser.test(/Chrome/i)); }); + +test('_parseBrowser', t => { + const b = parser._parseBrowser(); + t.is(b.name, 'Opera'); +});