Units: Add currency and energy units (#20428)

* Units: Add unit for currency - South Korean Won

* Units: Add support for 'femto' and 'pico' decimal SI prefix

* Units: Expand library of energy units

Add:
Power - Giga-watt (useful for viewing metrics of power flow through large-scale electrical infrastructure)
Electrical Resistance - Kilo-ohm, Mega-ohm (useful for viewing metrics of energy storage e.g. internal resistance of a battery and more)
Electrical Capacitance - Farad, Micro-farad, Nano-farad, Pico-farad, Femto-farad
Electrical Inductance - Henry, Milli-henry, Micro-henry

* Units: Add unit test for currency ₩

* Units: Add unit tests for energy units
pull/20685/head
Anirudh Ramesh 6 years ago committed by Dominik Prokop
parent 4afd40018b
commit 0a3662c359
  1. 12
      packages/grafana-data/src/valueFormats/categories.ts
  2. 3
      packages/grafana-data/src/valueFormats/symbolFormatters.test.ts
  3. 4
      packages/grafana-data/src/valueFormats/symbolFormatters.ts
  4. 77
      packages/grafana-data/src/valueFormats/valueFormats.test.ts

@ -117,6 +117,7 @@ export const getCategories = (): ValueFormatCategory[] => [
{ name: 'Bitcoin (฿)', id: 'currencyBTC', fn: currency('฿') },
{ name: 'South African Rand (R)', id: 'currencyZAR', fn: currency('R') },
{ name: 'Indian Rupee (₹)', id: 'currencyINR', fn: currency('₹') },
{ name: 'South Korean Won (₩)', id: 'currencyKRW', fn: currency('₩') },
],
},
{
@ -175,6 +176,7 @@ export const getCategories = (): ValueFormatCategory[] => [
{ name: 'Watt (W)', id: 'watt', fn: decimalSIPrefix('W') },
{ name: 'Kilowatt (kW)', id: 'kwatt', fn: decimalSIPrefix('W', 1) },
{ name: 'Megawatt (MW)', id: 'megwatt', fn: decimalSIPrefix('W', 2) },
{ name: 'Gigawatt (GW)', id: 'gwatt', fn: decimalSIPrefix('W', 3) },
{ name: 'Milliwatt (mW)', id: 'mwatt', fn: decimalSIPrefix('W', -1) },
{ name: 'Watt per square meter (W/m²)', id: 'Wm2', fn: toFixedUnit('W/m²') },
{ name: 'Volt-ampere (VA)', id: 'voltamp', fn: decimalSIPrefix('VA') },
@ -198,6 +200,16 @@ export const getCategories = (): ValueFormatCategory[] => [
{ name: 'Millivolt (mV)', id: 'mvolt', fn: decimalSIPrefix('V', -1) },
{ name: 'Decibel-milliwatt (dBm)', id: 'dBm', fn: decimalSIPrefix('dBm') },
{ name: 'Ohm (Ω)', id: 'ohm', fn: decimalSIPrefix('Ω') },
{ name: 'Kiloohm (kΩ)', id: 'kohm', fn: decimalSIPrefix('Ω', 1) },
{ name: 'Megaohm (MΩ)', id: 'Mohm', fn: decimalSIPrefix('Ω', 2) },
{ name: 'Farad (F)', id: 'farad', fn: decimalSIPrefix('F') },
{ name: 'Microfarad (µF)', id: 'µfarad', fn: decimalSIPrefix('F', -2) },
{ name: 'Nanofarad (nF)', id: 'nfarad', fn: decimalSIPrefix('F', -3) },
{ name: 'Picofarad (pF)', id: 'pfarad', fn: decimalSIPrefix('F', -4) },
{ name: 'Femtofarad (fF)', id: 'ffarad', fn: decimalSIPrefix('F', -5) },
{ name: 'Henry (H)', id: 'henry', fn: decimalSIPrefix('H') },
{ name: 'Millihenry (mH)', id: 'mhenry', fn: decimalSIPrefix('H', -1) },
{ name: 'Microhenry (µH)', id: 'µhenry', fn: decimalSIPrefix('H', -2) },
{ name: 'Lumens (Lm)', id: 'lumens', fn: decimalSIPrefix('Lm') },
],
},

@ -4,4 +4,7 @@ describe('Currency', () => {
it('should format as usd', () => {
expect(currency('$')(1532.82, 1, -1)).toEqual('$1.53K');
});
it('should format as krw', () => {
expect(currency('₩')(1532.82, 1, -1)).toEqual('₩1.53K');
});
});

@ -22,8 +22,8 @@ export function binarySIPrefix(unit: string, offset = 0) {
}
export function decimalSIPrefix(unit: string, offset = 0) {
let prefixes = ['n', 'µ', 'm', '', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'];
prefixes = prefixes.slice(3 + (offset || 0));
let prefixes = ['f', 'p', 'n', 'µ', 'm', '', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'];
prefixes = prefixes.slice(5 + (offset || 0));
const units = prefixes.map(p => {
return ' ' + p + unit;
});

@ -53,4 +53,81 @@ describe('valueFormats', () => {
expect(str).toBe('1.200 s');
});
});
describe('megawatt format when scaled decimals is null do not use it', () => {
it('should use specified decimals', () => {
const str = getValueFormat('megwatt')(1000, 3, null);
expect(str).toBe('1.000 GW');
});
});
describe('kiloohm format when scaled decimals is null do not use it', () => {
it('should use specified decimals', () => {
const str = getValueFormat('kohm')(1000, 3, null);
expect(str).toBe('1.000 MΩ');
});
});
describe('megaohm format when scaled decimals is null do not use it', () => {
it('should use specified decimals', () => {
const str = getValueFormat('Mohm')(1000, 3, null);
expect(str).toBe('1.000 GΩ');
});
});
describe('farad format when scaled decimals is null do not use it', () => {
it('should use specified decimals', () => {
const str = getValueFormat('farad')(1000, 3, null);
expect(str).toBe('1.000 kF');
});
});
describe('microfarad format when scaled decimals is null do not use it', () => {
it('should use specified decimals', () => {
const str = getValueFormat('µfarad')(1000, 3, null);
expect(str).toBe('1.000 mF');
});
});
describe('nanofarad format when scaled decimals is null do not use it', () => {
it('should use specified decimals', () => {
const str = getValueFormat('nfarad')(1000, 3, null);
expect(str).toBe('1.000 µF');
});
});
describe('picofarad format when scaled decimals is null do not use it', () => {
it('should use specified decimals', () => {
const str = getValueFormat('pfarad')(1000, 3, null);
expect(str).toBe('1.000 nF');
});
});
describe('femtofarad format when scaled decimals is null do not use it', () => {
it('should use specified decimals', () => {
const str = getValueFormat('ffarad')(1000, 3, null);
expect(str).toBe('1.000 pF');
});
});
describe('henry format when scaled decimals is null do not use it', () => {
it('should use specified decimals', () => {
const str = getValueFormat('henry')(1000, 3, null);
expect(str).toBe('1.000 kH');
});
});
describe('millihenry format when scaled decimals is null do not use it', () => {
it('should use specified decimals', () => {
const str = getValueFormat('mhenry')(1000, 3, null);
expect(str).toBe('1.000 H');
});
});
describe('microhenry format when scaled decimals is null do not use it', () => {
it('should use specified decimals', () => {
const str = getValueFormat('µhenry')(1000, 3, null);
expect(str).toBe('1.000 mH');
});
});
});

Loading…
Cancel
Save