Total Complexity | 50 |
Complexity/F | 2.94 |
Lines of Code | 377 |
Function Count | 17 |
Duplicated Lines | 59 |
Ratio | 15.65 % |
Changes | 4 | ||
Bugs | 2 | Features | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like js/coordinates.js 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 | /*jslint |
||
10 | var Coordinates = {}; |
||
11 | Coordinates.m_format = "DM"; |
||
12 | Coordinates.m_geod = GeographicLib.Geodesic.WGS84; |
||
13 | |||
14 | |||
15 | Coordinates.setFormat = function (format) { |
||
16 | 'use strict'; |
||
17 | |||
18 | if (format === "DM" || format === "DMS" || format === "D") { |
||
19 | this.m_format = format; |
||
20 | } |
||
21 | }; |
||
22 | |||
23 | |||
24 | Coordinates.validLat = function (lat) { |
||
25 | 'use strict'; |
||
26 | |||
27 | return lat !== null && lat !== undefined && !isNaN(lat) && -90.0 <= lat && lat <= 90.0; |
||
28 | }; |
||
29 | |||
30 | |||
31 | Coordinates.validLng = function (lng) { |
||
32 | 'use strict'; |
||
33 | |||
34 | return lng !== null && lng !== undefined && !isNaN(lng) && -180.0 <= lng && lng <= 180.0; |
||
35 | }; |
||
36 | |||
37 | |||
38 | Coordinates.valid = function (lat, lng) { |
||
39 | 'use strict'; |
||
40 | |||
41 | return this.validLat(lat) && this.validLng(lng); |
||
42 | }; |
||
43 | |||
44 | |||
45 | Coordinates.toLatLng = function (lat, lng) { |
||
46 | 'use strict'; |
||
47 | |||
48 | if (this.valid(lat, lng)) { |
||
49 | return new google.maps.LatLng(lat, lng); |
||
50 | } |
||
51 | |||
52 | return null; |
||
53 | }; |
||
54 | |||
55 | |||
56 | Coordinates.fromString = function (coordsString) { |
||
57 | 'use strict'; |
||
58 | |||
59 | var coords; |
||
60 | |||
61 | coords = this.fromStringHDM(coordsString); |
||
62 | if (coords) { |
||
63 | return coords; |
||
64 | } |
||
65 | |||
66 | coords = this.fromStringHDMS(coordsString); |
||
67 | if (coords) { |
||
68 | return coords; |
||
69 | } |
||
70 | |||
71 | coords = this.fromStringHD(coordsString); |
||
72 | if (coords) { |
||
73 | return coords; |
||
74 | View Code Duplication | } |
|
|
|||
75 | |||
76 | coords = this.fromStringD(coordsString); |
||
77 | if (coords) { |
||
78 | return coords; |
||
79 | } |
||
80 | |||
81 | return null; |
||
82 | }; |
||
83 | |||
84 | |||
85 | Coordinates.fromStringHDM = function (coordsString) { |
||
86 | 'use strict'; |
||
87 | |||
88 | var matches, pattern, |
||
89 | lat, lat_sign, lat_d, lat_m, |
||
90 | lng, lng_sign, lng_d, lng_m; |
||
91 | |||
92 | // H DDD MM.MMM |
||
93 | pattern = /^[^A-Z0-9.\-]*([NS])[^A-Z0-9.\-]*(\d+)[^A-Z0-9.\-]+([\d\.]+)[^A-Z0-9.\-]+([EW])[^A-Z0-9.\-]*(\d+)[^A-Z0-9.\-]+([\d\.]+)[^A-Z0-9.\-]*$/i; |
||
94 | // |
||
95 | matches = coordsString.toUpperCase().trim().match(pattern); |
||
96 | if (matches) { |
||
97 | lat_sign = (matches[1] === 'S') ? -1 : 1; |
||
98 | lng_sign = (matches[4] === 'W') ? -1 : 1; |
||
99 | |||
100 | lat_d = parseFloat(matches[2]); |
||
101 | lat_m = parseFloat(matches[3]); |
||
102 | |||
103 | lng_d = parseFloat(matches[5]); |
||
104 | lng_m = parseFloat(matches[6]); |
||
105 | View Code Duplication | ||
106 | lat = lat_sign * (lat_d + (lat_m / 60.0)); |
||
107 | lng = lng_sign * (lng_d + (lng_m / 60.0)); |
||
108 | |||
109 | return new google.maps.LatLng(lat, lng); |
||
110 | } |
||
111 | |||
112 | return null; |
||
113 | }; |
||
114 | |||
115 | |||
116 | Coordinates.fromStringHDMS = function (coordsString) { |
||
117 | 'use strict'; |
||
118 | |||
119 | var matches, pattern, |
||
120 | lat, lat_sign, lat_d, lat_m, lat_s, |
||
121 | lng, lng_sign, lng_d, lng_m, lng_s; |
||
122 | |||
123 | // H DDD MM SS.SSS |
||
124 | pattern = /^[^A-Z0-9.\-]*([NS])[^A-Z0-9.\-]*(\d+)[^A-Z0-9.\-]+(\d+)[^A-Z0-9.\-]+([\d\.]+)[^A-Z0-9.\-]+([EW])[^A-Z0-9.\-]*(\d+)[^A-Z0-9.\-]+(\d+)[^A-Z0-9.\-]+([\d\.]+)[^A-Z0-9.\-]*$/i; |
||
125 | // |
||
126 | matches = coordsString.toUpperCase().trim().match(pattern); |
||
127 | if (matches) { |
||
128 | lat_sign = (matches[1] === 'S') ? -1 : 1; |
||
129 | lng_sign = (matches[5] === 'W') ? -1 : 1; |
||
130 | |||
131 | lat_d = parseFloat(matches[2]); |
||
132 | lat_m = parseFloat(matches[3]); |
||
133 | lat_s = parseFloat(matches[4]); |
||
134 | |||
135 | lng_d = parseFloat(matches[6]); |
||
136 | lng_m = parseFloat(matches[7]); |
||
137 | lng_s = parseFloat(matches[8]); |
||
138 | |||
139 | lat = lat_sign * (lat_d + (lat_m / 60.0) + (lat_s / 3600.0)); |
||
140 | lng = lng_sign * (lng_d + (lng_m / 60.0) + (lng_s / 3600.0)); |
||
141 | |||
142 | return new google.maps.LatLng(lat, lng); |
||
143 | } |
||
144 | return null; |
||
145 | }; |
||
146 | |||
147 | |||
148 | Coordinates.fromStringHD = function (coordsString) { |
||
149 | 'use strict'; |
||
150 | |||
151 | var matches, pattern, |
||
152 | lat, lat_sign, |
||
153 | lng, lng_sign; |
||
154 | |||
155 | // H DDD.DDDDD |
||
156 | pattern = /^[^A-Z0-9.\-]*([NS])[^A-Z0-9.\-]*([\d\.]+)[^A-Z0-9.\-]+([EW])[^A-Z0-9.\-]*([\d\.]+)[^A-Z0-9.\-]*$/i; |
||
157 | matches = coordsString.toUpperCase().trim().match(pattern); |
||
158 | if (matches) { |
||
159 | lat_sign = (matches[1] === 'S') ? -1 : 1; |
||
160 | lng_sign = (matches[3] === 'W') ? -1 : 1; |
||
161 | |||
162 | lat = lat_sign * parseFloat(matches[2]); |
||
163 | lng = lng_sign * parseFloat(matches[4]); |
||
164 | |||
165 | return new google.maps.LatLng(lat, lng); |
||
166 | } |
||
167 | |||
168 | return null; |
||
169 | }; |
||
170 | |||
171 | |||
172 | Coordinates.fromStringD = function (coordsString) { |
||
173 | 'use strict'; |
||
174 | |||
175 | var matches, pattern, |
||
176 | lat, lat_sign, |
||
177 | lng, lng_sign; |
||
178 | |||
179 | // DDD.DDDDD |
||
180 | pattern = /^[^A-Z0-9.\-]*(-?)([\d\.]+)[^A-Z0-9.\-]+(-?)([\d\.]+)[^A-Z0-9.\-]*$/i; |
||
181 | matches = coordsString.toUpperCase().trim().match(pattern); |
||
182 | if (matches) { |
||
183 | lat_sign = (matches[1] === '-') ? -1 : 1; |
||
184 | lng_sign = (matches[3] === '-') ? -1 : 1; |
||
185 | |||
186 | lat = lat_sign * parseFloat(matches[2]); |
||
187 | lng = lng_sign * parseFloat(matches[4]); |
||
188 | |||
189 | return new google.maps.LatLng(lat, lng); |
||
190 | } |
||
191 | |||
192 | return null; |
||
193 | }; |
||
194 | |||
195 | |||
196 | Coordinates.toStringDM = function (coords) { |
||
197 | 'use strict'; |
||
198 | |||
199 | var lat = coords.lat(), |
||
200 | lat_h = "N", |
||
201 | lat_deg, |
||
202 | lat_min, |
||
203 | lat_mmin, |
||
204 | lat_rest, |
||
205 | lng = coords.lng(), |
||
206 | lng_h = "E", |
||
207 | lng_deg, |
||
208 | lng_min, |
||
209 | lng_mmin, |
||
210 | lng_rest, |
||
211 | s; |
||
212 | |||
213 | if (lat < 0) { |
||
214 | lat_h = "S"; |
||
215 | lat = -lat; |
||
216 | } |
||
217 | lat_deg = Math.floor(lat); |
||
218 | lat_rest = lat - lat_deg; |
||
219 | lat_min = Math.floor(lat_rest * 60); |
||
220 | lat_rest = lat_rest * 60 - lat_min; |
||
221 | lat_mmin = Math.floor(Math.round(lat_rest * 1000)); |
||
222 | while (lat_mmin >= 1000) { |
||
223 | lat_mmin -= 1000; |
||
224 | lat_min += 1; |
||
225 | } |
||
226 | |||
227 | if (lng < 0) { |
||
228 | lng_h = "W"; |
||
229 | lng = -lng; |
||
230 | } |
||
231 | lng_deg = Math.floor(lng); |
||
232 | lng_rest = lng - lng_deg; |
||
233 | lng_min = Math.floor(lng_rest * 60); |
||
234 | lng_rest = lng_rest * 60 - lng_min; |
||
235 | lng_mmin = Math.floor(Math.round(lng_rest * 1000)); |
||
236 | while (lng_mmin >= 1000) { |
||
237 | lng_mmin -= 1000; |
||
238 | lng_min += 1; |
||
239 | } |
||
240 | |||
241 | s = lat_h + |
||
242 | " " + |
||
243 | this.zeropad(lat_deg, 2) + |
||
244 | " " + |
||
245 | this.zeropad(lat_min, 2) + |
||
246 | "." + |
||
247 | this.zeropad(lat_mmin, 3) + |
||
248 | " " + |
||
249 | lng_h + |
||
250 | " " + |
||
251 | this.zeropad(lng_deg, 3) + |
||
252 | " " + |
||
253 | this.zeropad(lng_min, 2) + |
||
254 | "." + |
||
255 | this.zeropad(lng_mmin, 3); |
||
256 | return s; |
||
257 | }; |
||
258 | |||
259 | |||
260 | Coordinates.toStringDMS = function (coords) { |
||
261 | 'use strict'; |
||
262 | |||
263 | var lat = coords.lat(), |
||
264 | lat_h = "N", |
||
265 | lat_deg, |
||
266 | lat_min, |
||
267 | lat_sec, |
||
268 | lat_rest, |
||
269 | lng = coords.lng(), |
||
270 | lng_h = "E", |
||
271 | lng_deg, |
||
272 | lng_min, |
||
273 | lng_sec, |
||
274 | lng_rest, |
||
275 | s; |
||
276 | |||
277 | if (lat < 0) { |
||
278 | lat_h = "S"; |
||
279 | lat = -lat; |
||
280 | } |
||
281 | lat_deg = Math.floor(lat); |
||
282 | lat_rest = lat - lat_deg; |
||
283 | lat_min = Math.floor(lat_rest * 60); |
||
284 | lat_rest = lat_rest * 60 - lat_min; |
||
285 | lat_sec = lat_rest * 60.0; |
||
286 | |||
287 | if (lng < 0) { |
||
288 | lng_h = "W"; |
||
289 | lng = -lng; |
||
290 | } |
||
291 | lng_deg = Math.floor(lng); |
||
292 | lng_rest = lng - lng_deg; |
||
293 | lng_min = Math.floor(lng_rest * 60); |
||
294 | lng_rest = lng_rest * 60 - lng_min; |
||
295 | lng_sec = lng_rest * 60.0; |
||
296 | |||
297 | s = lat_h + |
||
298 | " " + |
||
299 | this.zeropad(lat_deg, 2) + |
||
300 | " " + |
||
301 | this.zeropad(lat_min, 2) + |
||
302 | " " + |
||
303 | this.zeropad(lat_sec.toFixed(2), 5) + |
||
304 | " " + |
||
305 | lng_h + |
||
306 | " " + |
||
307 | this.zeropad(lng_deg, 3) + |
||
308 | " " + |
||
309 | this.zeropad(lng_min, 2) + |
||
310 | " " + |
||
311 | this.zeropad(lng_sec.toFixed(2), 5); |
||
312 | |||
313 | return s; |
||
314 | }; |
||
315 | |||
316 | |||
317 | Coordinates.toStringD = function (coords) { |
||
318 | 'use strict'; |
||
319 | |||
320 | var lat = coords.lat(), |
||
321 | lat_h = "N", |
||
322 | lng = coords.lng(), |
||
323 | lng_h = "E"; |
||
324 | |||
325 | if (lat < 0) { |
||
326 | lat_h = "S"; |
||
327 | lat = -lat; |
||
328 | } |
||
329 | if (lng < 0) { |
||
330 | lng_h = "W"; |
||
331 | lng = -lng; |
||
332 | } |
||
333 | |||
334 | return lat_h + " " + lat.toFixed(6) + " " + lng_h + " " + lng.toFixed(6); |
||
335 | }; |
||
336 | |||
337 | |||
338 | Coordinates.toString = function (coords) { |
||
339 | 'use strict'; |
||
340 | |||
341 | if (this.m_format === "DM") { |
||
342 | return this.toStringDM(coords); |
||
343 | } |
||
344 | |||
345 | if (this.m_format === "DMS") { |
||
346 | return this.toStringDMS(coords); |
||
347 | } |
||
348 | |||
349 | if (this.m_format === "D") { |
||
350 | return this.toStringD(coords); |
||
351 | } |
||
352 | |||
353 | return this.toStringDM(coords); |
||
354 | }; |
||
355 | |||
356 | |||
357 | Coordinates.dist_angle_geodesic = function (startpos, endpos) { |
||
358 | 'use strict'; |
||
359 | |||
360 | var t = this.m_geod.Inverse(startpos.lat(), startpos.lng(), endpos.lat(), endpos.lng()), |
||
361 | a = t.azi1; |
||
362 | if (a < 0) { |
||
363 | a += 360.0; |
||
364 | } |
||
365 | |||
366 | return { dist: t.s12, angle: a }; |
||
367 | }; |
||
368 | |||
369 | |||
370 | Coordinates.projection_geodesic = function (startpos, angle, distance) { |
||
371 | 'use strict'; |
||
372 | |||
373 | var t = this.m_geod.Direct(startpos.lat(), startpos.lng(), angle, distance); |
||
374 | return new google.maps.LatLng(t.lat2, t.lon2); |
||
375 | }; |
||
376 | |||
377 | |||
378 | Coordinates.zeropad = function (num, width) { |
||
379 | 'use strict'; |
||
380 | |||
381 | var s = String(num); |
||
382 | while (s.length < width) { |
||
383 | s = "0" + s; |
||
384 | } |
||
385 | return s; |
||
386 | }; |
||
387 |