mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-03-26 18:31:52 +00:00
* feat: Add disk usage * Fixed: let the set config work with nesting. * chore: Changes * chore: Add default route * fix: Tor only config * chore
31 lines
870 B
TypeScript
31 lines
870 B
TypeScript
import { string } from "ts-matches"
|
|
|
|
export type TimeUnit = "d" | "h" | "s" | "ms" | "m" | "µs" | "ns"
|
|
export type Duration = `${number}${TimeUnit}`
|
|
|
|
const durationRegex = /^([0-9]*(\.[0-9]+)?)(ns|µs|ms|s|m|d)$/
|
|
|
|
export const matchDuration = string.refine(isDuration)
|
|
export function isDuration(value: string): value is Duration {
|
|
return durationRegex.test(value)
|
|
}
|
|
|
|
export function duration(timeValue: number, timeUnit: TimeUnit = "s") {
|
|
return `${timeValue > 0 ? timeValue : 0}${timeUnit}` as Duration
|
|
}
|
|
const unitsToSeconds: Record<string, number> = {
|
|
ns: 1e-9,
|
|
µs: 1e-6,
|
|
ms: 0.001,
|
|
s: 1,
|
|
m: 60,
|
|
h: 3600,
|
|
d: 86400,
|
|
}
|
|
|
|
export function fromDuration(duration: Duration | number): number {
|
|
if (typeof duration === "number") return duration
|
|
const [, num, , unit] = duration.match(durationRegex) || []
|
|
return Number(num) * unitsToSeconds[unit]
|
|
}
|