Skip to content
This repository was archived by the owner on Nov 13, 2025. It is now read-only.

Commit 30a5ea5

Browse files
committed
Initial commit
0 parents  commit 30a5ea5

File tree

9 files changed

+268
-0
lines changed

9 files changed

+268
-0
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
end_of_line = lf
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true
9+
indent_size = 2

.eslintrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "airbnb-base/legacy",
3+
"env": {
4+
"mocha": true
5+
}
6+
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
language: node_js
2+
node_js:
3+
- "6"
4+
- "4"

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Fast Monkeys (www.fastmonkeys.com)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# is-browser-supported [![Build Status](https://travis-ci.org/fastmonkeys/is-browser-supported.svg?branch=master)](https://travis-ci.org/fastmonkeys/is-browser-supported)
2+
3+
Check if user agent string matches with browser criterias like in [Autoprefixer](https://github.com/postcss/autoprefixer).
4+
5+
## Install
6+
7+
```
8+
$ npm install --save is-browser-supported
9+
```
10+
11+
12+
## Usage
13+
14+
```js
15+
const isBrowserSupported = require('is-browser-supported');
16+
17+
isBrowserSupported('Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)', 'last 1 version');
18+
//=> false
19+
20+
isBrowserSupported('Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko', 'last 1 version');
21+
//=> true
22+
```
23+
24+
25+
## API
26+
27+
### isBrowserSupported(userAgent, [selections])
28+
29+
#### userAgent
30+
31+
Type: `string`
32+
33+
A user agent string to be checked.
34+
35+
#### selections
36+
37+
Type: `string`<br>
38+
Default: [same as Browserslist](https://github.com/ai/browserslist#queries)
39+
40+
Browser criteria to check the user agent string against, see [Browserslist's documentation](https://github.com/ai/browserslist#queries) for more details.
41+
42+
43+
## License
44+
45+
MIT © [Fast Monkeys](http://www.fastmonkeys.com)

index.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
var browserslist = require('browserslist');
2+
var useragent = require('useragent');
3+
4+
function getBrowserName(family) {
5+
var browserNames = {
6+
'BlackBerry WebKit': 'bb',
7+
'Chrome Mobile': 'and_chr',
8+
'Firefox Mobile': 'and_ff',
9+
'IE Mobile': 'ie_mob',
10+
'Mobile Safari': 'ios_saf',
11+
'Opera Mobile': 'op_mob',
12+
'Samsung Internet': 'samsung',
13+
'UC Browser': 'and_uc'
14+
};
15+
return browserNames[family] || family;
16+
}
17+
18+
function getBrowserVersionFromUserAgent(userAgent) {
19+
var agent = useragent.parse(userAgent);
20+
var version = [agent.major, agent.minor, agent.patch];
21+
var browserName = getBrowserName(agent.family);
22+
while (version.length > 0) {
23+
try {
24+
return browserslist(browserName + ' ' + version.join('.'))[0];
25+
} catch (e) {
26+
// Ignore unknown browser query error
27+
}
28+
version.pop();
29+
}
30+
return 'unknown';
31+
}
32+
33+
module.exports = function isBrowserSupported(userAgent, selections) {
34+
var browsersSupported = browserslist(selections);
35+
var browser = getBrowserVersionFromUserAgent(userAgent);
36+
return browsersSupported.indexOf(browser) !== -1;
37+
};

package.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "is-browser-supported",
3+
"version": "1.0.0",
4+
"description": "Check if user agent string matches with browser criterias like in Autoprefixer",
5+
"license": "MIT",
6+
"repository": "fastmonkeys/is-browser-supported",
7+
"author": {
8+
"name": "Janne Vanhala",
9+
"email": "janne@fastmonkeys.com"
10+
},
11+
"engines": {
12+
"node": ">=4"
13+
},
14+
"scripts": {
15+
"test": "eslint . && mocha test.js"
16+
},
17+
"keywords": [
18+
"agent",
19+
"browser",
20+
"browser version",
21+
"user-agent",
22+
"user agent",
23+
"useragent",
24+
"version"
25+
],
26+
"dependencies": {
27+
"browserslist": "^1.4.0",
28+
"useragent": "^2.1.9"
29+
},
30+
"devDependencies": {
31+
"eslint": "^3.8.1",
32+
"eslint-config-airbnb-base": "^9.0.0",
33+
"eslint-plugin-import": "^2.0.1",
34+
"mocha": "^3.1.2"
35+
}
36+
}

test.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/* eslint-disable func-names */
2+
var assert = require('assert');
3+
var isBrowserSupported = require('.');
4+
5+
describe('isBrowserSupported()', function () {
6+
var tests = [
7+
{
8+
userAgent: 'Mozilla/5.0 (Linux; U; Android 6.0; en-US; iris 870 4G Build/MRA58K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 UCBrowser/11.0.5.850 U3/0.8.0 Mobile Safari/534.30',
9+
selections: 'and_uc >= 11',
10+
expected: true
11+
},
12+
{
13+
userAgent: 'Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30',
14+
selections: 'android >= 4',
15+
expected: true
16+
},
17+
{
18+
userAgent: 'Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.1.0.346 Mobile Safari/534.11+',
19+
selections: 'bb >= 6',
20+
expected: true
21+
},
22+
{
23+
userAgent: 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36',
24+
selections: 'chrome >= 40',
25+
expected: true
26+
},
27+
{
28+
userAgent: 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2769.0 Mobile Safari/537.36',
29+
selections: 'and_chr >= 53',
30+
expected: true
31+
},
32+
{
33+
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246',
34+
selections: 'edge >= 12',
35+
expected: true
36+
},
37+
{
38+
userAgent: 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1',
39+
selections: 'firefox >= 40',
40+
expected: true
41+
},
42+
{
43+
userAgent: 'Mozilla/5.0 (Android 4.4; Mobile; rv:41.0) Gecko/41.0 Firefox/49.0',
44+
selections: 'and_ff >= 49',
45+
expected: true
46+
},
47+
{
48+
userAgent: 'Mozilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0; en-US))',
49+
selections: 'ie >= 10',
50+
expected: false
51+
},
52+
{
53+
userAgent: 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)',
54+
selections: 'ie >= 10',
55+
expected: true
56+
},
57+
{
58+
userAgent: 'Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch; HTC; Windows Phone 8X by HTC)',
59+
selections: 'ie_mob >= 10',
60+
expected: true
61+
},
62+
{
63+
userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/8.0 Mobile/11A465 Safari/9537.53',
64+
selections: 'ios_saf >= 8.1',
65+
expected: false
66+
},
67+
{
68+
userAgent: 'Mozilla/5.0 (iPad; CPU OS 8_1 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) GSA/8.0.57838 Mobile/12B410 Safari/600.1.4',
69+
selections: 'ios_saf >= 8.1',
70+
expected: true
71+
},
72+
{
73+
userAgent: 'Opera/9.80 (X11; Linux i686; Ubuntu/14.10) Presto/2.12.388 Version/12.16',
74+
selections: 'opera >= 9',
75+
expected: true
76+
},
77+
{
78+
userAgent: 'Opera/12.02 (Android 4.1; Linux; Opera Mobi/ADR-1111101157; U; en-US) Presto/2.9.201 Version/12.02',
79+
selections: 'op_mob >= 12',
80+
expected: true
81+
},
82+
{
83+
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A',
84+
selections: 'safari >= 7',
85+
expected: true
86+
},
87+
{
88+
userAgent: 'Mozilla/5.0 (Linux; Android 6.0.1; SAMSUNG SM-G920F Build/MMB29K) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/4.0 Chrome/44.0.2403.133 Mobile Safari/537.36',
89+
selections: 'samsung >= 4',
90+
expected: true
91+
},
92+
{
93+
userAgent: 'Googlebot/2.1 (+http://www.google.com/bot.html)',
94+
expected: false
95+
}
96+
];
97+
98+
tests.forEach(function (test) {
99+
var description =
100+
JSON.stringify(test.userAgent) +
101+
(test.expected ? ' matches with ' : ' does not match with ') +
102+
JSON.stringify(test.selections);
103+
104+
it(description, function () {
105+
var actual = isBrowserSupported(test.userAgent, test.selections);
106+
assert.equal(test.expected, actual);
107+
});
108+
});
109+
});

0 commit comments

Comments
 (0)