Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: SURVEY-17487 Optional show zero seconds #331

Merged
merged 3 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
43 changes: 24 additions & 19 deletions src/utils/bearing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
28 changes: 18 additions & 10 deletions src/utils/bearing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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 => {
Expand All @@ -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(".");

Expand All @@ -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;
};

Expand Down