Skip to content
Snippets Groups Projects
Forked from funkwhale / funkwhale
6065 commits behind the upstream repository.
filters.js 1.46 KiB
import Vue from 'vue'

import moment from 'moment'

export function truncate (str, max, ellipsis) {
  max = max || 100
  ellipsis = ellipsis || '…'
  if (str.length <= max) {
    return str
  }
  return str.slice(0, max) + ellipsis
}

Vue.filter('truncate', truncate)

export function ago (date, locale) {
  locale = locale || 'en'
  const m = moment(date)
  m.locale(locale)
  return m.fromNow()
}

Vue.filter('ago', ago)

export function secondsToObject (seconds) {
  let m = moment.duration(seconds, 'seconds')
  return {
    minutes: m.minutes(),
    hours: parseInt(m.asHours())
  }
}

Vue.filter('secondsToObject', secondsToObject)

export function momentFormat (date, format) {
  format = format || 'lll'
  return moment(date).format(format)
}

Vue.filter('moment', momentFormat)

export function year (date) {
  return moment(date).year()
}

Vue.filter('year', year)

export function capitalize (str) {
  return str.charAt(0).toUpperCase() + str.slice(1)
}

Vue.filter('capitalize', capitalize)

export function humanSize (bytes) {
  let si = true
  var thresh = si ? 1000 : 1024
  if (Math.abs(bytes) < thresh) {
    return bytes + ' B'
  }
  var units = si
    ? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
    : ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']
  var u = -1
  do {
    bytes /= thresh
    ++u
  } while (Math.abs(bytes) >= thresh && u < units.length - 1)
  return bytes.toFixed(1) + ' ' + units[u]
}
Vue.filter('humanSize', humanSize)

export default {}