-
Notifications
You must be signed in to change notification settings - Fork 208
/
Formatter.ts
423 lines (359 loc) · 16.7 KB
/
Formatter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Quantity
*/
import { QuantityConstants } from "../Constants";
import { QuantityError, QuantityStatus } from "../Exception";
import { FormatterSpec } from "./FormatterSpec";
import { DecimalPrecision, FormatTraits, FormatType, FractionalPrecision, ScientificType, ShowSignOption } from "./FormatEnums";
/** rounding additive
* @internal
*/
const FPV_ROUNDFACTOR = 0.50000000001;
/** A private helper class used to format fraction part of value into a numerator and denominator.
* @internal
*/
class FractionalNumeric {
private _integral: number = 0;
private _numerator: number = 0;
private _denominator: number = 1;
private _greatestCommonFactor: number = 1;
private _textParts: string[] = [];
constructor(value: number, precision: FractionalPrecision, reduce: boolean) {
this.calculate(value, precision as number);
this.formTextParts(reduce);
}
private calculate(value: number, denominator: number) {
const positiveValue = Math.abs(value);
this._denominator = denominator;
this._integral = Math.floor(positiveValue);
const fractionPart = positiveValue - this._integral;
this._numerator = Math.floor(fractionPart * this._denominator + FPV_ROUNDFACTOR);
if (0 !== denominator && (this._numerator / this._denominator) === 1) {
this._numerator = 0;
this._integral += 1;
} else {
this._greatestCommonFactor = this.getGreatestCommonFactor(this._numerator, this._denominator);
}
}
/** Determine the GCD given two values. This value can be used to reduce a fraction.
* See algorithm description http://en.wikipedia.org/wiki/Euclidean_algorithm
*/
private getGreatestCommonFactor(numerator: number, denominator: number): number {
let r;
while (denominator !== 0) {
r = numerator % denominator;
numerator = denominator;
denominator = r;
}
return (numerator < 0) ? -numerator : numerator;
}
public get greatestCommonFactor(): number { return this._greatestCommonFactor; }
public get hasFractionPart(): boolean { return this._textParts.length > 0; }
public get isZero(): boolean { return 0 === this._numerator; }
public getIntegralString(): string {
if (this._textParts.length > 0)
return this._textParts[0];
return "";
}
public getNumeratorString(): string {
if (this._textParts.length >= 3)
return this._textParts[1];
return "";
}
public getDenominatorString(): string {
if (this._textParts.length >= 3)
return this._textParts[2];
return "";
}
private formTextParts(reduce: boolean): void {
let numerator = this._numerator;
let denominator = this._denominator;
if (reduce && this._greatestCommonFactor > 1) {
numerator /= this.greatestCommonFactor;
denominator /= this.greatestCommonFactor;
}
this._textParts.push(this._integral.toFixed(0));
if (numerator > 0) {
this._textParts.push(numerator.toFixed(0));
this._textParts.push(denominator.toFixed(0));
}
}
}
/** A helper class that contains methods used to format quantity values based on a format that are defined via the Format class.
* @beta
*/
export class Formatter {
// eslint-disable-next-line @typescript-eslint/naming-convention
private static FPV_MINTHRESHOLD = 1.0e-14;
private static isNegligible(value: number): boolean { return (Math.abs(value) < Formatter.FPV_MINTHRESHOLD); }
/** Return floating point value rounded by specific rounding factor.
* @param value Value to be rounded.
* @param roundTo Rounding factor.
*/
private static roundDouble(value: number, roundTo: number): number {
if (Formatter.isNegligible(roundTo))
return value;
roundTo = Math.abs(roundTo);
let rnd = FPV_ROUNDFACTOR + (value / roundTo);
const iVal = Math.floor(rnd);
rnd = iVal * roundTo;
return (value < 0.0) ? -rnd : rnd;
}
/** Generate a formatted text string integer value insert 1000 separators if appropriate.
* @param wholePart Integer value to be formatted.
*/
private static integerPartToText(wholePart: number, spec: FormatterSpec): string {
// build invariant string represent wholePart
let formattedValue = wholePart.toFixed(0);
if ((formattedValue.length > 3) && (spec.format.hasFormatTraitSet(FormatTraits.Use1000Separator) && (spec.format.thousandSeparator.length > 0))) {
let numSeparators = Math.floor(formattedValue.length / 3);
let groupLength = formattedValue.length % 3;
if (groupLength === 0) {
numSeparators = numSeparators - 1;
groupLength = groupLength + 3;
}
let outString = formattedValue.substring(0, groupLength);
for (let i = 1; i <= numSeparators; i += 1) {
outString = outString + spec.format.thousandSeparator + formattedValue.substring(groupLength, groupLength + 3);
groupLength = groupLength + 3;
}
formattedValue = outString;
}
return formattedValue;
}
/** Trim trailing "0" from the text that represent the fractional part of a floating point value.
* @param strVal The value string.
*/
private static trimTrailingZeroes(strVal: string): string {
let lastNonZeroCharIndex = -1;
for (let i = strVal.length - 1; i >= 0; i--) {
if (strVal.charCodeAt(i) !== QuantityConstants.CHAR_DIGIT_ZERO) {
lastNonZeroCharIndex = i;
break;
}
}
if (lastNonZeroCharIndex >= 0)
return strVal.substring(0, lastNonZeroCharIndex + 1);
return "";
}
/** Format a quantity value into a composite format such as ft-in or deg-min-sec.
* @param compositeValue The value for this part of the composite
* @param isLastPart If false the composite value should be a whole value, if true then the value should be formatted as a floating point value.
* @param label Label for this part of the composite. This will be either the default unit label or a custom label specified the format specification.
*/
private static formatCompositePart(compositeValue: number, isLastPart: boolean, label: string, spec: FormatterSpec): string {
let componentText = "";
if (!isLastPart) {
componentText = Formatter.integerPartToText(compositeValue, spec);
} else {
componentText = Formatter.formatMagnitude(compositeValue, spec);
}
if (spec.format.hasFormatTraitSet(FormatTraits.ShowUnitLabel)) {
componentText = componentText + spec.format.uomSeparator + label;
} else {
if (!isLastPart)
componentText = `${componentText}:`;
}
return componentText;
}
/** Format a quantity value into a composite format such as ft-in or deg-min-sec.
* @param magnitude quantity value
* @param fromUnit quantity unit
*/
private static formatComposite(magnitude: number, spec: FormatterSpec): string {
const compositeStrings: string[] = [];
// Caller will deal with appending +||-||() value sign as specified by formatting options so just format positive value
let posMagnitude = Math.abs(magnitude);
// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (let i = 0; i < spec.unitConversions.length; i++) {
const currentLabel = spec.unitConversions[i].label;
const unitConversion = spec.unitConversions[i].conversion;
if (i > 0 && unitConversion.factor < 1.0)
throw new QuantityError(QuantityStatus.InvalidCompositeFormat, `The Format ${spec.format.name} has a invalid unit specification..`);
if (i > 0 && unitConversion.offset !== 0)
throw new QuantityError(QuantityStatus.InvalidCompositeFormat, `The Format ${spec.format.name} has a invalid unit specification..`);
let unitValue = (posMagnitude * unitConversion.factor) + unitConversion.offset + Formatter.FPV_MINTHRESHOLD; // offset should only ever be defined for major unit
if (0 === i) {
const precisionScale = Math.pow(10, 8); // use a fixed round off precision of 8 to avoid loss of precision in actual magnitude
unitValue = Math.floor(unitValue * precisionScale + FPV_ROUNDFACTOR) / precisionScale;
if ((Math.abs(unitValue) < 0.0001) && spec.format.hasFormatTraitSet(FormatTraits.ZeroEmpty))
return "";
}
if (i < spec.format.units!.length - 1) {
const wholePart = Math.floor(unitValue);
const componentText = Formatter.formatCompositePart(wholePart, false, currentLabel, spec);
posMagnitude = unitValue - wholePart;
compositeStrings.push(componentText);
} else {
const componentText = Formatter.formatCompositePart(unitValue, true, currentLabel, spec);
compositeStrings.push(componentText);
}
}
return compositeStrings.join(spec.format.spacer ? spec.format.spacer : "");
}
/** Format a quantity value into a single text string. Imitate how formatting done by server method NumericFormatSpec::FormatDouble.
* @param magnitude quantity value
*/
private static formatMagnitude(magnitude: number, spec: FormatterSpec): string {
let posMagnitude = Math.abs(magnitude);
if ((Math.abs(posMagnitude) < 0.0001) && spec.format.hasFormatTraitSet(FormatTraits.ZeroEmpty))
return "";
if (spec.format.hasFormatTraitSet(FormatTraits.ApplyRounding))
posMagnitude = Math.abs(Formatter.roundDouble(magnitude, spec.format.roundFactor));
const isSci = ((posMagnitude > 1.0e12) || spec.format.type === FormatType.Scientific);
const isDecimal = (isSci || spec.format.type === FormatType.Decimal);
const isFractional = (!isDecimal && spec.format.type === FormatType.Fractional);
/* const usesStops = spec.format.type === FormatType.Station; */
const isPrecisionZero = spec.format.precision === DecimalPrecision.Zero;
const isKeepSingleZero = spec.format.hasFormatTraitSet(FormatTraits.KeepSingleZero);
const precisionScale = Math.pow(10.0, spec.format.precision);
const isKeepTrailingZeroes = spec.format.hasFormatTraitSet(FormatTraits.TrailZeroes);
let expInt = 0.0;
if (isSci && (posMagnitude !== 0.0)) {
let exp = Math.log10(posMagnitude);
let negativeExp = false;
if (exp < 0.0) {
exp = -exp;
negativeExp = true;
}
expInt = Math.floor(exp);
if (spec.format.type === FormatType.Scientific) {
if (spec.format.scientificType === ScientificType.ZeroNormalized && posMagnitude > 1.0)
expInt += 1.0;
else if (spec.format.scientificType === ScientificType.Normalized && posMagnitude < 1.0)
expInt += 1.0;
if (negativeExp)
expInt = -expInt;
}
const factor = Math.pow(10.0, -expInt);
posMagnitude *= factor;
}
let formattedValue = "";
if (isDecimal) {
const actualVal = isPrecisionZero ? posMagnitude + FPV_ROUNDFACTOR : posMagnitude + Formatter.FPV_MINTHRESHOLD;
let wholePart = Math.floor(actualVal);
let fractionPart = actualVal - wholePart;
if (!isPrecisionZero) {
fractionPart = Math.abs(fractionPart) * precisionScale + FPV_ROUNDFACTOR;
if (fractionPart >= precisionScale) {
wholePart += 1;
fractionPart -= precisionScale;
}
}
formattedValue = Formatter.integerPartToText(wholePart, spec);
if (isPrecisionZero) {
if (spec.format.hasFormatTraitSet(FormatTraits.KeepDecimalPoint) && !isKeepSingleZero)
formattedValue = formattedValue + spec.format.decimalSeparator;
else if (isKeepSingleZero)
formattedValue = `${formattedValue + spec.format.decimalSeparator}0`;
} else {
fractionPart = Math.floor(fractionPart) / precisionScale;
let fractionString = fractionPart.toFixed(spec.format.precision);
// remove leading "0."
fractionString = fractionString.substring(2).padEnd(spec.format.precision, "0");
if (!isKeepTrailingZeroes)
fractionString = Formatter.trimTrailingZeroes(fractionString);
if (fractionString.length > 0)
formattedValue = formattedValue + spec.format.decimalSeparator + fractionString;
else {
if (spec.format.hasFormatTraitSet(FormatTraits.KeepDecimalPoint))
formattedValue = formattedValue + spec.format.decimalSeparator + (isKeepSingleZero ? "0" : "");
}
}
if (isSci) {
const expString = `e${expInt.toFixed(0)}`;
formattedValue = formattedValue + expString;
}
} else if (isFractional) {
const fn = new FractionalNumeric(posMagnitude, spec.format.precision as FractionalPrecision, true);
formattedValue = fn.getIntegralString();
if (!fn.isZero && fn.hasFractionPart) {
const wholeFractionSeparator = spec.format.hasFormatTraitSet(FormatTraits.FractionDash) ? "-" : " ";
const fractionString = `${fn.getNumeratorString()}/${fn.getDenominatorString()}`;
formattedValue = formattedValue + wholeFractionSeparator + fractionString;
}
} else /* if (usesStops)*/ {
// we assume that stopping value is always positive
posMagnitude = Math.floor(posMagnitude * precisionScale + FPV_ROUNDFACTOR) / precisionScale;
const denominator = (Math.pow(10, spec.format.stationOffsetSize!));
const tVal = Math.floor(posMagnitude); // this is the integer part only
const hiPart = Math.floor(tVal / denominator);
const lowPart = tVal - hiPart * denominator;
const fract = posMagnitude - tVal;
const fractionPart = Math.floor(fract * precisionScale + FPV_ROUNDFACTOR);
const stationString = hiPart.toFixed(0) + spec.format.stationSeparator + lowPart.toFixed(0).padStart(spec.format.stationOffsetSize!, "0");
let fractionString = "";
if (fractionPart > 0) {
fractionString = (fractionPart/precisionScale).toFixed(spec.format.precision);
// remove leading "0."
fractionString = fractionString.substring(2).padEnd(spec.format.precision, "0");
if (!isKeepTrailingZeroes)
fractionString = Formatter.trimTrailingZeroes(fractionString);
formattedValue = stationString + spec.format.decimalSeparator + fractionString;
} else {
if (isKeepTrailingZeroes)
fractionString = spec.format.decimalSeparator + "".padEnd(spec.format.precision, "0");
else if (spec.format.hasFormatTraitSet(FormatTraits.KeepDecimalPoint))
fractionString = spec.format.decimalSeparator;
formattedValue = stationString + fractionString;
}
}
return formattedValue;
}
/** Format a quantity value into a single text string based on the current format specification of this class.
* @param magnitude defines the value to spec.format.
* @param spec A FormatterSpec object the defines specification for the magnitude and unit conversions for the formatter.
*/
public static formatQuantity(magnitude: number, spec: FormatterSpec): string {
const valueIsNegative = magnitude < 0.0;
let prefix = "";
let suffix = "";
let formattedValue = "";
switch (spec.format.showSignOption) {
case ShowSignOption.NegativeParentheses:
if (valueIsNegative) {
prefix = "(";
suffix = ")";
}
break;
case ShowSignOption.OnlyNegative:
if (valueIsNegative)
prefix = "-";
break;
case ShowSignOption.SignAlways:
if (valueIsNegative)
prefix = "-";
else
prefix = "+";
break;
case ShowSignOption.NoSign:
default:
break;
}
let formattedMagnitude = "";
if (spec.format.hasUnits) {
formattedMagnitude = Formatter.formatComposite(magnitude, spec);
} else {
// unitless quantity
formattedMagnitude = Formatter.formatMagnitude(magnitude, spec);
if (formattedMagnitude.length > 0 && spec.unitConversions.length > 0 && spec.format.hasFormatTraitSet(FormatTraits.ShowUnitLabel)) {
if (spec.format.hasFormatTraitSet(FormatTraits.PrependUnitLabel))
formattedMagnitude = spec.unitConversions[0].label + spec.format.uomSeparator + formattedMagnitude;
else
formattedMagnitude = formattedMagnitude + spec.format.uomSeparator + spec.unitConversions[0].label;
}
}
// add Sign prefix and suffix as necessary
if ((prefix.length > 0 || suffix.length > 0) && formattedMagnitude.length > 0)
formattedValue = prefix + formattedMagnitude + suffix;
else
formattedValue = formattedMagnitude;
if (spec.format.minWidth && spec.format.minWidth < formattedValue.length)
formattedValue.padStart(spec.format.minWidth, " ");
return formattedValue;
}
}