fe-drone-ci/src/filters/index.js

43 lines
1.1 KiB
JavaScript
Raw Normal View History

// set function parseTime,formatTime to filter
export { parseTime, formatTime } from '@/utils'
2017-04-18 07:09:13 +00:00
function pluralize(time, label) {
if (time === 1) {
return time + label
}
return time + label + 's'
}
2017-11-17 06:57:39 +00:00
2017-04-18 07:09:13 +00:00
export function timeAgo(time) {
2017-08-22 07:43:34 +00:00
const between = Date.now() / 1000 - Number(time)
2017-04-18 07:09:13 +00:00
if (between < 3600) {
return pluralize(~~(between / 60), ' minute')
} else if (between < 86400) {
return pluralize(~~(between / 3600), ' hour')
} else {
return pluralize(~~(between / 86400), ' day')
}
}
/* 数字 格式化*/
export function numberFormatter(num, digits) {
2017-04-18 07:09:13 +00:00
const si = [
2017-10-31 08:04:30 +00:00
{ value: 1E18, symbol: 'E' },
{ value: 1E15, symbol: 'P' },
{ value: 1E12, symbol: 'T' },
{ value: 1E9, symbol: 'G' },
{ value: 1E6, symbol: 'M' },
{ value: 1E3, symbol: 'k' }
2017-08-22 07:43:34 +00:00
]
2017-04-18 07:09:13 +00:00
for (let i = 0; i < si.length; i++) {
if (num >= si[i].value) {
2017-08-22 07:43:34 +00:00
return (num / si[i].value + 0.1).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol
2017-04-18 07:09:13 +00:00
}
}
2017-08-22 07:43:34 +00:00
return num.toString()
2017-04-18 07:09:13 +00:00
}
2018-09-03 07:03:00 +00:00
export function toThousandFilter(num) {
2017-08-22 07:43:34 +00:00
return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
2017-04-18 07:09:13 +00:00
}