forked from sunoj/teaclub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
67 lines (66 loc) · 1.93 KB
/
utils.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
import { DateTime } from 'luxon'
export const rand = function (n) {
return (Math.floor(Math.random() * n + 1));
}
export const price = function (price) {
return Number(Number(price).toFixed(2))
}
export const getSetting = function (settingKey, defaultValue) {
let setting = localStorage.getItem(settingKey)
if (setting) {
try {
setting = JSON.parse(setting)
} catch (error) { }
}
return setting ? setting : defaultValue
}
export const saveSetting = function (settingKey, value) {
return localStorage.setItem(settingKey, JSON.stringify(value))
}
export const readableTime = function (dateTime) {
if (DateTime.local().hasSame(dateTime, 'day')) {
return '今天 ' + dateTime.setLocale('zh-cn').toLocaleString(DateTime.TIME_SIMPLE)
}
if (DateTime.local().hasSame(dateTime.plus({ days: 1 }), 'day')) {
return '昨天 ' + dateTime.setLocale('zh-cn').toLocaleString(DateTime.TIME_SIMPLE)
}
return dateTime.setLocale('zh-cn').toFormat('f')
}
export const versionCompare = function (v1, v2, options) {
var lexicographical = options && options.lexicographical,
zeroExtend = options && options.zeroExtend,
v1parts = v1.split('.'),
v2parts = v2.split('.');
function isValidPart(x) {
return (lexicographical ? /^\d+[A-Za-z]*$/ : /^\d+$/).test(x);
}
if (!v1parts.every(isValidPart) || !v2parts.every(isValidPart)) {
return NaN;
}
if (zeroExtend) {
while (v1parts.length < v2parts.length) v1parts.push("0");
while (v2parts.length < v1parts.length) v2parts.push("0");
}
if (!lexicographical) {
v1parts = v1parts.map(Number);
v2parts = v2parts.map(Number);
}
for (var i = 0; i < v1parts.length; ++i) {
if (v2parts.length == i) {
return 1;
}
if (v1parts[i] == v2parts[i]) {
continue;
}
else if (v1parts[i] > v2parts[i]) {
return 1;
}
else {
return -1;
}
}
if (v1parts.length != v2parts.length) {
return -1;
}
return 0;
}