Fix balance formatting (#593)

This commit is contained in:
Jaco Greeff 2019-01-12 19:04:40 +01:00 committed by GitHub
parent 72e6f5f3e3
commit 1a8da504c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 2 additions and 216 deletions

View File

@ -11,11 +11,12 @@ import defaults from '@polkadot/rpc-provider/defaults';
import WsProvider from '@polkadot/rpc-provider/ws';
import { InputNumber } from '@polkadot/ui-app/InputNumber';
import keyring from '@polkadot/ui-keyring';
import { balanceFormat } from '@polkadot/ui-reactive/util/index';
import settings from '@polkadot/ui-settings';
import { ChainProperties } from '@polkadot/types';
import ApiContext from './ApiContext';
import { balanceFormat, isTestChain } from './util';
import { isTestChain } from './util';
type Props = {
children: React.ReactNode,

View File

@ -1,83 +0,0 @@
// Copyright 2017-2019 @polkadot/ui-app authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import BN from 'bn.js';
import { balanceFormat } from './';
describe('balanceFormat', () => {
const TESTVAL = new BN('123456789000');
it('formats 123,456,789,000 (decimals=15)', () => {
expect(
balanceFormat(TESTVAL, true, 15)
).toEqual('123.456µ');
});
it('formats 123,456,789,000 (decimals=12)', () => {
expect(
balanceFormat(TESTVAL, true, 12)
).toEqual('123.456m');
});
it('formats 123,456,789,000 (decimals=12, no SI)', () => {
expect(
balanceFormat(TESTVAL, false, 12)
).toEqual('123.456');
});
it('formats 123,456,789,000 (decimals=9)', () => {
expect(
balanceFormat(TESTVAL, true, 9)
).toEqual('123.456');
});
it('formats 123,456,789,000 (decimals=6)', () => {
expect(
balanceFormat(TESTVAL, true, 6)
).toEqual('123.456k');
});
it('formats 123,456,789,000 * 10 (decimals=12)', () => {
expect(
balanceFormat(TESTVAL.muln(10), true, 12)
).toEqual('1.234');
});
it('formats 123,456,789,000 * 100 (decimals=12)', () => {
expect(
balanceFormat(TESTVAL.muln(100), true, 12)
).toEqual('12.345');
});
it('formats 123,456,789,000 * 1000 (decimals=12)', () => {
expect(
balanceFormat(TESTVAL.muln(1000), true, 12)
).toEqual('123.456');
});
describe('findSi', () => {
it('finds the SI value', () => {
expect(
balanceFormat.findSi('k')
).toEqual({ power: 3, value: 'k', text: 'Kilo' });
});
it('returns default on not found', () => {
expect(
balanceFormat.findSi('blah')
).toEqual({ power: 0, value: '-', text: '----' });
});
});
describe('setDefaultDecimals', () => {
it('formats 123,456,789,000 (defaultDecimals=15)', () => {
balanceFormat.setDefaultDecimals(15);
expect(
balanceFormat(TESTVAL)
).toEqual('123.456µ');
});
});
});

View File

@ -1,95 +0,0 @@
// Copyright 2017-2019 @polkadot/ui-api authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import BN from 'bn.js';
import decimalFormat from './decimalFormat';
type SiDef = {
power: number,
text: string,
value: string
};
interface BalanceFormatter {
(input: string | BN, withSi?: boolean, decimals?: number): string;
findSi (type: string): SiDef;
getDefaultDecimals (): number;
getOptions (decimals?: number): Array<SiDef>;
setDefaultDecimals (decimals: number): void;
}
const SI: Array<SiDef> = [
{ power: -24, value: 'y', text: 'yocto' },
{ power: -21, value: 'z', text: 'zepto' },
{ power: -18, value: 'a', text: 'atto' },
{ power: -15, value: 'f', text: 'femto' },
{ power: -12, value: 'p', text: 'pico' },
{ power: -9, value: 'n', text: 'nano' },
{ power: -6, value: 'µ', text: 'micro' },
{ power: -3, value: 'm', text: 'milli' },
{ power: 0, value: '-', text: '----' }, // position 8
{ power: 3, value: 'k', text: 'Kilo' },
{ power: 6, value: 'M', text: 'Mega' },
{ power: 9, value: 'G', text: 'Giga' },
{ power: 12, value: 'T', text: 'Tera' },
{ power: 15, value: 'P', text: 'Peta' },
{ power: 18, value: 'E', text: 'Exa' },
{ power: 21, value: 'Z', text: 'Zeta' },
{ power: 24, value: 'Y', text: 'Yotta' }
];
const SI_MID = 8;
let defaultDecimals = 0;
export function calcSi (text: string, decimals: number = defaultDecimals): SiDef {
return SI[(SI_MID - 1) + Math.ceil((text.length - decimals) / 3)];
}
// Formats a string/number with <prefix>.<postfix><type> notation
function _balanceFormat (input: string | BN, withSi: boolean = true, decimals: number = defaultDecimals): string {
const text = (input || '').toString();
if (text.length === 0 || text === '0') {
return text;
}
// NOTE We start at midpoint (8) minus 1 - this means that values display as
// 123.456 instead of 0.123k (so always 6 relevant). Additionally we us ceil
// so there are at most 3 decimal before the decimal seperator
const si = calcSi(text, decimals);
const mid = text.length - (decimals + si.power);
const prefix = text.substr(0, mid);
const postfix = `${text.substr(mid)}000`.substr(0, 3);
return `${decimalFormat(prefix || '0')}.${postfix}${withSi ? (si.value === '-' ? '' : si.value) : ''}`;
}
const balanceFormat = _balanceFormat as BalanceFormatter;
// Given a SI type (e.g. k, m, Y) find the SI definition
balanceFormat.findSi = (type: string): SiDef => {
return SI.find(({ value }) => value === type) || SI[SI_MID];
};
balanceFormat.getDefaultDecimals = (): number => {
return defaultDecimals;
};
// get allowable options to display in a dropdown
balanceFormat.getOptions = (decimals: number = defaultDecimals): Array<SiDef> => {
return SI.filter(({ power }) =>
power < 0
? (decimals + power) >= 0
: true
);
};
// Sets the default decimals to use for formatting (chain-wide)
balanceFormat.setDefaultDecimals = (decimals: number = 0): void => {
defaultDecimals = decimals;
};
export default balanceFormat;

View File

@ -1,9 +0,0 @@
// Copyright 2017-2019 @polkadot/ui-api authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
const NUMBER_REGEX = new RegExp('(\\d+?)(?=(\\d{3})+(?!\\d)|$)', 'g');
export default function decimalFormat (value: string): string {
return (value.match(NUMBER_REGEX) || []).join(',');
}

View File

@ -2,23 +2,16 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import balanceFormat, { calcSi } from './balanceFormat';
import decimalFormat from './decimalFormat';
import intervalObservable from './intervalObservable';
import intervalTimer from './intervalTimer';
import isEqual from './isEqual';
import isTestChain from './isTestChain';
import numberFormat from './numberFormat';
import triggerChange from './triggerChange';
export {
balanceFormat,
calcSi,
decimalFormat,
intervalObservable,
intervalTimer,
isEqual,
isTestChain,
numberFormat,
triggerChange
};

View File

@ -1,21 +0,0 @@
// Copyright 2017-2019 @polkadot/ui-api authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import BN from 'bn.js';
import { Compact } from '@polkadot/types/codec';
import { bnToBn } from '@polkadot/util';
import decimalFormat from './decimalFormat';
export default function numberFormat (_value?: Compact | BN | number | null): string {
if (!_value) {
return '0';
}
const value = _value instanceof Compact
? _value.toBn()
: bnToBn(_value);
return decimalFormat(value.toString());
}