OSDN Git Service

add the byte unit.
authorZhiting Lin <zlin035@uottawa.ca>
Fri, 7 Dec 2018 07:45:08 +0000 (15:45 +0800)
committerZhiting Lin <zlin035@uottawa.ca>
Fri, 7 Dec 2018 07:45:08 +0000 (15:45 +0800)
src/features/peers/components/ListItem.jsx
src/utility/math.js

index 177a6b1..87e3fa6 100644 (file)
@@ -1,5 +1,6 @@
 import React from 'react'
 import {withNamespaces} from 'react-i18next'
+import { formatBytes } from 'utility/math'
 
 class ListItem extends React.Component {
   render() {
@@ -11,7 +12,7 @@ class ListItem extends React.Component {
         <td><code>{item.height}</code></td>
         <td><code>{item.ping}</code></td>
         <td><code>{item.duration}</code></td>
-        <td>{ item.totalSent+ item.totalReceived }</td>
+        <td><code>{ formatBytes(item.totalSent+ item.totalReceived, 0) }</code></td>
         <td>
           <button className='btn btn-link' onClick={() => this.props.disconnect(item.peerId)}>
             {t('peers.disconnect')}
index 56b2c12..92c2d0c 100644 (file)
@@ -5,3 +5,12 @@ export const sum = function(items, prop){
     return a + Number(_.get(b,prop))
   }, 0)
 }
+
+export const formatBytes = function(bytes,decimals) {
+  if (bytes == 0) return '0 Bytes'
+  let k = 1024,
+    dm = decimals <= 0 ? 0 : decimals || 2,
+    sizes = ['B', 'KB', 'MB', 'GB', 'TB'],
+    i = Math.floor(Math.log(bytes) / Math.log(k))
+  return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]
+}