mirror of
https://github.com/YunoHost/yunohost-admin.git
synced 2024-09-03 20:06:15 +02:00
ts: add typing to filters
This commit is contained in:
parent
a875d6f74e
commit
9fac9f31de
2 changed files with 20 additions and 13 deletions
|
@ -1,15 +1,21 @@
|
||||||
import formatDistanceToNow from 'date-fns/formatDistanceToNow'
|
import { formatDistanceToNow } from 'date-fns/formatDistanceToNow'
|
||||||
import format from 'date-fns/format'
|
import { format } from 'date-fns/format'
|
||||||
|
|
||||||
import { dateFnsLocale as locale } from '@/i18n/helpers'
|
import { dateFnsLocale as locale } from '@/i18n/helpers'
|
||||||
|
|
||||||
export function distanceToNow(date, addSuffix = true, isTimestamp = false) {
|
export function distanceToNow(
|
||||||
return formatDistanceToNow(new Date(isTimestamp ? date * 1000 : date), {
|
date: string | number,
|
||||||
addSuffix,
|
addSuffix = true,
|
||||||
locale,
|
isTimestamp = false,
|
||||||
})
|
) {
|
||||||
|
const tsOrDate = isTimestamp && typeof date === 'number' ? date * 1000 : date
|
||||||
|
return formatDistanceToNow(new Date(tsOrDate), { addSuffix, locale })
|
||||||
}
|
}
|
||||||
|
|
||||||
export function readableDate(date, isTimestamp = false) {
|
export function readableDate(
|
||||||
return format(new Date(isTimestamp ? date * 1000 : date), 'PPPpp', { locale })
|
date: string | number,
|
||||||
|
isTimestamp = false,
|
||||||
|
): string {
|
||||||
|
const tsOrDate = isTimestamp && typeof date === 'number' ? date * 1000 : date
|
||||||
|
return format(new Date(tsOrDate), 'PPPpp', { locale })
|
||||||
}
|
}
|
||||||
|
|
|
@ -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']
|
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']
|
||||||
if (bytes === 0) return 'n/a'
|
if (bytes === 0) return 'n/a'
|
||||||
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)))
|
const i = Math.floor(Math.log(b) / Math.log(1024))
|
||||||
return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i]
|
return (b / Math.pow(1024, i)).toFixed(2) + ' ' + sizes[i]
|
||||||
}
|
}
|
||||||
|
|
||||||
export function humanPermissionName(text) {
|
export function humanPermissionName(text: string) {
|
||||||
return text
|
return text
|
||||||
.split('.')[1]
|
.split('.')[1]
|
||||||
.replace('_', ' ')
|
.replace('_', ' ')
|
||||||
|
|
Loading…
Add table
Reference in a new issue