Skip to content

Commit

Permalink
feat: add formatDuration to dates
Browse files Browse the repository at this point in the history
  • Loading branch information
frytg committed Dec 12, 2024
1 parent 964cb1b commit 63f7e39
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 2 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ This repository is work in progress.
## Tools

- [`@frytg/check-required-env`](./check-required-env/README.md) - Check a required environment variable
- [`@frytg/logger`](./logger/README.md) - Winston logging wrapper
- [`@frytg/dates`](./dates/README.md) - Date utilities around Luxon
- [`@frytg/logger`](./logger/README.md) - Pre-configuredWinston logging wrapper

## Lint

Expand All @@ -23,6 +24,14 @@ deno task check

See [_Writing documentation_](https://jsr.io/docs/writing-docs) for details about writing JSDoc.

## Testing

This uses [`@cross/test`](https://jsr.io/@cross/test) and [`sinon`](https://sinonjs.org) to run the tests.

```bash
deno task test
```

## Publish

Locally check if everything is ok:
Expand All @@ -34,6 +43,13 @@ deno publish --dry-run
Then once everything is pushed or merged on `main`, run the GitHub actions workflow to publish the packages to JSR
(see [_publishing packages_](https://jsr.io/docs/publishing-packages) for more details).

## More Tooling

Other tools that I regularly use and don't feel the need to optimize or re-create in this utility package:

- [`axios`](https://github.com/axios/axios) - _Promise based HTTP client for the browser and node.js_
- [`hono`](https://jsr.io/@hono/hono) - _small, simple, and ultrafast web framework built on Web Standards_

## Author

Created by [@frytg](https://github.com/frytg) / [frytg.digital](https://www.frytg.digital)
Expand Down
9 changes: 9 additions & 0 deletions dates/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Dates Changelog

## 2024-12-12 - 0.1.0

- feat: added `formatDuration` function to format durations in milliseconds to a human readable string.

## 2024-11-23 - 0.0.1

- feat: added basic setup
9 changes: 9 additions & 0 deletions dates/dates.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// load package
import { format as stdFormat } from '@std/fmt/duration'
import { DateTime } from 'luxon'

const LOCAL_TIMEZONE = 'Europe/Amsterdam'
Expand Down Expand Up @@ -199,3 +200,11 @@ export const getDateHourMinutes = (date: DateTime): string =>
* ```
*/
export const getFullRelativeTime = (date: DateTime): string => `${getDateHourMinutes(date)} (${getRelative(date)})`

/**
* Format a duration in milliseconds to a human readable string.
*
* @param {number} duration - the duration in milliseconds
* @returns {string} the formatted duration (e.g. `1m 39s`)
*/
export const formatDuration = (duration: number) => stdFormat(duration, { ignoreZero: true })
3 changes: 2 additions & 1 deletion dates/deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"$schema": "https://jsr.io/schema/config-file.v1.json",
"name": "@frytg/dates",
"version": "0.0.1",
"version": "0.1.0",
"exports": "./dates.ts",
"imports": {
"@std/fmt": "jsr:@std/fmt@^1.0.3",
"luxon": "npm:luxon@^3.5.0"
}
}
21 changes: 21 additions & 0 deletions dates/duration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { test } from '@cross/test'
import { assertEquals } from '@std/assert'

import { formatDuration } from './dates.ts'

test('formatDuration - formats milliseconds correctly', () => {
const testCases = [
{ input: 1000, expected: '1s' },
{ input: 60000, expected: '1m' },
{ input: 3600000, expected: '1h' },
{ input: 86400000, expected: '1d' },
{ input: 99123, expected: '1m 39s 123ms' },
{ input: 3723000, expected: '1h 2m 3s' },
{ input: 0, expected: '' },
{ input: 500, expected: '500ms' },
]

for (const { input, expected } of testCases) {
assertEquals(formatDuration(input), expected, `formatDuration(${input}) should return "${expected}"`)
}
})
5 changes: 5 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions logger/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Logger Changelog

## 2024-11-22 - 0.0.2

- feat: added basic setup
File renamed without changes.

0 comments on commit 63f7e39

Please sign in to comment.