@@ -10,97 +10,65 @@ import type { FormatProps } from "@itwin/core-quantity";
10
10
import { FormatTraits , type FormatterSpec } from "@itwin/core-quantity" ;
11
11
import { MeasureTools } from "../MeasureTools.js" ;
12
12
13
- export class FormatterUtils {
14
- private static removeUnitSuffixes ( s : string ) {
15
- s = s . replace ( / m / g, "" ) ;
16
- s = s . replace ( / f t / g, "" ) ;
17
- return s ;
18
- }
13
+ export namespace FormatterUtils {
19
14
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 ;
35
18
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 ( ", " ) ;
47
24
}
48
25
49
- public static async formatCoordinates ( point : Point3d ) : Promise < string > {
26
+ export async function formatCoordinates ( point : Point3d ) : Promise < string > {
50
27
const coordSpec =
51
28
await IModelApp . quantityFormatter . getFormatterSpecByQuantityType (
52
29
QuantityType . Coordinate
53
30
) ;
54
- if ( undefined === coordSpec ) return "" ;
55
-
56
- return FormatterUtils . formatCoordinatesWithSpec ( point , coordSpec ) ;
31
+ return formatCoordinatesImmediate ( point , coordSpec ) ;
57
32
}
58
33
59
- public static formatCoordinatesImmediate ( point : Point3d , coordSpec ?: FormatterSpec ) : string {
60
- let result : string ;
34
+ export function formatCoordinatesImmediate ( point : Point3d , coordSpec ?: FormatterSpec ) : string {
61
35
if ( ! coordSpec ) {
62
36
coordSpec =
63
37
IModelApp . quantityFormatter . findFormatterSpecByQuantityType (
64
38
QuantityType . Coordinate
65
39
) ;
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
73
40
}
41
+ if ( undefined === coordSpec ) return "" ;
74
42
75
- return result ;
43
+ return formatValuesWithNoUnitLabel ( [ point . x , point . y , point . z ] , coordSpec ) ;
76
44
}
77
45
78
- public static async formatCoordinatesXY ( point : XAndY ) : Promise < string > {
46
+ export async function formatCoordinatesXY ( point : XAndY ) : Promise < string > {
79
47
const coordSpec =
80
48
await IModelApp . quantityFormatter . getFormatterSpecByQuantityType (
81
49
QuantityType . Coordinate
82
50
) ;
83
- if ( undefined === coordSpec ) return "" ;
84
-
85
- return FormatterUtils . formatCoordinatesXYWithSpec ( point , coordSpec ) ;
51
+ return formatCoordinatesXYImmediate ( point , coordSpec ) ;
86
52
}
87
53
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
+ }
93
61
if ( undefined === coordSpec ) return "" ;
94
62
95
- return FormatterUtils . formatCoordinatesXYWithSpec ( point , coordSpec ) ;
63
+ return formatValuesWithNoUnitLabel ( [ point . x , point . y ] , coordSpec ) ;
96
64
}
97
65
98
66
/** Formats the input angle into DD°MM'SS.SS" format.
99
67
* NOTE: uses the same symbols as the IModelApp's quantityFormatter for minute and second.
100
68
* The minute symbol is an apostrophe ' while it should be a prime (\u2032)
101
69
* The second symbol is a quotation mark " while it should be a double prime (\u2033)
102
70
*/
103
- public static formatAngleToDMS ( angleInDegrees : number ) : string {
71
+ export function formatAngleToDMS ( angleInDegrees : number ) : string {
104
72
const isNegative = angleInDegrees < 0 ;
105
73
angleInDegrees = Math . abs ( angleInDegrees ) ;
106
74
@@ -118,7 +86,7 @@ export class FormatterUtils {
118
86
return str ;
119
87
}
120
88
121
- public static formatCartographicToLatLongDMS ( c : Cartographic ) : string {
89
+ export function formatCartographicToLatLongDMS ( c : Cartographic ) : string {
122
90
const latSuffixKey =
123
91
0 < c . latitude
124
92
? "MeasureTools:Generic.latitudeNorthSuffix"
@@ -136,7 +104,7 @@ export class FormatterUtils {
136
104
return str ;
137
105
}
138
106
139
- public static async formatCartographicToLatLong (
107
+ export async function formatCartographicToLatLong (
140
108
c : Cartographic , angleSpec ?: FormatterSpec
141
109
) : Promise < string > {
142
110
if ( ! angleSpec ) {
@@ -168,7 +136,7 @@ export class FormatterUtils {
168
136
return str ;
169
137
}
170
138
171
- public static formatSlope (
139
+ export function formatSlope (
172
140
slopeInPercent : number ,
173
141
withSlopeRatio : boolean
174
142
) : string {
@@ -185,7 +153,7 @@ export class FormatterUtils {
185
153
return `${ fSlope } (${ fSlopeRatio } )` ;
186
154
}
187
155
188
- public static async formatStation ( station : number , stationSpec ?: FormatterSpec ) : Promise < string > {
156
+ export async function formatStation ( station : number , stationSpec ?: FormatterSpec ) : Promise < string > {
189
157
if ( ! stationSpec ) {
190
158
stationSpec =
191
159
await IModelApp . quantityFormatter . getFormatterSpecByQuantityType (
@@ -195,7 +163,7 @@ export class FormatterUtils {
195
163
return IModelApp . quantityFormatter . formatQuantity ( station , stationSpec ) ;
196
164
}
197
165
198
- public static async formatLength ( length : number , lengthSpec ?: FormatterSpec ) : Promise < string > {
166
+ export async function formatLength ( length : number , lengthSpec ?: FormatterSpec ) : Promise < string > {
199
167
if ( ! lengthSpec ) {
200
168
lengthSpec =
201
169
await IModelApp . quantityFormatter . getFormatterSpecByQuantityType (
@@ -208,13 +176,13 @@ export class FormatterUtils {
208
176
/**
209
177
* @returns The bearing in radians, where 0 is North and π/2 is East.
210
178
*/
211
- public static calculateBearing ( dx : number , dy : number ) : number {
179
+ export function calculateBearing ( dx : number , dy : number ) : number {
212
180
let bearing = Math . atan2 ( dx , dy ) ; // radians, 0 = North, π/2 = East
213
181
if ( bearing < 0 ) bearing += 2 * Math . PI ; // Normalize to [0, 2π)
214
182
return bearing ;
215
183
}
216
184
217
- public static getDefaultBearingFormatProps ( ) : FormatProps {
185
+ export function getDefaultBearingFormatProps ( ) : FormatProps {
218
186
return {
219
187
minWidth : 2 ,
220
188
precision : 0 ,
0 commit comments