From d5bcf57bd821d3564850d0c92bc600a6c7619097 Mon Sep 17 00:00:00 2001 From: Patrizio Bekerle <patrizio@bekerle.com> Date: Fri, 20 Mar 2020 14:02:34 +0100 Subject: [PATCH] Add function humanFileSize --- packages/common/i18next.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/packages/common/i18next.js b/packages/common/i18next.js index 03882eec..b6908149 100644 --- a/packages/common/i18next.js +++ b/packages/common/i18next.js @@ -28,6 +28,22 @@ export function numberFormat(i18n, number, options) { return new Intl.NumberFormat(i18n.languages, options).format(number); } +export function humanFileSize(bytes, si) { + const thresh = si ? 1000 : 1024; + if(Math.abs(bytes) < thresh) { + return bytes + ' B'; + } + const units = si + ? ['kB','MB','GB','TB','PB','EB','ZB','YB'] + : ['KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB']; + let u = -1; + do { + bytes /= thresh; + ++u; + } while(Math.abs(bytes) >= thresh && u < units.length - 1); + return bytes.toFixed(1)+' '+units[u]; +} + /** * Creates a new i18next instance that is fully initialized. * -- GitLab