-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix time duration formatting as per SI
This commit fixes the formatting of time durations to have a spcae between the quantity and the unit symbol which is compliant with the SI. It also consolidates all of the time duration formatting code into formatTime() in jest-util to keep the code DRY and so that future updates are easy.
- Loading branch information
Showing
19 changed files
with
111 additions
and
27 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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import formatTime from '../formatTime'; | ||
|
||
it('defaults to milliseconds', () => { | ||
expect(formatTime(42)).toBe('42 ms'); | ||
}); | ||
|
||
it('formats seconds properly', () => { | ||
expect(formatTime(42, 0)).toBe('42 s'); | ||
}); | ||
|
||
it('formats milliseconds properly', () => { | ||
expect(formatTime(42, -3)).toBe('42 ms'); | ||
}); | ||
|
||
it('formats microseconds properly', () => { | ||
expect(formatTime(42, -6)).toBe('42 μs'); | ||
}); | ||
|
||
it('formats nanoseconds properly', () => { | ||
expect(formatTime(42, -9)).toBe('42 ns'); | ||
}); | ||
|
||
it('interprets lower than lowest powers as nanoseconds', () => { | ||
expect(formatTime(42, -12)).toBe('42 ns'); | ||
}); | ||
|
||
it('interprets higher than highest powers as seconds', () => { | ||
expect(formatTime(42, 3)).toBe('42 s'); | ||
}); | ||
|
||
it('interprets non-multiple-of-3 powers as next higher prefix', () => { | ||
expect(formatTime(42, -4)).toBe('42 ms'); | ||
}); | ||
|
||
it('formats the quantity properly when pad length is lower', () => { | ||
expect(formatTime(42, -3, 1)).toBe('42 ms'); | ||
}); | ||
|
||
it('formats the quantity properly when pad length is equal', () => { | ||
expect(formatTime(42, -3, 2)).toBe('42 ms'); | ||
}); | ||
|
||
it('left pads the quantity properly when pad length is higher', () => { | ||
expect(formatTime(42, -3, 5)).toBe(' 42 ms'); | ||
}); |
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,22 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
export default function formatTime( | ||
time: number, | ||
prefixPower: number = -3, | ||
padLeftLength: number = 0, | ||
): string { | ||
const prefixes = ['n', 'μ', 'm', '']; | ||
const prefixIndex = Math.max( | ||
0, | ||
Math.min( | ||
Math.trunc(prefixPower / 3) + prefixes.length - 1, | ||
prefixes.length - 1, | ||
), | ||
); | ||
return `${String(time).padStart(padLeftLength)} ${prefixes[prefixIndex]}s`; | ||
} |
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