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

Change descriptors naming and add some information about them

This commit is contained in:
Denis Demchenko 2017-05-18 22:57:18 +03:00
parent 8808d5747b
commit 8b37abf268
3 changed files with 75 additions and 42 deletions

View File

@ -1,3 +1,28 @@
/**
* Browsers' descriptors
*
* The idea of descriptors is simple. You should know about them two simple things:
* 1. Every descriptor has a method or property called `test` and a `describe` method.
* 2. Order of descriptors is important.
*
* More details:
* 1. Method or property `test` serves as a way to detect whether the UA string
* matches some certain browser or not. The `describe` method helps to make a result
* object with params that show some browser-specific things: name, version, etc.
* 2. Order of descriptors is important because a Parser goes through them one by one
* in course. For example, if you insert Chrome's descriptor as the first one,
* more then a half of browsers will be described as Chrome, because they will pass
* the Chrome descriptor's test.
*
* Descriptor's `test` could be a property with an array of RegExps, where every RegExp
* will be applied to a UA string to test it whether it matches or not.
* If a descriptor has two or more regexps in the `test` array it tests them one by one
* with a logical sum operation. Parser stops if it has found any RegExp that matches the UA.
*
* Or `test` could be a method. In that case it gets a Parser instance and should
* return true/false to get the Parser know if this browser descriptor matches the UA or not.
*/
import { import {
getFirstMatch, getFirstMatch,
getSecondMatch getSecondMatch
@ -8,7 +33,7 @@ const commonVersionIdentifier = /version\/(\d+(\.\d+)?)/i;
const browsersList = [ const browsersList = [
{ {
test: [/opera/i], test: [/opera/i],
detect(ua) { describe(ua) {
const version = getFirstMatch(commonVersionIdentifier, ua) || getFirstMatch(/(?:opera)[\s\/](\S+)/i, ua); const version = getFirstMatch(commonVersionIdentifier, ua) || getFirstMatch(/(?:opera)[\s\/](\S+)/i, ua);
return { return {
@ -19,7 +44,7 @@ const browsersList = [
}, },
{ {
test: [/opr|opios/i], test: [/opr|opios/i],
detect(ua) { describe(ua) {
const version = getFirstMatch(/(?:opr|opios)[\s\/](\S+)/i, ua) || getFirstMatch(commonVersionIdentifier, ua); const version = getFirstMatch(/(?:opr|opios)[\s\/](\S+)/i, ua) || getFirstMatch(commonVersionIdentifier, ua);
return { return {
@ -30,7 +55,7 @@ const browsersList = [
}, },
{ {
test: [/SamsungBrowser/i], test: [/SamsungBrowser/i],
detect(ua) { describe(ua) {
const version = getFirstMatch(commonVersionIdentifier, ua) || getFirstMatch(/(?:SamsungBrowser)[\s\/](\S+)/i, ua); const version = getFirstMatch(commonVersionIdentifier, ua) || getFirstMatch(/(?:SamsungBrowser)[\s\/](\S+)/i, ua);
return { return {
@ -41,7 +66,7 @@ const browsersList = [
}, },
{ {
test: [/coast/i], test: [/coast/i],
detect(ua) { describe(ua) {
const version = getFirstMatch(commonVersionIdentifier, ua) || getFirstMatch(/(?:coast)[\s\/](\S+)/i, ua); const version = getFirstMatch(commonVersionIdentifier, ua) || getFirstMatch(/(?:coast)[\s\/](\S+)/i, ua);
return { return {
@ -52,7 +77,7 @@ const browsersList = [
}, },
{ {
test: [/yabrowser/i], test: [/yabrowser/i],
detect(ua) { describe(ua) {
const version = getFirstMatch(commonVersionIdentifier, ua) || getFirstMatch(/(?:yabrowser)[\s\/](\S+)/i, ua); const version = getFirstMatch(commonVersionIdentifier, ua) || getFirstMatch(/(?:yabrowser)[\s\/](\S+)/i, ua);
return { return {
@ -63,7 +88,7 @@ const browsersList = [
}, },
{ {
test: [/ucbrowser/i], test: [/ucbrowser/i],
detect(ua) { describe(ua) {
const version = getFirstMatch(commonVersionIdentifier, ua) || getFirstMatch(/(?:ucbrowser)[\s\/](\S+)/i, ua); const version = getFirstMatch(commonVersionIdentifier, ua) || getFirstMatch(/(?:ucbrowser)[\s\/](\S+)/i, ua);
return { return {
@ -74,7 +99,7 @@ const browsersList = [
}, },
{ {
test: [/mxios/i], test: [/mxios/i],
detect(ua) { describe(ua) {
const version = getFirstMatch(commonVersionIdentifier, ua) || getFirstMatch(/(?:mxios)[\s\/](\S+)/i, ua); const version = getFirstMatch(commonVersionIdentifier, ua) || getFirstMatch(/(?:mxios)[\s\/](\S+)/i, ua);
return { return {
@ -85,7 +110,7 @@ const browsersList = [
}, },
{ {
test: [/epiphany/i], test: [/epiphany/i],
detect(ua) { describe(ua) {
const version = getFirstMatch(commonVersionIdentifier, ua) || getFirstMatch(/(?:epiphany)[\s\/](\S+)/i, ua); const version = getFirstMatch(commonVersionIdentifier, ua) || getFirstMatch(/(?:epiphany)[\s\/](\S+)/i, ua);
return { return {
@ -96,7 +121,7 @@ const browsersList = [
}, },
{ {
test: [/puffin/i], test: [/puffin/i],
detect(ua) { describe(ua) {
const version = getFirstMatch(commonVersionIdentifier, ua) || getFirstMatch(/(?:puffin)[\s\/](\S+)/i, ua); const version = getFirstMatch(commonVersionIdentifier, ua) || getFirstMatch(/(?:puffin)[\s\/](\S+)/i, ua);
return { return {
@ -107,7 +132,7 @@ const browsersList = [
}, },
{ {
test: [/sleipnir/i], test: [/sleipnir/i],
detect(ua) { describe(ua) {
const version = getFirstMatch(commonVersionIdentifier, ua) || getFirstMatch(/(?:sleipnir)[\s\/](\S+)/i, ua); const version = getFirstMatch(commonVersionIdentifier, ua) || getFirstMatch(/(?:sleipnir)[\s\/](\S+)/i, ua);
return { return {
@ -118,7 +143,7 @@ const browsersList = [
}, },
{ {
test: [/k-meleon/i], test: [/k-meleon/i],
detect(ua) { describe(ua) {
const version = getFirstMatch(commonVersionIdentifier, ua) || getFirstMatch(/(?:k-meleon)[\s\/](\S+)/i, ua); const version = getFirstMatch(commonVersionIdentifier, ua) || getFirstMatch(/(?:k-meleon)[\s\/](\S+)/i, ua);
return { return {
@ -129,7 +154,7 @@ const browsersList = [
}, },
{ {
test: [/msie|trident/i], test: [/msie|trident/i],
detect(ua) { describe(ua) {
const version = getFirstMatch(/(?:msie |rv:)(\S+)/i, ua); const version = getFirstMatch(/(?:msie |rv:)(\S+)/i, ua);
return { return {
@ -140,7 +165,7 @@ const browsersList = [
}, },
{ {
test: [/chrome.+? edge/i], test: [/chrome.+? edge/i],
detect(ua) { describe(ua) {
const version = getFirstMatch(/edge\/(\S+)/i, ua); const version = getFirstMatch(/edge\/(\S+)/i, ua);
return { return {
@ -151,7 +176,7 @@ const browsersList = [
}, },
{ {
test: [/vivaldi/i], test: [/vivaldi/i],
detect(ua) { describe(ua) {
const version = getFirstMatch(/vivaldi\/(\S+)/i, ua); const version = getFirstMatch(/vivaldi\/(\S+)/i, ua);
return { return {
@ -162,7 +187,7 @@ const browsersList = [
}, },
{ {
test: [/seamonkey/i], test: [/seamonkey/i],
detect(ua) { describe(ua) {
const version = getFirstMatch(/seamonkey\/(\S+)/i, ua); const version = getFirstMatch(/seamonkey\/(\S+)/i, ua);
return { return {
name: 'SeaMonkey', name: 'SeaMonkey',
@ -172,7 +197,7 @@ const browsersList = [
}, },
{ {
test: [/firefox|iceweasel|fxios/i], test: [/firefox|iceweasel|fxios/i],
detect(ua) { describe(ua) {
const version = getFirstMatch(/(?:firefox|iceweasel|fxios)[ \/](\S+)/i, ua); const version = getFirstMatch(/(?:firefox|iceweasel|fxios)[ \/](\S+)/i, ua);
return { return {
name: 'Firefox' name: 'Firefox'
@ -181,7 +206,7 @@ const browsersList = [
}, },
{ {
test: [/silk/i], test: [/silk/i],
detect(ua) { describe(ua) {
const version = getFirstMatch(/silk\/(\S+)/i, ua); const version = getFirstMatch(/silk\/(\S+)/i, ua);
return { return {
name: 'Amazon Silk', name: 'Amazon Silk',
@ -191,7 +216,7 @@ const browsersList = [
}, },
{ {
test: [/phantom/i], test: [/phantom/i],
detect(ua) { describe(ua) {
const version = getFirstMatch(/phantomjs\/(\S+)/i, ua); const version = getFirstMatch(/phantomjs\/(\S+)/i, ua);
return { return {
@ -202,7 +227,7 @@ const browsersList = [
}, },
{ {
test: [/slimerjs/i], test: [/slimerjs/i],
detect(ua) { describe(ua) {
const version = getFirstMatch(/slimerjs\/(\S+)/i, ua); const version = getFirstMatch(/slimerjs\/(\S+)/i, ua);
return { return {
@ -213,7 +238,7 @@ const browsersList = [
}, },
{ {
test: [/blackberry|\bbb\d+/i, /rim\stablet/i], test: [/blackberry|\bbb\d+/i, /rim\stablet/i],
detect(ua) { describe(ua) {
const version = getFirstMatch(commonVersionIdentifier, ua) || getFirstMatch(/blackberry[\d]+\/(\S+)/i, ua); const version = getFirstMatch(commonVersionIdentifier, ua) || getFirstMatch(/blackberry[\d]+\/(\S+)/i, ua);
return { return {
@ -224,7 +249,7 @@ const browsersList = [
}, },
{ {
test: [/(web|hpw)os/i], test: [/(web|hpw)os/i],
detect(ua) { describe(ua) {
const version = getFirstMatch(commonVersionIdentifier, ua) || getFirstMatch(/w(?:eb)?osbrowser\/(\S+)/i, ua); const version = getFirstMatch(commonVersionIdentifier, ua) || getFirstMatch(/w(?:eb)?osbrowser\/(\S+)/i, ua);
return { return {
@ -235,7 +260,7 @@ const browsersList = [
}, },
{ {
test: [/bada/i], test: [/bada/i],
detect(ua) { describe(ua) {
const version = getFirstMatch(/dolfin\/(\S+)/i, ua); const version = getFirstMatch(/dolfin\/(\S+)/i, ua);
return { return {
@ -246,7 +271,7 @@ const browsersList = [
}, },
{ {
test: [/tizen/i], test: [/tizen/i],
detect(ua) { describe(ua) {
const version = getFirstMatch(/(?:tizen\s?)?browser\/(\S+)/i, ua) || getFirstMatch(commonVersionIdentifier, ua); const version = getFirstMatch(/(?:tizen\s?)?browser\/(\S+)/i, ua) || getFirstMatch(commonVersionIdentifier, ua);
return { return {
@ -257,7 +282,7 @@ const browsersList = [
}, },
{ {
test: [/qupzilla/i], test: [/qupzilla/i],
detect(ua) { describe(ua) {
const version = getFirstMatch(/(?:qupzilla)[\s\/](\S+)/i, ua) || getFirstMatch(commonVersionIdentifier, ua); const version = getFirstMatch(/(?:qupzilla)[\s\/](\S+)/i, ua) || getFirstMatch(commonVersionIdentifier, ua);
return { return {
@ -268,7 +293,7 @@ const browsersList = [
}, },
{ {
test: [/chromium/i], test: [/chromium/i],
detect(ua) { describe(ua) {
const version = getFirstMatch(/(?:chromium)[\s\/](\S+)/i, ua) || getFirstMatch(commonVersionIdentifier, ua); const version = getFirstMatch(/(?:chromium)[\s\/](\S+)/i, ua) || getFirstMatch(commonVersionIdentifier, ua);
return { return {
@ -279,7 +304,7 @@ const browsersList = [
}, },
{ {
test: [/chrome|crios|crmo/i], test: [/chrome|crios|crmo/i],
detect(ua) { describe(ua) {
const version = getFirstMatch(/(?:chrome|crios|crmo)\/(\S+)/i, ua); const version = getFirstMatch(/(?:chrome|crios|crmo)\/(\S+)/i, ua);
return { return {
@ -296,7 +321,7 @@ const browsersList = [
const butAndroid = parser.test(/android/i); const butAndroid = parser.test(/android/i);
return notLikeAndroid && butAndroid; return notLikeAndroid && butAndroid;
}, },
detect(ua) { describe(ua) {
const version = getFirstMatch(commonVersionIdentifier, ua); const version = getFirstMatch(commonVersionIdentifier, ua);
return { return {
@ -309,7 +334,7 @@ const browsersList = [
/* Safari */ /* Safari */
{ {
test: [/safari|applewebkit/i], test: [/safari|applewebkit/i],
detect(ua) { describe(ua) {
const version = getFirstMatch(commonVersionIdentifier, ua); const version = getFirstMatch(commonVersionIdentifier, ua);
return { return {
@ -322,7 +347,7 @@ const browsersList = [
/* Googlebot */ /* Googlebot */
{ {
test: [/googlebot/i], test: [/googlebot/i],
detect(ua) { describe(ua) {
const version = getFirstMatch(/googlebot\/(\d+(\.\d+))/i, ua) || getFirstMatch(commonVersionIdentifier, ua); const version = getFirstMatch(/googlebot\/(\d+(\.\d+))/i, ua) || getFirstMatch(commonVersionIdentifier, ua);
return { return {
@ -335,7 +360,7 @@ const browsersList = [
/* Something else */ /* Something else */
{ {
test: [/.*/i], test: [/.*/i],
detect(ua) { describe(ua) {
return { return {
name: getFirstMatch(/^(.*)\/(.*) /, ua), name: getFirstMatch(/^(.*)\/(.*) /, ua),
version: getSecondMatch(/^(.*)\/(.*) /, ua) version: getSecondMatch(/^(.*)\/(.*) /, ua)

View File

@ -3,7 +3,7 @@ import { getFirstMatch } from './utils';
export default [ export default [
{ {
test: [/windows phone/i], test: [/windows phone/i],
detect(ua) { describe(ua) {
const version = getFirstMatch(/windows phone (?:os)?\s?(\d+(\.\d+)*)/i, ua); const version = getFirstMatch(/windows phone (?:os)?\s?(\d+(\.\d+)*)/i, ua);
return { return {
name: 'Windows Phone', name: 'Windows Phone',
@ -13,7 +13,7 @@ export default [
}, },
{ {
test: [/macintosh/i], test: [/macintosh/i],
detect(ua) { describe(ua) {
const version = getFirstMatch(/mac os x (\d+([_\s]\d+)*)/i, ua).replace(/[_\s]/g, '.'); const version = getFirstMatch(/mac os x (\d+([_\s]\d+)*)/i, ua).replace(/[_\s]/g, '.');
return { return {
name: 'macOS', name: 'macOS',

View File

@ -1,5 +1,5 @@
import browsersList from './parser-browsers'; import browserParsersList from './parser-browsers';
import osList from './parser-os'; import osParsersList from './parser-os';
class Parser { class Parser {
/** /**
@ -45,7 +45,7 @@ class Parser {
_parseBrowser() { _parseBrowser() {
this.parsedResult.browser = {}; this.parsedResult.browser = {};
const browser = browsersList.find(_browser => { const browserDescriptor = browserParsersList.find(_browser => {
if (typeof _browser.test === 'function') { if (typeof _browser.test === 'function') {
return _browser.test(this); return _browser.test(this);
} }
@ -59,8 +59,8 @@ class Parser {
throw new Error("Browser's test function is not valid"); throw new Error("Browser's test function is not valid");
}); });
if (browser) { if (browserDescriptor) {
this.parsedResult.browser = browser.detect(this.getUA()); this.parsedResult.browser = browserDescriptor.describe(this.getUA());
} }
return this.parsedResult.browser; return this.parsedResult.browser;
@ -108,7 +108,7 @@ class Parser {
_parseOS() { _parseOS() {
this.parsedResult.os = {}; this.parsedResult.os = {};
const os = osList.find(_os => { const os = osParsersList.find(_os => {
if (typeof _os.test === 'function') { if (typeof _os.test === 'function') {
return _os.test(this); return _os.test(this);
} }
@ -123,7 +123,7 @@ class Parser {
}); });
if (os) { if (os) {
this.parsedResult.os = os.detect(this.getUA()); this.parsedResult.os = os.describe(this.getUA());
} }
return this.parsedResult.os; return this.parsedResult.os;
@ -137,7 +137,15 @@ class Parser {
return this.getOS().version; return this.getOS().version;
} }
parseFullInfo(){} /**
* Parse full information about the browser
*/
parse(){
this._parseBrowser();
this._parseOS();
return this.parsedResult;
}
} }
export default Parser; export default Parser;