tools.js 579 B

1234567891011121314151617181920212223
  1. export function bytesToSize (bytes) {
  2. const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']
  3. if (bytes === 0) return 'n/a'
  4. const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)))
  5. if (i === 0) return bytes + ' ' + sizes[i]
  6. return (bytes / Math.pow(1024, i)).toFixed(1) + ' ' + sizes[i]
  7. }
  8. export function vaildData (key, defaultValue = Boolean) {
  9. if (key) {
  10. return true
  11. } else {
  12. return defaultValue
  13. }
  14. }
  15. export function objectToParams (objs) {
  16. const query = []
  17. for (const k in objs) {
  18. query.push(k + '=' + objs[k])
  19. }
  20. return query.join('&')
  21. }