This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(duration-formatter): Adds formatting modes and improves output c…
…onfiguration. Fixes #444 BREAKING CHANGE: Removed experiementalFormatTime and DtExperimentalFormatTime in favor of dtDuration pipe.
- Loading branch information
1 parent
d1502af
commit 9d6199a
Showing
23 changed files
with
1,462 additions
and
352 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
libs/barista-components/formatters/src/duration/duration-formatter-constants.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* @license | ||
* Copyright 2020 Dynatrace LLC | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { DtTimeUnit } from '../unit'; | ||
|
||
export type DurationMode = 'DEFAULT' | 'PRECISE' | number; | ||
|
||
export function toDurationMode( | ||
formatMethod: string | number, | ||
): DurationMode | undefined { | ||
if (formatMethod === 'DEFAULT') { | ||
return 'DEFAULT'; | ||
} else if (formatMethod === 'PRECISE') { | ||
return 'PRECISE'; | ||
} else if (typeof formatMethod === 'number') { | ||
return formatMethod; | ||
} | ||
} | ||
|
||
// tslint:disable: no-magic-numbers | ||
/** Factorials needed for converting milliseconds to other time units */ | ||
export const CONVERSION_FACTORS_TO_MS = new Map<DtTimeUnit, number>([ | ||
[DtTimeUnit.YEAR, 12 * 30.41666 * 24 * 60 * 60 * 1000], | ||
[DtTimeUnit.MONTH, 30.41666 * 24 * 60 * 60 * 1000], | ||
[DtTimeUnit.DAY, 24 * 60 * 60 * 1000], | ||
[DtTimeUnit.HOUR, 60 * 60 * 1000], | ||
[DtTimeUnit.MINUTE, 60 * 1000], | ||
[DtTimeUnit.SECOND, 1000], | ||
[DtTimeUnit.MILLISECOND, 1], | ||
[DtTimeUnit.MICROSECOND, 1000], // Has to be handled differently because IEEE can't handle floating point numbers very well | ||
[DtTimeUnit.NANOSECOND, 1], // Has to be handled differently because IEEE can't handle floating point numbers very well | ||
]); | ||
|
||
/** Default for the conversionunit when no formatmethod is passed as a number. */ | ||
export const CONVERSIONUNITS = 3; | ||
|
||
/** Use when converting a duration to micro- or nanoseconds */ | ||
export const MOVE_COMMA = 1000000; |
43 changes: 43 additions & 0 deletions
43
...ta-components/formatters/src/duration/duration-formatter-utils/convert-to-milliseconds.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* @license | ||
* Copyright 2020 Dynatrace LLC | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { DtTimeUnit } from '../../unit'; | ||
import { | ||
CONVERSION_FACTORS_TO_MS, | ||
MOVE_COMMA, | ||
} from '../duration-formatter-constants'; | ||
|
||
/** | ||
* Converts any duration to milliseconds | ||
* @param duration numeric time value | ||
* @param inputUnit dtTimeUnit value describing which unit the duration is in | ||
*/ | ||
export function dtConvertToMilliseconds( | ||
duration: number, | ||
inputUnit: DtTimeUnit, | ||
): number | undefined { | ||
if (duration >= 0) { | ||
console.log( | ||
duration * CONVERSION_FACTORS_TO_MS.get(inputUnit)! * MOVE_COMMA, | ||
inputUnit, | ||
duration, | ||
); | ||
return inputUnit === DtTimeUnit.MICROSECOND || | ||
inputUnit === DtTimeUnit.NANOSECOND | ||
? (duration * CONVERSION_FACTORS_TO_MS.get(inputUnit)!) / MOVE_COMMA | ||
: duration * CONVERSION_FACTORS_TO_MS.get(inputUnit)!; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
libs/barista-components/formatters/src/duration/duration-formatter-utils/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* @license | ||
* Copyright 2020 Dynatrace LLC | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
export * from './convert-to-milliseconds'; | ||
export * from './transform-result'; | ||
export * from './transform-result-precise'; |
66 changes: 66 additions & 0 deletions
66
...a-components/formatters/src/duration/duration-formatter-utils/transform-result-precise.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* @license | ||
* Copyright 2020 Dynatrace LLC | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { DtTimeUnit } from '../../unit'; | ||
import { | ||
CONVERSION_FACTORS_TO_MS, | ||
DurationMode, | ||
MOVE_COMMA, | ||
} from '../duration-formatter-constants'; | ||
import { dtConvertToMilliseconds } from './convert-to-milliseconds'; | ||
|
||
/** | ||
* Calculates the duration precisely. Will convert duration to the inputUnit or to the outputUnit if set. (floating point number for its corelated unit) | ||
* @param duration numeric time value | ||
* @param inputUnit dtTimeUnit value describing which unit the duration is in | ||
* @param outputUnit dtTimeUnit | undefined value describing the unit to which it should format | ||
* @param formatMethod the formatting method | ||
*/ | ||
|
||
export function dtTransformResultPrecise( | ||
duration: number, | ||
inputUnit: DtTimeUnit, | ||
outputUnit: DtTimeUnit | undefined, | ||
formatMethod: DurationMode, | ||
): Map<DtTimeUnit, string> | undefined { | ||
const amount = | ||
inputUnit === DtTimeUnit.MILLISECOND | ||
? duration | ||
: dtConvertToMilliseconds(duration, inputUnit); | ||
return outputUnit !== undefined | ||
? calcResult(amount!, formatMethod, outputUnit) | ||
: calcResult(amount!, formatMethod, inputUnit); | ||
} | ||
|
||
function calcResult( | ||
amount: number, | ||
formatMethod: DurationMode, | ||
unit: DtTimeUnit, | ||
): Map<DtTimeUnit, string> { | ||
let result = new Map<DtTimeUnit, string>(); | ||
if (formatMethod === 'PRECISE') { | ||
amount = amount / CONVERSION_FACTORS_TO_MS.get(unit)!; | ||
// Need to move the comma since IEEE can't handle floating point numbers very well. | ||
if (unit === DtTimeUnit.MICROSECOND || unit === DtTimeUnit.NANOSECOND) { | ||
amount *= MOVE_COMMA; | ||
} | ||
result.set(unit, amount.toString()); | ||
} else { | ||
amount = Math.trunc(amount / CONVERSION_FACTORS_TO_MS.get(unit)!); | ||
amount < 1 ? result.set(unit, '< 1') : result.set(unit, amount.toString()); | ||
} | ||
return result; | ||
} |
62 changes: 62 additions & 0 deletions
62
libs/barista-components/formatters/src/duration/duration-formatter-utils/transform-result.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* @license | ||
* Copyright 2020 Dynatrace LLC | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { DtTimeUnit } from '../../unit'; | ||
import { | ||
CONVERSION_FACTORS_TO_MS, | ||
CONVERSIONUNITS, | ||
DurationMode, | ||
MOVE_COMMA, | ||
} from '../duration-formatter-constants'; | ||
|
||
/** | ||
* Calculates output duration in either "DEFAULT" or "CUSTOM" mode. | ||
* If precision is DEFAULT then displays a maximum of three units, but | ||
* if precision is a number, then displays that amount of units. | ||
* @param duration numeric time value | ||
* @param inputUnit dtTimeUnit value describing which unit the duration is in | ||
* @param formatMethod the formatting method | ||
*/ | ||
export function dtTransformResult( | ||
duration: number, | ||
inputUnit: DtTimeUnit, | ||
formatMethod: DurationMode, | ||
): Map<DtTimeUnit, string> | undefined { | ||
const result = new Map<DtTimeUnit, string>(); | ||
|
||
let rest = duration * CONVERSION_FACTORS_TO_MS.get(inputUnit)!; | ||
let displayedUnits = 0; | ||
let unitsToDisplay = | ||
typeof formatMethod === 'number' ? formatMethod : CONVERSIONUNITS; | ||
for (const key of Array.from(CONVERSION_FACTORS_TO_MS.keys())) { | ||
if (key === DtTimeUnit.MICROSECOND) { | ||
rest = Math.round(rest * MOVE_COMMA); // handles IEEE floating point number problem | ||
} | ||
const amount = Math.trunc(rest / CONVERSION_FACTORS_TO_MS.get(key)!); | ||
if (displayedUnits < unitsToDisplay) { | ||
if (amount > 0) { | ||
result.set(key, amount.toString()); | ||
// Only increase when a unit with a value bigger than 0 exists | ||
displayedUnits++; | ||
} else if (displayedUnits > 0) { | ||
// Only increase when a unit with a value is already set | ||
displayedUnits++; | ||
} | ||
} | ||
rest = rest - amount * CONVERSION_FACTORS_TO_MS.get(key)!; | ||
} | ||
return result; | ||
} |
Oops, something went wrong.