2020-06-10 20:00:43 +00:00
|
|
|
export const megabytesToBytes = (mb: number) => Math.floor(mb * 1024 * 1024);
|
|
|
|
|
2019-07-28 03:23:51 +00:00
|
|
|
export function bytesToHuman (bytes: number): string {
|
2019-08-17 23:03:10 +00:00
|
|
|
if (bytes === 0) {
|
|
|
|
return '0 kB';
|
|
|
|
}
|
|
|
|
|
2020-06-10 20:00:43 +00:00
|
|
|
const i = Math.floor(Math.log(bytes) / Math.log(1024));
|
|
|
|
return `${Number((bytes / Math.pow(1024, i)).toFixed(2))} ${[ 'Bytes', 'kB', 'MB', 'GB', 'TB' ][i]}`;
|
2019-07-28 03:23:51 +00:00
|
|
|
}
|
2019-09-29 20:23:15 +00:00
|
|
|
|
2020-06-10 20:00:43 +00:00
|
|
|
export function megabytesToHuman (mb: number): string {
|
|
|
|
return bytesToHuman(megabytesToBytes(mb));
|
|
|
|
}
|
2020-04-10 19:41:08 +00:00
|
|
|
|
|
|
|
export const randomInt = (low: number, high: number) => Math.floor(Math.random() * (high - low) + low);
|
2020-04-10 20:57:24 +00:00
|
|
|
|
2021-01-02 00:55:09 +00:00
|
|
|
export const cleanDirectoryPath = (path: string) => path.replace(/(\/(\/*))|(^$)/g, '/');
|
2020-07-05 04:46:49 +00:00
|
|
|
|
|
|
|
export const capitalize = (s: string) => s.charAt(0).toUpperCase() + s.slice(1).toLowerCase();
|
2020-11-29 21:46:35 +00:00
|
|
|
|
|
|
|
export function fileBitsToString (mode: string, directory: boolean): string {
|
|
|
|
const m = parseInt(mode, 8);
|
|
|
|
|
|
|
|
let buf = '';
|
|
|
|
'dalTLDpSugct?'.split('').forEach((c, i) => {
|
|
|
|
if ((m & (1 << (32 - 1 - i))) !== 0) {
|
|
|
|
buf = buf + c;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (buf.length === 0) {
|
|
|
|
// If the file is directory, make sure it has the directory flag.
|
|
|
|
if (directory) {
|
|
|
|
buf = 'd';
|
|
|
|
} else {
|
|
|
|
buf = '-';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
'rwxrwxrwx'.split('').forEach((c, i) => {
|
|
|
|
if ((m & (1 << (9 - 1 - i))) !== 0) {
|
|
|
|
buf = buf + c;
|
|
|
|
} else {
|
|
|
|
buf = buf + '-';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
2021-01-02 00:55:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* URL-encodes the segments of a path.
|
|
|
|
* This allows to use the path as part of a URL while preserving the slashes.
|
|
|
|
* @param path the path to encode
|
|
|
|
*/
|
|
|
|
export function encodePathSegments (path: string): string {
|
|
|
|
return path.split('/').map(s => encodeURIComponent(s)).join('/');
|
|
|
|
}
|
|
|
|
|
|
|
|
export function hashToPath (hash: string): string {
|
|
|
|
return hash.length > 0 ? decodeURIComponent(hash.substr(1)) : '/';
|
|
|
|
}
|
2021-12-04 18:35:55 +00:00
|
|
|
|
|
|
|
export function formatIp (ip: string): string {
|
|
|
|
return /([a-f0-9:]+:+)+[a-f0-9]+/.test(ip) ? `[${ip}]` : ip;
|
|
|
|
}
|
2022-06-11 18:52:33 +00:00
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
|
|
export const isObject = (o: unknown): o is {} => typeof o === 'object' && o !== null;
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
|
|
export const isEmptyObject = (o: {}): boolean =>
|
|
|
|
Object.keys(o).length === 0 && Object.getPrototypeOf(o) === Object.prototype;
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
|
|
export const getObjectKeys = <T extends {}> (o: T): Array<keyof T> => Object.keys(o) as Array<keyof T>;
|