mirror of
https://github.com/hibiken/asynqmon.git
synced 2025-10-26 16:26:12 +08:00
(ui): Add custom duration field
This commit is contained in:
@@ -123,3 +123,24 @@ export function prettifyPayload(p: string) {
|
||||
export function currentUnixtime(): number {
|
||||
return Math.floor(Date.now() / 1000);
|
||||
}
|
||||
|
||||
const durationRegex = /[1-9]([0-9]*)[s|m|h]/;
|
||||
// Parses the given string and returns the number of seconds if the input is valid.
|
||||
// Otherwise, it throws an error
|
||||
// Supported time units are "s", "m", "h"
|
||||
export function parseDuration(s: string): number {
|
||||
if (!durationRegex.test(s)) {
|
||||
throw new Error("invalid duration");
|
||||
}
|
||||
const val = parseInt(s.slice(0, -1));
|
||||
switch (s.slice(-1)) {
|
||||
case "s":
|
||||
return val;
|
||||
case "m":
|
||||
return val * 60;
|
||||
case "h":
|
||||
return val * 6 * 60;
|
||||
default:
|
||||
throw new Error("invalid duration unit");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user