Ver código fonte

utils: prettyBytes 1000 --> 1024 (#1732)

* Replace prettier-bytes with its copy, only use 1024 instead of 1000 to justify KB vs kB

* Add tests

* added License

* pretty-bytes to @uppy/utils/lib/prettyBytes everywhere
Artur Paikin 5 anos atrás
pai
commit
7873f057ca

+ 1 - 1
packages/@uppy/core/src/index.js

@@ -2,7 +2,7 @@ const Translator = require('@uppy/utils/lib/Translator')
 const ee = require('namespace-emitter')
 const cuid = require('cuid')
 const throttle = require('lodash.throttle')
-const prettyBytes = require('prettier-bytes')
+const prettyBytes = require('@uppy/utils/lib/prettyBytes')
 const match = require('mime-match')
 const DefaultStore = require('@uppy/store-default')
 const getFileType = require('@uppy/utils/lib/getFileType')

+ 1 - 1
packages/@uppy/core/src/index.test.js

@@ -1,6 +1,6 @@
 const fs = require('fs')
 const path = require('path')
-const prettyBytes = require('prettier-bytes')
+const prettyBytes = require('@uppy/utils/lib/prettyBytes')
 const Core = require('./index')
 const Plugin = require('./Plugin')
 const AcquirerPlugin1 = require('../../../../test/mocks/acquirerPlugin1')

+ 1 - 1
packages/@uppy/dashboard/src/components/FileItem/FileInfo/index.js

@@ -1,5 +1,5 @@
 const { h } = require('preact')
-const prettyBytes = require('prettier-bytes')
+const prettyBytes = require('@uppy/utils/lib/prettyBytes')
 const truncateString = require('../../../utils/truncateString')
 
 const renderAcquirerIcon = (acquirer, props) =>

+ 1 - 1
packages/@uppy/golden-retriever/src/IndexedDBStore.js

@@ -1,4 +1,4 @@
-const prettyBytes = require('prettier-bytes')
+const prettyBytes = require('@uppy/utils/lib/prettyBytes')
 const indexedDB = typeof window !== 'undefined' &&
   (window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.OIndexedDB || window.msIndexedDB)
 

+ 1 - 1
packages/@uppy/status-bar/src/StatusBar.js

@@ -1,7 +1,7 @@
 const throttle = require('lodash.throttle')
 const classNames = require('classnames')
 const statusBarStates = require('./StatusBarStates')
-const prettyBytes = require('prettier-bytes')
+const prettyBytes = require('@uppy/utils/lib/prettyBytes')
 const prettyETA = require('@uppy/utils/lib/prettyETA')
 const { h } = require('preact')
 

+ 34 - 0
packages/@uppy/utils/src/prettyBytes.js

@@ -0,0 +1,34 @@
+// Adapted from https://github.com/Flet/prettier-bytes/
+// Changing 1000 bytes to 1024, so we can keep uppercase KB vs kB
+// ISC License (c) Dan Flettre https://github.com/Flet/prettier-bytes/blob/master/LICENSE
+
+module.exports = prettierBytes
+
+function prettierBytes (num) {
+  if (typeof num !== 'number' || isNaN(num)) {
+    throw new TypeError('Expected a number, got ' + typeof num)
+  }
+
+  var neg = num < 0
+  var units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
+
+  if (neg) {
+    num = -num
+  }
+
+  if (num < 1) {
+    return (neg ? '-' : '') + num + ' B'
+  }
+
+  var exponent = Math.min(Math.floor(Math.log(num) / Math.log(1024)), units.length - 1)
+  num = Number(num / Math.pow(1024, exponent))
+  var unit = units[exponent]
+
+  if (num >= 10 || num % 1 === 0) {
+    // Do not show decimals when the number is two-digit, or if the number has no
+    // decimal component.
+    return (neg ? '-' : '') + num.toFixed(0) + ' ' + unit
+  } else {
+    return (neg ? '-' : '') + num.toFixed(1) + ' ' + unit
+  }
+}

+ 40 - 0
packages/@uppy/utils/src/prettyBytes.test.js

@@ -0,0 +1,40 @@
+const prettyBytes = require('./prettyBytes')
+
+const testData = [
+  [2, '2 B'],
+  [9, '9 B'],
+  [25, '25 B'],
+  [235, '235 B'],
+  [2335, '2.3 KB'],
+  [23552, '23 KB'],
+  [235520, '230 KB'],
+  [2355520, '2.2 MB'],
+  [23555520, '22 MB'],
+  [235555520, '225 MB'],
+  [2355555520, '2.2 GB'],
+  [23555555520, '22 GB'],
+  [235556555520, '219 GB'],
+  [2355556655520, '2.1 TB'],
+  [23555566655520, '21 TB'],
+  [235555566665520, '214 TB']
+]
+
+describe('prettyBytes', () => {
+  it('should convert the specified number of bytes to a human-readable string like 236 MB', () => {
+    testData.forEach(function (data) {
+      expect(prettyBytes(data[0])).toEqual(data[1])
+    })
+  })
+
+  it('throws on non-number', () => {
+    expect(() => {
+      prettyBytes('this is a string')
+    }).toThrow()
+  })
+
+  it('throws on NaN', () => {
+    expect(() => {
+      prettyBytes(NaN)
+    }).toThrow()
+  })
+})