Skip to content

Commit

Permalink
fix: extract lat/long formatting to utils [PT-188059329]
Browse files Browse the repository at this point in the history
  • Loading branch information
pjanik committed Aug 22, 2024
1 parent fc59395 commit fcc3125
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
13 changes: 3 additions & 10 deletions src/components/location-tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@ import { ICodapDataContextInfo, ILocation } from "../types";
import { LocationPicker } from "./location-picker";

import "../assets/scss/location-tab.scss";

function formatLatLng(latOrLng: number | string): string {
// Convert the number to a string with up to 2 decimal places
const formatted = Number(latOrLng).toFixed(2);
// Remove trailing zeros after the decimal point
const trimmed = formatted.replace(/(\.\d*?[1-9])0+$/, "$1").replace(/\.0+$/, "");
return trimmed;
}
import { formatLatLongNumber } from "../utils/daylight-utils";

interface LocationTabProps {
latitude: string;
Expand Down Expand Up @@ -71,8 +64,8 @@ export const LocationTab: React.FC<LocationTabProps> = ({
};

const handleLocationSelect = (selectedLocation: ILocation) => {
setLatitude(formatLatLng(selectedLocation.latitude));
setLongitude(formatLatLng(selectedLocation.longitude));
setLatitude(formatLatLongNumber(selectedLocation.latitude));
setLongitude(formatLatLongNumber(selectedLocation.longitude));
setLocationSearch(selectedLocation.name);
};

Expand Down
8 changes: 1 addition & 7 deletions src/grasp-seasons/components/seasons.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState, useEffect, useRef, ChangeEvent, useCallback } from "react";
import Slider from "./slider/slider";
import { changeMonthOfDayOfYear, getSolarNoonIntensity, isValidLatitude, isValidLongitude } from "../../utils/daylight-utils";
import { changeMonthOfDayOfYear, formatLatLongNumber, getSolarNoonIntensity, isValidLatitude, isValidLongitude } from "../../utils/daylight-utils";
import InfiniteDaySlider from "./slider/infinite-day-slider";
import MyLocations from "./my-locations";
import getURLParam from "../utils/utils";
Expand Down Expand Up @@ -47,12 +47,6 @@ function capitalize(string: string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}

function formatLatLongNumber(value: number) {
// This function ensures that the number is formatted up to 5 decimal points and removes any trailing zeros and
// the decimal point if not necessary.
return value.toFixed(2).replace(/\.?0+$/, "");
}

interface IProps {
lang?: Language;
initialState?: Partial<ISimState>;
Expand Down
6 changes: 6 additions & 0 deletions src/utils/daylight-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ export function isValidLatitude(latitude: string): boolean {
return !isNaN(parsed) && parsed >= -90 && parsed <= 90;
}

export function formatLatLongNumber(value: number | string) {
// This function ensures that the number is formatted up to 5 decimal points and removes any trailing zeros and
// the decimal point if not necessary.
return Number(value).toFixed(2).replace(/\.?0+$/, "");
}

export function changeMonthOfDayOfYear(dayOfYearIndex: number, monthsToAdd: number): number {
if (dayOfYearIndex < 0 || dayOfYearIndex >= 365) {
throw new Error("dayOfYearIndex must be between 0 and 364");
Expand Down

0 comments on commit fcc3125

Please sign in to comment.