2a245bffc5e66a7b1c21f75f4cb9b5d447852527.svn-base 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // import parseTime, formatTime and set to filter
  2. export { parseTime, formatTime } from '@/utils'
  3. /**
  4. * Show plural label if time is plural number
  5. * @param {number} time
  6. * @param {string} label
  7. * @return {string}
  8. */
  9. function pluralize(time, label) {
  10. if (time === 1) {
  11. return time + label
  12. }
  13. return time + label + 's'
  14. }
  15. /**
  16. * @param {number} time
  17. */
  18. export function timeAgo(time) {
  19. const between = Date.now() / 1000 - Number(time)
  20. if (between < 3600) {
  21. return pluralize(~~(between / 60), ' minute')
  22. } else if (between < 86400) {
  23. return pluralize(~~(between / 3600), ' hour')
  24. } else {
  25. return pluralize(~~(between / 86400), ' day')
  26. }
  27. }
  28. /**
  29. * Number formatting
  30. * like 10000 => 10k
  31. * @param {number} num
  32. * @param {number} digits
  33. */
  34. export function numberFormatter(num, digits) {
  35. const si = [
  36. { value: 1E18, symbol: 'E' },
  37. { value: 1E15, symbol: 'P' },
  38. { value: 1E12, symbol: 'T' },
  39. { value: 1E9, symbol: 'G' },
  40. { value: 1E6, symbol: 'M' },
  41. { value: 1E3, symbol: 'k' }
  42. ]
  43. for (let i = 0; i < si.length; i++) {
  44. if (num >= si[i].value) {
  45. return (num / si[i].value + 0.1).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol
  46. }
  47. }
  48. return num.toString()
  49. }
  50. /**
  51. * 10000 => "10,000"
  52. * @param {number} num
  53. */
  54. export function toThousandFilter(num) {
  55. return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
  56. }
  57. /**
  58. * Upper case first char
  59. * @param {String} string
  60. */
  61. export function uppercaseFirst(string) {
  62. return string.charAt(0).toUpperCase() + string.slice(1)
  63. }