-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
49 lines (44 loc) · 1.46 KB
/
utils.ts
File metadata and controls
49 lines (44 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* @author: lencx
* @create_at: Jun 14, 2020
*/
/**
* @method fmtFileSize
* @param {number} bytes
* @param {boolean} bits - enables bit sizes, default is `false`
* @param {number} dp - decimal place, default is `2`
* @see https://stackoverflow.com/questions/10420352/converting-file-size-in-bytes-to-human-readable-string/10420404
*/
export function fmtFileSize(bytes: number, bit: boolean = false, dp: number = 2) {
const thresh: number = bit ? 1000 : 1024;
if (bytes <= 0) {
return '0 B';
}
if (Math.abs(bytes) < thresh) {
return bytes + ' B';
}
const units = bit
? ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
let u = -1;
const r = 10**dp;
do {
bytes /= thresh;
++u;
} while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1);
return `${bytes.toFixed(dp)} ${units[u]}`;
}
export const isStr = (arg: any): boolean => typeof arg === 'string';
// exists directory or file
export const exists = async (filename: string): Promise<boolean> => {
try {
await Deno.stat(filename);
return true;
} catch (e) {
return false;
}
};
// https://stackoverflow.com/questions/190852/how-can-i-get-file-extensions-with-javascript
export const fileExt = (fname: string): string => fname.slice((fname.lastIndexOf('.') - 1 >>> 0) + 2);
// example: './a/b/' => 'a/b'
export const trimPath = (path: string): string => path.replace(/^\.\/|\/$/g, '');