diff --git a/packages/grafana-data/src/valueFormats/symbolFormatters.test.ts b/packages/grafana-data/src/valueFormats/symbolFormatters.test.ts index ac0fa6ef38c..278caf1ebb6 100644 --- a/packages/grafana-data/src/valueFormats/symbolFormatters.test.ts +++ b/packages/grafana-data/src/valueFormats/symbolFormatters.test.ts @@ -1,4 +1,4 @@ -import { currency } from './symbolFormatters'; +import { currency, SIPrefix } from './symbolFormatters'; describe('currency', () => { const symbol = '@'; @@ -8,6 +8,7 @@ describe('currency', () => { it.each` value | expectedSuffix | expectedText + ${0} | ${''} | ${'0'} ${999} | ${''} | ${'999'} ${1000} | ${'K'} | ${'1'} ${1000000} | ${'M'} | ${'1'} @@ -33,6 +34,7 @@ describe('currency', () => { it.each` value | expectedSuffix | expectedText + ${0} | ${'@'} | ${'0'} ${999} | ${'@'} | ${'999'} ${1000} | ${'K@'} | ${'1'} ${1000000} | ${'M@'} | ${'1'} @@ -53,3 +55,30 @@ describe('currency', () => { }); }); }); + +describe('SIPrefix', () => { + const symbol = 'V'; + const fmtFunc = SIPrefix(symbol); + + it.each` + value | expectedSuffix | expectedText + ${0} | ${' V'} | ${'0'} + ${999} | ${' V'} | ${'999'} + ${1000} | ${' kV'} | ${'1'} + ${1000000} | ${' MV'} | ${'1'} + ${1000000000} | ${' GV'} | ${'1'} + ${1000000000000} | ${' TV'} | ${'1'} + ${1000000000000000} | ${' PV'} | ${'1'} + ${-1000000000000} | ${' TV'} | ${'-1'} + ${-1000000000} | ${' GV'} | ${'-1'} + ${-1000000} | ${' MV'} | ${'-1'} + ${-1000} | ${' kV'} | ${'-1'} + ${-999} | ${' V'} | ${'-999'} + `('when called with value:{$value}', ({ value, expectedSuffix, expectedText }) => { + const { prefix, suffix, text } = fmtFunc(value); + + expect(prefix).toEqual(undefined); + expect(suffix).toEqual(expectedSuffix); + expect(text).toEqual(expectedText); + }); +}); diff --git a/packages/grafana-data/src/valueFormats/valueFormats.test.ts b/packages/grafana-data/src/valueFormats/valueFormats.test.ts index 1ad66c8fdff..e3281cc7b79 100644 --- a/packages/grafana-data/src/valueFormats/valueFormats.test.ts +++ b/packages/grafana-data/src/valueFormats/valueFormats.test.ts @@ -54,6 +54,7 @@ describe('valueFormats', () => { ${'b'} | ${0} | ${1532.82} | ${'1533 b'} ${'prefix:b'} | ${undefined} | ${1532.82} | ${'b1533'} ${'suffix:d'} | ${undefined} | ${1532.82} | ${'1533 d'} + ${'si:µF'} | ${2} | ${0} | ${'0.00 µF'} ${'si:µF'} | ${2} | ${1234} | ${'1.23 mF'} ${'si:µF'} | ${2} | ${1234000000} | ${'1.23 kF'} ${'si:µF'} | ${2} | ${1234000000000000} | ${'1.23 GF'} diff --git a/packages/grafana-data/src/valueFormats/valueFormats.ts b/packages/grafana-data/src/valueFormats/valueFormats.ts index b150afa7da5..4920fe2814c 100644 --- a/packages/grafana-data/src/valueFormats/valueFormats.ts +++ b/packages/grafana-data/src/valueFormats/valueFormats.ts @@ -143,7 +143,7 @@ export function scaledUnits(factor: number, extArray: string[], offset = 0): Val return { text: size.toLocaleString() }; } - const siIndex = Math.floor(logb(factor, Math.abs(size))); + const siIndex = size === 0 ? 0 : Math.floor(logb(factor, Math.abs(size))); const suffix = extArray[clamp(offset + siIndex, 0, extArray.length - 1)]; return {