Skip to content
Snippets Groups Projects
Commit cadf0c6b authored by Konrad Mohrfeldt's avatar Konrad Mohrfeldt :koala:
Browse files

feat: add parseTime helper function

parent c82ca0e8
No related branches found
No related tags found
No related merge requests found
......@@ -296,3 +296,29 @@ export function areSetsEqual<T extends string | number | symbol>(
return true
}
function arrayPadLeft<T>(array: T[], maxLength: number, fillValue: T): T[] {
return Array(Math.max(0, maxLength - array.length))
.fill(fillValue)
.concat(array)
}
export function parseTime(time: string): number {
let [days, hours, minutes, seconds] = Array(4).fill(0)
if (time.includes(':')) {
;[days, hours, minutes, seconds] = arrayPadLeft<number | string>(time.split(':'), 4, 0)
} else {
// parses strings like: "3m32", "3 h 2s ", "9d2h3min1"
const format =
/^\s*(?:(?<days>\d+)\s*d)?\s*(?:(?<hours>\d+)\s*h)?\s*(?:(?<minutes>\d+)\s*m(?:in)?)?\s*(?:(?<seconds>\d+)\s*s?)?\s*$/
;({ days, hours, minutes, seconds } = time.match(format)?.groups ?? {})
}
return (
parseInt(days || 0) * 24 * 60 * 60 +
parseInt(hours || 0) * 60 * 60 +
parseInt(minutes || 0) * 60 +
parseInt(seconds || 0)
)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment