mirror of https://github.com/wg-easy/wg-easy
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.7 KiB
58 lines
1.7 KiB
export function bytes(
|
|
bytes: number,
|
|
decimals = 2,
|
|
kib = false,
|
|
maxunit?: string
|
|
) {
|
|
if (bytes === 0) return '0 B';
|
|
if (Number.isNaN(bytes) && !Number.isFinite(bytes)) return 'NaN';
|
|
const k = kib ? 1024 : 1000;
|
|
const dm =
|
|
decimals != null && !Number.isNaN(decimals) && decimals >= 0 ? decimals : 2;
|
|
const sizes = kib
|
|
? ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB', 'BiB']
|
|
: ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB', 'BB'];
|
|
let i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
if (maxunit !== undefined) {
|
|
const index = sizes.indexOf(maxunit);
|
|
if (index !== -1) i = index;
|
|
}
|
|
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`;
|
|
}
|
|
|
|
export function dateTime(value: Date) {
|
|
return new Intl.DateTimeFormat(undefined, {
|
|
year: 'numeric',
|
|
month: 'short',
|
|
day: 'numeric',
|
|
hour: 'numeric',
|
|
minute: 'numeric',
|
|
}).format(value);
|
|
}
|
|
|
|
/**
|
|
* Sorts an array of objects by a specified property in ascending or descending order.
|
|
*
|
|
* @param array The array of objects to be sorted.
|
|
* @param property The property to sort the array by.
|
|
* @param sort Whether to sort the array in ascending (default, true) or descending order (false).
|
|
*/
|
|
export function sortByProperty<T>(
|
|
array: T[],
|
|
property: keyof T,
|
|
sort: boolean = true
|
|
): T[] {
|
|
if (sort) {
|
|
return array.sort((a, b) =>
|
|
typeof a[property] === 'string'
|
|
? (a[property] as string).localeCompare(b[property] as string)
|
|
: (a[property] as number) - (b[property] as number)
|
|
);
|
|
}
|
|
|
|
return array.sort((a, b) =>
|
|
typeof a[property] === 'string'
|
|
? (b[property] as string).localeCompare(a[property] as string)
|
|
: (b[property] as number) - (a[property] as number)
|
|
);
|
|
}
|
|
|