All files / app/utils nominatim.ts

100% Statements 15/15
83.33% Branches 5/6
100% Functions 3/3
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 56 57 58 59 60                                                    51x 27x                   27x   27x 189x 57x     27x     51x 12x 16x 16x   16x         12x    
import { Coordinates } from "features/search/constants";
 
export interface NominatimPlace {
  address: {
    [city: string]: string;
    state_district: string;
    state: string;
    postcode: string;
    country: string;
    country_code: string;
  };
  boundingbox: Coordinates;
  category: string;
  display_name: string;
  icon: string;
  importance: number;
  lat: string;
  licence: string;
  lon: string;
  osm_type: string;
  osm_id: string;
  place_id: number;
  place_rank: number;
  type: string;
}
 
export const simplifyPlaceDisplayName = (place: NominatimPlace) => {
  const addressFields = [
    "village",
    "town",
    "neighbourhood",
    "suburb",
    "city",
    "state",
    "country",
  ];
 
  const addressParts: Array<string> = [];
 
  for (const field of addressFields) {
    if (field in place.address) {
      addressParts.push(place.address[field]);
    }
  }
  return addressParts.join(", ");
};
 
export const filterDuplicatePlaces = (places: NominatimPlace[] = []) => {
  const deduplicatedPlaces = places.reduce((previousRecord, currentPlace) => {
    const importance = currentPlace.importance ?? 0;
    const displayName = simplifyPlaceDisplayName(currentPlace);
 
    return previousRecord[displayName]?.importance >= importance
      ? previousRecord
      : { ...previousRecord, [displayName]: currentPlace };
  }, {} as Record<string, NominatimPlace>);
 
  return Object.values(deduplicatedPlaces);
};