-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutilities.ts
43 lines (30 loc) Β· 1.31 KB
/
utilities.ts
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
import { DistanceCalculator } from "https://deno.land/x/distancecalculator@1.0.0/distance-calculator.ts";
export class Utility {
public static getClosestEntry(items: any[], lat: number, lon: number): any {
let itemsWithDistance = Utility.enrichDistance(items, lat, lon)
let sortedItemsWithDistance = Utility.sortByDistance(itemsWithDistance)
const theClosest = sortedItemsWithDistance[0]
theClosest.tempDistance = undefined // ensuring the original structure
return theClosest
}
private static sortByDistance(items: any[]): any[] {
return items.sort((c1, c2) => {
if (c1.tempDistance > c2.tempDistance) {
return 1
}
if (c1.tempDistance < c2.tempDistance) {
return -1
}
return 0
})
}
private static enrichDistance(items: any[], lat: number, lon: number): any[] {
for (const item of items) {
if (item.lat === undefined || item.lon === undefined || item.tempDistance !== undefined){
throw new Error('Each entry should have lat and lon and no tempDistance.')
}
item.tempDistance = DistanceCalculator.getDistanceInKilometers(item.lat, item.lon, lat, lon)
}
return items
}
}