diff options
Diffstat (limited to 'packages/gateway/src/commands/parseCommandDuration.ts')
| -rw-r--r-- | packages/gateway/src/commands/parseCommandDuration.ts | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/packages/gateway/src/commands/parseCommandDuration.ts b/packages/gateway/src/commands/parseCommandDuration.ts new file mode 100644 index 0000000..946e3cb --- /dev/null +++ b/packages/gateway/src/commands/parseCommandDuration.ts @@ -0,0 +1,25 @@ +export const parseCommandDurationToMilliseconds = ( + duration: string, +): number | null => { + const durationMatch = duration.match(/^(\d+)([smhd])$/); + + if (!durationMatch) return null; + + const durationValue = Number.parseInt(durationMatch[1], 10); + const durationUnit = durationMatch[2]; + + if (durationValue < 1) return null; + + switch (durationUnit) { + case "s": + return durationValue * 1000; + case "m": + return durationValue * 60 * 1000; + case "h": + return durationValue * 60 * 60 * 1000; + case "d": + return durationValue * 24 * 60 * 60 * 1000; + default: + return null; + } +}; |