Skip to content

Commit

Permalink
Merge branch 'release/release/0.13.21'
Browse files Browse the repository at this point in the history
  • Loading branch information
holtwick committed Oct 18, 2023
2 parents e4c3e6e + cb11223 commit 35d9466
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "zeed",
"type": "module",
"version": "0.13.20",
"version": "0.13.21",
"description": "🌱 Simple foundation library",
"author": {
"name": "Dirk Holtwick",
Expand Down Expand Up @@ -72,7 +72,7 @@
"@types/node": "^20",
"c8": "^8.0.1",
"cross-fetch": "^4.0.0",
"esbuild": "^0.19.4",
"esbuild": "^0.19.5",
"eslint": "^8.51.0",
"madge": "^6.1.0",
"tsup": "^7.2.0",
Expand Down
23 changes: 23 additions & 0 deletions src/common/data/format.spec.ts
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"')
})
})
28 changes: 28 additions & 0 deletions src/common/data/format.ts
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)
}
4 changes: 3 additions & 1 deletion src/common/time.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ describe("time.spec", () => {
it("should measure", async () => {
const getDuration = duration()
await sleep(50)
expect(/\d+.\d\dms/.test(getDuration())).toBe(true)
const elapsed = getDuration()
// console.log(`elapsed time: ${elapsed}`)
expect(/\d+.\d\d ms/.test(elapsed)).toBe(true)
})
})
2 changes: 1 addition & 1 deletion src/common/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function getTimestamp(): number {
// typeof performance !== "undefined" ? performance.now() : new Date().getTime()

export function formatMilliseconds(ms: number): string {
return ms > 999 ? `${(ms / 1000).toFixed(1)}s` : `${ms.toFixed(2)}ms`
return ms > 999 ? `${(ms / 1000).toFixed(1)} s` : `${ms.toFixed(2)} ms`
}

export function parseDate(
Expand Down

0 comments on commit 35d9466

Please sign in to comment.