Continue writing

pull/227/head
Denis Demchenko 7 years ago
parent 575ba8076e
commit 3bb6654320

@ -0,0 +1,3 @@
{
"presets": ["env"]
}

@ -17,8 +17,16 @@
"url": "git+https://github.com/ded/bowser.git"
},
"devDependencies": {
"smoosh": "*",
"mocha": "*"
"ava": "^0.19.0",
"babel-cli": "^6.24.1",
"babel-preset-env": "^1.3.3",
"babel-register": "^6.24.1"
},
"ava": {
"require": [
"babel-register"
],
"babel": "inherit"
},
"bugs": {
"url": "https://github.com/ded/bowser/issues"
@ -27,7 +35,7 @@
"test": "test"
},
"scripts": {
"test": "make test",
"test": "ava",
"prepublish": "make boosh"
},
"license": "MIT"

@ -1,4 +1,6 @@
const getFirstMatch = require('./utils').getFirstMatch;
import {
getFirstMatch
} from './utils';
const commonVersionIdentifier = /version\/(\d+(\.\d+)?)/i;
@ -179,7 +181,7 @@ const browsersList = [
{
test: [/silk/i],
detect(ua) {
const version = getFirstMatch(/silk\/(\d+(\.\d+)?)/i);
const version = getFirstMatch(/silk\/(\d+(\.\d+)?)/i, ua);
return {
name: 'Amazon Silk',
version
@ -189,7 +191,7 @@ const browsersList = [
{
test: [/phantom/i],
detect(ua) {
const version = /phantomjs\/(\d+(\.\d+)?)/i;
const version = getFirstMatch(/phantomjs\/(\d+(\.\d+)?)/i, ua);
return {
name: 'PhantomJS',
@ -200,7 +202,7 @@ const browsersList = [
{
test: [/slimerjs/i],
detect(ua) {
const version = /slimerjs\/(\d+(\.\d+)?)/i;
const version = getFirstMatch(/slimerjs\/(\d+(\.\d+)?)/i, ua);
return {
name: 'SlimerJS',
@ -211,27 +213,85 @@ const browsersList = [
{
test: [/blackberry|\bbb\d+/i, /rim\stablet/i],
detect(ua) {
const version = /phantomjs\/(\d+(\.\d+)?)/i;
const version = commonVersionIdentifier || getFirstMatch(/blackberry[\d]+\/(\d+(\.\d+)?)/i, ua);
return {
name: 'PhantomJS',
name: 'BlackBerry',
version
}
}
},
{
test: [/phantom/i],
test: [/(web|hpw)os/i],
detect(ua) {
const version = /phantomjs\/(\d+(\.\d+)?)/i;
const version = commonVersionIdentifier || getFirstMatch(/w(?:eb)?osbrowser\/(\d+(\.\d+)?)/i, ua);
return {
name: 'PhantomJS',
name: 'WebOS Browser',
version
}
}
},
{
test: [/phantom/i],
test: [/bada/i],
detect(ua) {
const version = getFirstMatch(/dolfin\/(\d+(\.\d+)?)/i, ua);
return {
name: 'Bada',
version
}
}
},
{
test: [/tizen/i],
detect(ua) {
const version = getFirstMatch(/(?:tizen\s?)?browser\/(\d+(\.\d+)?)/i, ua) || commonVersionIdentifier;
return {
name: 'Tizen',
version
}
}
},
{
test: [/qupzilla/i],
detect(ua) {
const version = getFirstMatch(/(?:qupzilla)[\s\/](\d+(?:\.\d+)+)/i, ua) || commonVersionIdentifier;
return {
name: 'QupZilla',
version
}
}
},
{
test: [/chromium/i],
detect(ua) {
const version = getFirstMatch(/(?:chromium)[\s\/](\d+(?:\.\d+)?)/i, ua) || commonVersionIdentifier;
return {
name: 'Chromium',
version
}
}
},
{
test: [/chrome|crios|crmo/i],
detect(ua) {
const version = getFirstMatch(/(?:chrome|crios|crmo)\/(\d+(\.\d+)?)/i, ua);
return {
name: 'Chrome',
version
}
}
},
{
test(parser) {
const UA = parser.getUA();
return UA.test(/^((?!like android).)*$/i);
},
detect(ua) {
const version = /phantomjs\/(\d+(\.\d+)?)/i;
@ -254,4 +314,4 @@ const browsersList = [
},
];
module.exports = browsersList;
export default browsersList;

@ -1,19 +1,62 @@
var browsers = require('./parser-browsers');
import browsersList from './parser-browsers';
class Parser {
/**
* Create instance of Parser
* @param UA
* @throw
* @constructor
*/
constructor(UA) {
if (UA === void(0) || UA === null || UA === '') {
throw new Error("UserAgent parameter can't be empty");
}
this._ua = UA;
this.result = {};
this.parsedResult = {};
}
/**
* Get UserAgent string of current Parser instance
* @return {String}
*
* @public
*/
getUA() {
return this._ua;
}
/**
* Get parsed browser object
* @return {Object}
*
* @public
*/
parseBrowser() {
if (this.result.browser) {
return this.result.browser;
if (this.parsedResult.browser) {
return this.parsedResult.browser;
}
const browser = browsers.find((browser) => {
return browser.test.some((result, item) => { item.test(this._ua)});
this.parsedResult.browser = {};
const browser = browsersList.find((browser) => {
return browser.test(this);
});
if (browser) {
this.parsedResult.browser = browser.parse(this.getUA());
}
return this.parsedResult.browser;
}
/**
* Test a UA string for a regexp
* @param {RegExp} regex
* @return {Boolean}
*/
test(regex) {
return regex.test(this._ua);
}
parseBrowserName() {}
@ -25,4 +68,4 @@ class Parser {
parseFullInfo(){}
}
module.exports = Parser;
export default Parser;

@ -13,6 +13,7 @@ class Utils {
/**
* Get second matched item for a string
* @param regexp
* @param {String} ua
* @return {Array|{index: number, input: string}|*|boolean|string}
*/
static getSecondMatch(regexp, ua) {

@ -0,0 +1,17 @@
import test from 'ava';
import Parser from '../../src/parser';
const UA = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36 OPR/43.0.2442.1165';
const parser = new Parser(UA);
test('constructor', t => {
t.truthy(parser instanceof Parser);
});
test('getUA', t => {
t.is(parser.getUA(), UA);
});
test('test', t => {
t.truthy(parser.test(/Chrome/i));
});

@ -0,0 +1,7 @@
import test from 'ava';
import { getFirstMatch } from '../../src/utils';
test('getFirstMatch', t => {
const matchedVersion = getFirstMatch(/version\/(\S+)/i, 'Chrome Version/11.11.11');
t.is(matchedVersion, '11.11.11');
});
Loading…
Cancel
Save