aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/root-helpers
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2020-08-14 17:28:54 +0200
committerChocobozzz <me@florianbigard.com>2020-08-14 17:28:54 +0200
commitb4c3c51dc874711febf43b719ca878436b31084d (patch)
tree19cc50cc8e28c9de33f001595a4c6d4ced9690cf /client/src/root-helpers
parent93e903ac165eed918986e415127d6ea9730e572c (diff)
downloadPeerTube-b4c3c51dc874711febf43b719ca878436b31084d.tar.gz
PeerTube-b4c3c51dc874711febf43b719ca878436b31084d.tar.zst
PeerTube-b4c3c51dc874711febf43b719ca878436b31084d.zip
Fix circular dependencies
Diffstat (limited to 'client/src/root-helpers')
-rw-r--r--client/src/root-helpers/bytes.ts31
-rw-r--r--client/src/root-helpers/index.ts1
2 files changed, 32 insertions, 0 deletions
diff --git a/client/src/root-helpers/bytes.ts b/client/src/root-helpers/bytes.ts
new file mode 100644
index 000000000..ec8b55faa
--- /dev/null
+++ b/client/src/root-helpers/bytes.ts
@@ -0,0 +1,31 @@
1const dictionary: Array<{ max: number; type: string }> = [
2 { max: 1024, type: 'B' },
3 { max: 1048576, type: 'KB' },
4 { max: 1073741824, type: 'MB' },
5 { max: 1.0995116e12, type: 'GB' }
6]
7
8function getBytes (value: number, precision?: number | undefined): string | number {
9 const format = dictionary.find(d => value < d.max) || dictionary[dictionary.length - 1]
10 const calc = value / (format.max / 1024)
11
12 const num = precision === undefined
13 ? calc
14 : applyPrecision(calc, precision)
15
16 return `${num} ${format.type}`
17}
18
19function applyPrecision (num: number, precision: number) {
20 if (precision <= 0) {
21 return Math.round(num)
22 }
23
24 const tho = 10 ** precision
25
26 return Math.round(num * tho) / tho
27}
28
29export {
30 getBytes
31}
diff --git a/client/src/root-helpers/index.ts b/client/src/root-helpers/index.ts
index 59468b31c..036a7677d 100644
--- a/client/src/root-helpers/index.ts
+++ b/client/src/root-helpers/index.ts
@@ -1,3 +1,4 @@
1export * from './users' 1export * from './users'
2export * from './bytes'
2export * from './peertube-web-storage' 3export * from './peertube-web-storage'
3export * from './utils' 4export * from './utils'