diff --git a/packages/ui-util/src/formatBalance.ts b/packages/ui-util/src/formatBalance.ts index 86004b12..d49fd1e4 100644 --- a/packages/ui-util/src/formatBalance.ts +++ b/packages/ui-util/src/formatBalance.ts @@ -5,14 +5,9 @@ import BN from 'bn.js'; import { isUndefined } from '@polkadot/util'; +import { SI, SI_MID, SiDef, calcSi, findSi } from './si'; import formatDecimal from './formatDecimal'; -type SiDef = { - power: number, - text: string, - value: string -}; - type Defaults = { decimals?: number, unit?: string @@ -20,43 +15,19 @@ type Defaults = { interface BalanceFormatter { (input?: number | string | BN, withSi?: boolean, decimals?: number): string; + calcSi (text: string, decimals: number): SiDef; findSi (type: string): SiDef; getDefaults (): Defaults; getOptions (decimals?: number): Array; setDefaults (defaults: Defaults): void; } -const SI_MID = 8; const DEFAULT_DECIMALS = 0; -const DEFAULT_UNIT = 'Unit'; +const DEFAULT_UNIT = SI[SI_MID].text; let defaultDecimals = DEFAULT_DECIMALS; let defaultUnit = DEFAULT_UNIT; -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: defaultUnit }, // 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' } -]; - -export function calcSi (text: string, decimals: number = defaultDecimals): SiDef { - return SI[(SI_MID - 1) + Math.ceil((text.length - decimals) / 3)] || SI[SI.length - 1]; -} - // Formats a string/number with . notation function _formatBalance (input?: number | string | BN, withSi: boolean = true, decimals: number = defaultDecimals): string { const text = (input || '').toString(); @@ -85,10 +56,8 @@ function _formatBalance (input?: number | string | BN, withSi: boolean = true, d const formatBalance = _formatBalance as BalanceFormatter; -// Given a SI type (e.g. k, m, Y) find the SI definition -formatBalance.findSi = (type: string): SiDef => { - return SI.find(({ value }) => value === type) || SI[SI_MID]; -}; +formatBalance.calcSi = calcSi; +formatBalance.findSi = findSi; formatBalance.getDefaults = (): Defaults => { return { diff --git a/packages/ui-util/src/index.ts b/packages/ui-util/src/index.ts index 6edac748..f04598cd 100644 --- a/packages/ui-util/src/index.ts +++ b/packages/ui-util/src/index.ts @@ -7,3 +7,4 @@ export { default as formatDecimal } from './formatDecimal'; export { default as formatElapsed } from './formatElapsed'; export { default as formatNumber } from './formatNumber'; export { default as isTestChain } from './isTestChain'; +export { calcSi, findSi } from './si'; diff --git a/packages/ui-util/src/si.ts b/packages/ui-util/src/si.ts new file mode 100644 index 00000000..1dc1e292 --- /dev/null +++ b/packages/ui-util/src/si.ts @@ -0,0 +1,40 @@ +// Copyright 2017-2019 @polkadot/ui-util authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +export type SiDef = { + power: number, + text: string, + value: string +}; + +export const SI_MID = 8; + +export 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: 'Unit' }, // 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' } +]; + +export function calcSi (text: string, decimals: number): SiDef { + return SI[(SI_MID - 1) + Math.ceil((text.length - decimals) / 3)] || SI[SI.length - 1]; +} + +// Given a SI type (e.g. k, m, Y) find the SI definition +export function findSi (type: string): SiDef { + return SI.find(({ value }) => value === type) || SI[SI_MID]; +}