From 575ba8076efda1731a6a416bf2d61d65a28d5af0 Mon Sep 17 00:00:00 2001 From: Denis Demchenko Date: Tue, 4 Apr 2017 23:03:47 +0300 Subject: [PATCH] fist commit --- src/new-bowser.js | 89 ++++++++++++++ src/parser-browsers.js | 257 +++++++++++++++++++++++++++++++++++++++++ src/parser.js | 28 +++++ src/utils.js | 24 ++++ 4 files changed, 398 insertions(+) create mode 100644 src/new-bowser.js create mode 100644 src/parser-browsers.js create mode 100644 src/parser.js create mode 100644 src/utils.js diff --git a/src/new-bowser.js b/src/new-bowser.js new file mode 100644 index 0000000..1e211c4 --- /dev/null +++ b/src/new-bowser.js @@ -0,0 +1,89 @@ +var Parser = require('./parser'); +/*! + * Bowser - a browser detector + * https://github.com/ded/bowser + * MIT License | (c) Dustin Diaz 2012-2015 + * MIT License | (c) Denis Demchenko 2015-2017 + */ + +/* +* Ideas +* - Cacheable response +* */ + +/** + * Bowser class + */ +class Bowser { + /** + * Creates an object that parse UA + * @param UA + * + * @example + * const bowser = new Bowser(window.navigator.userAgent); + * bowser.getBrowser() + */ + constructor(UA) { + if (!UA) { + throw new Error('UserAgent is not defined'); + } + this._ua = UA; + this._parser = new Parser(UA); + } + + // static filter(UACollection, range) {} + // static inRange(range, UACollection) {} + + /** + * Get browser's version + * @returns {String} + */ + getBrowserVersion() {} + + /** + * Get a browser's name + * @returns {String} + */ + getBrowserName() {} + + /** + * Get an object with a name and version of the browser + * @returns {Object} + */ + getBrowser() {} + + /** + * Get an object with a name and version of the OS if it's defined + * @returns {Object} + */ + getOS() {} + + /** + * Get name of OS + * @returns {String} + */ + getOSName() {} + + /** + * Get OS version + */ + getOSVersion() {} + + /** + * Check if the browser is in range or not + * @param {Object} range + * @returns {Boolean} + */ + inRange(range) { + if (!range) { + throw new Error('Range can not be empty'); + } + } + + /** + * Check if the browser is NOT in range or not + * @param {Object} range + * @returns {Boolean} + */ + notInRange(range) {} +} diff --git a/src/parser-browsers.js b/src/parser-browsers.js new file mode 100644 index 0000000..ad7bdc1 --- /dev/null +++ b/src/parser-browsers.js @@ -0,0 +1,257 @@ +const getFirstMatch = require('./utils').getFirstMatch; + +const commonVersionIdentifier = /version\/(\d+(\.\d+)?)/i; + +const browsersList = [ + { + test: [/opera/i], + detect(ua) { + const version = getFirstMatch(commonVersionIdentifier, ua) || getFirstMatch(/(?:opera)[\s\/](\d+(\.\d+)?)/i, ua); + + return { + name: 'Opera', + version + }; + } + }, + { + test: [/opr|opios/i], + detect(ua) { + const version = getFirstMatch(/(?:opr|opios)[\s\/](\d+(\.\d+)?)/i, ua) || getFirstMatch(commonVersionIdentifier, ua); + + return { + name: 'Opera', + version + }; + } + }, + { + test: [/SamsungBrowser/i], + detect(ua) { + const version = getFirstMatch(commonVersionIdentifier, ua) || getFirstMatch(/(?:SamsungBrowser)[\s\/](\d+(\.\d+)?)/i, ua); + + return { + name: 'Samsung Internet for Android', + version + }; + } + }, + { + test: [/coast/i], + detect(ua) { + const version = getFirstMatch(commonVersionIdentifier, ua) || getFirstMatch(/(?:coast)[\s\/](\d+(\.\d+)?)/i, ua); + + return { + name: 'Opera Coast', + version + }; + } + }, + { + test: [/yabrowser/i], + detect(ua) { + const version = getFirstMatch(commonVersionIdentifier, ua) || getFirstMatch(/(?:yabrowser)[\s\/](\d+(\.\d+)?)/i, ua); + + return { + name: 'Yandex Browser', + version + }; + } + }, + { + test: [/ucbrowser/i], + detect(ua) { + const version = getFirstMatch(commonVersionIdentifier, ua) || getFirstMatch(/(?:ucbrowser)[\s\/](\d+(?:\.\d+)+)/i, ua); + + return { + name: 'UC Browser', + version + }; + } + }, + { + test: [/mxios/i], + detect(ua) { + const version = getFirstMatch(commonVersionIdentifier, ua) || getFirstMatch(/(?:mxios)[\s\/](\d+(?:\.\d+)+)/i, ua); + + return { + name: 'Maxthon', + version + }; + } + }, + { + test: [/epiphany/i], + detect(ua) { + const version = getFirstMatch(commonVersionIdentifier, ua) || getFirstMatch(/(?:epiphany)[\s\/](\d+(?:\.\d+)+)/i, ua); + + return { + name: 'Epiphany', + version + }; + } + }, + { + test: [/puffin/i], + detect(ua) { + const version = getFirstMatch(commonVersionIdentifier, ua) || getFirstMatch(/(?:puffin)[\s\/](\d+(?:\.\d+)?)/i, ua); + + return { + name: 'Puffin', + version + }; + } + }, + { + test: [/sleipnir/i], + detect(ua) { + const version = getFirstMatch(commonVersionIdentifier, ua) || getFirstMatch(/(?:sleipnir)[\s\/](\d+(?:\.\d+)+)/i, ua); + + return { + name: 'Sleipnir', + version + }; + } + }, + { + test: [/k-meleon/i], + detect(ua) { + const version = getFirstMatch(commonVersionIdentifier, ua) || getFirstMatch(/(?:k-meleon)[\s\/](\d+(?:\.\d+)+)/i, ua); + + return { + name: 'K-Meleon', + version + }; + } + }, + { + test: [/msie|trident/i], + detect(ua) { + const version = getFirstMatch(/(?:msie |rv:)(\d+(\.\d+)?)/i, ua); + + return { + name: 'Internet Explorer', + version + }; + } + }, + { + test: [/chrome.+? edge/i], + detect(ua) { + const version = getFirstMatch(/edge\/(\d+(\.\d+)?)/i, ua); + + return { + name: 'Microsoft Edge', + version + } + } + }, + { + test: [/vivaldi/i], + detect(ua) { + const version = getFirstMatch(/vivaldi\/(\d+(\.\d+)?)/i, ua); + + return { + name: 'Vivaldi', + version + } + } + }, + { + test: [/seamonkey/i], + detect(ua) { + const version = getFirstMatch(/seamonkey\/(\d+(\.\d+)?)/i, ua); + return { + name: 'SeaMonkey', + version + } + } + }, + { + test: [/firefox|iceweasel|fxios/i], + detect(ua) { + const version = getFirstMatch(/(?:firefox|iceweasel|fxios)[ \/](\d+(\.\d+)?)/i, ua); + return { + name: 'Firefox' + } + } + }, + { + test: [/silk/i], + detect(ua) { + const version = getFirstMatch(/silk\/(\d+(\.\d+)?)/i); + return { + name: 'Amazon Silk', + version + } + } + }, + { + test: [/phantom/i], + detect(ua) { + const version = /phantomjs\/(\d+(\.\d+)?)/i; + + return { + name: 'PhantomJS', + version + } + } + }, + { + test: [/slimerjs/i], + detect(ua) { + const version = /slimerjs\/(\d+(\.\d+)?)/i; + + return { + name: 'SlimerJS', + version + } + } + }, + { + test: [/blackberry|\bbb\d+/i, /rim\stablet/i], + detect(ua) { + const version = /phantomjs\/(\d+(\.\d+)?)/i; + + return { + name: 'PhantomJS', + version + } + } + }, + { + test: [/phantom/i], + detect(ua) { + const version = /phantomjs\/(\d+(\.\d+)?)/i; + + return { + name: 'PhantomJS', + version + } + } + }, + { + test: [/phantom/i], + detect(ua) { + const version = /phantomjs\/(\d+(\.\d+)?)/i; + + return { + name: 'PhantomJS', + version + } + } + }, + { + test: [/phantom/i], + detect(ua) { + const version = /phantomjs\/(\d+(\.\d+)?)/i; + + return { + name: 'PhantomJS', + version + } + } + }, +]; + +module.exports = browsersList; diff --git a/src/parser.js b/src/parser.js new file mode 100644 index 0000000..2541429 --- /dev/null +++ b/src/parser.js @@ -0,0 +1,28 @@ +var browsers = require('./parser-browsers'); + +class Parser { + constructor(UA) { + this._ua = UA; + this.result = {}; + } + + parseBrowser() { + if (this.result.browser) { + return this.result.browser; + } + + const browser = browsers.find((browser) => { + return browser.test.some((result, item) => { item.test(this._ua)}); + }); + } + + parseBrowserName() {} + parseBrowserVersion() {} + parsePlatform(){} + parseOS(){} + parseOSName(){} + parseOSVersion(){} + parseFullInfo(){} +} + +module.exports = Parser; diff --git a/src/utils.js b/src/utils.js new file mode 100644 index 0000000..8a52f61 --- /dev/null +++ b/src/utils.js @@ -0,0 +1,24 @@ +class Utils { + /** + * Get first matched item for a string + * @param {RegExp} regexp + * @param {String} ua + * @return {Array|{index: number, input: string}|*|boolean|string} + */ + static getFirstMatch(regexp, ua) { + const match = ua.match(regexp); + return (match && match.length > 0 && match[1]) || ''; + } + + /** + * Get second matched item for a string + * @param regexp + * @return {Array|{index: number, input: string}|*|boolean|string} + */ + static getSecondMatch(regexp, ua) { + const match = ua.match(regexp); + return (match && match.length > 1 && match[2]) || ''; + } +} + +module.exports = Utils;