Skip to content

Commit b4b9a3b

Browse files
committed
feat(dms): cleanup edge cases
1 parent 92a6346 commit b4b9a3b

2 files changed

Lines changed: 33 additions & 33 deletions

File tree

src/compute-engine/latex-syntax/dictionary/definitions-units.ts

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import {
3939
parseUnitDSL,
4040
type UnitExpression,
4141
} from '../../numerics/unit-data';
42-
import { normalizeAngle, degreesToDMS } from '../serialize-dms';
42+
import { normalizeAngle, formatDMS } from '../serialize-dms';
4343

4444
// ---------------------------------------------------------------------------
4545
// Helpers
@@ -380,7 +380,8 @@ export const DEFINITIONS_UNITS: LatexDictionary = [
380380

381381
if (
382382
isAngleUnit &&
383-
(options.dmsFormat || options.angleNormalization !== 'none')
383+
(options.dmsFormat ||
384+
(options.angleNormalization && options.angleNormalization !== 'none'))
384385
) {
385386
// Get numeric value
386387
const magnitudeValue = machineValue(magnitude);
@@ -403,33 +404,11 @@ export const DEFINITIONS_UNITS: LatexDictionary = [
403404
}
404405

405406
// Apply normalization
406-
if (options.angleNormalization !== 'none') {
407+
if (options.angleNormalization && options.angleNormalization !== 'none')
407408
degrees = normalizeAngle(degrees, options.angleNormalization);
408-
}
409409

410-
// Format as DMS if requested
411-
if (options.dmsFormat) {
412-
const { deg, min, sec } = degreesToDMS(degrees);
413-
414-
let result = `${deg}°`;
415-
416-
if (Math.abs(sec) > 0.001) {
417-
// Include seconds
418-
const secStr = sec % 1 === 0 ? sec.toString() : sec.toFixed(2);
419-
result += `${Math.abs(min)}'${Math.abs(Number(secStr))}"`;
420-
} else if (Math.abs(min) > 0) {
421-
// Include minutes only
422-
result += `${Math.abs(min)}'`;
423-
} else {
424-
// Degrees only, show 0'0" for consistency
425-
result += `0'0"`;
426-
}
427-
428-
return result;
429-
} else {
430-
// Just normalize, use decimal degrees
431-
return `${degrees}°`;
432-
}
410+
if (options.dmsFormat) return formatDMS(degrees);
411+
return `${degrees}°`;
433412
}
434413

435414
// Fall through to default Quantity serialization

src/compute-engine/latex-syntax/serialize-dms.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ export function normalizeAngle(
2020
if (mode === '0...360') {
2121
// Normalize to [0, 360)
2222
const normalized = degrees % 360;
23-
return normalized < 0 ? normalized + 360 : normalized;
23+
return (normalized < 0 ? normalized + 360 : normalized) || 0; // Avoid -0
2424
}
2525

2626
if (mode === '-180...180') {
2727
// Normalize to [-180, 180]
2828
let normalized = degrees % 360;
2929
if (normalized > 180) normalized -= 360;
3030
if (normalized < -180) normalized += 360;
31-
return normalized;
31+
return normalized || 0; // Avoid -0
3232
}
3333

3434
return degrees;
@@ -54,7 +54,7 @@ export function degreesToDMS(totalDegrees: number): DMSComponents {
5454
let finalMin = min;
5555
let finalDeg = deg;
5656

57-
if (sec >= 59.999) {
57+
if (sec >= 60) {
5858
sec = 0;
5959
finalMin++;
6060
}
@@ -66,8 +66,29 @@ export function degreesToDMS(totalDegrees: number): DMSComponents {
6666
}
6767

6868
return {
69-
deg: sign * finalDeg,
70-
min: sign * finalMin,
71-
sec: sec === 0 ? 0 : sign * sec, // Avoid -0
69+
deg: sign * finalDeg || 0, // Avoid -0
70+
min: sign * finalMin || 0, // Avoid -0
71+
sec: sec === 0 ? 0 : sign * sec,
7272
};
7373
}
74+
75+
/**
76+
* Format a decimal degree value as a DMS LaTeX string.
77+
* Used by both Degrees and Quantity serializers.
78+
*/
79+
export function formatDMS(degrees: number): string {
80+
const { deg, min, sec } = degreesToDMS(degrees);
81+
82+
let result = `${deg}°`;
83+
84+
if (Math.abs(sec) > 0.001) {
85+
const secStr = sec % 1 === 0 ? sec.toString() : sec.toFixed(2);
86+
result += `${Math.abs(min)}'${Math.abs(Number(secStr))}"`;
87+
} else if (Math.abs(min) > 0) {
88+
result += `${Math.abs(min)}'`;
89+
} else {
90+
result += `0'0"`;
91+
}
92+
93+
return result;
94+
}

0 commit comments

Comments
 (0)