1
0
Fork 0
mirror of https://github.com/Chocobozzz/PeerTube.git synced 2025-10-04 02:09:37 +02:00

Cleanup utils helper

This commit is contained in:
Chocobozzz 2018-08-14 15:28:30 +02:00
parent 59c76ffa8f
commit 06215f15e0
No known key found for this signature in database
GPG key ID: 583A612D890159BE
20 changed files with 173 additions and 166 deletions

View file

@ -14,6 +14,35 @@ import * as rimraf from 'rimraf'
import { URL } from 'url'
import { truncate } from 'lodash'
const timeTable = {
ms: 1,
second: 1000,
minute: 60000,
hour: 3600000,
day: 3600000 * 24,
week: 3600000 * 24 * 7,
month: 3600000 * 24 * 30
}
export function parseDuration (duration: number | string): number {
if (typeof duration === 'number') return duration
if (typeof duration === 'string') {
const split = duration.match(/^([\d\.,]+)\s?(\w+)$/)
if (split.length === 3) {
const len = parseFloat(split[1])
let unit = split[2].replace(/s$/i,'').toLowerCase()
if (unit === 'm') {
unit = 'ms'
}
return (len || 1) * (timeTable[unit] || 0)
}
}
throw new Error('Duration could not be properly parsed')
}
function sanitizeUrl (url: string) {
const urlObject = new URL(url)