perf: add jsdoc for utils and format some code (#1883)

This commit is contained in:
Tuan Duong
2019-04-11 11:41:36 +09:00
committed by 花裤衩
parent 43ae8520b3
commit d0651ad2f2
11 changed files with 250 additions and 156 deletions

View File

@@ -1,6 +1,12 @@
// set function parseTime,formatTime to filter
export { parseTime, formatTime } from '@/utils'
/**
* Show plural label if time is plural number
* @param {number} time
* @param {string} label
* @return {string}
*/
function pluralize(time, label) {
if (time === 1) {
return time + label
@@ -8,6 +14,9 @@ function pluralize(time, label) {
return time + label + 's'
}
/**
* @param {number} time
*/
export function timeAgo(time) {
const between = Date.now() / 1000 - Number(time)
if (between < 3600) {
@@ -19,7 +28,12 @@ export function timeAgo(time) {
}
}
/* 数字 格式化*/
/**
* Number formatting
* like 10000 => 10k
* @param {number} num
* @param {number} digits
*/
export function numberFormatter(num, digits) {
const si = [
{ value: 1E18, symbol: 'E' },
@@ -37,6 +51,10 @@ export function numberFormatter(num, digits) {
return num.toString()
}
/**
* 10000 => "10,000"
* @param {number} num
*/
export function toThousandFilter(num) {
return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
}