* added hex units

* deleted blank line, to fix style error

* added unit tests

* updated unit tests and changed the way it handles negative hex0x
This commit is contained in:
bmundt 2016-07-07 02:54:51 -04:00 committed by Torkel Ödegaard
parent 60abe75615
commit e58ccc5a83
2 changed files with 65 additions and 0 deletions

View File

@ -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' },
]
},
{

View File

@ -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');
});
});
});