-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
28 lines (23 loc) · 797 Bytes
/
util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { getTimeParts } from "@josephuspaye/timer";
const timeFactors = [
1, // seconds in a second
60, // seconds in a minute
60 * 60, // seconds in an hour
24 * 60 * 60 // seconds in a day
];
export function parseTime(timeString) {
const parts = timeString
.split(":") // split on color delimiter
.map(part => Number(part.trim())) // trim parts and convert to number
.reverse(); // Reverse for [seconds, minutes, ...]
const totalSeconds = parts
.slice(0, 4) // working with only seconds, minutes, hours, and days
.reduce((total, currentPart, index) => {
return total + currentPart * timeFactors[index];
}, 0);
return totalSeconds * 1000;
}
export function formatTime(time) {
const { h, m, s } = getTimeParts(time);
return `${h}:${m}:${s}`;
}