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

fist commit

This commit is contained in:
Denis Demchenko 2017-04-04 23:03:47 +03:00
parent f93b3f1f1b
commit 575ba8076e
4 changed files with 398 additions and 0 deletions

89
src/new-bowser.js Normal file
View File

@ -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) {}
}

257
src/parser-browsers.js Normal file
View File

@ -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;

28
src/parser.js Normal file
View File

@ -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;

24
src/utils.js Normal file
View File

@ -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;