Skip to content

Commit e278913

Browse files
committed
init
0 parents  commit e278913

File tree

7 files changed

+4809
-0
lines changed

7 files changed

+4809
-0
lines changed

.github/workflows/test.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: CI
2+
on: push
3+
jobs:
4+
build:
5+
runs-on: ubuntu-latest
6+
steps:
7+
- uses: actions/checkout@v2
8+
- name: Install modules
9+
run: yarn
10+
- name: Run tests
11+
run: yarn test

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.vscode
2+
.DS_Store
3+
node_modules

babel.config.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = {
2+
presets: [
3+
[
4+
"@babel/preset-env",
5+
{
6+
targets: {
7+
node: "current",
8+
},
9+
},
10+
],
11+
],
12+
};

index.js

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
const userAgent = () =>
2+
((navigator && navigator.userAgent) || "").toLowerCase();
3+
const vendor = () => ((navigator && navigator.vendor) || "").toLowerCase();
4+
5+
const comparator = {
6+
"<": (a, b) => a < b,
7+
"<=": (a, b) => a <= b,
8+
">": (a, b) => a > b,
9+
">=": (a, b) => a >= b,
10+
};
11+
12+
function compareVersion(version, range) {
13+
let string = range + "";
14+
let n = +(string.match(/\d+/) || NaN);
15+
let op = string.match(/^[<>]=?|/)[0];
16+
return comparator[op] ? comparator[op](version, n) : version == n || n !== n;
17+
}
18+
19+
export let supportedBrowsers = {
20+
msie: "none", //ie / internet explorer
21+
edge: ">1", // example version range for now
22+
chrome: "any",
23+
firefox: "any",
24+
opera: "none",
25+
};
26+
27+
function isSupported(browsers = supportedBrowsers) {
28+
let browser = getBrowser();
29+
let version = browsers[browser];
30+
if (!version || version == "none") return false;
31+
if (version == "any") return true;
32+
switch (browser) {
33+
case "msie":
34+
return isIe(version);
35+
case "edge":
36+
return isEdge(version);
37+
case "firefox":
38+
return isFirefox(version);
39+
case "chrome":
40+
return isChrome(version);
41+
}
42+
}
43+
44+
const browsersRegex = new RegExp(Object.keys(supportedBrowsers).join("|"));
45+
46+
function getBrowser() {
47+
let match = userAgent().match(browsersRegex);
48+
if (!match) return null;
49+
return match[0];
50+
}
51+
// can take a number or a string. Can also take a string prefixed with a comparable like, > or >=
52+
// example, chrome('>=90')
53+
function isChrome(range) {
54+
let match = /google inc/.test(vendor())
55+
? userAgent().match(/(?:chrome|crios)\/(\d+)/)
56+
: null;
57+
return match !== null && compareVersion(match[1], range);
58+
}
59+
60+
function isIe(range) {
61+
let match = userAgent().match(/(?:msie |trident.+?; rv:)(\d+)/);
62+
return match !== null && compareVersion(match[1], range);
63+
}
64+
65+
function isEdge(range) {
66+
let match = userAgent().match(/edge\/(\d+)/);
67+
return match !== null && compareVersion(match[1], range);
68+
}
69+
70+
function isFirefox(range) {
71+
let match = userAgent().match(/(?:firefox|fxios)\/(\d+)/);
72+
return match !== null && compareVersion(match[1], range);
73+
}
74+
75+
export default {
76+
supportedBrowsers,
77+
compareVersion,
78+
isSupported,
79+
isChrome,
80+
isIe,
81+
isEdge,
82+
isFirefox,
83+
};

package.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "is-supported",
3+
"version": "1.0.0",
4+
"description": "determines if client is running on a supported browser",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "jest --runInBand --forceExit"
8+
},
9+
"author": "Randy Lough",
10+
"license": "ISC",
11+
"devDependencies": {
12+
"@babel/core": "^7.14.3",
13+
"@babel/preset-env": "^7.14.2",
14+
"babel-jest": "^26.6.3",
15+
"jest": "^26.6.3"
16+
}
17+
}

0 commit comments

Comments
 (0)