diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e6f2c33d..869761ec5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # 0.76.1 +- Pull in formatBalance, formatDecimal, formatElapsed, formatNumber, calcSi, findSi & isTestChain util fuinctions from the originals in @polkadot/ui-util - Swap DB interfaces to optional LRU, removing transactionAsync (client-only changes) - Swap out `wasm-schnorrkel` & `wasm-ed25519` for combined version with `wasm-crypto` diff --git a/packages/util/src/format/formatBalance.spec.ts b/packages/util/src/format/formatBalance.spec.ts new file mode 100644 index 000000000..14a0a3dcb --- /dev/null +++ b/packages/util/src/format/formatBalance.spec.ts @@ -0,0 +1,121 @@ +// Copyright 2017-2019 @polkadot/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. + +import BN from 'bn.js'; + +import formatBalance from './formatBalance'; + +describe('formatBalance', () => { + const TESTVAL = new BN('123456789000'); + + it('formats empty to 0', () => { + expect(formatBalance()).toEqual('0'); + expect(formatBalance('0')).toEqual('0'); + }); + + it('formats 123,456,789,000 (decimals=15)', () => { + expect( + formatBalance(TESTVAL, true, 15) + ).toEqual('123.456µ Unit'); + }); + + it('formats 123,456,789,000 (decimals=12)', () => { + expect( + formatBalance(TESTVAL, true, 12) + ).toEqual('123.456m Unit'); + }); + + it('formats 123,456,789,000 (decimals=12, no SI)', () => { + expect( + formatBalance(TESTVAL, false, 12) + ).toEqual('123.456'); + }); + + it('formats 123,456,789,000 (decimals=9)', () => { + expect( + formatBalance(TESTVAL, true, 9) + ).toEqual('123.456 Unit'); + }); + + it('formats 123,456,789,000 (decimals=6)', () => { + expect( + formatBalance(TESTVAL, true, 6) + ).toEqual('123.456k Unit'); + }); + + it('formats 123,456,789,000 * 10 (decimals=12)', () => { + expect( + formatBalance(TESTVAL.muln(10), true, 12) + ).toEqual('1.234 Unit'); + }); + + it('formats 123,456,789,000 * 100 (decimals=12)', () => { + expect( + formatBalance(TESTVAL.muln(100), true, 12) + ).toEqual('12.345 Unit'); + }); + + it('formats 123,456,789,000 * 1000 (decimals=12)', () => { + expect( + formatBalance(TESTVAL.muln(1000), true, 12) + ).toEqual('123.456 Unit'); + }); + + describe('findSi', () => { + it('finds the SI value', () => { + expect( + formatBalance.findSi('k') + ).toEqual({ power: 3, value: 'k', text: 'Kilo' }); + }); + + it('returns default on not found', () => { + expect( + formatBalance.findSi('blah') + ).toEqual({ power: 0, value: '-', text: 'Unit' }); + }); + }); + + describe('defaults', () => { + it('returns defaults', () => { + expect(formatBalance.getDefaults()).toEqual({ + decimals: 0, + unit: 'Unit' + }); + }); + + it('formats 123,456,789,000 (defaultDecimals=12)', () => { + formatBalance.setDefaults({ decimals: 12 }); + + expect( + formatBalance(TESTVAL) + ).toEqual('123.456m Unit'); + }); + + it('formats 123,456,789,000 (defaultUnit=TEST)', () => { + formatBalance.setDefaults({ unit: 'TEST' }); + + expect( + formatBalance(TESTVAL) + ).toEqual('123.456m TEST'); + }); + }); + + it('returns options for dropdown', () => { + formatBalance.setDefaults({ decimals: 0, unit: 'TEST' }); + + expect( + formatBalance.getOptions() + ).toEqual([ + { power: 0, value: '-', text: 'TEST' }, + { 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' } + ]); + }); +}); diff --git a/packages/util/src/format/formatBalance.ts b/packages/util/src/format/formatBalance.ts new file mode 100644 index 000000000..7b4b4d497 --- /dev/null +++ b/packages/util/src/format/formatBalance.ts @@ -0,0 +1,87 @@ +// Copyright 2017-2019 @polkadot/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. + +import BN from 'bn.js'; + +import isUndefined from '../is/undefined'; +import { SI, SI_MID, SiDef, calcSi, findSi } from './si'; +import formatDecimal from './formatDecimal'; + +type Defaults = { + decimals: number, + unit: string +}; + +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: Partial): void; +} + +const DEFAULT_DECIMALS = 0; +const DEFAULT_UNIT = SI[SI_MID].text; + +let defaultDecimals = DEFAULT_DECIMALS; +let defaultUnit = DEFAULT_UNIT; + +// Formats a string/number with . notation +function _formatBalance (input?: number | string | BN, withSi: boolean = true, decimals: number = defaultDecimals): string { + const text = (input || '').toString(); + + if (text.length === 0 || text === '0') { + return '0'; + } + + // 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); + const units = withSi + ? ( + si.value === '-' + ? ` ${si.text}` + : `${si.value} ${SI[SI_MID].text}` + ) + : ''; + + return `${formatDecimal(prefix || '0')}.${postfix}${units}`; +} + +const formatBalance = _formatBalance as BalanceFormatter; + +formatBalance.calcSi = (text: string, decimals: number = defaultDecimals): SiDef => + calcSi(text, decimals); +formatBalance.findSi = findSi; + +formatBalance.getDefaults = (): Defaults => { + return { + decimals: defaultDecimals, + unit: defaultUnit + }; +}; + +// get allowable options to display in a dropdown +formatBalance.getOptions = (decimals: number = defaultDecimals): Array => { + return SI.filter(({ power }) => + power < 0 + ? (decimals + power) >= 0 + : true + ); +}; + +// Sets the default decimals to use for formatting (ui-wide) +formatBalance.setDefaults = ({ decimals, unit }: Partial): void => { + defaultDecimals = isUndefined(decimals) ? defaultDecimals : decimals; + defaultUnit = isUndefined(unit) ? defaultUnit : unit; + + SI[SI_MID].text = defaultUnit; +}; + +export default formatBalance; diff --git a/packages/util/src/format/formatDecimal.spec.ts b/packages/util/src/format/formatDecimal.spec.ts new file mode 100644 index 000000000..49cbb207c --- /dev/null +++ b/packages/util/src/format/formatDecimal.spec.ts @@ -0,0 +1,19 @@ +// Copyright 2017-2019 @polkadot/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. + +import formatDecimal from './formatDecimal'; + +describe('formatDecimal', () => { + it('formats decimals in number groupings', () => { + expect(formatDecimal('12345')).toEqual('12,345'); + }); + + it('formats decimal-only in number groupings', () => { + expect(formatDecimal('test6789')).toEqual('6,789'); + }); + + it('returns input for non-decimal', () => { + expect(formatDecimal('test')).toEqual('test'); + }); +}); diff --git a/packages/util/src/format/formatDecimal.ts b/packages/util/src/format/formatDecimal.ts new file mode 100644 index 000000000..097c21462 --- /dev/null +++ b/packages/util/src/format/formatDecimal.ts @@ -0,0 +1,13 @@ +// Copyright 2017-2019 @polkadot/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. + +const NUMBER_REGEX = new RegExp('(\\d+?)(?=(\\d{3})+(?!\\d)|$)', 'g'); + +export default function formatDecimal (value: string): string { + const matched = value.match(NUMBER_REGEX); + + return matched + ? matched.join(',') + : value; +} diff --git a/packages/util/src/format/formatElapsed.spec.ts b/packages/util/src/format/formatElapsed.spec.ts new file mode 100644 index 000000000..8ec261b75 --- /dev/null +++ b/packages/util/src/format/formatElapsed.spec.ts @@ -0,0 +1,44 @@ +// Copyright 2017-2019 @polkadot/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. + +import { Compact } from './types'; + +import BN from 'bn.js'; + +import formatElapsed from './formatElapsed'; + +describe('formatElapsed', () => { + const start = 12345678; + const now = new Date(12345678); + + it('formats a Date', () => { + expect( + formatElapsed(now, new Date(start + 9700)) + ).toEqual('9.7s'); + }); + + it('formats a BN', () => { + expect( + formatElapsed(now, new BN(start + 42700)) + ).toEqual('42s'); + }); + + it('formats a Compact', () => { + expect( + formatElapsed(now, { unwrap: () => new BN(start + (5.3 * 60000)) } as Compact) + ).toEqual('5m'); + }); + + it('formats a number', () => { + expect( + formatElapsed(now, start + (42 * 60 * 60000)) + ).toEqual('42h'); + }); + + it('formats defaults', () => { + expect( + formatElapsed() + ).toEqual('0.0s'); + }); +}); diff --git a/packages/util/src/format/formatElapsed.ts b/packages/util/src/format/formatElapsed.ts new file mode 100644 index 000000000..18ff900f2 --- /dev/null +++ b/packages/util/src/format/formatElapsed.ts @@ -0,0 +1,43 @@ +// 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. + +import { Compact } from './types'; + +import BN from 'bn.js'; + +function getValue (value?: BN | Compact | Date | number | null): number { + if (value) { + if ((value as any).unwrap) { + return getValue((value as Compact).unwrap()); + } else if (value instanceof Date) { + return getValue(value.getTime()); + } else if (value instanceof BN) { + return getValue(value.toNumber()); + } + } + + return (value as number) || 0; +} + +export default function formatElapsed (now?: Date | null, value?: BN | Compact | Date | number | null): string { + const tsNow = (now && now.getTime()) || 0; + const tsValue = getValue(value); + let display = '0.0s'; + + if (tsNow && tsValue) { + const elapsed = Math.max(Math.abs(tsNow - tsValue), 0) / 1000; + + if (elapsed < 15) { + display = `${elapsed.toFixed(1)}s`; + } else if (elapsed < 60) { + display = `${elapsed | 0}s`; + } else if (elapsed < 3600) { + display = `${elapsed / 60 | 0}m`; + } else { + display = `${elapsed / 3600 | 0}h`; + } + } + + return display; +} diff --git a/packages/util/src/format/formatNumber.ts b/packages/util/src/format/formatNumber.ts new file mode 100644 index 000000000..710021903 --- /dev/null +++ b/packages/util/src/format/formatNumber.ts @@ -0,0 +1,22 @@ +// Copyright 2017-2019 @polkadot/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. + +import { Compact } from './types'; + +import BN from 'bn.js'; + +import bnToBn from '../bn/toBn'; +import formatDecimal from './formatDecimal'; + +export default function formatNumber (_value?: Compact | BN | number | null): string { + if (!_value) { + return '0'; + } + + const value = (_value as any).toBn + ? (_value as Compact).toBn() + : bnToBn(_value as BN); + + return formatDecimal(value.toString()); +} diff --git a/packages/util/src/format/index.ts b/packages/util/src/format/index.ts new file mode 100644 index 000000000..222e553f8 --- /dev/null +++ b/packages/util/src/format/index.ts @@ -0,0 +1,9 @@ +// Copyright 2017-2019 @polkadot/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 { default as formatBalance } from './formatBalance'; +export { default as formatDecimal } from './formatDecimal'; +export { default as formatElapsed } from './formatElapsed'; +export { default as formatNumber } from './formatNumber'; +export { calcSi, findSi } from './si'; diff --git a/packages/util/src/format/si.ts b/packages/util/src/format/si.ts new file mode 100644 index 000000000..0f0b4763f --- /dev/null +++ b/packages/util/src/format/si.ts @@ -0,0 +1,47 @@ +// Copyright 2017-2019 @polkadot/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 { + // use a loop here, better RN support (which doesn't have [].find) + for (let i = 0; i < SI.length; i++) { + if (SI[i].value === type) { + return SI[i]; + } + } + + return SI[SI_MID]; +} diff --git a/packages/util/src/format/types.ts b/packages/util/src/format/types.ts new file mode 100644 index 000000000..3fc6b4a1a --- /dev/null +++ b/packages/util/src/format/types.ts @@ -0,0 +1,10 @@ +// Copyright 2017-2019 @polkadot/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. + +import BN from 'bn.js'; + +export interface Compact { + toBn: () => BN; + unwrap: () => BN | Date; +} diff --git a/packages/util/src/index.ts b/packages/util/src/index.ts index b4c3a5f9b..3bb8f1112 100644 --- a/packages/util/src/index.ts +++ b/packages/util/src/index.ts @@ -18,6 +18,7 @@ export * from './bn'; export * from './buffer'; export * from './compact'; export * from './ext'; +export * from './format'; export * from './hex'; export * from './is'; export * from './number'; diff --git a/packages/util/src/is/index.ts b/packages/util/src/is/index.ts index 0db1214fb..d1cde591d 100644 --- a/packages/util/src/is/index.ts +++ b/packages/util/src/is/index.ts @@ -20,5 +20,6 @@ export { default as isNumber } from './number'; export { default as isObject } from './object'; export { default as isObservable } from './observable'; export { default as isString } from './string'; +export { default as isTestChain } from './testChain'; export { default as isU8a } from './u8a'; export { default as isUndefined } from './undefined'; diff --git a/packages/util/src/is/testChain.spec.ts b/packages/util/src/is/testChain.spec.ts new file mode 100644 index 000000000..7fb8e1f3c --- /dev/null +++ b/packages/util/src/is/testChain.spec.ts @@ -0,0 +1,24 @@ +// Copyright 2017-2019 @polkadot/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. + +import isTestChain from './testChain'; + +describe('isTestChain', () => { + it('enables test environment when chain specification matches text of dev or loc(al)', () => { + const validTestModeChainSpecsWithDev = ['Development']; + const validTestModeChainSpecsWithLoc = ['Local Testnet']; + + validTestModeChainSpecsWithDev.concat(validTestModeChainSpecsWithLoc).forEach((s) => + expect(isTestChain(s)).toEqual(true) + ); + }); + + it('disables keyring test mode when chain specification is not a test mode or undefined or number type', () => { + const invalidTestModeChainSpecs = ['dev', 'local', 'development', 'PoC-1 Testnet', 'Staging Testnet', 'future PoC-2 Testnet', 'a pocadot?', undefined]; + + invalidTestModeChainSpecs.forEach((s) => + expect(isTestChain(s)).toEqual(false) + ); + }); +}); diff --git a/packages/util/src/is/testChain.ts b/packages/util/src/is/testChain.ts new file mode 100644 index 000000000..4238327a6 --- /dev/null +++ b/packages/util/src/is/testChain.ts @@ -0,0 +1,13 @@ +// Copyright 2017-2019 @polkadot/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. + +const re = new RegExp('^(Development|Local Testnet)$'); + +export default function isTestChain (chain?: string | null): boolean { + if (!chain) { + return false; + } + + return !!re.test(chain.toString()); +}