Skip to content

Commit 4287359

Browse files
authored
Some cleanup of the FormatterUtils functions. Also converted class to a namespace which is more fitting. (#1346)
1 parent 3e97fe5 commit 4287359

File tree

1 file changed

+32
-64
lines changed

1 file changed

+32
-64
lines changed

packages/itwin/measure-tools/src/api/FormatterUtils.ts

Lines changed: 32 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -10,97 +10,65 @@ import type { FormatProps } from "@itwin/core-quantity";
1010
import { FormatTraits, type FormatterSpec } from "@itwin/core-quantity";
1111
import { MeasureTools } from "../MeasureTools.js";
1212

13-
export class FormatterUtils {
14-
private static removeUnitSuffixes(s: string) {
15-
s = s.replace(/ m/g, "");
16-
s = s.replace(/ ft/g, "");
17-
return s;
18-
}
13+
export namespace FormatterUtils {
1914

20-
private static formatCoordinatesWithSpec(
21-
point: Point3d,
22-
spec: FormatterSpec
23-
): string {
24-
const xStr = FormatterUtils.removeUnitSuffixes(
25-
IModelApp.quantityFormatter.formatQuantity(point.x, spec)
26-
);
27-
const yStr = FormatterUtils.removeUnitSuffixes(
28-
IModelApp.quantityFormatter.formatQuantity(point.y, spec)
29-
);
30-
const zStr = FormatterUtils.removeUnitSuffixes(
31-
IModelApp.quantityFormatter.formatQuantity(point.z, spec)
32-
);
33-
return `${xStr}, ${yStr}, ${zStr}`;
34-
}
15+
/** Formats a sequence of values with spec without the unit label */
16+
function formatValuesWithNoUnitLabel(values: number[], spec: FormatterSpec): string {
17+
const oldFormatTraits = spec.format.formatTraits;
3518

36-
private static formatCoordinatesXYWithSpec(
37-
point: XAndY,
38-
spec: FormatterSpec
39-
): string {
40-
const xStr = FormatterUtils.removeUnitSuffixes(
41-
IModelApp.quantityFormatter.formatQuantity(point.x, spec)
42-
);
43-
const yStr = FormatterUtils.removeUnitSuffixes(
44-
IModelApp.quantityFormatter.formatQuantity(point.y, spec)
45-
);
46-
return `${xStr}, ${yStr}`;
19+
spec.format.formatTraits &= ~FormatTraits.ShowUnitLabel; // Bit-wise remove ShowUnitLabel trait if exists
20+
const strs = values.map((value) => IModelApp.quantityFormatter.formatQuantity(value, spec));
21+
spec.format.formatTraits = oldFormatTraits; // Restore original format traits
22+
23+
return strs.join(", ");
4724
}
4825

49-
public static async formatCoordinates(point: Point3d): Promise<string> {
26+
export async function formatCoordinates(point: Point3d): Promise<string> {
5027
const coordSpec =
5128
await IModelApp.quantityFormatter.getFormatterSpecByQuantityType(
5229
QuantityType.Coordinate
5330
);
54-
if (undefined === coordSpec) return "";
55-
56-
return FormatterUtils.formatCoordinatesWithSpec(point, coordSpec);
31+
return formatCoordinatesImmediate(point, coordSpec);
5732
}
5833

59-
public static formatCoordinatesImmediate(point: Point3d, coordSpec?: FormatterSpec): string {
60-
let result: string;
34+
export function formatCoordinatesImmediate(point: Point3d, coordSpec?: FormatterSpec): string {
6135
if (!coordSpec) {
6236
coordSpec =
6337
IModelApp.quantityFormatter.findFormatterSpecByQuantityType(
6438
QuantityType.Coordinate
6539
);
66-
if (undefined === coordSpec) return "";
67-
result = FormatterUtils.formatCoordinatesWithSpec(point, coordSpec);
68-
} else {
69-
const oldFormatTraits = coordSpec.format.formatTraits;
70-
coordSpec.format.formatTraits &= ~FormatTraits.ShowUnitLabel; // Bit-wise remove ShowUnitLabel trait if exists
71-
result = FormatterUtils.formatCoordinatesWithSpec(point, coordSpec);
72-
coordSpec.format.formatTraits = oldFormatTraits; // Restore original format traits
7340
}
41+
if (undefined === coordSpec) return "";
7442

75-
return result;
43+
return formatValuesWithNoUnitLabel([point.x, point.y, point.z], coordSpec);
7644
}
7745

78-
public static async formatCoordinatesXY(point: XAndY): Promise<string> {
46+
export async function formatCoordinatesXY(point: XAndY): Promise<string> {
7947
const coordSpec =
8048
await IModelApp.quantityFormatter.getFormatterSpecByQuantityType(
8149
QuantityType.Coordinate
8250
);
83-
if (undefined === coordSpec) return "";
84-
85-
return FormatterUtils.formatCoordinatesXYWithSpec(point, coordSpec);
51+
return formatCoordinatesXYImmediate(point, coordSpec);
8652
}
8753

88-
public static formatCoordinatesXYImmediate(point: XAndY): string {
89-
const coordSpec =
90-
IModelApp.quantityFormatter.findFormatterSpecByQuantityType(
91-
QuantityType.Coordinate
92-
);
54+
export function formatCoordinatesXYImmediate(point: XAndY, coordSpec?: FormatterSpec): string {
55+
if (!coordSpec) {
56+
coordSpec =
57+
IModelApp.quantityFormatter.findFormatterSpecByQuantityType(
58+
QuantityType.Coordinate
59+
);
60+
}
9361
if (undefined === coordSpec) return "";
9462

95-
return FormatterUtils.formatCoordinatesXYWithSpec(point, coordSpec);
63+
return formatValuesWithNoUnitLabel([point.x, point.y], coordSpec);
9664
}
9765

9866
/** Formats the input angle into DD°MM'SS.SS" format.
9967
* NOTE: uses the same symbols as the IModelApp's quantityFormatter for minute and second.
10068
* The minute symbol is an apostrophe ' while it should be a prime (\u2032)
10169
* The second symbol is a quotation mark " while it should be a double prime (\u2033)
10270
*/
103-
public static formatAngleToDMS(angleInDegrees: number): string {
71+
export function formatAngleToDMS(angleInDegrees: number): string {
10472
const isNegative = angleInDegrees < 0;
10573
angleInDegrees = Math.abs(angleInDegrees);
10674

@@ -118,7 +86,7 @@ export class FormatterUtils {
11886
return str;
11987
}
12088

121-
public static formatCartographicToLatLongDMS(c: Cartographic): string {
89+
export function formatCartographicToLatLongDMS(c: Cartographic): string {
12290
const latSuffixKey =
12391
0 < c.latitude
12492
? "MeasureTools:Generic.latitudeNorthSuffix"
@@ -136,7 +104,7 @@ export class FormatterUtils {
136104
return str;
137105
}
138106

139-
public static async formatCartographicToLatLong(
107+
export async function formatCartographicToLatLong(
140108
c: Cartographic, angleSpec?: FormatterSpec
141109
): Promise<string> {
142110
if (!angleSpec) {
@@ -168,7 +136,7 @@ export class FormatterUtils {
168136
return str;
169137
}
170138

171-
public static formatSlope(
139+
export function formatSlope(
172140
slopeInPercent: number,
173141
withSlopeRatio: boolean
174142
): string {
@@ -185,7 +153,7 @@ export class FormatterUtils {
185153
return `${fSlope} (${fSlopeRatio})`;
186154
}
187155

188-
public static async formatStation(station: number, stationSpec?: FormatterSpec): Promise<string> {
156+
export async function formatStation(station: number, stationSpec?: FormatterSpec): Promise<string> {
189157
if (!stationSpec) {
190158
stationSpec =
191159
await IModelApp.quantityFormatter.getFormatterSpecByQuantityType(
@@ -195,7 +163,7 @@ export class FormatterUtils {
195163
return IModelApp.quantityFormatter.formatQuantity(station, stationSpec);
196164
}
197165

198-
public static async formatLength(length: number, lengthSpec?: FormatterSpec): Promise<string> {
166+
export async function formatLength(length: number, lengthSpec?: FormatterSpec): Promise<string> {
199167
if (!lengthSpec) {
200168
lengthSpec =
201169
await IModelApp.quantityFormatter.getFormatterSpecByQuantityType(
@@ -208,13 +176,13 @@ export class FormatterUtils {
208176
/**
209177
* @returns The bearing in radians, where 0 is North and π/2 is East.
210178
*/
211-
public static calculateBearing(dx: number, dy: number): number {
179+
export function calculateBearing(dx: number, dy: number): number {
212180
let bearing = Math.atan2(dx, dy); // radians, 0 = North, π/2 = East
213181
if (bearing < 0) bearing += 2 * Math.PI; // Normalize to [0, 2π)
214182
return bearing;
215183
}
216184

217-
public static getDefaultBearingFormatProps(): FormatProps {
185+
export function getDefaultBearingFormatProps(): FormatProps {
218186
return {
219187
minWidth: 2,
220188
precision: 0,

0 commit comments

Comments
 (0)