Parse duration strings for /fseek and /seek (#565)

This commit is contained in:
Max Isom 2022-03-13 18:30:36 -04:00 committed by GitHub
parent 03d5cfffd1
commit 6c00727a4a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 58 additions and 13 deletions

View file

@ -0,0 +1,21 @@
import parse from 'parse-duration';
/**
* Parse duration strings to seconds.
* @param str any common duration format, like 1m or 1hr 30s. If the input is a number it's assumed to be in seconds.
* @returns seconds
*/
const durationStringToSeconds = (str: string) => {
let seconds;
const isInputSeconds = Boolean(/\d+$/.exec(str));
if (isInputSeconds) {
seconds = Number.parseInt(str, 10);
} else {
seconds = parse(str) / 1000;
}
return seconds;
};
export default durationStringToSeconds;