From b02fd5279e79af44b83eb0e20d53bb9ee57c988d Mon Sep 17 00:00:00 2001 From: Reza Kargar <29206777+RezaKargar@users.noreply.github.com> Date: Thu, 28 Mar 2024 12:20:01 +0330 Subject: [PATCH] Add minute support in duration (#703) (#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 --- src/util.ts | 4 +++- test/util.test.js | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/util.ts b/src/util.ts index b900a0f65b..0fe12b4bde 100644 --- a/src/util.ts +++ b/src/util.ts @@ -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') { @@ -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}".`) } diff --git a/test/util.test.js b/test/util.test.js index 17dc23796d..3f52aa88d9 100644 --- a/test/util.test.js +++ b/test/util.test.js @@ -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))