فهرست منبع

Hex units (#5530)

* added hex units

* deleted blank line, to fix style error

* added unit tests

* updated unit tests and changed the way it handles negative hex0x
bmundt 9 سال پیش
والد
کامیت
e58ccc5a83
2فایلهای تغییر یافته به همراه65 افزوده شده و 0 حذف شده
  1. 19 0
      public/app/core/utils/kbn.js
  2. 46 0
      public/test/core/utils/kbn_specs.js

+ 19 - 0
public/app/core/utils/kbn.js

@@ -368,6 +368,23 @@ function($, _, moment) {
     return kbn.toFixed(100*size, decimals) + '%';
   };
 
+  /* Formats the value to hex. Uses float if specified decimals are not 0.
+   * There are two options, one with 0x, and one without */
+
+  kbn.valueFormats.hex = function(value, decimals) {
+    if (value == null) { return ""; }
+    return parseFloat(kbn.toFixed(value, decimals)).toString(16).toUpperCase();
+  };
+
+  kbn.valueFormats.hex0x = function(value, decimals) {
+    if (value == null) { return ""; }
+    var hexString = kbn.valueFormats.hex(value, decimals);
+    if (hexString.substring(0,1) === "-") {
+      return "-0x" + hexString.substring(1);
+    }
+    return "0x" + hexString;
+  };
+
   // Currencies
   kbn.valueFormats.currencyUSD = kbn.formatBuilders.currency('$');
   kbn.valueFormats.currencyGBP = kbn.formatBuilders.currency('£');
@@ -617,6 +634,8 @@ function($, _, moment) {
           {text: 'Humidity (%H)',     value: 'humidity'   },
           {text: 'ppm',               value: 'ppm'        },
           {text: 'decibel',           value: 'dB'         },
+          {text: 'hexadecimal (0x)',  value: 'hex0x'      },
+          {text: 'hexadecimal',       value: 'hex'        },
         ]
       },
       {

+ 46 - 0
public/test/core/utils/kbn_specs.js

@@ -161,4 +161,50 @@ define([
       expect(str).to.be('15ms');
     });
   });
+
+  describe('hex', function() {
+      it('positive integer', function() {
+	var str = kbn.valueFormats.hex(100, 0);
+	expect(str).to.be('64');
+      });
+      it('negative integer', function() {
+	var str = kbn.valueFormats.hex(-100, 0);
+	expect(str).to.be('-64');
+      });
+      it('null', function() {
+	var str = kbn.valueFormats.hex(null, 0);
+	expect(str).to.be('');
+      });
+      it('positive float', function() {
+	var str = kbn.valueFormats.hex(50.52, 1);
+	expect(str).to.be('32.8');
+      }); 
+      it('negative float', function() {
+	var str = kbn.valueFormats.hex(-50.333, 2);
+	expect(str).to.be('-32.547AE147AE14');
+      });
+  });
+
+  describe('hex 0x', function() {
+    it('positive integeter', function() {
+      var str = kbn.valueFormats.hex0x(7999,0);
+      expect(str).to.be('0x1F3F');
+    });
+    it('negative integer', function() {
+      var str = kbn.valueFormats.hex0x(-584,0);
+      expect(str).to.be('-0x248');
+    });
+    it('null', function() {
+      var str = kbn.valueFormats.hex0x(null, 0);
+      expect(str).to.be('');
+    });
+    it('positive float', function() {
+      var str = kbn.valueFormats.hex0x(74.443, 3);
+      expect(str).to.be('0x4A.716872B020C4');
+    });
+    it('negative float', function() {
+      var str = kbn.valueFormats.hex0x(-65.458, 1);
+      expect(str).to.be('-0x41.8');
+    });
+  });
 });