Skip to content

Commit

Permalink
Merge pull request #27 from cssinjs/update-bowser-to-2
Browse files Browse the repository at this point in the history
Update to bowser 2
  • Loading branch information
AleshaOleg authored Mar 20, 2019
2 parents ea47ae8 + f4d00c7 commit fafb36f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"webpack-cli": "^3.1.2"
},
"dependencies": {
"bowser": "^1.9.4",
"bowser": "^2.1.2",
"caniuse-lite": "^1.0.30000951"
}
}
29 changes: 16 additions & 13 deletions src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* of the MIT license. See the LICENSE file for details.
*/

import * as bowser from "bowser";
import * as Bowser from "bowser";

export type BrowserID = "unknown" | "ie" | "edge" | "firefox" | "chrome" | "safari" | "opera" |
"and_uc" | "samsung" | "bb" | "android" | "ios_saf" | "and_chr" | "ie_mob";
Expand All @@ -18,46 +18,49 @@ export interface Browser {

// bowser id -> caniuse id
const browserIDMap: { [id: string]: BrowserID } = {
msie: "ie",
msedge: "edge",
"internet explorer": "ie",
"microsoft edge": "edge",
firefox: "firefox",
chrome: "chrome",
safari: "safari",
opera: "opera",
ucbrowser: "and_uc",
samsungBrowser: "samsung",
"uc browser": "and_uc",
"samsung internet for android": "samsung",
blackberry: "bb",
android: "android",
"android browser": "android",
ios: "ios_saf",
};

/**
* @param {String} ua optional user-agent
*/
export function detectBrowser(ua?: string): Browser {
const bowserInst = ua ? bowser._detect(ua) : bowser;
let version = bowserInst.version.toString();
if (!ua) {
return;
}
const bowserInst = Bowser.getParser(ua);
let version = bowserInst.getBrowserVersion().toString();
let bid: BrowserID = "unknown";
for (const b in browserIDMap) {
if (bowserInst[b]) {
if (bowserInst.getBrowserName().toLowerCase() === b) {
bid = browserIDMap[b];
break;
}
}
// no opera mini support yet: https://github.com/ded/bowser/issues/158
// no chrome for android detection as it largely matches the desktop version which
// is better tracked in the caniuse database.
if (bid === "safari" && bowserInst.ios) {
if (bid === "safari" && bowserInst.getOS().name === "iOS") {
bid = "ios_saf";
} else if (bid === "ie" && bowserInst.windowsphone) {
} else if (bid === "ie" && bowserInst.getOS().name === "Windows Phone") {
bid = "ie_mob";
}

// use mobile os version.
if (bid === "android") {
version = bowserInst.osversion.toString();
version = bowserInst.getOSVersion().toString();
} else if (bid === "ios_saf") {
version = bowserInst.osversion.toString();
version = bowserInst.getOSVersion().toString();
}

return { id: bid, version };
Expand Down
10 changes: 5 additions & 5 deletions test/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

const fixture: any = {
"Mozilla/5.0 (Linux; Android 5.1.1; Nexus 9 Build/LMY48T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.83 Safari/537.36": {
browser: { id: "chrome", version: "47.0" },
browser: { id: "chrome", version: "47.0.2526.83" },
versionIndex: "47",
features: {
transforms2d: { level: "full", needPrefix: false, notes: [] },
Expand Down Expand Up @@ -66,7 +66,7 @@ const fixture: any = {
},
},
"Mozilla/5.0 (PlayBook; U; RIM Tablet OS 2.1.0; en-US) AppleWebKit/536.2+ (KHTML, like Gecko) Version/7.2.1.0 Safari/536.2+": {
browser: { id: "bb", version: "7.2" },
browser: { id: "bb", version: "7.2.1.0" },
versionIndex: "7",
features: {
transforms2d: { level: "full", needPrefix: true, notes: [] },
Expand Down Expand Up @@ -114,23 +114,23 @@ const fixture: any = {
},
},
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.13+ (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2": {
browser: { id: "safari", version: "5.1" },
browser: { id: "safari", version: "5.1.7" },
versionIndex: "TP",
features: {
transforms2d: { level: "full", needPrefix: false, notes: [] },
flexbox: { level: "full", needPrefix: false, notes: [] },
},
},
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.52 Safari/537.36 OPR/15.0.1147.100": {
browser: { id: "opera", version: "15.0" },
browser: { id: "opera", version: "15.0.1147.100" },
versionIndex: "15",
features: {
transforms2d: { level: "full", needPrefix: true, notes: [] },
flexbox: { level: "full", needPrefix: true, notes: [] },
},
},
"Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.462.0 Safari/534.3": {
browser: { id: "chrome", version: "6.0" },
browser: { id: "chrome", version: "6.0.462.0" },
versionIndex: "6",
features: {
transforms2d: { level: "full", needPrefix: true, notes: [] },
Expand Down

0 comments on commit fafb36f

Please sign in to comment.