Total Complexity | 46 |
Complexity/F | 3.83 |
Lines of Code | 307 |
Function Count | 12 |
Duplicated Lines | 32 |
Ratio | 10.42 % |
Changes | 5 | ||
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) && |
||
28 | -90.0 <= lat && lat <= 90.0; |
||
29 | }; |
||
30 | |||
31 | |||
32 | Coordinates.validLng = function (lng) { |
||
33 | 'use strict'; |
||
34 | |||
35 | return lng !== null && lng !== undefined && !isNaN(lng) && |
||
36 | View Code Duplication | -180.0 <= lng && lng <= 180.0; |
|
|
|||
37 | }; |
||
38 | |||
39 | |||
40 | Coordinates.valid = function (lat, lng) { |
||
41 | 'use strict'; |
||
42 | |||
43 | return this.validLat(lat) && this.validLng(lng); |
||
44 | }; |
||
45 | |||
46 | |||
47 | Coordinates.fromString = function (coordsString) { |
||
48 | 'use strict'; |
||
49 | |||
50 | coordsString = coordsString.trim(); |
||
51 | |||
52 | var matches, pattern, |
||
53 | lat, lat_sign, lat_d, lat_m, lat_s, |
||
54 | lng, lng_sign, lng_d, lng_m, lng_s; |
||
55 | View Code Duplication | ||
56 | // H DDD MM.MMM |
||
57 | pattern = /^[^A-Za-z0-9.\-]*([ns])[^A-Za-z0-9.\-]*(\d+)[^A-Za-z0-9.\-]+([\d\.]+)[^A-Za-z0-9.\-]+([we])[^A-Za-z0-9.\-]*(\d+)[^A-Za-z0-9.\-]+([\d\.]+)[^A-Za-z0-9.\-]*$/i; |
||
58 | matches = coordsString.match(pattern); |
||
59 | if (matches) { |
||
60 | lat_sign = (matches[1] === 's' || matches[1] === 'S') ? -1 : 1; |
||
61 | lng_sign = (matches[4] === 'w' || matches[4] === 'W') ? -1 : 1; |
||
62 | |||
63 | lat_d = parseFloat(matches[2]); |
||
64 | lat_m = parseFloat(matches[3]); |
||
65 | |||
66 | lng_d = parseFloat(matches[5]); |
||
67 | lng_m = parseFloat(matches[6]); |
||
68 | |||
69 | lat = lat_sign * (lat_d + (lat_m / 60.0)); |
||
70 | lng = lng_sign * (lng_d + (lng_m / 60.0)); |
||
71 | |||
72 | return new google.maps.LatLng(lat, lng); |
||
73 | } |
||
74 | |||
75 | // H DDD MM SS.SSS |
||
76 | pattern = /^[^A-Za-z0-9.\-]*([ns])[^A-Za-z0-9.\-]*(\d+)[^A-Za-z0-9.\-]+(\d+)[^A-Za-z0-9.\-]+([\d\.]+)[^A-Za-z0-9.\-]+([we])[^A-Za-z0-9.\-]*(\d+)[^A-Za-z0-9.\-]+(\d+)[^A-Za-z0-9.\-]+([\d\.]+)[^A-Za-z0-9.\-]*$/i; |
||
77 | matches = coordsString.match(pattern); |
||
78 | if (matches) { |
||
79 | lat_sign = (matches[1] === 's' || matches[1] === 'S') ? -1 : 1; |
||
80 | lng_sign = (matches[5] === 'w' || matches[5] === 'W') ? -1 : 1; |
||
81 | |||
82 | lat_d = parseFloat(matches[2]); |
||
83 | lat_m = parseFloat(matches[3]); |
||
84 | lat_s = parseFloat(matches[4]); |
||
85 | |||
86 | lng_d = parseFloat(matches[6]); |
||
87 | lng_m = parseFloat(matches[7]); |
||
88 | lng_s = parseFloat(matches[8]); |
||
89 | |||
90 | lat = lat_sign * (lat_d + (lat_m / 60.0) + (lat_s / 3600.0)); |
||
91 | lng = lng_sign * (lng_d + (lng_m / 60.0) + (lng_s / 3600.0)); |
||
92 | |||
93 | return new google.maps.LatLng(lat, lng); |
||
94 | } |
||
95 | |||
96 | // H DDD.DDDDD |
||
97 | pattern = /^[^A-Za-z0-9.\-]*([ns])[^A-Za-z0-9.\-]*([\d\.]+)[^A-Za-z0-9.\-]+([we])[^A-Za-z0-9.\-]*([\d\.]+)[^A-Za-z0-9.\-]*$/i; |
||
98 | matches = coordsString.match(pattern); |
||
99 | if (matches) { |
||
100 | lat_sign = (matches[1] === 's' || matches[1] === 'S') ? -1 : 1; |
||
101 | lng_sign = (matches[3] === 'w' || matches[3] === 'W') ? -1 : 1; |
||
102 | |||
103 | lat = lat_sign * parseFloat(matches[2]); |
||
104 | lng = lng_sign * parseFloat(matches[4]); |
||
105 | |||
106 | return new google.maps.LatLng(lat, lng); |
||
107 | } |
||
108 | |||
109 | // H DDD.DDDDD |
||
110 | pattern = /^[^A-Za-z0-9.\-]*(-?)([\d\.]+)[^A-Za-z0-9.\-]+(-?)([\d\.]+)[^A-Za-z0-9.\-]*$/i; |
||
111 | matches = coordsString.match(pattern); |
||
112 | if (matches) { |
||
113 | lat_sign = (matches[1] === '-') ? -1 : 1; |
||
114 | lng_sign = (matches[3] === '-') ? -1 : 1; |
||
115 | |||
116 | lat = lat_sign * parseFloat(matches[2]); |
||
117 | lng = lng_sign * parseFloat(matches[4]); |
||
118 | |||
119 | return new google.maps.LatLng(lat, lng); |
||
120 | } |
||
121 | |||
122 | return null; |
||
123 | }; |
||
124 | |||
125 | |||
126 | Coordinates.toStringDM = function (coords) { |
||
127 | 'use strict'; |
||
128 | |||
129 | var lat = coords.lat(), |
||
130 | lat_h = "N", |
||
131 | lat_deg, |
||
132 | lat_min, |
||
133 | lat_mmin, |
||
134 | lat_rest, |
||
135 | lng = coords.lng(), |
||
136 | lng_h = "E", |
||
137 | lng_deg, |
||
138 | lng_min, |
||
139 | lng_mmin, |
||
140 | lng_rest, |
||
141 | s; |
||
142 | |||
143 | if (lat < 0) { |
||
144 | lat_h = "S"; |
||
145 | lat = -lat; |
||
146 | } |
||
147 | lat_deg = Math.floor(lat); |
||
148 | lat_rest = lat - lat_deg; |
||
149 | lat_min = Math.floor(lat_rest * 60); |
||
150 | lat_rest = lat_rest * 60 - lat_min; |
||
151 | lat_mmin = Math.floor(Math.round(lat_rest * 1000)); |
||
152 | while (lat_mmin >= 1000) { |
||
153 | lat_mmin -= 1000; |
||
154 | lat_min += 1; |
||
155 | } |
||
156 | |||
157 | if (lng < 0) { |
||
158 | lng_h = "W"; |
||
159 | lng = -lng; |
||
160 | } |
||
161 | lng_deg = Math.floor(lng); |
||
162 | lng_rest = lng - lng_deg; |
||
163 | lng_min = Math.floor(lng_rest * 60); |
||
164 | lng_rest = lng_rest * 60 - lng_min; |
||
165 | lng_mmin = Math.floor(Math.round(lng_rest * 1000)); |
||
166 | while (lng_mmin >= 1000) { |
||
167 | lng_mmin -= 1000; |
||
168 | lng_min += 1; |
||
169 | } |
||
170 | |||
171 | s = lat_h + |
||
172 | " " + |
||
173 | this.zeropad(lat_deg, 2) + |
||
174 | " " + |
||
175 | this.zeropad(lat_min, 2) + |
||
176 | "." + |
||
177 | this.zeropad(lat_mmin, 3) + |
||
178 | " " + |
||
179 | lng_h + |
||
180 | " " + |
||
181 | this.zeropad(lng_deg, 3) + |
||
182 | " " + |
||
183 | this.zeropad(lng_min, 2) + |
||
184 | "." + |
||
185 | this.zeropad(lng_mmin, 3); |
||
186 | return s; |
||
187 | }; |
||
188 | |||
189 | |||
190 | Coordinates.toStringDMS = function (coords) { |
||
191 | 'use strict'; |
||
192 | |||
193 | var lat = coords.lat(), |
||
194 | lat_h = "N", |
||
195 | lat_deg, |
||
196 | lat_min, |
||
197 | lat_sec, |
||
198 | lat_rest, |
||
199 | lng = coords.lng(), |
||
200 | lng_h = "E", |
||
201 | lng_deg, |
||
202 | lng_min, |
||
203 | lng_sec, |
||
204 | lng_rest, |
||
205 | s; |
||
206 | |||
207 | if (lat < 0) { |
||
208 | lat_h = "S"; |
||
209 | lat = -lat; |
||
210 | } |
||
211 | lat_deg = Math.floor(lat); |
||
212 | lat_rest = lat - lat_deg; |
||
213 | lat_min = Math.floor(lat_rest * 60); |
||
214 | lat_rest = lat_rest * 60 - lat_min; |
||
215 | lat_sec = lat_rest * 60.0; |
||
216 | |||
217 | if (lng < 0) { |
||
218 | lng_h = "W"; |
||
219 | lng = -lng; |
||
220 | } |
||
221 | lng_deg = Math.floor(lng); |
||
222 | lng_rest = lng - lng_deg; |
||
223 | lng_min = Math.floor(lng_rest * 60); |
||
224 | lng_rest = lng_rest * 60 - lng_min; |
||
225 | lng_sec = lng_rest * 60.0; |
||
226 | |||
227 | s = lat_h + |
||
228 | " " + |
||
229 | this.zeropad(lat_deg, 2) + |
||
230 | " " + |
||
231 | this.zeropad(lat_min, 2) + |
||
232 | " " + |
||
233 | this.zeropad(lat_sec.toFixed(2), 5) + |
||
234 | " " + |
||
235 | lng_h + |
||
236 | " " + |
||
237 | this.zeropad(lng_deg, 3) + |
||
238 | " " + |
||
239 | this.zeropad(lng_min, 2) + |
||
240 | " " + |
||
241 | this.zeropad(lng_sec.toFixed(2), 5); |
||
242 | |||
243 | return s; |
||
244 | }; |
||
245 | |||
246 | |||
247 | Coordinates.toStringD = function (coords) { |
||
248 | 'use strict'; |
||
249 | |||
250 | var lat = coords.lat(), |
||
251 | lat_h = "N", |
||
252 | lng = coords.lng(), |
||
253 | lng_h = "E"; |
||
254 | |||
255 | if (lat < 0) { |
||
256 | lat_h = "S"; |
||
257 | lat = -lat; |
||
258 | } |
||
259 | if (lng < 0) { |
||
260 | lng_h = "W"; |
||
261 | lng = -lng; |
||
262 | } |
||
263 | |||
264 | return lat_h + " " + lat.toFixed(6) + " " + lng_h + " " + lng.toFixed(6); |
||
265 | }; |
||
266 | |||
267 | |||
268 | Coordinates.toString = function (coords) { |
||
269 | 'use strict'; |
||
270 | |||
271 | if (this.m_format === "DM") { |
||
272 | return this.toStringDM(coords); |
||
273 | } |
||
274 | |||
275 | if (this.m_format === "DMS") { |
||
276 | return this.toStringDMS(coords); |
||
277 | } |
||
278 | |||
279 | if (this.m_format === "D") { |
||
280 | return this.toStringD(coords); |
||
281 | } |
||
282 | |||
283 | return this.toStringDM(coords); |
||
284 | }; |
||
285 | |||
286 | |||
287 | Coordinates.dist_angle_geodesic = function (startpos, endpos) { |
||
288 | 'use strict'; |
||
289 | |||
290 | var t = this.m_geod.Inverse(startpos.lat(), startpos.lng(), endpos.lat(), endpos.lng()), |
||
291 | a = t.azi1; |
||
292 | if (a < 0) { |
||
293 | a += 360.0; |
||
294 | } |
||
295 | |||
296 | return { dist: t.s12, angle: a }; |
||
297 | }; |
||
298 | |||
299 | |||
300 | Coordinates.projection_geodesic = function (startpos, angle, distance) { |
||
301 | 'use strict'; |
||
302 | |||
303 | var t = this.m_geod.Direct(startpos.lat(), startpos.lng(), angle, distance); |
||
304 | return new google.maps.LatLng(t.lat2, t.lon2); |
||
305 | }; |
||
306 | |||
307 | |||
308 | Coordinates.zeropad = function (num, width) { |
||
309 | 'use strict'; |
||
310 | |||
311 | var s = String(num); |
||
312 | while (s.length < width) { |
||
313 | s = "0" + s; |
||
314 | } |
||
315 | return s; |
||
316 | }; |
||
317 |