Skip to content

Commit a216ce7

Browse files
no23reasonclaude
andcommitted
fix: correct hemisphere letter for negative coords in demo
Rewrite formatDegrees without the in-expression param mutation and the `0 | x` floor hacks. The mutation reassigned `degrees` to its absolute value before getDirection ran, so negative coordinates always rendered as E/N. Passing the original signed value fixes the direction (W/S). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent c1844fd commit a216ce7

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

demo/Demo.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ const getDirection = (degrees: number, isLongitude: boolean) =>
55
degrees > 0 ? (isLongitude ? "E" : "N") : isLongitude ? "W" : "S";
66

77
// adapted from http://stackoverflow.com/a/5786281/2546338
8-
const formatDegrees = (degrees: number, isLongitude: boolean) =>
9-
`${0 | degrees}° ${
10-
0 | (((degrees < 0 ? (degrees = -degrees) : degrees) % 1) * 60)
11-
}' ${0 | (((degrees * 60) % 1) * 60)}" ${getDirection(
12-
degrees,
13-
isLongitude,
14-
)}`;
8+
const formatDegrees = (value: number, isLongitude: boolean) => {
9+
const abs = Math.abs(value);
10+
const degrees = Math.trunc(value);
11+
const minutes = Math.floor((abs % 1) * 60);
12+
const seconds = Math.floor(((abs * 60) % 1) * 60);
13+
return `${degrees}° ${minutes}' ${seconds}" ${getDirection(value, isLongitude)}`;
14+
};
1515

1616
export const Demo = () => {
1717
const {

0 commit comments

Comments
 (0)