ts: add typing to filters

This commit is contained in:
axolotle 2024-07-25 18:04:35 +02:00
parent a875d6f74e
commit 9fac9f31de
2 changed files with 20 additions and 13 deletions

View file

@ -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 })
}

View file

@ -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('_', ' ')