UNPKG

819 BJavaScriptView Raw
1export function humanize(str) {
2 return str
3 .replace(/^[\s_]+|[\s_]+$/g, '')
4 .replace(/[_\s]+/g, ' ')
5 .replace(/^[a-z]/, function (m) { return m.toUpperCase();});
6}
7
8export function getFileName(originName) {
9 const name = originName.replace(/\.[^/.]+$/, '');
10
11 return humanize(name);
12}
13
14export function getFileSize(bytesSize, decimalPoint) {
15 const bytes = bytesSize;
16
17 if (bytes === 0) return '0 Bytes';
18
19 const k = 1000;
20 const dm = decimalPoint || 2;
21 const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
22 const i = Math.floor(Math.log(bytes) / Math.log(k));
23
24 return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
25}
26
27export function getFileFormat(name) {
28 const format = (/[.]/.exec(name)) ? /[^.]+$/.exec(name) : 'not identified';
29
30 return format;
31}