All files / applications/go-by-transit/src/view/nearby/util turfUtils.ts

66.66% Statements 8/12
50% Branches 2/4
66.66% Functions 2/3
66.66% Lines 8/12

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      22x           469x             303x         303x 207x   207x   96x   96x                          
import distance from "@turf/distance";
import { round } from "lodash";
 
const MILE_IN_FEET = 5280;
 
export function getDistance(
  currentLocation: number[],
  arrivalLocation: number[]
): number {
  return distance(currentLocation, arrivalLocation, { units: "miles" });
}
 
export function getNormalizedDistanceString(
  currentLocation: number[],
  arrivalLocation: number[]
) {
  const distanceFromCurrentLocation = getDistance(
    currentLocation,
    arrivalLocation
  );
 
  if (distanceFromCurrentLocation < 1) {
    const distanceInFeet = round(MILE_IN_FEET * distanceFromCurrentLocation, 2);
 
    return `${distanceInFeet} feet away`;
  } else {
    const roundedMiles = round(distanceFromCurrentLocation, 2);
 
    return `${roundedMiles} miles away`;
  }
}
 
export function getNormalizedDistanceFromFeet(feet: number) {
  const diff = feet / MILE_IN_FEET;
 
  if (diff < 1) {
    return `${round(diff, 2)} feet away`;
  } else {
    return `${round(diff, 2)} miles away`;
  }
}