1
0
mirror of https://github.com/lancedikson/bowser synced 2026-03-02 03:40:27 +00:00

Add support for using short version for browser name in satisfies

This commit is contained in:
Will Soares
2019-02-17 22:05:40 -03:00
parent 28bff841b3
commit a307533f74
6 changed files with 106 additions and 6 deletions

41
src/constants.js Normal file
View File

@@ -0,0 +1,41 @@
// NOTE: this list must be up-to-date with browsers listed in
// test/acceptance/useragentstrings.yml
const BROWSER_ALIASES_MAP = {
'Amazon Silk': 'amazonSilk',
'Android Browser': 'android',
Bada: 'bada',
BlackBerry: 'blackBerry',
Chrome: 'chrome',
Chromium: 'chromium',
Epiphany: 'epiphany',
Firefox: 'firefox',
Focus: 'focus',
Generic: 'generic',
Googlebot: 'googlebot',
'Internet Explorer': 'ie',
'K-Meleon': 'kMeleon',
Maxthon: 'maxthon',
'Microsoft Edge': 'edge',
'MZ Browser': 'mz',
'NAVER Whale Browser': 'naver',
Opera: 'opera',
'Opera Coast': 'operaCoast',
PhantomJS: 'phantomJS',
Puffin: 'puffin',
QupZilla: 'qupZilla',
Safari: 'safari',
Sailfish: 'sailfish',
SeaMonkey: 'seaMonkey',
Sleipnir: 'sleipnir',
Swing: 'swing',
Tizen: 'tizen',
'UC Browser': 'uc',
Vivaldi: 'vivaldi',
'WebOS Browser': 'webOS',
WeChat: 'weChat',
'Yandex Browser': 'yandex',
};
module.exports = {
BROWSER_ALIASES_MAP,
};

View File

@@ -392,7 +392,7 @@ class Parser {
if (browsersCounter > 0) {
const browserNames = Object.keys(browsers);
const matchingDefinition = browserNames.find(name => (this.isBrowser(name)));
const matchingDefinition = browserNames.find(name => (this.isBrowser(name, true)));
if (matchingDefinition !== void 0) {
return this.compareVersion(browsers[matchingDefinition]);
@@ -402,8 +402,15 @@ class Parser {
return undefined;
}
isBrowser(browserName) {
return this.getBrowserName(true) === String(browserName).toLowerCase();
isBrowser(browserName, loosely = false) {
const defaultBrowserName = this.getBrowserName();
const possibleNames = [defaultBrowserName.toLowerCase()];
if (loosely) {
possibleNames.push(getBrowserAlias(defaultBrowserName).toLowerCase());
}
return possibleNames.indexOf(browserName.toLowerCase()) !== -1;
}
compareVersion(version) {

View File

@@ -1,3 +1,5 @@
import { BROWSER_ALIASES_MAP } from './constants';
export default class Utils {
/**
* Get first matched item for a string
@@ -187,4 +189,17 @@ export default class Utils {
}
return result;
}
/**
* Get short version/alias for a browser name
*
* @example
* getBrowserAlias('Microsoft Edge') // edge
*
* @param {string} browserName
* @return {string}
*/
static getBrowserAlias(browserName) {
return BROWSER_ALIASES_MAP[browserName];
}
}