Skip to content

Commit 7a9fd7a

Browse files
committed
feat: add formatDurationHumanReadable
1 parent 5c99d94 commit 7a9fd7a

3 files changed

Lines changed: 25 additions & 1 deletion

File tree

src/helpers/duration.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import * as handlebars from 'handlebars'
2+
3+
export function formatDurationHumanReadableHelper(): void {
4+
handlebars.registerHelper(
5+
'formatDurationHumanReadable',
6+
(durationMs: number) => {
7+
if (durationMs < 1) {
8+
return `1ms`
9+
} else if (durationMs < 1000) {
10+
return `${Math.floor(durationMs)}ms`
11+
} else if (durationMs < 60000) {
12+
return `${(durationMs / 1000).toFixed(1)}s`
13+
} else {
14+
const minutes = Math.floor(durationMs / 60000)
15+
const seconds = Math.floor((durationMs % 60000) / 1000)
16+
return `${minutes}m${seconds}s`
17+
}
18+
}
19+
)
20+
}

src/helpers/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { uppercaseHelper } from './upper-case'
2+
import { formatDurationHumanReadableHelper } from './duration'
23

34
export function registerAllHelpers(): void {
45
uppercaseHelper()
6+
formatDurationHumanReadableHelper()
57
}

templates/example/example.hbs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@
2020
- Created At: {{github.pullRequest.createdAt}}
2121
- Additions: {{github.pullRequest.additions}}
2222
- Deletions: {{github.pullRequest.deletions}}
23-
{{/if}}
23+
{{/if}}
24+
25+
Helper formatDurationHumanReadable: {{ formatDurationHumanReadable 3000 }}

0 commit comments

Comments
 (0)