diff options
author | Filipa Lacerda <filipa@gitlab.com> | 2017-11-13 19:51:14 +0000 |
---|---|---|
committer | Filipa Lacerda <filipa@gitlab.com> | 2017-11-13 19:51:14 +0000 |
commit | 476b28a1a28cfbd692c1ccd06f69097cc553e2ef (patch) | |
tree | 1d8b51e2123fafe52af063bc7b800b072be04f64 /app/assets/javascripts/lib/utils/number_utils.js | |
parent | c8d594e04e2d12a1c0d68ed80998f0b42f0fc8c5 (diff) | |
download | gitlab-ce-476b28a1a28cfbd692c1ccd06f69097cc553e2ef.tar.gz |
Formats bytes to human reabale number in registry table40092-fix-cluster-size
Diffstat (limited to 'app/assets/javascripts/lib/utils/number_utils.js')
-rw-r--r-- | app/assets/javascripts/lib/utils/number_utils.js | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/app/assets/javascripts/lib/utils/number_utils.js b/app/assets/javascripts/lib/utils/number_utils.js index 917a45eb06b..a02c79b787e 100644 --- a/app/assets/javascripts/lib/utils/number_utils.js +++ b/app/assets/javascripts/lib/utils/number_utils.js @@ -52,3 +52,31 @@ export function bytesToKiB(number) { export function bytesToMiB(number) { return number / (BYTES_IN_KIB * BYTES_IN_KIB); } + +/** + * Utility function that calculates GiB of the given bytes. + * @param {Number} number + * @returns {Number} + */ +export function bytesToGiB(number) { + return number / (BYTES_IN_KIB * BYTES_IN_KIB * BYTES_IN_KIB); +} + +/** + * Port of rails number_to_human_size + * Formats the bytes in number into a more understandable + * representation (e.g., giving it 1500 yields 1.5 KB). + * + * @param {Number} size + * @returns {String} + */ +export function numberToHumanSize(size) { + if (size < BYTES_IN_KIB) { + return `${size} bytes`; + } else if (size < BYTES_IN_KIB * BYTES_IN_KIB) { + return `${bytesToKiB(size).toFixed(2)} KiB`; + } else if (size < BYTES_IN_KIB * BYTES_IN_KIB * BYTES_IN_KIB) { + return `${bytesToMiB(size).toFixed(2)} MiB`; + } + return `${bytesToGiB(size).toFixed(2)} GiB`; +} |