-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (41 loc) · 1.01 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*!
* browserr <https://github.com/dehli/browserr>
*
* C.P. Dehli
*/
// TODO:
// Figure out why build is failing, and use some newer
// browser features (for...of, destructuring, etc.)
import tests from './tests.js';
function browserr() {
// Generate various errors; and use the error
// messages to determine the user's browser.
const scores = {};
for (var i = 0; i < tests.length; ++i) {
const test = tests[i];
const js = test.js;
try {
eval(js);
}
catch (error) {
const message = error.message;
for (var key in test) {
if (key !== 'js' && message === test[key]) {
scores[key] ?
scores[key]++ :
scores[key] = 1;
}
}
}
}
// Return whichever browser had the highest score
var browser = null;
const keys = Object.keys(scores);
for (var i = 0; i < keys.length; ++i) {
if (browser === null || scores[keys[i]] > scores[browser]) {
browser = keys[i];
}
}
return browser;
}
export default browserr;