-
Notifications
You must be signed in to change notification settings - Fork 0
/
cityservice.ts
55 lines (37 loc) · 2.45 KB
/
cityservice.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
44
45
46
47
48
49
50
51
52
53
54
55
// import { cities } from './cities.ts'
import { DistanceCalculator } from "https://deno.land/x/distancecalculator/distance-calculator.ts"
import { cities } from 'https://deno.land/x/cities/cities.ts'
import { ICityInfo } from "https://deno.land/x/cities/interfaces.ts"
export class CityService {
private static minimumPopulation = 40000
public static getCityInfo(countryCode: string, cityName: string): ICityInfo {
return cities.filter((city) => city.country === countryCode && city.name.toLowerCase() === cityName.toLowerCase())[0]
}
public static getCitiesByCountryCode(countryCode: string, minimumPopulation: number = CityService.minimumPopulation, maximumPopulation: number = 10000000000): ICityInfo[] {
if (minimumPopulation < CityService.minimumPopulation) {
throw new Error(`This service includes only cities with at least ${CityService.minimumPopulation} inhabitants`)
}
return cities.filter((city) => city.country.toLowerCase() === countryCode.toLowerCase() && city.population >= minimumPopulation && city.population <= maximumPopulation)
}
public static getCitiesByPopulation(minimumPopulation: number, maximumPopulation: number = 10000000000): ICityInfo[] {
if (minimumPopulation < CityService.minimumPopulation) {
throw new Error(`This service includes only cities with at least ${CityService.minimumPopulation} inhabitants`)
}
return cities.filter((city) => city.population >= minimumPopulation && city.population <= maximumPopulation)
}
public static getClosestCity(latitude: number, longitude: number, minimumPopulation: number = CityService.minimumPopulation, maximumPopulation: number = 10000000000): ICityInfo {
if (minimumPopulation < CityService.minimumPopulation) {
throw new Error(`This service includes only cities with at least ${CityService.minimumPopulation} inhabitants`)
}
let cityWithLowestDistance
let lowestDistance = 10000000000
for (const city of cities.filter((entry: any) => entry.population >= minimumPopulation && entry.population <= maximumPopulation)){
const distance = DistanceCalculator.getDistanceInKilometers(latitude, longitude, city.lat, city.lon)
if (distance < lowestDistance) {
lowestDistance = distance
cityWithLowestDistance = city
}
}
return cityWithLowestDistance as ICityInfo
}
}