-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'release/release/0.13.21'
- Loading branch information
Showing
5 changed files
with
57 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { formatMilliseconds } from "../time" | ||
import { formatBytesToHuman, formatSecondsToTime } from "./format" | ||
|
||
describe("format.spec", () => { | ||
it("should format bytes", async () => { | ||
expect(formatBytesToHuman(123)).toMatchInlineSnapshot('"123 bytes"') | ||
expect(formatBytesToHuman(16*1024)).toMatchInlineSnapshot('"16.00 KiB"') | ||
expect(formatBytesToHuman(16*1024*1024)).toMatchInlineSnapshot('"16.00 MiB"') | ||
expect(formatBytesToHuman(16*1024 + 123)).toMatchInlineSnapshot('"16.12 KiB"') | ||
expect(formatBytesToHuman(16*1024 + 123, 0)).toMatchInlineSnapshot('"16 KiB"') | ||
expect(formatBytesToHuman(-123)).toMatchInlineSnapshot('"0 bytes"') | ||
expect(formatBytesToHuman(0)).toMatchInlineSnapshot('"0 bytes"') | ||
}) | ||
|
||
it("should format time", async () => { | ||
expect(formatSecondsToTime(0)).toMatchInlineSnapshot('"0:00"') | ||
expect(formatSecondsToTime(-123)).toMatchInlineSnapshot('"-2:03"') | ||
expect(formatSecondsToTime(123)).toMatchInlineSnapshot('"2:03"') | ||
expect(formatSecondsToTime(60*60*12)).toMatchInlineSnapshot('"12:00:00"') | ||
expect(formatSecondsToTime(60*60*12, '.')).toMatchInlineSnapshot('"12.00.00"') | ||
expect(formatSecondsToTime(60*60*1000)).toMatchInlineSnapshot('"1000:00:00"') | ||
}) | ||
}) |
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,28 @@ | ||
/** Just a simple yet fast helper. Alternatively you may use Intl formatters http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat */ | ||
export function formatBytesToHuman(bytes: number, decimals = 2) { | ||
// https://en.wikipedia.org/wiki/Orders_of_magnitude_(data) | ||
const units = ['bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB'] // etc | ||
const c = 1 / 1023 // change it to 1024 and see the diff | ||
let i = 0 | ||
let h = 0 | ||
if (bytes < 0) | ||
bytes = 0 | ||
for (; h < c && i < units.length; i++) { | ||
h = 1024 ** i / bytes | ||
if (h >= c) | ||
break | ||
} | ||
|
||
return (`${(1 / h).toFixed(i > 0 ? decimals : 0).toLocaleString()} ${units[i]}`) | ||
} | ||
|
||
/** Just a simple yet fast helper. Alternatively you may use Intl formatters http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat */ | ||
export function formatSecondsToTime(seconds: number, separator = ':') { | ||
const prefix = seconds < 0 ? '-' : '' | ||
if (seconds < 0) | ||
seconds = -1 * seconds | ||
const h = Math.floor(seconds / 3600) | ||
const m = Math.floor((seconds % 3600) / 60) | ||
const s = Math.round(seconds % 60) | ||
return prefix + [h, m > 9 ? m : h ? `0${m}` : m || '0', s > 9 ? s : `0${s}`].filter(Boolean).join(separator) | ||
} |
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