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
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
export function fileBitsToString(mode: string, directory: boolean): string {
|
2020-11-29 21:46:35 +00:00
|
|
|
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
|
|
|
|
*/
|
2022-06-26 19:13:52 +00:00
|
|
|
export function encodePathSegments(path: string): string {
|
|
|
|
return path
|
|
|
|
.split('/')
|
|
|
|
.map((s) => encodeURIComponent(s))
|
|
|
|
.join('/');
|
2021-01-02 00:55:09 +00:00
|
|
|
}
|
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
export function hashToPath(hash: string): string {
|
2021-01-02 00:55:09 +00:00
|
|
|
return hash.length > 0 ? decodeURIComponent(hash.substr(1)) : '/';
|
|
|
|
}
|