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

This commit is contained in:
Joao Silva 2022-06-14 16:05:59 +01:00 committed by GitHub
parent 58e7769158
commit f9d31d0612
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 2 deletions

View File

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

View File

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

View File

@ -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 {