og-tools 0.0.8 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/index.js +11 -5
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -97,8 +97,14 @@ function DaysBetween(ts1, ts2) {
|
|
|
97
97
|
return Math.floor(diferencia / (60 * 60 * 24));
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
-
function Distance(
|
|
100
|
+
function Distance(ts1, ts2, unit = 'km') {
|
|
101
101
|
try {
|
|
102
|
+
if (ts1 == null || ts2 == null) return 0;
|
|
103
|
+
|
|
104
|
+
const n1 = Number(ts1);
|
|
105
|
+
const n2 = Number(ts2);
|
|
106
|
+
|
|
107
|
+
if (isNaN(n1) || isNaN(n2)) return 0;
|
|
102
108
|
const RADIO_TIERRA = { km: 6371, mi: 3958.8, m: 6371000 };
|
|
103
109
|
|
|
104
110
|
if (!RADIO_TIERRA[unit]) {
|
|
@@ -107,13 +113,13 @@ function Distance(location1, location2, unit = 'km') {
|
|
|
107
113
|
|
|
108
114
|
const toRad = (deg) => (deg * Math.PI) / 180;
|
|
109
115
|
|
|
110
|
-
const dLat = toRad(
|
|
111
|
-
const dLon = toRad(
|
|
116
|
+
const dLat = toRad(ts2.lat - ts1.lat);
|
|
117
|
+
const dLon = toRad(ts2.lon - ts1.lon);
|
|
112
118
|
|
|
113
119
|
const a =
|
|
114
120
|
Math.sin(dLat / 2) ** 2 +
|
|
115
|
-
Math.cos(toRad(
|
|
116
|
-
Math.cos(toRad(
|
|
121
|
+
Math.cos(toRad(ts1.lat)) *
|
|
122
|
+
Math.cos(toRad(ts2.lat)) *
|
|
117
123
|
Math.sin(dLon / 2) ** 2;
|
|
118
124
|
|
|
119
125
|
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|