-
Notifications
You must be signed in to change notification settings - Fork 0
/
数据操作技巧1.js
78 lines (73 loc) · 1.84 KB
/
数据操作技巧1.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// 星级评分
const StartScore = rate => "★★★★★☆☆☆☆☆".slice(5 - rate, 10 - rate);
const start = StartScore(3);
// start => "★★★"
// config 对象字面量
const env = "prod";
const link = {
dev: "Development Address",
test: "Testing Address",
prod: "Production Address"
} [env];
// link => "Production Address"
// 对象属性变量
const flag = false;
const obj = {
a: 0,
b: 1,
[flag ? "c" : "d"]: 2
};
// obj => { a: 0, b: 1, d: 2 }
/*
短网址函数
*/
function string10to62(number) {
var chars = '0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ'.split(''),
radix = chars.length,
qutient = +number,
arr = [];
do {
mod = qutient % radix;
qutient = (qutient - mod) / radix;
arr.unshift(chars[mod]);
} while (qutient);
return arr.join('');
}
function string62to10(number_code) {
var chars = '0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ',
radix = chars.length,
number_code = String(number_code),
len = number_code.length,
i = 0,
origin_number = 0;
while (i < len) {
origin_number += Math.pow(radix, i++) * chars.indexOf(number_code.charAt(len - i) || 0);
}
return origin_number;
}
console.log(string10to62(string62to10('udtrgdp')), 0)
console.log(string62to10('udtrgdp'), 1)
/**
* 文件大小格式化
* @param {*} size
*/
function formatSize(size) {
if (size < 1024) {
return size + ' bytes';
} else if (size < 1024 * 1024) {
return (size / 1024.0).toFixed(0) + ' KB';
} else if (size < 1024 * 1024 * 1024) {
return (size / 1024.0 / 1024.0).toFixed(1) + ' MB';
} else {
return (size / 1024.0 / 1024.0 / 1024.0).toFixed(1) + ' GB';
}
}
console.log(formatSize(50),
formatSize(500),
formatSize(5000),
formatSize(50000),
formatSize(50000000),
formatSize(500000000),
formatSize(5000000000),
formatSize(50000000000)
);