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

Merge pull request #325 from 1615450788/master

Add constant output so that users can quickly get all types
This commit is contained in:
Denis Demchenko 2019-05-27 00:44:08 +03:00 committed by GitHub
commit 41bbfc1600
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 146 additions and 52 deletions

View File

@ -5,6 +5,12 @@
* MIT License | (c) Denis Demchenko 2015-2017 * MIT License | (c) Denis Demchenko 2015-2017
*/ */
import Parser from './parser.js'; import Parser from './parser.js';
import {
BROWSER_MAP,
ENGINE_MAP,
OS_MAP,
PLATFORMS_MAP,
} from './constants.js';
/** /**
* Bowser class. * Bowser class.
@ -47,4 +53,9 @@ class Bowser {
} }
} }
Bowser.BROWSER_MAP = BROWSER_MAP;
Bowser.ENGINE_MAP = ENGINE_MAP;
Bowser.OS_MAP = OS_MAP;
Bowser.PLATFORMS_MAP = PLATFORMS_MAP;
export default Bowser; export default Bowser;

View File

@ -36,3 +36,71 @@ export const BROWSER_ALIASES_MAP = {
WeChat: 'wechat', WeChat: 'wechat',
'Yandex Browser': 'yandex', 'Yandex Browser': 'yandex',
}; };
export const BROWSER_MAP = {
amazon_silk: 'Amazon Silk',
android: 'Android Browser',
bada: 'Bada',
blackberry: 'BlackBerry',
chrome: 'Chrome',
chromium: 'Chromium',
epiphany: 'Epiphany',
firefox: 'Firefox',
focus: 'Focus',
generic: 'Generic',
googlebot: 'Googlebot',
ie: 'Internet Explorer',
k_meleon: 'K-Meleon',
maxthon: 'Maxthon',
edge: 'Microsoft Edge',
mz: 'MZ Browser',
naver: 'NAVER Whale Browser',
opera: 'Opera',
opera_coast: 'Opera Coast',
phantomjs: 'PhantomJS',
puffin: 'Puffin',
qupzilla: 'QupZilla',
safari: 'Safari',
sailfish: 'Sailfish',
samsung_internet: 'Samsung Internet for Android',
seamonkey: 'SeaMonkey',
sleipnir: 'Sleipnir',
swing: 'Swing',
tizen: 'Tizen',
uc: 'UC Browser',
vivaldi: 'Vivaldi',
webos: 'WebOS Browser',
wechat: 'WeChat',
yandex: 'Yandex Browser',
};
export const PLATFORMS_MAP = {
tablet: 'tablet',
mobile: 'mobile',
desktop: 'desktop',
tv: 'tv',
};
export const OS_MAP = {
WindowsPhone: 'Windows Phone',
Windows: 'Windows',
MacOS: 'macOS',
iOS: 'iOS',
Android: 'Android',
WebOS: 'WebOS',
BlackBerry: 'BlackBerry',
Bada: 'Bada',
Tizen: 'Tizen',
Linux: 'Linux',
ChromeOS: 'Chrome OS',
PlayStation4: 'PlayStation 4',
};
export const ENGINE_MAP = {
EdgeHTML: 'EdgeHTML',
Blink: 'Blink',
Trident: 'Trident',
Presto: 'Presto',
Gecko: 'Gecko',
WebKit: 'WebKit',
};

View File

@ -1,4 +1,5 @@
import Utils from './utils.js'; import Utils from './utils.js';
import { ENGINE_MAP } from './constants.js';
/* /*
* More specific goes first * More specific goes first
@ -15,7 +16,7 @@ export default [
// return blink if it's blink-based one // return blink if it's blink-based one
if (isBlinkBased) { if (isBlinkBased) {
return { return {
name: 'Blink', name: ENGINE_MAP.Blink,
}; };
} }
@ -23,7 +24,7 @@ export default [
const version = Utils.getFirstMatch(/edge\/(\d+(\.?_?\d+)+)/i, ua); const version = Utils.getFirstMatch(/edge\/(\d+(\.?_?\d+)+)/i, ua);
return { return {
name: 'EdgeHTML', name: ENGINE_MAP.EdgeHTML,
version, version,
}; };
}, },
@ -34,7 +35,7 @@ export default [
test: [/trident/i], test: [/trident/i],
describe(ua) { describe(ua) {
const engine = { const engine = {
name: 'Trident', name: ENGINE_MAP.Trident,
}; };
const version = Utils.getFirstMatch(/trident\/(\d+(\.?_?\d+)+)/i, ua); const version = Utils.getFirstMatch(/trident\/(\d+(\.?_?\d+)+)/i, ua);
@ -54,7 +55,7 @@ export default [
}, },
describe(ua) { describe(ua) {
const engine = { const engine = {
name: 'Presto', name: ENGINE_MAP.Presto,
}; };
const version = Utils.getFirstMatch(/presto\/(\d+(\.?_?\d+)+)/i, ua); const version = Utils.getFirstMatch(/presto\/(\d+(\.?_?\d+)+)/i, ua);
@ -76,7 +77,7 @@ export default [
}, },
describe(ua) { describe(ua) {
const engine = { const engine = {
name: 'Gecko', name: ENGINE_MAP.Gecko,
}; };
const version = Utils.getFirstMatch(/gecko\/(\d+(\.?_?\d+)+)/i, ua); const version = Utils.getFirstMatch(/gecko\/(\d+(\.?_?\d+)+)/i, ua);
@ -94,7 +95,7 @@ export default [
test: [/(apple)?webkit\/537\.36/i], test: [/(apple)?webkit\/537\.36/i],
describe() { describe() {
return { return {
name: 'Blink', name: ENGINE_MAP.Blink,
}; };
}, },
}, },
@ -104,7 +105,7 @@ export default [
test: [/(apple)?webkit/i], test: [/(apple)?webkit/i],
describe(ua) { describe(ua) {
const engine = { const engine = {
name: 'WebKit', name: ENGINE_MAP.WebKit,
}; };
const version = Utils.getFirstMatch(/webkit\/(\d+(\.?_?\d+)+)/i, ua); const version = Utils.getFirstMatch(/webkit\/(\d+(\.?_?\d+)+)/i, ua);

View File

@ -1,4 +1,5 @@
import Utils from './utils.js'; import Utils from './utils.js';
import { OS_MAP } from './constants.js';
export default [ export default [
/* Windows Phone */ /* Windows Phone */
@ -7,7 +8,7 @@ export default [
describe(ua) { describe(ua) {
const version = Utils.getFirstMatch(/windows phone (?:os)?\s?(\d+(\.\d+)*)/i, ua); const version = Utils.getFirstMatch(/windows phone (?:os)?\s?(\d+(\.\d+)*)/i, ua);
return { return {
name: 'Windows Phone', name: OS_MAP.WindowsPhone,
version, version,
}; };
}, },
@ -21,7 +22,7 @@ export default [
const versionName = Utils.getWindowsVersionName(version); const versionName = Utils.getWindowsVersionName(version);
return { return {
name: 'Windows', name: OS_MAP.Windows,
version, version,
versionName, versionName,
}; };
@ -34,7 +35,7 @@ export default [
describe(ua) { describe(ua) {
const version = Utils.getFirstMatch(/mac os x (\d+(\.?_?\d+)+)/i, ua).replace(/[_\s]/g, '.'); const version = Utils.getFirstMatch(/mac os x (\d+(\.?_?\d+)+)/i, ua).replace(/[_\s]/g, '.');
return { return {
name: 'macOS', name: OS_MAP.MacOS,
version, version,
}; };
}, },
@ -47,7 +48,7 @@ export default [
const version = Utils.getFirstMatch(/os (\d+([_\s]\d+)*) like mac os x/i, ua).replace(/[_\s]/g, '.'); const version = Utils.getFirstMatch(/os (\d+([_\s]\d+)*) like mac os x/i, ua).replace(/[_\s]/g, '.');
return { return {
name: 'iOS', name: OS_MAP.iOS,
version, version,
}; };
}, },
@ -64,7 +65,7 @@ export default [
const version = Utils.getFirstMatch(/android[\s/-](\d+(\.\d+)*)/i, ua); const version = Utils.getFirstMatch(/android[\s/-](\d+(\.\d+)*)/i, ua);
const versionName = Utils.getAndroidVersionName(version); const versionName = Utils.getAndroidVersionName(version);
const os = { const os = {
name: 'Android', name: OS_MAP.Android,
version, version,
}; };
if (versionName) { if (versionName) {
@ -80,7 +81,7 @@ export default [
describe(ua) { describe(ua) {
const version = Utils.getFirstMatch(/(?:web|hpw)[o0]s\/(\d+(\.\d+)*)/i, ua); const version = Utils.getFirstMatch(/(?:web|hpw)[o0]s\/(\d+(\.\d+)*)/i, ua);
const os = { const os = {
name: 'WebOS', name: OS_MAP.WebOS,
}; };
if (version && version.length) { if (version && version.length) {
@ -99,7 +100,7 @@ export default [
|| Utils.getFirstMatch(/\bbb(\d+)/i, ua); || Utils.getFirstMatch(/\bbb(\d+)/i, ua);
return { return {
name: 'BlackBerry', name: OS_MAP.BlackBerry,
version, version,
}; };
}, },
@ -112,7 +113,7 @@ export default [
const version = Utils.getFirstMatch(/bada\/(\d+(\.\d+)*)/i, ua); const version = Utils.getFirstMatch(/bada\/(\d+(\.\d+)*)/i, ua);
return { return {
name: 'Bada', name: OS_MAP.Bada,
version, version,
}; };
}, },
@ -125,7 +126,7 @@ export default [
const version = Utils.getFirstMatch(/tizen[/\s](\d+(\.\d+)*)/i, ua); const version = Utils.getFirstMatch(/tizen[/\s](\d+(\.\d+)*)/i, ua);
return { return {
name: 'Tizen', name: OS_MAP.Tizen,
version, version,
}; };
}, },
@ -136,7 +137,7 @@ export default [
test: [/linux/i], test: [/linux/i],
describe() { describe() {
return { return {
name: 'Linux', name: OS_MAP.Linux,
}; };
}, },
}, },
@ -146,7 +147,7 @@ export default [
test: [/CrOS/], test: [/CrOS/],
describe() { describe() {
return { return {
name: 'Chrome OS', name: OS_MAP.ChromeOS,
}; };
}, },
}, },
@ -157,7 +158,7 @@ export default [
describe(ua) { describe(ua) {
const version = Utils.getFirstMatch(/PlayStation 4[/\s](\d+(\.\d+)*)/i, ua); const version = Utils.getFirstMatch(/PlayStation 4[/\s](\d+(\.\d+)*)/i, ua);
return { return {
name: 'PlayStation 4', name: OS_MAP.PlayStation4,
version, version,
}; };
}, },

View File

@ -1,11 +1,5 @@
import Utils from './utils.js'; import Utils from './utils.js';
import { PLATFORMS_MAP } from './constants.js';
const TYPES_LABELS = {
tablet: 'tablet',
mobile: 'mobile',
desktop: 'desktop',
tv: 'tv',
};
/* /*
* Tablets go first since usually they have more specific * Tablets go first since usually they have more specific
@ -30,7 +24,7 @@ export default [
describe(ua) { describe(ua) {
const model = Utils.getFirstMatch(/(can-l01)/i, ua) && 'Nova'; const model = Utils.getFirstMatch(/(can-l01)/i, ua) && 'Nova';
const platform = { const platform = {
type: TYPES_LABELS.mobile, type: PLATFORMS_MAP.mobile,
vendor: 'Huawei', vendor: 'Huawei',
}; };
if (model) { if (model) {
@ -45,7 +39,7 @@ export default [
test: [/nexus\s*(?:7|8|9|10).*/i], test: [/nexus\s*(?:7|8|9|10).*/i],
describe() { describe() {
return { return {
type: TYPES_LABELS.tablet, type: PLATFORMS_MAP.tablet,
vendor: 'Nexus', vendor: 'Nexus',
}; };
}, },
@ -56,7 +50,7 @@ export default [
test: [/ipad/i], test: [/ipad/i],
describe() { describe() {
return { return {
type: TYPES_LABELS.tablet, type: PLATFORMS_MAP.tablet,
vendor: 'Apple', vendor: 'Apple',
model: 'iPad', model: 'iPad',
}; };
@ -68,7 +62,7 @@ export default [
test: [/kftt build/i], test: [/kftt build/i],
describe() { describe() {
return { return {
type: TYPES_LABELS.tablet, type: PLATFORMS_MAP.tablet,
vendor: 'Amazon', vendor: 'Amazon',
model: 'Kindle Fire HD 7', model: 'Kindle Fire HD 7',
}; };
@ -80,7 +74,7 @@ export default [
test: [/silk/i], test: [/silk/i],
describe() { describe() {
return { return {
type: TYPES_LABELS.tablet, type: PLATFORMS_MAP.tablet,
vendor: 'Amazon', vendor: 'Amazon',
}; };
}, },
@ -91,7 +85,7 @@ export default [
test: [/tablet/i], test: [/tablet/i],
describe() { describe() {
return { return {
type: TYPES_LABELS.tablet, type: PLATFORMS_MAP.tablet,
}; };
}, },
}, },
@ -106,7 +100,7 @@ export default [
describe(ua) { describe(ua) {
const model = Utils.getFirstMatch(/(ipod|iphone)/i, ua); const model = Utils.getFirstMatch(/(ipod|iphone)/i, ua);
return { return {
type: TYPES_LABELS.mobile, type: PLATFORMS_MAP.mobile,
vendor: 'Apple', vendor: 'Apple',
model, model,
}; };
@ -118,7 +112,7 @@ export default [
test: [/nexus\s*[0-6].*/i, /galaxy nexus/i], test: [/nexus\s*[0-6].*/i, /galaxy nexus/i],
describe() { describe() {
return { return {
type: TYPES_LABELS.mobile, type: PLATFORMS_MAP.mobile,
vendor: 'Nexus', vendor: 'Nexus',
}; };
}, },
@ -129,7 +123,7 @@ export default [
test: [/[^-]mobi/i], test: [/[^-]mobi/i],
describe() { describe() {
return { return {
type: TYPES_LABELS.mobile, type: PLATFORMS_MAP.mobile,
}; };
}, },
}, },
@ -141,7 +135,7 @@ export default [
}, },
describe() { describe() {
return { return {
type: TYPES_LABELS.mobile, type: PLATFORMS_MAP.mobile,
vendor: 'BlackBerry', vendor: 'BlackBerry',
}; };
}, },
@ -154,7 +148,7 @@ export default [
}, },
describe() { describe() {
return { return {
type: TYPES_LABELS.mobile, type: PLATFORMS_MAP.mobile,
}; };
}, },
}, },
@ -166,7 +160,7 @@ export default [
}, },
describe() { describe() {
return { return {
type: TYPES_LABELS.mobile, type: PLATFORMS_MAP.mobile,
vendor: 'Microsoft', vendor: 'Microsoft',
}; };
}, },
@ -180,7 +174,7 @@ export default [
}, },
describe() { describe() {
return { return {
type: TYPES_LABELS.tablet, type: PLATFORMS_MAP.tablet,
}; };
}, },
}, },
@ -192,7 +186,7 @@ export default [
}, },
describe() { describe() {
return { return {
type: TYPES_LABELS.mobile, type: PLATFORMS_MAP.mobile,
}; };
}, },
}, },
@ -204,7 +198,7 @@ export default [
}, },
describe() { describe() {
return { return {
type: TYPES_LABELS.desktop, type: PLATFORMS_MAP.desktop,
vendor: 'Apple', vendor: 'Apple',
}; };
}, },
@ -217,7 +211,7 @@ export default [
}, },
describe() { describe() {
return { return {
type: TYPES_LABELS.desktop, type: PLATFORMS_MAP.desktop,
}; };
}, },
}, },
@ -229,7 +223,7 @@ export default [
}, },
describe() { describe() {
return { return {
type: TYPES_LABELS.desktop, type: PLATFORMS_MAP.desktop,
}; };
}, },
}, },
@ -241,7 +235,7 @@ export default [
}, },
describe() { describe() {
return { return {
type: TYPES_LABELS.tv, type: PLATFORMS_MAP.tv,
}; };
}, },
}, },

View File

@ -409,15 +409,14 @@ class Parser {
* @returns {boolean} * @returns {boolean}
*/ */
isBrowser(browserName, includingAlias = false) { isBrowser(browserName, includingAlias = false) {
const defaultBrowserName = this.getBrowserName(); const defaultBrowserName = this.getBrowserName().toLowerCase();
const possibleNames = [defaultBrowserName.toLowerCase()]; let browserNameLower = browserName.toLowerCase();
const alias = Utils.getBrowserAlias(defaultBrowserName); const alias = Utils.getBrowserTypeByAlias(browserNameLower);
if (includingAlias && typeof alias !== 'undefined') { if (includingAlias && alias) {
possibleNames.push(alias.toLowerCase()); browserNameLower = alias.toLowerCase();
} }
return browserNameLower === defaultBrowserName;
return possibleNames.indexOf(browserName.toLowerCase()) !== -1;
} }
compareVersion(version) { compareVersion(version) {

View File

@ -1,4 +1,4 @@
import { BROWSER_ALIASES_MAP } from './constants.js'; import { BROWSER_MAP, BROWSER_ALIASES_MAP } from './constants.js';
export default class Utils { export default class Utils {
/** /**
@ -202,4 +202,17 @@ export default class Utils {
static getBrowserAlias(browserName) { static getBrowserAlias(browserName) {
return BROWSER_ALIASES_MAP[browserName]; return BROWSER_ALIASES_MAP[browserName];
} }
/**
* Get short version/alias for a browser name
*
* @example
* getBrowserAlias('edge') // Microsoft Edge
*
* @param {string} browserName
* @return {string}
*/
static getBrowserTypeByAlias(browserAlia) {
return BROWSER_MAP[browserAlia] || '';
}
} }

View File

@ -1,6 +1,7 @@
import test from 'ava'; import test from 'ava';
import sinon from 'sinon'; import sinon from 'sinon';
import Parser from '../../src/parser'; import Parser from '../../src/parser';
import Bowser from '../../src/bowser';
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 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, true); const parser = new Parser(UA, true);
@ -164,6 +165,12 @@ test('Parser.is should pass', (t) => {
t.is(parser.is('macos'), true); t.is(parser.is('macos'), true);
}); });
test('Parser.is using constants should pass', (t) => {
t.is(parser.is(Bowser.BROWSER_MAP.opera), true);
t.is(parser.is(Bowser.PLATFORMS_MAP.desktop), true);
t.is(parser.is(Bowser.OS_MAP.MacOS), true);
});
test('Parser.some should pass', (t) => { test('Parser.some should pass', (t) => {
t.is(parser.some(['opera', 'chrome', 'firefox']), true); t.is(parser.some(['opera', 'chrome', 'firefox']), true);
t.is(parser.some(['macos', 'windows']), true); t.is(parser.some(['macos', 'windows']), true);