Skip to content

Commit e3703e1

Browse files
committed
Merge pull request #5 from PeterMK85/extend-functionality--which
Extend functionality with which(Browser & OS)
2 parents 551542a + 0a09108 commit e3703e1

File tree

9 files changed

+215
-18
lines changed

9 files changed

+215
-18
lines changed

bower.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "date-fns",
3-
"version": "0.1.5",
2+
"name": "sniff-fns",
3+
"version": "0.3.0",
44
"homepage": "https://github.com/js-fns/sniff-fns",
55
"authors": [
66
"Ivan Shornikov <[email protected]>"

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sniff-fns",
3-
"version": "0.2.1",
3+
"version": "0.3.0",
44
"author": "Ivan Shornikov <[email protected]>",
55
"description": "Browser agent sniffing functions",
66
"repository": "https://github.com/js-fns/sniff-fns",

src/__tests__/sniff_fns_test.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
var sniffFns = require('../sniff_fns');
22

3-
describe('bundle sniffFns', function(){
4-
it('returns object with functions', function(){
5-
var keys = ["isAndroid", "isMobileAndroid", "isBb10", "browser", "isIphone", "isIpad", "isMac", "isWindowsPhone", "isMobile"];
3+
describe('bundle sniffFns', function () {
4+
it('returns object with functions', function () {
5+
var keys = [
6+
"isAndroid",
7+
"isMobileAndroid",
8+
"isBb10",
9+
"browser",
10+
"isIphone",
11+
"isIpad",
12+
"isMac",
13+
"isWindows",
14+
"isWindowsPhone",
15+
"isMobile",
16+
"whichOs"
17+
];
618

719
expect(Object.keys(sniffFns)).is.eql(keys);
820
});
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
var whichBrowser = require('../which_browser');
2+
3+
describe('whichBrowser', function () {
4+
it('returns \'chrome\'', function () {
5+
var chromeUA = [
6+
'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36',
7+
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36',
8+
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36'
9+
].forEach(function (userAgent) {
10+
expect(whichBrowser(userAgent)).to.equal('chrome');
11+
})
12+
});
13+
14+
it('returns \'chrome mobile\'', function () {
15+
var chromeMobileUA = [
16+
'Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19',
17+
'Mozilla/5.0 (iPhone; U; CPU iPhone OS 5_1_1 like Mac OS X; en) AppleWebKit/534.46.0 (KHTML, like Gecko) CriOS/19.0.1084.60 Mobile/9B206 Safari/7534.48.3'
18+
].forEach(function (userAgent) {
19+
expect(whichBrowser(userAgent)).to.equal('chrome mobile');
20+
})
21+
});
22+
23+
it('returns \'firefox\'', function () {
24+
var firefoxUA = [
25+
'Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0',
26+
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10; rv:33.0) Gecko/20100101 Firefox/33.0',
27+
'Mozilla/5.0 (X11; Linux i586; rv:31.0) Gecko/20100101 Firefox/31.0',
28+
'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20130401 Firefox/31.0',
29+
'Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0'
30+
].forEach(function (userAgent) {
31+
expect(whichBrowser(userAgent)).to.equal('firefox');
32+
})
33+
});
34+
35+
it('returns \'opera\'', function () {
36+
var operaUA = [
37+
'Opera/9.80 (Android; Opera Mini/7.5.33361/31.1448; U; en) Presto/2.8.119 Version/11.1010',
38+
'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36 OPR/16.0.1196.73',
39+
'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'
40+
].forEach(function (userAgent) {
41+
expect(whichBrowser(userAgent)).to.equal('opera');
42+
})
43+
});
44+
45+
it('returns \'safari\'', function () {
46+
var safariUA = [
47+
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9) AppleWebKit/537.71 (KHTML, like Gecko) Version/7.0 Safari/537.71',
48+
'Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25'
49+
].forEach(function (userAgent) {
50+
expect(whichBrowser(userAgent)).to.equal('safari');
51+
})
52+
});
53+
54+
it('returns \'unknown browser\'', function () {
55+
var unknownUA = [
56+
'Mozilla/5.0 (Windows; U; Windows CE 5.2; en-US; rv:1.9.2a1pre) Gecko/20090210 Fennec/0.11'
57+
].forEach(function (userAgent) {
58+
expect(whichBrowser(userAgent)).to.equal('unknown browser');
59+
})
60+
});
61+
});

src/__tests__/which_os_test.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
var whichOs = require('../which_os');
2+
3+
describe('whichOs', function () {
4+
it('returns \'android\'', function () {
5+
var androidUA = [
6+
'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',
7+
'Mozilla/5.0 (Linux; U; Android 4.0.3; de-ch; HTC Sensation Build/IML74K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30',
8+
'Mozilla/5.0 (Linux; U; Android 2.3; en-us) AppleWebKit/999+ (KHTML, like Gecko) Safari/999.9'
9+
].forEach(function (userAgent) {
10+
expect(whichOs(userAgent)).to.equal('android');
11+
})
12+
});
13+
14+
it('returns \'blackberry\'', function () {
15+
var bbUA = [
16+
'Mozilla/5.0 (BB10; <Device Model>) AppleWebKit/<WebKit Version> (KHTML, like Gecko) Version/<BB Version #> Mobile Safari/<WebKit Version>'
17+
].forEach(function (userAgent) {
18+
expect(whichOs(userAgent)).to.equal('blackberry');
19+
})
20+
});
21+
22+
it('returns \'iphone\'', function () {
23+
var iphoneUA = [
24+
'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1C25 Safari/419.3',
25+
'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7',
26+
'Mozilla/5.0 (iPod touch; CPU iPhone OS 7_0_3 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11B511 Safari/9537.53'
27+
].forEach(function (userAgent) {
28+
expect(whichOs(userAgent)).to.equal('iphone');
29+
})
30+
});
31+
32+
it('returns \'mac\'', function () {
33+
var macUA = [
34+
'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10',
35+
'Mozilla/5.0 (iPad; CPU OS 7_0 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53',
36+
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2'
37+
].forEach(function (userAgent) {
38+
expect(whichOs(userAgent)).to.equal('mac');
39+
})
40+
});
41+
42+
it('returns \'windows\'', function () {
43+
var windowsUA = [
44+
'Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14',
45+
'Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0',
46+
'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'
47+
].forEach(function (userAgent) {
48+
expect(whichOs(userAgent)).to.equal('windows');
49+
})
50+
});
51+
52+
it('returns \'windows mobile\'', function () {
53+
var WindowsUA = [
54+
'Mozilla/5.0 (Windows Phone 8.1; ARM; Trident/7.0; Touch; rv:11; IEMobile/11.0) like Android 4.1.2; compatible) like iPhone OS 7_0_3 Mac OS X WebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.99 Mobile Safari /537.36',
55+
'Mozilla/5.0 (Windows Phone 8.1; ARM; Trident/7.0; Touch; rv:11; IEMobile/11.0; NOKIA; Lumia 928) like Gecko',
56+
'Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch; rv:11; NOKIA; Lumia 920) like Gecko'
57+
].forEach(function (userAgent) {
58+
expect(whichOs(userAgent)).to.equal('windows mobile');
59+
})
60+
});
61+
62+
it('returns \'unknown os\'', function () {
63+
var unknown = [
64+
'Opera/9.80 (NT 6.0) Presto/2.12.388 Version/12.14'
65+
].forEach(function (userAgent) {
66+
expect(whichOs(userAgent)).to.equal('unknown os');
67+
})
68+
});
69+
});

src/is_android.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
var testString = require('./test_string')
1+
var testString = require('./test_string');
22

3-
var isAndroid = function(str){
4-
if (testString(str, 'android')) {
5-
return parseFloat(str.replace(/^.* android (\d+)\.(\d+).*$/, "$1.$2")) || true
6-
} else {
7-
return false
8-
}
3+
var isAndroid = function (str) {
4+
return testString(str, '; android');
95
};
106

117
module.exports = isAndroid;

src/sniff_fns.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1+
// Browsers
12
var isChrome = require('./is_browser_chrome');
23
var isChromeMobile = require('./is_browser_chrome_mobile');
3-
var isAndroid = require('./is_android');
4-
var isAndroidMobile = require('./is_android_mobile');
5-
var isBB = require('./is_blackberry');
64
var isFF = require('./is_browser_firefox');
75
var isOpera = require('./is_browser_opera');
86
var isSafari = require('./is_browser_safari');
7+
var whichBrowser = require('./which_browser');
8+
9+
// OSes
10+
var isAndroid = require('./is_android');
11+
var isAndroidMobile = require('./is_android_mobile');
12+
var isBB = require('./is_blackberry');
913
var isIphone = require('./is_iphone');
1014
var isIpad = require('./is_ipad');
1115
var isMac = require('./is_mac');
16+
var isWindows = require('./is_windows');
1217
var isWP = require('./is_windows_phone');
1318
var isMobile = require('./is_mobile');
19+
var whichOs = require('./which_os');
1420

1521
var sniffFns = {
1622
isAndroid: isAndroid,
@@ -21,13 +27,16 @@ var sniffFns = {
2127
isChromeMobile: isChromeMobile,
2228
isFirefox: isFF,
2329
isOpera: isOpera,
24-
isSafari: isSafari
30+
isSafari: isSafari,
31+
whichBrowser: whichBrowser
2532
},
2633
isIphone: isIphone,
2734
isIpad: isIpad,
2835
isMac: isMac,
36+
isWindows: isWindows,
2937
isWindowsPhone: isWP,
30-
isMobile: isMobile
38+
isMobile: isMobile,
39+
whichOs: whichOs
3140
}
3241

3342
module.exports = sniffFns;

src/which_browser.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var isChrome = require('./is_browser_chrome');
2+
var isChromeMobile = require('./is_browser_chrome_mobile');
3+
var isFF = require('./is_browser_firefox');
4+
var isOpera = require('./is_browser_opera');
5+
var isSafari = require('./is_browser_safari');
6+
7+
var whichBrowser = function (str) {
8+
if (isOpera(str)) {
9+
return 'opera'
10+
} else if (isChromeMobile(str)) {
11+
return 'chrome mobile'
12+
} else if (isChrome(str)) {
13+
return 'chrome'
14+
} else if (isFF(str)) {
15+
return 'firefox'
16+
} else if (isSafari(str)) {
17+
return 'safari'
18+
} else {
19+
return 'unknown browser'
20+
}
21+
};
22+
23+
module.exports = whichBrowser;

src/which_os.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var isAndroid = require('./is_android');
2+
var isAndroidMobile = require('./is_android_mobile');
3+
var isBB = require('./is_blackberry');
4+
var isIphone = require('./is_iphone');
5+
var isMac = require('./is_mac');
6+
var isWindows = require('./is_windows');
7+
var isWP = require('./is_windows_phone');
8+
9+
var whichOs = function (str) {
10+
if (isAndroid(str)) {
11+
return 'android'
12+
} else if (isBB(str)) {
13+
return 'blackberry'
14+
} else if (isIphone(str)) {
15+
return 'iphone'
16+
} else if (isWP(str)) {
17+
return 'windows mobile'
18+
} else if (isMac(str)) {
19+
return 'mac'
20+
} else if (isWindows(str)) {
21+
return 'windows'
22+
} else {
23+
return 'unknown os'
24+
}
25+
};
26+
27+
module.exports = whichOs;

0 commit comments

Comments
 (0)