GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 67de7b...c2f046 )
by Florian
01:44
created

js/coordinates.js   B

Complexity

Total Complexity 48
Complexity/F 3

Size

Lines of Code 376
Function Count 16

Duplication

Duplicated Lines 63
Ratio 16.76 %

Importance

Changes 5
Bugs 2 Features 0
Metric Value
cc 0
nc 38400
dl 63
loc 376
rs 8.4864
c 5
b 2
f 0
wmc 48
mnd 2
bc 38
fnc 16
bpm 2.375
cpm 3
noi 0

16 Functions

Rating   Name   Duplication   Size   Complexity  
A Coordinates.setFormat 0 7 4
A Coordinates.validLat 0 6 1
A Coordinates.zeropad 0 9 2
A Coordinates.validLng 0 6 1
A Coordinates.projection_geodesic 0 6 1
A Coordinates.dist_angle_geodesic 0 11 2
A Coordinates.valid 0 5 1
B Coordinates.fromStringHDMS 32 32 4
B Coordinates.fromStringHDM 31 31 4
B Coordinates.toStringDM 0 62 5
B Coordinates.fromString 0 27 5
A Coordinates.toString 0 17 4
A Coordinates.toStringDMS 0 55 3
B Coordinates.fromStringHD 0 24 4
B Coordinates.fromStringD 0 24 4
A Coordinates.toStringD 0 19 3

How to fix   Duplicated Code    Complexity   

Duplicated Code

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:

Complexity

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

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
2
  regexp: true
3
  indent: 4
4
*/
5
6
/*global
7
  GeographicLib, google
8
*/
9
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
        -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
    var coords;
51
52
    coords = this.fromStringHDM(coordsString);
53
    if (coords) {
54
        return coords;
55
    }
56
57
    coords = this.fromStringHDMS(coordsString);
58
    if (coords) {
59
        return coords;
60
    }
61
62
    coords = this.fromStringHD(coordsString);
63
    if (coords) {
64
        return coords;
65
    }
66
67
    coords = this.fromStringD(coordsString);
68
    if (coords) {
69
        return coords;
70
    }
71
72
    return null;
73
};
74
75
76 View Code Duplication
Coordinates.fromStringHDM = function (coordsString) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
77
    'use strict';
78
79
    coordsString = coordsString.upperCase().trim();
80
81
    var matches, pattern,
82
        lat, lat_sign, lat_d, lat_m,
83
        lng, lng_sign, lng_d, lng_m;
84
85
    // H DDD MM.MMM
86
    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;
87
    //
88
    matches = coordsString.match(pattern);
89
    if (matches) {
90
        lat_sign = (matches[1] === 'S') ? -1 : 1;
91
        lng_sign = (matches[4] === 'W') ? -1 : 1;
92
93
        lat_d = parseFloat(matches[2]);
94
        lat_m = parseFloat(matches[3]);
95
96
        lng_d = parseFloat(matches[5]);
97
        lng_m = parseFloat(matches[6]);
98
99
        lat = lat_sign * (lat_d + (lat_m / 60.0));
100
        lng = lng_sign * (lng_d + (lng_m / 60.0));
101
102
        return new google.maps.LatLng(lat, lng);
103
    }
104
105
    return null;
106
};
107
108
109 View Code Duplication
Coordinates.fromStringHDMS = function (coordsString) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
110
    'use strict';
111
112
    coordsString = coordsString.upperCase().trim();
113
114
    var matches, pattern,
115
        lat, lat_sign, lat_d, lat_m, lat_s,
116
        lng, lng_sign, lng_d, lng_m, lng_s;
117
118
    // H DDD MM SS.SSS
119
    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;
120
    //
121
    matches = coordsString.match(pattern);
122
    if (matches) {
123
        lat_sign = (matches[1] === 'S') ? -1 : 1;
124
        lng_sign = (matches[5] === 'W') ? -1 : 1;
125
126
        lat_d = parseFloat(matches[2]);
127
        lat_m = parseFloat(matches[3]);
128
        lat_s = parseFloat(matches[4]);
129
130
        lng_d = parseFloat(matches[6]);
131
        lng_m = parseFloat(matches[7]);
132
        lng_s = parseFloat(matches[8]);
133
134
        lat = lat_sign * (lat_d + (lat_m / 60.0) + (lat_s / 3600.0));
135
        lng = lng_sign * (lng_d + (lng_m / 60.0) + (lng_s / 3600.0));
136
137
        return new google.maps.LatLng(lat, lng);
138
    }
139
    return null;
140
};
141
142
143
Coordinates.fromStringHD = function (coordsString) {
144
    'use strict';
145
146
    coordsString = coordsString.upperCase().trim();
147
148
    var matches, pattern,
149
        lat, lat_sign,
150
        lng, lng_sign;
151
152
    // H DDD.DDDDD
153
    pattern = /^[^A-Z0-9.\-]*([NS])[^A-Z0-9.\-]*([\d\.]+)[^A-Z0-9.\-]+([EW])[^A-Z0-9.\-]*([\d\.]+)[^A-Z0-9.\-]*$/i;
154
    matches = coordsString.match(pattern);
155
    if (matches) {
156
        lat_sign = (matches[1] === 'S') ? -1 : 1;
157
        lng_sign = (matches[3] === 'W') ? -1 : 1;
158
159
        lat = lat_sign * parseFloat(matches[2]);
160
        lng = lng_sign * parseFloat(matches[4]);
161
162
        return new google.maps.LatLng(lat, lng);
163
    }
164
165
    return null;
166
};
167
168
169
Coordinates.fromStringD = function (coordsString) {
170
    'use strict';
171
172
    coordsString = coordsString.upperCase().trim();
173
174
    var matches, pattern,
175
        lat, lat_sign,
176
        lng, lng_sign;
177
178
    // DDD.DDDDD
179
    pattern = /^[^A-Z0-9.\-]*(-?)([\d\.]+)[^A-Z0-9.\-]+(-?)([\d\.]+)[^A-Z0-9.\-]*$/i;
180
    matches = coordsString.match(pattern);
181
    if (matches) {
182
        lat_sign = (matches[1] === '-') ? -1 : 1;
183
        lng_sign = (matches[3] === '-') ? -1 : 1;
184
185
        lat = lat_sign * parseFloat(matches[2]);
186
        lng = lng_sign * parseFloat(matches[4]);
187
188
        return new google.maps.LatLng(lat, lng);
189
    }
190
191
    return null;
192
};
193
194
195
Coordinates.toStringDM = function (coords) {
196
    'use strict';
197
198
    var lat = coords.lat(),
199
        lat_h = "N",
200
        lat_deg,
201
        lat_min,
202
        lat_mmin,
203
        lat_rest,
204
        lng = coords.lng(),
205
        lng_h = "E",
206
        lng_deg,
207
        lng_min,
208
        lng_mmin,
209
        lng_rest,
210
        s;
211
212
    if (lat < 0) {
213
        lat_h = "S";
214
        lat = -lat;
215
    }
216
    lat_deg = Math.floor(lat);
217
    lat_rest = lat - lat_deg;
218
    lat_min = Math.floor(lat_rest * 60);
219
    lat_rest = lat_rest * 60 - lat_min;
220
    lat_mmin = Math.floor(Math.round(lat_rest * 1000));
221
    while (lat_mmin >= 1000) {
222
        lat_mmin -= 1000;
223
        lat_min += 1;
224
    }
225
226
    if (lng < 0) {
227
        lng_h = "W";
228
        lng = -lng;
229
    }
230
    lng_deg = Math.floor(lng);
231
    lng_rest = lng - lng_deg;
232
    lng_min = Math.floor(lng_rest * 60);
233
    lng_rest = lng_rest * 60 - lng_min;
234
    lng_mmin = Math.floor(Math.round(lng_rest * 1000));
235
    while (lng_mmin >= 1000) {
236
        lng_mmin -= 1000;
237
        lng_min += 1;
238
    }
239
240
    s = lat_h +
241
        " " +
242
        this.zeropad(lat_deg, 2) +
243
        " " +
244
        this.zeropad(lat_min, 2) +
245
        "." +
246
        this.zeropad(lat_mmin, 3) +
247
        " " +
248
        lng_h +
249
        " " +
250
        this.zeropad(lng_deg, 3) +
251
        " " +
252
        this.zeropad(lng_min, 2) +
253
        "." +
254
        this.zeropad(lng_mmin, 3);
255
    return s;
256
};
257
258
259
Coordinates.toStringDMS = function (coords) {
260
    'use strict';
261
262
    var lat = coords.lat(),
263
        lat_h = "N",
264
        lat_deg,
265
        lat_min,
266
        lat_sec,
267
        lat_rest,
268
        lng = coords.lng(),
269
        lng_h = "E",
270
        lng_deg,
271
        lng_min,
272
        lng_sec,
273
        lng_rest,
274
        s;
275
276
    if (lat < 0) {
277
        lat_h = "S";
278
        lat = -lat;
279
    }
280
    lat_deg = Math.floor(lat);
281
    lat_rest = lat - lat_deg;
282
    lat_min = Math.floor(lat_rest * 60);
283
    lat_rest = lat_rest * 60 - lat_min;
284
    lat_sec = lat_rest * 60.0;
285
286
    if (lng < 0) {
287
        lng_h = "W";
288
        lng = -lng;
289
    }
290
    lng_deg = Math.floor(lng);
291
    lng_rest = lng - lng_deg;
292
    lng_min = Math.floor(lng_rest * 60);
293
    lng_rest = lng_rest * 60 - lng_min;
294
    lng_sec = lng_rest * 60.0;
295
296
    s = lat_h +
297
        " " +
298
        this.zeropad(lat_deg, 2) +
299
        " " +
300
        this.zeropad(lat_min, 2) +
301
        " " +
302
        this.zeropad(lat_sec.toFixed(2), 5) +
303
        " " +
304
        lng_h +
305
        " " +
306
        this.zeropad(lng_deg, 3) +
307
        " " +
308
        this.zeropad(lng_min, 2) +
309
        " " +
310
        this.zeropad(lng_sec.toFixed(2), 5);
311
312
    return s;
313
};
314
315
316
Coordinates.toStringD = function (coords) {
317
    'use strict';
318
319
    var lat = coords.lat(),
320
        lat_h = "N",
321
        lng = coords.lng(),
322
        lng_h = "E";
323
324
    if (lat < 0) {
325
        lat_h = "S";
326
        lat = -lat;
327
    }
328
    if (lng < 0) {
329
        lng_h = "W";
330
        lng = -lng;
331
    }
332
333
    return lat_h + " " + lat.toFixed(6) + " " + lng_h + " " + lng.toFixed(6);
334
};
335
336
337
Coordinates.toString = function (coords) {
338
    'use strict';
339
340
    if (this.m_format === "DM") {
341
        return this.toStringDM(coords);
342
    }
343
344
    if (this.m_format === "DMS") {
345
        return this.toStringDMS(coords);
346
    }
347
348
    if (this.m_format === "D") {
349
        return this.toStringD(coords);
350
    }
351
352
    return this.toStringDM(coords);
353
};
354
355
356
Coordinates.dist_angle_geodesic = function (startpos, endpos) {
357
    'use strict';
358
359
    var t = this.m_geod.Inverse(startpos.lat(), startpos.lng(), endpos.lat(), endpos.lng()),
360
        a = t.azi1;
361
    if (a < 0) {
362
        a += 360.0;
363
    }
364
365
    return { dist: t.s12, angle: a };
366
};
367
368
369
Coordinates.projection_geodesic = function (startpos, angle, distance) {
370
    'use strict';
371
372
    var t = this.m_geod.Direct(startpos.lat(), startpos.lng(), angle, distance);
373
    return new google.maps.LatLng(t.lat2, t.lon2);
374
};
375
376
377
Coordinates.zeropad = function (num, width) {
378
    'use strict';
379
380
    var s = String(num);
381
    while (s.length < width) {
382
        s = "0" + s;
383
    }
384
    return s;
385
};
386