Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add formatDuration to dates #6

Merged
merged 4 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ permissions:
contents: read

env:
JSR_DEPENDENCIES: "@cross/test @std/assert @frytg/logger"
JSR_DEPENDENCIES: "@cross/test @std/assert @std/fmt @frytg/logger @frytg/dates"
NPM_DEPENDENCIES: "luxon sinon"

jobs:
Expand Down
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
20 changes: 20 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,22 @@ 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`)
*
* @example
* ```ts
* import { formatDuration } from 'jsr:@frytg/dates'
*
* formatDuration(1000) // 1s
*
* const startTs = getMs()
* // do something...
* formatDuration(getMsOffset(startTs)) // time taken
* ```
*/
export const formatDuration = (duration: number): string => 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.
Loading