Skip to content

Commit

Permalink
Add minute support in duration (#703) (#704)
Browse files Browse the repository at this point in the history
* Add minute support for parseDuration

* Add minute to Duration type

* Fix formatting issue of Duration type in util.ts

* Add test for minute in duration parsing

---------

Co-authored-by: Anton Medvedev <anton@medv.io>
  • Loading branch information
RezaKargar and antonmedv committed Mar 28, 2024
1 parent 8a7a8fe commit b02fd52
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/util.ts
Expand Up @@ -216,7 +216,7 @@ export function errnoMessage(errno: number | undefined): string {
)
}

export type Duration = number | `${number}s` | `${number}ms`
export type Duration = number | `${number}m` | `${number}s` | `${number}ms`

export function parseDuration(d: Duration) {
if (typeof d == 'number') {
Expand All @@ -226,6 +226,8 @@ export function parseDuration(d: Duration) {
return +d.slice(0, -1) * 1000
} else if (/\d+ms/.test(d)) {
return +d.slice(0, -2)
} else if (/\d+m/.test(d)) {
return +d.slice(0, -1) * 1000 * 60
}
throw new Error(`Unknown duration: "${d}".`)
}
Expand Down
1 change: 1 addition & 0 deletions test/util.test.js
Expand Up @@ -67,6 +67,7 @@ describe('util', () => {
assert.equal(parseDuration(1000), 1000)
assert.equal(parseDuration('2s'), 2000)
assert.equal(parseDuration('500ms'), 500)
assert.equal(parseDuration('2m'), 120000)
assert.throws(() => parseDuration('100'))
assert.throws(() => parseDuration(NaN))
assert.throws(() => parseDuration(-1))
Expand Down

0 comments on commit b02fd52

Please sign in to comment.