-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
55 lines (48 loc) · 1.57 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
var coords = {
fromSexagesimal: function(x) {
var deg = 0,
r = /(\d+)[˚°]\s*(\d+)?[′‘']?\s*(\d+)?[″"]?\s*(S|N|E|W)?/,
m = x.match(r);
if (!m) return null;
if (m[1]) deg += parseFloat(m[1]);
if (m[2]) deg += parseFloat(m[2]) / 60;
if (m[3]) deg += parseFloat(m[3]) / 3600;
if (m[4] && m[4] === 'S' || m[4] === 'E') deg *= -1;
return deg;
},
fromDecimal: function(x) {
if (x.match(/[˚°]/)) return null;
var m = parseFloat(x);
if (!isNaN(m)) return m;
},
toSexagesimal: function(x, format) {
var deg = Math.floor(x);
var min = (x - Math.floor(x)) * 60;
var sec = (min - Math.floor(min)) * 60;
min = Math.floor(min);
sec = Math.floor(sec);
return deg + '° ' + min + '′ ' + sec + '″';
},
parse: function(x) {
return this.fromDecimal(x) || this.fromSexagesimal(x) || 0;
},
parsePair: function(x) {
if (this.fromDecimal(x) !== null) {
var p = x.split(/\s+/);
if (p.length !== 2) throw Error('Two numbers are expected in a pair');
return [
this.fromDecimal(p[0]),
this.fromDecimal(p[1])];
} else {
var a = this.fromSexagesimal(x);
return [a, a];
}
},
generate: function(x, format) {
return this.toSexagesimal(x);
},
generatePair: function(x, format) {
return this.toSexagesimal(x);
}
};
if (typeof module !== 'undefined') module.exports = coords;