From 51509bb2ffdb6794933456cec401174245198285 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=A4ggmark?= Date: Thu, 14 May 2020 13:49:21 +0200 Subject: [PATCH] Units: adds scale symbol for currencies with suffixed symbol (#24678) --- .../src/valueFormats/symbolFormatters.test.ts | 55 +++++++++++++++++++ .../src/valueFormats/symbolFormatters.ts | 2 +- 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 packages/grafana-data/src/valueFormats/symbolFormatters.test.ts diff --git a/packages/grafana-data/src/valueFormats/symbolFormatters.test.ts b/packages/grafana-data/src/valueFormats/symbolFormatters.test.ts new file mode 100644 index 00000000000..63f0e466f0e --- /dev/null +++ b/packages/grafana-data/src/valueFormats/symbolFormatters.test.ts @@ -0,0 +1,55 @@ +import { currency } from './symbolFormatters'; + +describe('currency', () => { + const symbol = '@'; + + describe('when called without asSuffix', () => { + const fmtFunc = currency(symbol); + + it.each` + value | expectedSuffix | expectedText + ${999} | ${''} | ${'999'} + ${1000} | ${'K'} | ${'1'} + ${1000000} | ${'M'} | ${'1'} + ${1000000000} | ${'B'} | ${'1'} + ${1000000000000} | ${'T'} | ${'1'} + ${1000000000000000} | ${undefined} | ${'NA'} + ${-1000000000000} | ${'T'} | ${'-1'} + ${-1000000000} | ${'B'} | ${'-1'} + ${-1000000} | ${'M'} | ${'-1'} + ${-1000} | ${'K'} | ${'-1'} + ${-999} | ${''} | ${'-999'} + `('when called with value:{$value}', ({ value, expectedSuffix, expectedText }) => { + const { prefix, suffix, text } = fmtFunc(value); + + expect(prefix).toEqual(symbol); + expect(suffix).toEqual(expectedSuffix); + expect(text).toEqual(expectedText); + }); + }); + + describe('when called with asSuffix', () => { + const fmtFunc = currency(symbol, true); + + it.each` + value | expectedSuffix | expectedText + ${999} | ${'@'} | ${'999'} + ${1000} | ${'K@'} | ${'1'} + ${1000000} | ${'M@'} | ${'1'} + ${1000000000} | ${'B@'} | ${'1'} + ${1000000000000} | ${'T@'} | ${'1'} + ${1000000000000000} | ${undefined} | ${'NA'} + ${-1000000000000} | ${'T@'} | ${'-1'} + ${-1000000000} | ${'B@'} | ${'-1'} + ${-1000000} | ${'M@'} | ${'-1'} + ${-1000} | ${'K@'} | ${'-1'} + ${-999} | ${'@'} | ${'-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/symbolFormatters.ts b/packages/grafana-data/src/valueFormats/symbolFormatters.ts index b8ce69b3e40..cd3a29707b4 100644 --- a/packages/grafana-data/src/valueFormats/symbolFormatters.ts +++ b/packages/grafana-data/src/valueFormats/symbolFormatters.ts @@ -10,7 +10,7 @@ export function currency(symbol: string, asSuffix?: boolean): ValueFormatter { } const scaled = scaler(size, decimals, scaledDecimals); if (asSuffix) { - scaled.suffix = symbol; + scaled.suffix = scaled.suffix !== undefined ? `${scaled.suffix}${symbol}` : undefined; } else { scaled.prefix = symbol; }