Formatting: Fixes valueFormats for a value of 0 (#50719) (#50805)

(cherry picked from commit f9d31d0612)

Co-authored-by: Joao Silva <100691367+JoaoSilvaGrafana@users.noreply.github.com>
This commit is contained in:
Grot (@grafanabot)
2022-06-14 17:19:03 +02:00
committed by GitHub
co-authored by Joao Silva
parent e35b901ed6
commit ef7148fc4a
3 changed files with 32 additions and 2 deletions
@@ -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);
});
});
@@ -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'}
@@ -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 {