-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathtext.js
35 lines (26 loc) · 1007 Bytes
/
text.js
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
export const getLinesCount = content => {
if (content.length == 0) {
return 0;
}
return splitByKK(content.split(/\r\n|\r|\n/).length);
};
export const splitByKK = content => (content > 999 ? content.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ' ') : content);
export const getContentSize = content => {
let size = content.length;
for (let i = content.length - 1; i >= 0; i--) {
const code = content.charCodeAt(i);
if (code > 0x7f && code <= 0x7ff) size++;
else if (code > 0x7ff && code <= 0xffff) size += 2;
if (code >= 0xdc00 && code <= 0xdfff) i--;
}
return bytesToSize(size);
};
export const bytesToSize = bytes => {
if (bytes == 0) return '0 B';
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
return `${Math.round(bytes / Math.pow(1024, i))} ${sizes[i]}`;
};
export const msecToSec = msec => {
return (Math.round(msec) / 1000).toFixed(2);
};