From 1a8da504c868ce942ecc26fca8c031d388823c58 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Sat, 12 Jan 2019 19:04:40 +0100 Subject: [PATCH] Fix balance formatting (#593) --- packages/ui-api/src/Api.tsx | 3 +- .../ui-api/src/util/balanceFormat.spec.js | 83 ---------------- packages/ui-api/src/util/balanceFormat.ts | 95 ------------------- packages/ui-api/src/util/decimalFormat.ts | 9 -- packages/ui-api/src/util/index.ts | 7 -- packages/ui-api/src/util/numberFormat.ts | 21 ---- 6 files changed, 2 insertions(+), 216 deletions(-) delete mode 100644 packages/ui-api/src/util/balanceFormat.spec.js delete mode 100644 packages/ui-api/src/util/balanceFormat.ts delete mode 100644 packages/ui-api/src/util/decimalFormat.ts delete mode 100644 packages/ui-api/src/util/numberFormat.ts diff --git a/packages/ui-api/src/Api.tsx b/packages/ui-api/src/Api.tsx index 8942f76170..34b1915051 100644 --- a/packages/ui-api/src/Api.tsx +++ b/packages/ui-api/src/Api.tsx @@ -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, diff --git a/packages/ui-api/src/util/balanceFormat.spec.js b/packages/ui-api/src/util/balanceFormat.spec.js deleted file mode 100644 index 98b33ced84..0000000000 --- a/packages/ui-api/src/util/balanceFormat.spec.js +++ /dev/null @@ -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µ'); - }); - }); -}); diff --git a/packages/ui-api/src/util/balanceFormat.ts b/packages/ui-api/src/util/balanceFormat.ts deleted file mode 100644 index af4a0ce1ac..0000000000 --- a/packages/ui-api/src/util/balanceFormat.ts +++ /dev/null @@ -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; - setDefaultDecimals (decimals: number): void; -} - -const SI: Array = [ - { 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 . 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 => { - 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; diff --git a/packages/ui-api/src/util/decimalFormat.ts b/packages/ui-api/src/util/decimalFormat.ts deleted file mode 100644 index 9ec6a40373..0000000000 --- a/packages/ui-api/src/util/decimalFormat.ts +++ /dev/null @@ -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(','); -} diff --git a/packages/ui-api/src/util/index.ts b/packages/ui-api/src/util/index.ts index 88833102d3..6dbbbc9273 100644 --- a/packages/ui-api/src/util/index.ts +++ b/packages/ui-api/src/util/index.ts @@ -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 }; diff --git a/packages/ui-api/src/util/numberFormat.ts b/packages/ui-api/src/util/numberFormat.ts deleted file mode 100644 index 10fa31bbea..0000000000 --- a/packages/ui-api/src/util/numberFormat.ts +++ /dev/null @@ -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()); -}