-
Notifications
You must be signed in to change notification settings - Fork 3
/
DefaultTransformImpl.js
executable file
·82 lines (72 loc) · 2.27 KB
/
DefaultTransformImpl.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"use strict";
var MobileDetect = require('mobile-detect'),
versionInfo;
versionInfo = {
'iPad': Math.floor,
'iPhone': Math.floor,
'iPod': Math.floor,
'iOS': Math.floor,
'Chrome': Math.floor,
'Firefox': Math.floor,
'IE': Math.floor,
'Webkit': Math.floor,
'Android': round1,
'Windows Phone OS': round1,
'Windows NT': round1,
'Symbian': Math.floor,
'Kindle': Math.floor,
'Blackberry': Math.floor
};
module.exports = function defaultTransformImpl(hit) {
var md = new MobileDetect(hit.userAgent),
versions = {},
formfactor = null,
desktopBrowser = null;
Object.keys(versionInfo).forEach(function (key) {
var version = md.version(key);
if (version !== null && !isNaN(version)) {
version = versionInfo[key](Number(version));
if (version > 0) {
versions[key] = version;
}
}
});
if (md.phone()) {
formfactor = md.tablet() ? 'Phone/Tablet' : 'Phone';
} else if (md.tablet()) {
formfactor = 'Tablet';
} else if (md.is('Bot') || md.is('MobileBot')) {
formfactor = 'Bot';
} else if (md.is('TV') || md.is('Console')) {
formfactor = 'TV/Console';
} else {
formfactor = null;
desktopBrowser = checkVersion(md, 'Firefox') || checkVersion(md, 'IE') ||
checkVersion(md, 'Chrome') || checkVersion(md, 'Opera') || checkVersion(md, 'Safari') ||
'(unknown)';
}
// if (desktopBrowser==='(unknown)') console.log(hit.userAgent);
// console.log(md.mobileGrade() + ' ' + hit.userAgent);
return {
os: md.os(),
phone: md.phone(),
tablet: md.tablet(),
mobileGrade: md.mobile() && md.mobileGrade(),
mobileGradeAll: md.mobileGrade(),
userAgent: md.userAgent(),
formfactor: formfactor,
desktopBrowser: desktopBrowser,
versions: versions
};
};
function checkVersion(md, versionKey) {
var version = md.version(versionKey);
return (version !== null && !isNaN(version)) ? versionKey : false;
}
function round1(version) {
var versionStr = '' + Math.floor(Number(version) * 10) / 10;
if (versionStr.indexOf('.') < 0) {
versionStr += '.0';
}
return versionStr;
}