Helper-Device.ts ➔ get_device   F
last analyzed

Complexity

Conditions 31

Size

Total Lines 205
Code Lines 164

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 31
eloc 164
dl 0
loc 205
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like Helper-Device.ts ➔ get_device often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
/**
2
 * Detect is mobile
3
 */
4
function isMobile() {
5
  var target = navigator.userAgent || navigator.vendor || window.opera;
6
  if (
7
    /(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(
8
      target
9
    ) ||
10
    /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(
11
      target.substr(0, 4)
12
    )
13
  ) {
14
    if (!Cookies.has("deviceInfo")) {
15
      if (typeof toastr != "undefined") {
16
        toastr.info("is Mobile", "Your Device");
17
      } else {
18
        console.log("isMobile");
19
      }
20
      Cookies.set("deviceInfo", "x", 1, "d", null, null);
21
    }
22
    return true;
23
  } else {
24
    if (!Cookies.has("deviceInfo")) {
25
      if (typeof toastr != "undefined") {
26
        toastr.info("is Desktop", "Your Device");
27
      } else {
28
        console.log("isDesktop");
29
      }
30
      Cookies.set("deviceInfo", "x", 1, "d", null, null);
31
    }
32
    return false;
33
  }
34
}
35
36
function get_device() {
37
  var unknown: string,
38
    width: string | number,
39
    height: string,
40
    screenSize: string,
41
    browser: string,
42
    version: string,
43
    majorVersion: number,
44
    mobile: boolean,
45
    os: string,
46
    osVersion: string | RegExpExecArray | number[],
47
    cookieEnabled: boolean,
48
    flashVersion = "-";
49
  if (!isnode()) {
50
    // screen
51
52
    if (screen.width) {
53
      width = screen.width ? screen.width : "";
54
      height = screen.height ? screen.height.toString() : "";
55
      screenSize += "" + width + " x " + height;
56
    }
57
58
    // browser
59
    var nVer = navigator.appVersion;
60
    var nAgt = navigator.userAgent;
61
    browser = navigator.appName;
62
    version = "" + parseFloat(navigator.appVersion);
63
    majorVersion = parseInt(navigator.appVersion, 10);
64
    var nameOffset: number, verOffset: number, ix: any;
65
66
    // Opera
67
    if ((verOffset = nAgt.indexOf("Opera")) != -1) {
68
      browser = "Opera";
69
      version = nAgt.substring(verOffset + 6);
70
      if ((verOffset = nAgt.indexOf("Version")) != -1) {
71
        version = nAgt.substring(verOffset + 8);
72
      }
73
    }
74
    // Opera Next
75
    if ((verOffset = nAgt.indexOf("OPR")) != -1) {
76
      browser = "Opera";
77
      version = nAgt.substring(verOffset + 4);
78
    }
79
    // Edge
80
    else if ((verOffset = nAgt.indexOf("Edge")) != -1) {
81
      browser = "Microsoft Edge";
82
      version = nAgt.substring(verOffset + 5);
83
    }
84
    // MSIE
85
    else if ((verOffset = nAgt.indexOf("MSIE")) != -1) {
86
      browser = "Microsoft Internet Explorer";
87
      version = nAgt.substring(verOffset + 5);
88
    }
89
    // Chrome
90
    else if ((verOffset = nAgt.indexOf("Chrome")) != -1) {
91
      browser = "Chrome";
92
      version = nAgt.substring(verOffset + 7);
93
    }
94
    // Safari
95
    else if ((verOffset = nAgt.indexOf("Safari")) != -1) {
96
      browser = "Safari";
97
      version = nAgt.substring(verOffset + 7);
98
      if ((verOffset = nAgt.indexOf("Version")) != -1) {
99
        version = nAgt.substring(verOffset + 8);
100
      }
101
    }
102
    // Firefox
103
    else if ((verOffset = nAgt.indexOf("Firefox")) != -1) {
104
      browser = "Firefox";
105
      version = nAgt.substring(verOffset + 8);
106
    }
107
    // MSIE 11+
108
    else if (nAgt.indexOf("Trident/") != -1) {
109
      browser = "Microsoft Internet Explorer";
110
      version = nAgt.substring(nAgt.indexOf("rv:") + 3);
111
    }
112
    // Other browsers
113
    else if (
114
      (nameOffset = nAgt.lastIndexOf(" ") + 1) <
115
      (verOffset = nAgt.lastIndexOf("/"))
116
    ) {
117
      browser = nAgt.substring(nameOffset, verOffset);
118
      version = nAgt.substring(verOffset + 1);
119
      if (browser.toLowerCase() == browser.toUpperCase()) {
120
        browser = navigator.appName;
121
      }
122
    }
123
    // trim the version string
124
    if ((ix = version.indexOf(";")) != -1) version = version.substring(0, ix);
125
    if ((ix = version.indexOf(" ")) != -1) version = version.substring(0, ix);
126
    if ((ix = version.indexOf(")")) != -1) version = version.substring(0, ix);
127
128
    majorVersion = parseInt("" + version, 10);
129
    if (isNaN(majorVersion)) {
130
      version = "" + parseFloat(navigator.appVersion);
131
      majorVersion = parseInt(navigator.appVersion, 10);
132
    }
133
134
    // mobile version
135
    mobile = /Mobile|mini|Fennec|Android|iP(ad|od|hone)/.test(nVer);
136
137
    // cookie
138
    cookieEnabled = navigator.cookieEnabled ? true : false;
139
140
    if (typeof navigator.cookieEnabled == "undefined" && !cookieEnabled) {
141
      document.cookie = "testcookie";
142
      cookieEnabled =
143
        document.cookie.indexOf("testcookie") != -1 ? true : false;
144
    }
145
146
    // system
147
    var os = unknown;
148
    var clientStrings = [
149
      { s: "Windows 10", r: /(Windows 10.0|Windows NT 10.0)/ },
150
      { s: "Windows 8.1", r: /(Windows 8.1|Windows NT 6.3)/ },
151
      { s: "Windows 8", r: /(Windows 8|Windows NT 6.2)/ },
152
      { s: "Windows 7", r: /(Windows 7|Windows NT 6.1)/ },
153
      { s: "Windows Vista", r: /Windows NT 6.0/ },
154
      { s: "Windows Server 2003", r: /Windows NT 5.2/ },
155
      { s: "Windows XP", r: /(Windows NT 5.1|Windows XP)/ },
156
      { s: "Windows 2000", r: /(Windows NT 5.0|Windows 2000)/ },
157
      { s: "Windows ME", r: /(Win 9x 4.90|Windows ME)/ },
158
      { s: "Windows 98", r: /(Windows 98|Win98)/ },
159
      { s: "Windows 95", r: /(Windows 95|Win95|Windows_95)/ },
160
      {
161
        s: "Windows NT 4.0",
162
        r: /(Windows NT 4.0|WinNT4.0|WinNT|Windows NT)/,
163
      },
164
      { s: "Windows CE", r: /Windows CE/ },
165
      { s: "Windows 3.11", r: /Win16/ },
166
      { s: "Android", r: /Android/ },
167
      { s: "Open BSD", r: /OpenBSD/ },
168
      { s: "Sun OS", r: /SunOS/ },
169
      { s: "Chrome OS", r: /CrOS/ },
170
      { s: "Linux", r: /(Linux|X11(?!.*CrOS))/ },
171
      { s: "iOS", r: /(iPhone|iPad|iPod)/ },
172
      { s: "Mac OS X", r: /Mac OS X/ },
173
      { s: "Mac OS", r: /(MacPPC|MacIntel|Mac_PowerPC|Macintosh)/ },
174
      { s: "QNX", r: /QNX/ },
175
      { s: "UNIX", r: /UNIX/ },
176
      { s: "BeOS", r: /BeOS/ },
177
      { s: "OS/2", r: /OS\/2/ },
178
      {
179
        s: "Search Bot",
180
        r: /(nuhk|Googlebot|Yammybot|Openbot|Slurp|MSNBot|Ask Jeeves\/Teoma|ia_archiver)/,
181
      },
182
    ];
183
    for (var id in clientStrings) {
184
      var cs = clientStrings[id];
185
      if (cs.r.test(nAgt)) {
186
        os = cs.s;
187
        break;
188
      }
189
    }
190
191
    osVersion = unknown;
192
193
    if (/Windows/.test(os)) {
194
      osVersion = /Windows (.*)/.exec(os)[1];
195
      os = "Windows";
196
    }
197
198
    switch (os) {
199
      case "Mac OS X":
200
        osVersion = /Mac OS X (10[\.\_\d]+)/.exec(nAgt)[1];
201
        break;
202
203
      case "Android":
204
        osVersion = /Android ([\.\_\d]+)/.exec(nAgt)[1];
205
        break;
206
207
      case "iOS":
208
        osVersion = /OS (\d+)_(\d+)_?(\d+)?/.exec(nVer);
209
        var calc = osVersion.exists(3) ? osVersion[3] : osVersion[0];
210
        osVersion = osVersion[1] + "." + osVersion[2] + "." + calc; //(osVersion[3] | 0);
211
        break;
212
    }
213
214
    // flash (you'll need to include swfobject)
215
    /* script src="//ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js" */
216
    var flashVersion = "no check";
217
    if (typeof swfobject != "undefined") {
218
      var fv = swfobject.getFlashPlayerVersion();
219
      if (fv.major > 0) {
220
        flashVersion = fv.major + "." + fv.minor + " r" + fv.release;
221
      } else {
222
        flashVersion = unknown;
223
      }
224
    }
225
  } else {
226
    const terminal = require("os");
227
    os = terminal.platform();
228
    version = terminal.version();
229
  }
230
  return {
231
    screen: screenSize,
232
    browser: browser,
233
    browserVersion: version,
234
    browserMajorVersion: majorVersion,
235
    mobile: mobile,
236
    os: os,
237
    osVersion: osVersion,
238
    cookies: cookieEnabled,
239
    flashVersion: flashVersion,
240
  };
241
}
242
243
if (typeof module != "undefined") {
244
  module.exports.get_device = get_device;
245
}
246