Skip to content

Commit b02fd52

Browse files
Add minute support in duration (google#703) (google#704)
* 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>
1 parent 8a7a8fe commit b02fd52

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

src/util.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ export function errnoMessage(errno: number | undefined): string {
216216
)
217217
}
218218

219-
export type Duration = number | `${number}s` | `${number}ms`
219+
export type Duration = number | `${number}m` | `${number}s` | `${number}ms`
220220

221221
export function parseDuration(d: Duration) {
222222
if (typeof d == 'number') {
@@ -226,6 +226,8 @@ export function parseDuration(d: Duration) {
226226
return +d.slice(0, -1) * 1000
227227
} else if (/\d+ms/.test(d)) {
228228
return +d.slice(0, -2)
229+
} else if (/\d+m/.test(d)) {
230+
return +d.slice(0, -1) * 1000 * 60
229231
}
230232
throw new Error(`Unknown duration: "${d}".`)
231233
}

test/util.test.js

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ describe('util', () => {
6767
assert.equal(parseDuration(1000), 1000)
6868
assert.equal(parseDuration('2s'), 2000)
6969
assert.equal(parseDuration('500ms'), 500)
70+
assert.equal(parseDuration('2m'), 120000)
7071
assert.throws(() => parseDuration('100'))
7172
assert.throws(() => parseDuration(NaN))
7273
assert.throws(() => parseDuration(-1))

0 commit comments

Comments
 (0)