forked from KeJunMao/bilibili-box
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
44 lines (36 loc) · 1.03 KB
/
utils.ts
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
export function intToString(value: number) {
const suffixes = ["", "k", "m", "b", "t"];
const suffixNum = Math.floor(("" + value).length / 3);
let shortValue = parseFloat(
(suffixNum != 0 ? value / Math.pow(1000, suffixNum) : value).toPrecision(2)
);
if (shortValue % 1 != 0) {
shortValue = parseFloat(shortValue.toFixed(1));
}
return shortValue + suffixes[suffixNum];
}
export function getBLen(str: string) {
return str.replace(/[^\x00-\xff]/g, "01").length;
}
export function cutString(str: string, len: number) {
if (getBLen(str) <= len) {
return str + new Array(len - getBLen(str)).fill(" ").join("");
}
var strlen = 0;
var s = "";
for (var i = 0; i < str.length; i++) {
s = s + str.charAt(i);
if (str.charCodeAt(i) > 128) {
strlen = strlen + 2;
if (strlen >= len) {
return s.substring(0, s.length - 1) + "...";
}
} else {
strlen = strlen + 1;
if (strlen >= len) {
return s.substring(0, s.length - 2) + "...";
}
}
}
return s;
}