From 9b41bd1b52c3b1c9febf72036569443838008378 Mon Sep 17 00:00:00 2001 From: egeofalcao Date: Wed, 21 Jun 2023 15:05:47 +1200 Subject: [PATCH 1/3] feat: SURVEY-17487 Optional show zero seconds --- src/utils/bearing.test.ts | 2 ++ src/utils/bearing.ts | 13 ++++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/utils/bearing.test.ts b/src/utils/bearing.test.ts index a65d080a..de128057 100644 --- a/src/utils/bearing.test.ts +++ b/src/utils/bearing.test.ts @@ -31,6 +31,8 @@ describe("bearing", () => { 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, false, true)).toBe("300° 00' 00\""); + expect(convertDDToDMS(0, false, false, true)).toBe("0° 00' 00\""); }); test("bearingStringValidator", () => { diff --git a/src/utils/bearing.ts b/src/utils/bearing.ts index d0dd5ee3..b450aa42 100644 --- a/src/utils/bearing.ts +++ b/src/utils/bearing.ts @@ -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, true, true); } - return convertDDToDMS(safeValue, true, true); + return convertDDToDMS(safeValue, true, true, true); }; export const bearingNumberParser = (value: string): number | null => { @@ -41,7 +41,12 @@ export const bearingStringValidator = ( }; // Decimal-ish degrees to Degrees Minutes Seconds converter -export const convertDDToDMS = (dd: number | null, showPositiveSymbol = true, addTrailingZeros = true): string => { +export const convertDDToDMS = ( + dd: number | null, + showPositiveSymbol = true, + addTrailingZeros = true, + showZeroSeconds = false, +): string => { if (dd == null) return "–"; if (dd === 0) addTrailingZeros = false; @@ -75,6 +80,8 @@ export const convertDDToDMS = (dd: number | null, showPositiveSymbol = true, add dmsString += `\xa0${minString}'\xa0${secString}.${deciSecString}"`; // "\xa0" is here for non-breaking space } else if (secNumeric != 0) { dmsString += `\xa0${minString}'\xa0${secString}"`; + } else if (showZeroSeconds && secNumeric == 0) { + dmsString += `\xa0${minString}'\xa000"`; } else { dmsString += `\xa0${minString}'`; } From 63c828ccfc5578e9c96b217008d97cce4ec66994 Mon Sep 17 00:00:00 2001 From: egeofalcao Date: Thu, 22 Jun 2023 08:43:43 +1200 Subject: [PATCH 2/3] feat: SURVEY-17487 Use trailingZeroDp instead of addTrailingZeros --- src/utils/bearing.test.ts | 45 +++++++++++++++++++++------------------ src/utils/bearing.ts | 35 +++++++++++++++--------------- 2 files changed, 42 insertions(+), 38 deletions(-) diff --git a/src/utils/bearing.test.ts b/src/utils/bearing.test.ts index de128057..987cc1a6 100644 --- a/src/utils/bearing.test.ts +++ b/src/utils/bearing.test.ts @@ -7,32 +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, false, true)).toBe("300° 00' 00\""); - expect(convertDDToDMS(0, false, false, true)).toBe("0° 00' 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 b450aa42..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, true); + return convertDDToDMS(bearingNumberParser(safeValue), true, 4); } - return convertDDToDMS(safeValue, true, true, true); + return convertDDToDMS(safeValue, true, 4); }; export const bearingNumberParser = (value: string): number | null => { @@ -40,17 +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, - showZeroSeconds = false, -): 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("."); @@ -76,16 +80,13 @@ export const convertDDToDMS = ( 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 if (showZeroSeconds && secNumeric == 0) { - dmsString += `\xa0${minString}'\xa000"`; } else { dmsString += `\xa0${minString}'`; } - return dmsString; }; From 5f6e5b125749a64efeb833d095dbdeb96b71c343 Mon Sep 17 00:00:00 2001 From: egeofalcao Date: Thu, 22 Jun 2023 09:29:59 +1200 Subject: [PATCH 3/3] feat: SURVEY-17487 Fixed storybook error --- .../GridFormEditBearingCorrectionInteraction.stories.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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();