diff --git a/app/src/helpers/filters/date.ts b/app/src/helpers/filters/date.ts index 69907ee5..32d829c1 100644 --- a/app/src/helpers/filters/date.ts +++ b/app/src/helpers/filters/date.ts @@ -1,15 +1,21 @@ -import formatDistanceToNow from 'date-fns/formatDistanceToNow' -import format from 'date-fns/format' +import { formatDistanceToNow } from 'date-fns/formatDistanceToNow' +import { format } from 'date-fns/format' import { dateFnsLocale as locale } from '@/i18n/helpers' -export function distanceToNow(date, addSuffix = true, isTimestamp = false) { - return formatDistanceToNow(new Date(isTimestamp ? date * 1000 : date), { - addSuffix, - locale, - }) +export function distanceToNow( + date: string | number, + addSuffix = true, + isTimestamp = false, +) { + const tsOrDate = isTimestamp && typeof date === 'number' ? date * 1000 : date + return formatDistanceToNow(new Date(tsOrDate), { addSuffix, locale }) } -export function readableDate(date, isTimestamp = false) { - return format(new Date(isTimestamp ? date * 1000 : date), 'PPPpp', { locale }) +export function readableDate( + date: string | number, + isTimestamp = false, +): string { + const tsOrDate = isTimestamp && typeof date === 'number' ? date * 1000 : date + return format(new Date(tsOrDate), 'PPPpp', { locale }) } diff --git a/app/src/helpers/filters/human.ts b/app/src/helpers/filters/human.ts index eb95dbc1..91344597 100644 --- a/app/src/helpers/filters/human.ts +++ b/app/src/helpers/filters/human.ts @@ -1,11 +1,12 @@ -export function humanSize(bytes) { +export function humanSize(bytes: string | number) { + const b = typeof bytes === 'string' ? parseFloat(bytes) : bytes const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'] if (bytes === 0) return 'n/a' - const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024))) - return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i] + const i = Math.floor(Math.log(b) / Math.log(1024)) + return (b / Math.pow(1024, i)).toFixed(2) + ' ' + sizes[i] } -export function humanPermissionName(text) { +export function humanPermissionName(text: string) { return text .split('.')[1] .replace('_', ' ')