diff --git a/src/stories/grid/gridFormInteraction/GridFormEditBearingCorrectionInteraction.stories.tsx b/src/stories/grid/gridFormInteraction/GridFormEditBearingCorrectionInteraction.stories.tsx index 376220d9..62e09a5e 100644 --- a/src/stories/grid/gridFormInteraction/GridFormEditBearingCorrectionInteraction.stories.tsx +++ b/src/stories/grid/gridFormInteraction/GridFormEditBearingCorrectionInteraction.stories.tsx @@ -54,7 +54,7 @@ GridFormEditBearingCorrectionInteractions_.play = async ({ canvasElement }) => { // Test formatting a bearing expect(inputField).toBeInTheDocument(); userEvent.type(inputField, "1.2345"); - expect(await canvas.findByText("+1° 23' 45.0\"")).toBeInTheDocument(); + expect(await canvas.findByText("+1° 23' 45\"")).toBeInTheDocument(); // Test enter to save updateValue.mockClear(); diff --git a/src/utils/bearing.test.ts b/src/utils/bearing.test.ts index a65d080a..987cc1a6 100644 --- a/src/utils/bearing.test.ts +++ b/src/utils/bearing.test.ts @@ -7,30 +7,35 @@ import { describe("bearing", () => { test("convertDDToDMS converts decimal-ish degrees to DMS", () => { - expect(convertDDToDMS(-0.001, false, false)).toBe("-0° 00' 10\""); - expect(convertDDToDMS(-10.001, false, false)).toBe("-10° 00' 10\""); - expect(convertDDToDMS(-370.001, false, false)).toBe("-10° 00' 10\""); - expect(convertDDToDMS(359.595999, false, false)).toBe("0° 00'"); - expect(convertDDToDMS(369.696999, false, false)).toBe("10° 10' 10\""); - expect(convertDDToDMS(221.555999, false, false)).toBe("221° 56'"); - expect(convertDDToDMS(221.555999, false, true)).toBe("221° 56' 00.0\""); - expect(convertDDToDMS(5)).toBe("+5° 00' 00.0\""); - expect(convertDDToDMS(5.0)).toBe("+5° 00' 00.0\""); - expect(convertDDToDMS(5.00001)).toBe("+5° 00' 00.1\""); - expect(convertDDToDMS(5.1)).toBe("+5° 10' 00.0\""); + expect(convertDDToDMS(-0.001, false)).toBe("-0° 00' 10\""); + expect(convertDDToDMS(-10.001, false)).toBe("-10° 00' 10\""); + expect(convertDDToDMS(-370.001, false)).toBe("-10° 00' 10\""); + expect(convertDDToDMS(359.595999, false, 2)).toBe("0° 00'"); + expect(convertDDToDMS(359.595999, false, 4)).toBe("0° 00' 00\""); + expect(convertDDToDMS(359.595999, false, 5)).toBe("0° 00' 00.0\""); + expect(convertDDToDMS(369.696999, false)).toBe("10° 10' 10\""); + expect(convertDDToDMS(369.696999, false, 4)).toBe("10° 10' 10\""); + expect(convertDDToDMS(221.555999, false, 2)).toBe("221° 56'"); + expect(convertDDToDMS(221.555999, false, 5)).toBe("221° 56' 00.0\""); + expect(convertDDToDMS(5, true, 5)).toBe("+5° 00' 00.0\""); + expect(convertDDToDMS(5.0, true, 5)).toBe("+5° 00' 00.0\""); + expect(convertDDToDMS(5.00001, true, 5)).toBe("+5° 00' 00.1\""); + expect(convertDDToDMS(5.1, true, 5)).toBe("+5° 10' 00.0\""); expect(convertDDToDMS(5.12345)).toBe("+5° 12' 34.5\""); expect(convertDDToDMS(5.12345, false)).toBe("5° 12' 34.5\""); - - expect(convertDDToDMS(300)).toBe("+300° 00' 00.0\""); - expect(convertDDToDMS(300.0)).toBe("+300° 00' 00.0\""); + expect(convertDDToDMS(5.12345, false, 2)).toBe("5° 12' 34.5\""); + expect(convertDDToDMS(300, true, 5)).toBe("+300° 00' 00.0\""); + expect(convertDDToDMS(300.0, true, 5)).toBe("+300° 00' 00.0\""); expect(convertDDToDMS(300.00001)).toBe("+300° 00' 00.1\""); - expect(convertDDToDMS(300.1)).toBe("+300° 10' 00.0\""); + expect(convertDDToDMS(300.1, true, 5)).toBe("+300° 10' 00.0\""); expect(convertDDToDMS(300.12345)).toBe("+300° 12' 34.5\""); expect(convertDDToDMS(300.12345, false)).toBe("300° 12' 34.5\""); - - expect(convertDDToDMS(300, false, false)).toBe("300° 00'"); - expect(convertDDToDMS(300.1, false, false)).toBe("300° 10'"); - expect(convertDDToDMS(0, false)).toBe("0° 00'"); + expect(convertDDToDMS(300, false, 2)).toBe("300° 00'"); + expect(convertDDToDMS(300.1, false, 2)).toBe("300° 10'"); + expect(convertDDToDMS(0, false, 2)).toBe("0° 00'"); + expect(convertDDToDMS(0.0, false)).toBe("0° 00'"); + expect(convertDDToDMS(0.0, false, 4)).toBe("0° 00' 00\""); + expect(convertDDToDMS(0.0, false, 5)).toBe("0° 00' 00.0\""); }); test("bearingStringValidator", () => { diff --git a/src/utils/bearing.ts b/src/utils/bearing.ts index d0dd5ee3..a42d7ae2 100644 --- a/src/utils/bearing.ts +++ b/src/utils/bearing.ts @@ -3,7 +3,7 @@ export const bearingValueFormatter = (value: any): string => { if (safeValue == null) { return "–"; } - return convertDDToDMS(safeValue, false, false); + return convertDDToDMS(safeValue, false); }; export const bearingCorrectionValueFormatter = (value: any): string => { @@ -12,9 +12,9 @@ export const bearingCorrectionValueFormatter = (value: any): string => { return "–"; } if (typeof safeValue === "string") { - return convertDDToDMS(bearingNumberParser(safeValue), true, true); + return convertDDToDMS(bearingNumberParser(safeValue), true, 4); } - return convertDDToDMS(safeValue, true, true); + return convertDDToDMS(safeValue, true, 4); }; export const bearingNumberParser = (value: string): number | null => { @@ -40,12 +40,21 @@ export const bearingStringValidator = ( return customInvalid ? customInvalid(bearing) : null; }; -// Decimal-ish degrees to Degrees Minutes Seconds converter -export const convertDDToDMS = (dd: number | null, showPositiveSymbol = true, addTrailingZeros = true): string => { +/** + *Decimal-ish degrees to Degrees Minutes Seconds converter + * + * @param dd Decimal-ish degrees + * @param showPositiveSymbol whether the + sign appears before the number + * @param trailingZeroDp 2 | 4 | 5 + * Example: + * 2 = 300° 00' + * 4 = 300° 00' 00" + * 5 = 300° 00' 00.0" + * @returns Degrees Minutes Seconds + */ +export const convertDDToDMS = (dd: number | null, showPositiveSymbol = true, trailingZeroDp: 2 | 4 | 5 = 2): string => { if (dd == null) return "–"; - if (dd === 0) addTrailingZeros = false; - // toFixed rounds parts up greater than 60, which has to be corrected below const [bearingWholeString, bearingDecimalString] = dd.toFixed(5).split("."); @@ -71,14 +80,13 @@ export const convertDDToDMS = (dd: number | null, showPositiveSymbol = true, add const deciSecString = bearingDecimalString?.substring(4, 5); let dmsString = `${showPositiveSymbol && dd > 0 ? "+" : ""}${dd < 0 ? "-" : ""}${bearingWhole}°`; - if (addTrailingZeros || deciSecString != "0") { + if (trailingZeroDp === 5 || deciSecString != "0") { dmsString += `\xa0${minString}'\xa0${secString}.${deciSecString}"`; // "\xa0" is here for non-breaking space - } else if (secNumeric != 0) { + } else if (trailingZeroDp === 4 || secNumeric != 0) { dmsString += `\xa0${minString}'\xa0${secString}"`; } else { dmsString += `\xa0${minString}'`; } - return dmsString; };