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

Finish Parser#_parseBrowser function

This commit is contained in:
Denis Demchenko 2017-04-09 22:09:47 +03:00
parent 9e46db26ed
commit d57094d857
3 changed files with 61 additions and 16 deletions

View File

@ -1,5 +1,6 @@
import { import {
getFirstMatch getFirstMatch,
getSecondMatch
} from './utils'; } from './utils';
const commonVersionIdentifier = /version\/(\d+(\.\d+)?)/i; const commonVersionIdentifier = /version\/(\d+(\.\d+)?)/i;
@ -213,7 +214,7 @@ const browsersList = [
{ {
test: [/blackberry|\bbb\d+/i, /rim\stablet/i], test: [/blackberry|\bbb\d+/i, /rim\stablet/i],
detect(ua) { 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 { return {
name: 'BlackBerry', name: 'BlackBerry',
@ -224,7 +225,7 @@ const browsersList = [
{ {
test: [/(web|hpw)os/i], test: [/(web|hpw)os/i],
detect(ua) { 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 { return {
name: 'WebOS Browser', name: 'WebOS Browser',
@ -246,7 +247,7 @@ const browsersList = [
{ {
test: [/tizen/i], test: [/tizen/i],
detect(ua) { 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 { return {
name: 'Tizen', name: 'Tizen',
@ -257,7 +258,7 @@ const browsersList = [
{ {
test: [/qupzilla/i], test: [/qupzilla/i],
detect(ua) { 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 { return {
name: 'QupZilla', name: 'QupZilla',
@ -268,7 +269,7 @@ const browsersList = [
{ {
test: [/chromium/i], test: [/chromium/i],
detect(ua) { 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 { return {
name: 'Chromium', name: 'Chromium',
@ -287,31 +288,60 @@ const browsersList = [
} }
} }
}, },
/* Android Browser */
{ {
test(parser) { test(parser) {
const UA = parser.getUA(); const notLikeAndroid = !parser.test(/^((?!like android).)*$/i);
return UA.test(/^((?!like android).)*$/i); const butAndroid = parser.test(/android/i);
return notLikeAndroid && butAndroid;
}, },
detect(ua) { detect(ua) {
const version = /phantomjs\/(\d+(\.\d+)?)/i; const version = getFirstMatch(commonVersionIdentifier, ua);
return { return {
name: 'PhantomJS', name: 'Android Browser',
version version
} }
} }
}, },
/* Safari */
{ {
test: [/phantom/i], test: [/safari|applewebkit/i],
detect(ua) { detect(ua) {
const version = /phantomjs\/(\d+(\.\d+)?)/i; const version = getFirstMatch(commonVersionIdentifier, ua);
return { return {
name: 'PhantomJS', name: 'Safari',
version 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; export default browsersList;

View File

@ -35,12 +35,22 @@ class Parser {
_parseBrowser() { _parseBrowser() {
this.parsedResult.browser = {}; this.parsedResult.browser = {};
const browser = browsersList.find((browser) => { const browser = browsersList.find(_browser => {
return browser.test(this); 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) { if (browser) {
this.parsedResult.browser = browser.parse(this.getUA()); this.parsedResult.browser = browser.detect(this.getUA());
} }
return this.parsedResult.browser; return this.parsedResult.browser;

View File

@ -15,3 +15,8 @@ test('getUA', t => {
test('test', t => { test('test', t => {
t.truthy(parser.test(/Chrome/i)); t.truthy(parser.test(/Chrome/i));
}); });
test('_parseBrowser', t => {
const b = parser._parseBrowser();
t.is(b.name, 'Opera');
});