| Conditions | 31 |
| Total Lines | 205 |
| Code Lines | 164 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
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 | /** |
||
| 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 | }; |
||
| 246 |