From e58ccc5a831f7f4f51f18293101f8a7922c56323 Mon Sep 17 00:00:00 2001 From: bmundt Date: Thu, 7 Jul 2016 02:54:51 -0400 Subject: [PATCH] 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 --- public/app/core/utils/kbn.js | 19 ++++++++++++ public/test/core/utils/kbn_specs.js | 46 +++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/public/app/core/utils/kbn.js b/public/app/core/utils/kbn.js index fbbab141614..29cd7c49890 100644 --- a/public/app/core/utils/kbn.js +++ b/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' }, ] }, { diff --git a/public/test/core/utils/kbn_specs.js b/public/test/core/utils/kbn_specs.js index 2c828235c55..e851e9b82f5 100644 --- a/public/test/core/utils/kbn_specs.js +++ b/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'); + }); + }); });