From 88d33553ee1aa2fac99ec90bc185486a953c3b2a Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Thu, 23 Apr 2015 18:42:11 +0200 Subject: [PATCH] feature #3748: Add humanize util --- src/sunstone/public/app/utils/humanize.js | 55 +++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/sunstone/public/app/utils/humanize.js diff --git a/src/sunstone/public/app/utils/humanize.js b/src/sunstone/public/app/utils/humanize.js new file mode 100644 index 0000000000..915d035155 --- /dev/null +++ b/src/sunstone/public/app/utils/humanize.js @@ -0,0 +1,55 @@ +define(function(require) { + /* + Returns a human readable size in Kilo, Mega, Giga or Tera bytes + if no from_bytes, assumes value comes in Ks + */ + var _size = function(value, from_bytes, sufix) { + if (typeof(value) === "undefined") { + value = 0; + } + var binarySufix = ["", "K", "M", "G", "T"]; + + var i = from_bytes ? 0 : 1; + while (value >= 1024 && i < 4) { + value = value / 1024; + i++; + } + value = Math.round(value * 10) / 10; + + if (value - Math.round(value) == 0) { + value = Math.round(value); + } + + if (sufix == undefined) { + sufix = "B"; + } + + var st = value + binarySufix[i] + sufix; + return st; + } + + var _sizeFromMB = function(value) { + if (typeof(value) === "undefined") { + value = 0; + } + var binarySufix = ["MB", "GB", "TB"]; + var i = 0; + while (value >= 1024 && i < 2) { + value = value / 1024; + i++; + } + value = Math.round(value * 10) / 10; + + if (value - Math.round(value) == 0) { + value = Math.round(value); + } + + var st = value + binarySufix[i]; + return st; + } + + return { + 'size': _size, + 'sizeFromMB': _sizeFromMB + } +})