Skip to content

Commit 66613f0

Browse files
committed
perf[filter]: remove duplicate code PanJiaChen#661
1 parent 6795c26 commit 66613f0

File tree

1 file changed

+4
-66
lines changed

1 file changed

+4
-66
lines changed

src/filters/index.js

Lines changed: 4 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// set function parseTime,formatTime to filter
2+
export { parseTime, formatTime } from '@/utils'
3+
14
function pluralize(time, label) {
25
if (time === 1) {
36
return time + label
@@ -16,67 +19,8 @@ export function timeAgo(time) {
1619
}
1720
}
1821

19-
export function parseTime(time, cFormat) {
20-
if (arguments.length === 0) {
21-
return null
22-
}
23-
24-
if ((time + '').length === 10) {
25-
time = +time * 1000
26-
}
27-
28-
const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
29-
let date
30-
if (typeof time === 'object') {
31-
date = time
32-
} else {
33-
date = new Date(parseInt(time))
34-
}
35-
const formatObj = {
36-
y: date.getFullYear(),
37-
m: date.getMonth() + 1,
38-
d: date.getDate(),
39-
h: date.getHours(),
40-
i: date.getMinutes(),
41-
s: date.getSeconds(),
42-
a: date.getDay()
43-
}
44-
const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
45-
let value = formatObj[key]
46-
if (key === 'a') return ['一', '二', '三', '四', '五', '六', '日'][value - 1]
47-
if (result.length > 0 && value < 10) {
48-
value = '0' + value
49-
}
50-
return value || 0
51-
})
52-
return time_str
53-
}
54-
55-
export function formatTime(time, option) {
56-
time = +time * 1000
57-
const d = new Date(time)
58-
const now = Date.now()
59-
60-
const diff = (now - d) / 1000
61-
62-
if (diff < 30) {
63-
return '刚刚'
64-
} else if (diff < 3600) { // less 1 hour
65-
return Math.ceil(diff / 60) + '分钟前'
66-
} else if (diff < 3600 * 24) {
67-
return Math.ceil(diff / 3600) + '小时前'
68-
} else if (diff < 3600 * 24 * 2) {
69-
return '1天前'
70-
}
71-
if (option) {
72-
return parseTime(time, option)
73-
} else {
74-
return d.getMonth() + 1 + '月' + d.getDate() + '日' + d.getHours() + '时' + d.getMinutes() + '分'
75-
}
76-
}
77-
7822
/* 数字 格式化*/
79-
export function nFormatter(num, digits) {
23+
export function numberFormatter(num, digits) {
8024
const si = [
8125
{ value: 1E18, symbol: 'E' },
8226
{ value: 1E15, symbol: 'P' },
@@ -93,12 +37,6 @@ export function nFormatter(num, digits) {
9337
return num.toString()
9438
}
9539

96-
export function html2Text(val) {
97-
const div = document.createElement('div')
98-
div.innerHTML = val
99-
return div.textContent || div.innerText
100-
}
101-
10240
export function toThousandslsFilter(num) {
10341
return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
10442
}

0 commit comments

Comments
 (0)