From 0bc85d27f8b4cf91e79f76cf2be4814f322921fe Mon Sep 17 00:00:00 2001 From: Greg Look Date: Wed, 14 Oct 2015 12:56:31 -0700 Subject: [PATCH] Group rounding and fixed number functions. --- public/app/components/kbn.js | 79 +++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 38 deletions(-) diff --git a/public/app/components/kbn.js b/public/app/components/kbn.js index be1f37dfacd..0fe0320feb8 100644 --- a/public/app/components/kbn.js +++ b/public/app/components/kbn.js @@ -199,30 +199,6 @@ function($, _) { return new RegExp(match[1], match[2]); }; - kbn.formatFuncCreator = function(factor, extArray) { - return function(size, decimals, scaledDecimals) { - if (size === null) { - return ""; - } - - var steps = 0; - var limit = extArray.length; - - while (Math.abs(size) >= factor) { - steps++; - size /= factor; - - if (steps >= limit) { return "NA"; } - } - - if (steps > 0 && scaledDecimals !== null) { - decimals = scaledDecimals + (3 * steps); - } - - return kbn.toFixed(size, decimals) + extArray[steps]; - }; - }; - kbn.toFixed = function(value, decimals) { if (value === null) { return ""; @@ -249,6 +225,46 @@ function($, _) { return formatted; }; + kbn.toFixedScaled = function(value, decimals, scaledDecimals, additionalDecimals, ext) { + if (scaledDecimals === null) { + return kbn.toFixed(value, decimals) + ext; + } else { + return kbn.toFixed(value, scaledDecimals + additionalDecimals) + ext; + } + }; + + kbn.roundValue = function (num, decimals) { + if (num === null) { return null; } + var n = Math.pow(10, decimals); + return Math.round((n * num).toFixed(decimals)) / n; + }; + + ///// FORMAT FUNCTION CONSTRUCTORS ///// + + kbn.formatFuncCreator = function(factor, extArray) { + return function(size, decimals, scaledDecimals) { + if (size === null) { + return ""; + } + + var steps = 0; + var limit = extArray.length; + + while (Math.abs(size) >= factor) { + steps++; + size /= factor; + + if (steps >= limit) { return "NA"; } + } + + if (steps > 0 && scaledDecimals !== null) { + decimals = scaledDecimals + (3 * steps); + } + + return kbn.toFixed(size, decimals) + extArray[steps]; + }; + }; + ///// VALUE FORMATS ///// // Dimensionless Units @@ -257,6 +273,7 @@ function($, _) { kbn.valueFormats.ppm = function(value, decimals) { return kbn.toFixed(value, decimals) + ' ppm'; }; kbn.valueFormats.percent = function(size, decimals) { + if (size == null) { return ""; } return kbn.toFixed(size, decimals) + '%'; }; @@ -300,20 +317,6 @@ function($, _) { // Time kbn.valueFormats.hertz = kbn.formatFuncCreator(1000, [' Hz', ' kHz', ' MHz', ' GHz', ' THz', ' PHz', ' EHz', ' ZHz', ' YHz']); - kbn.roundValue = function (num, decimals) { - if (num === null) { return null; } - var n = Math.pow(10, decimals); - return Math.round((n * num).toFixed(decimals)) / n; - }; - - kbn.toFixedScaled = function(value, decimals, scaledDecimals, additionalDecimals, ext) { - if (scaledDecimals === null) { - return kbn.toFixed(value, decimals) + ext; - } else { - return kbn.toFixed(value, scaledDecimals + additionalDecimals) + ext; - } - }; - kbn.valueFormats.ms = function(size, decimals, scaledDecimals) { if (size === null) { return ""; }