parse.ts (2081B)
1 import type { 2 GeocoderReverseResult, 3 GeolocationAddress, 4 GeolocationPoint, 5 GeolocationPointTuple, 6 GeometryPoint, 7 LocationBasis, 8 } from "./types.js"; 9 10 export const parse_geop_point = (point: GeolocationPoint): GeolocationPoint => { 11 const { lat, lng } = point; 12 return { lat, lng }; 13 }; 14 15 export const parse_geol_coords = (number: number): number => { 16 return Math.round(number * 1e7) / 1e7; 17 }; 18 19 export const parse_geolocation_address = ( 20 addr?: GeolocationAddress 21 ): GeolocationAddress | undefined => { 22 if (!addr) return undefined; 23 const { primary, admin, country } = addr; 24 return { primary, admin, country }; 25 }; 26 27 export const parse_geolocation_point = ( 28 point?: GeometryPoint 29 ): GeolocationPoint | undefined => { 30 if (!point) return undefined; 31 return { 32 lat: point.coordinates[1], 33 lng: point.coordinates[0], 34 }; 35 }; 36 37 export const geo_point_to_geometry = ( 38 point?: GeolocationPoint 39 ): GeometryPoint | undefined => { 40 if (!point) return undefined; 41 return { 42 type: "Point", 43 coordinates: [point.lng, point.lat], 44 }; 45 }; 46 47 export const location_basis_to_geo_point = ( 48 basis?: LocationBasis 49 ): GeolocationPoint | undefined => { 50 if (!basis) return undefined; 51 return { 52 lat: basis.point.lat, 53 lng: basis.point.lng, 54 }; 55 }; 56 57 export const parse_geocode_address = ( 58 geoc?: GeocoderReverseResult 59 ): GeolocationAddress | undefined => { 60 if (!geoc) return undefined; 61 const { name: primary, admin1_name: admin, country_id: country } = geoc; 62 return { primary, admin, country }; 63 }; 64 65 export const parse_geom_point_tup = ( 66 point: GeometryPoint 67 ): GeolocationPointTuple => { 68 return [point.coordinates[0], point.coordinates[1]]; 69 }; 70 71 export const parse_geol_point_tup = ( 72 point: GeolocationPoint 73 ): GeolocationPointTuple => { 74 return [point.lng, point.lat]; 75 }; 76 77 export const parse_tup_geop_point = ( 78 map_center: GeolocationPointTuple 79 ): GeolocationPoint => { 80 return { 81 lat: map_center[1], 82 lng: map_center[0], 83 }; 84 };