[Kimchi-devel] [PATCH] [Wok 1/3] Introduced converters in formatMeasurement

pkulkark at in.ibm.com pkulkark at in.ibm.com
Fri May 13 14:13:47 UTC 2016


From: Pooja Kulkarni <pkulkark at linux.vnet.ibm.com>

1)Added converters in formatMeasurement
which enables formatting of data to be
displayed.
2)Renamed datetimeLocaleConverters to
localeConverters in order to make it
generic and added number converter.

Signed-off-by: Pooja Kulkarni <pkulkark at linux.vnet.ibm.com>
---
 ui/js/src/wok.utils.js | 33 +++++++++++++++++++++++++--------
 1 file changed, 25 insertions(+), 8 deletions(-)

diff --git a/ui/js/src/wok.utils.js b/ui/js/src/wok.utils.js
index 74b623c..81e7c4f 100644
--- a/ui/js/src/wok.utils.js
+++ b/ui/js/src/wok.utils.js
@@ -140,7 +140,13 @@ wok.changetoProperUnit = function(numOrg, digits, base) {
             return new Formatted(number, unit);
         }
 
-        var n_locale = settings['locale'] || null;
+        // Introduce converter to format data.
+        // Converter's to function will be used for formatting.
+        // if not passed then 'to' formatting will not be done.
+        // e.g. formatMeasurement( 2,
+        //          { .. , converter: wok.localeConverters["number-locale-converter"]})
+        var converter = settings['converter'] || null;
+        var converter_to_fun = converter?converter['to']:''
         var fixed = settings['fixed'];
 
         var unitMapping = unitBaseMapping[base];
@@ -154,14 +160,14 @@ wok.changetoProperUnit = function(numOrg, digits, base) {
 
             var formatted = number / startingValue;
             formatted = fixed ? formatted.toFixed(fixed) : formatted;
-            formatted = n_locale ? Number(formatted).toLocaleString(n_locale) : Number(formatted).toLocaleString()
+            formatted = converter_to_fun ? converter_to_fun(Number(formatted)) : formatted;
             return new Formatted(formatted, suffix + unit);
         }
 
         formatted = fixed ? number.toFixed(fixed) : number;
-        /* format the formatted number as per settings's locale, if not present format it with default locale. */
-        formatted_locale = n_locale ? Number(formatted).toLocaleString(n_locale) : Number(formatted).toLocaleString()
-        return new Formatted(formatted_locale, unit);
+        /* format the formatted number as per settings's converter, if not present return as it is. */
+        formatted_val = converter_to_fun ? converter_to_fun(Number(formatted)) : formatted;
+        return new Formatted(formatted_val, unit);
     };
 
     wok.formatMeasurement = format;
@@ -237,7 +243,13 @@ wok.timeLocaleConverter = function timeLocaleConverter(time_string, locale){
 }
 
 
-wok.dateTimeLocaleConverters = {
+wok.numberLocaleConverter = function numberConverter(number, locale){
+     number = (typeof(number)==='number')?number.toLocaleString(wok.lang.get_locale()):number;
+     return number;
+}
+
+
+wok.localeConverters = {
        "date-locale-converter": {
            to: function(date){
               return wok.dateLocaleConverter(date, wok.lang.get_locale());
@@ -252,5 +264,10 @@ wok.dateTimeLocaleConverters = {
             to: function(datetime){
                 return wok.datetimeLocaleConverter(datetime,  wok.lang.get_locale());
             }
-       }
- }
+       },
+       "number-locale-converter":{
+           to: function(number){
+              return wok.numberLocaleConverter(number, wok.lang.get_locale());
+           }
+      }
+}
-- 
2.1.0




More information about the Kimchi-devel mailing list