Pull in formatting from util (#110)

This commit is contained in:
Jaco Greeff
2019-04-02 14:37:06 +02:00
committed by GitHub
parent 7f156bf4bc
commit 0ec979706a
16 changed files with 69 additions and 451 deletions
+1 -1
View File
@@ -26,7 +26,7 @@
"devDependencies": {
"@babel/core": "^7.4.0",
"@babel/runtime": "^7.4.2",
"@polkadot/dev-react": "^0.29.0-beta.1",
"@polkadot/dev-react": "^0.29.1",
"@polkadot/ts": "^0.1.56",
"empty": "^0.10.1",
"gh-pages": "^2.0.1"
+2 -2
View File
@@ -24,8 +24,8 @@
"react": "*"
},
"devDependencies": {
"@polkadot/keyring": "^0.76.0-beta.3",
"@polkadot/util-crypto": "^0.76.0-beta.3",
"@polkadot/keyring": "^0.76.0-beta.5",
"@polkadot/util-crypto": "^0.76.0-beta.5",
"xmlserializer": "^0.6.1"
}
}
+2 -2
View File
@@ -17,9 +17,9 @@
"styled-components": "^4.2.0"
},
"devDependencies": {
"@polkadot/keyring": "^0.76.0-beta.3",
"@polkadot/keyring": "^0.76.0-beta.5",
"@polkadot/types": "^0.76.0-beta.1",
"@polkadot/util": "^0.76.0-beta.3"
"@polkadot/util": "^0.76.0-beta.5"
},
"peerDependencies": {
"@polkadot/keyring": "*",
+1 -1
View File
@@ -10,7 +10,7 @@
"license": "Apache-2.0",
"dependencies": {
"@babel/runtime": "^7.4.2",
"@polkadot/util": "^0.76.0-beta.3",
"@polkadot/util": "^0.76.0-beta.5",
"@types/store": "^2.0.1",
"store": "^2.0.12"
}
-121
View File
@@ -1,121 +0,0 @@
// 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 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' }
]);
});
});
-87
View File
@@ -1,87 +0,0 @@
// 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 BN from 'bn.js';
import { isUndefined } from '@polkadot/util';
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<SiDef>;
setDefaults (defaults: Partial<Defaults>): 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 <prefix>.<postfix><type> 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<SiDef> => {
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<Defaults>): void => {
defaultDecimals = isUndefined(decimals) ? defaultDecimals : decimals;
defaultUnit = isUndefined(unit) ? defaultUnit : unit;
SI[SI_MID].text = defaultUnit;
};
export default formatBalance;
@@ -1,19 +0,0 @@
// 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 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');
});
});
-13
View File
@@ -1,13 +0,0 @@
// 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.
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;
}
@@ -1,45 +0,0 @@
// 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 BN from 'bn.js';
import { Compact, UInt } from '@polkadot/types';
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', () => {
const C = Compact.with(UInt);
expect(
formatElapsed(now, new C(start + (5.3 * 60000)))
).toEqual('5m');
});
it('formats a number', () => {
expect(
formatElapsed(now, start + (42 * 60 * 60000))
).toEqual('42h');
});
it('formats defaults', () => {
expect(
formatElapsed()
).toEqual('0.0s');
});
});
-40
View File
@@ -1,40 +0,0 @@
// 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 BN from 'bn.js';
import { Compact } from '@polkadot/types';
function getValue (value?: BN | Compact | Date | number | null): number {
if (value instanceof Compact) {
return getValue(value.unwrap());
} else if (value instanceof Date) {
return getValue(value.getTime());
} else if (value instanceof BN) {
return getValue(value.toNumber());
}
return value || 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;
}
-21
View File
@@ -1,21 +0,0 @@
// 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 BN from 'bn.js';
import { Compact } from '@polkadot/types';
import { bnToBn } from '@polkadot/util';
import formatDecimal from './formatDecimal';
export default function formatNumber (_value?: Compact | BN | number | null): string {
if (!_value) {
return '0';
}
const value = _value instanceof Compact
? _value.toBn()
: bnToBn(_value);
return formatDecimal(value.toString());
}
+3 -6
View File
@@ -2,9 +2,6 @@
// 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 { default as isTestChain } from './isTestChain';
export { calcSi, findSi } from './si';
console.warn('@polkadot/ui-util has been incorporated into @polkadot/util (with the same export names)');
export { formatBalance, formatDecimal, formatElapsed, formatNumber, calcSi, findSi, isTestChain } from '@polkadot/util';
-24
View File
@@ -1,24 +0,0 @@
// 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 isTestChain from './isTestChain';
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)
);
});
});
-13
View File
@@ -1,13 +0,0 @@
// 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.
const re = new RegExp('^(Development|Local Testnet)$');
export default function isTestChain (chain?: string | null): boolean {
if (!chain) {
return false;
}
return !!re.test(chain.toString());
}
-47
View File
@@ -1,47 +0,0 @@
// 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<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: '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];
}
+60 -9
View File
@@ -1676,15 +1676,15 @@
universal-user-agent "^2.0.0"
url-template "^2.0.8"
"@polkadot/dev-react@^0.29.0-beta.1":
version "0.29.0-beta.1"
resolved "https://registry.yarnpkg.com/@polkadot/dev-react/-/dev-react-0.29.0-beta.1.tgz#3f20001850d91428b4e8e5d9f070e1f73c3b1b65"
integrity sha512-hONIgASNgbvEXbCXgCrVjgepMDY4Pr645ZcNNRNlIfDKigN3BH9D7FiynpAZVhzNqlPwdIf2It76csZ8qVN20A==
"@polkadot/dev-react@^0.29.1":
version "0.29.1"
resolved "https://registry.yarnpkg.com/@polkadot/dev-react/-/dev-react-0.29.1.tgz#c264e64cb2ed8405bc411ea76aa0965217f394cf"
integrity sha512-7VFiylCVnjhLTqUAGQogQOaqMKSDgfdIi60X7WMSbQ5LKyI0aczCGg5ggsE5lCIgcFeLYrJJTNRrOTMZya4GGg==
dependencies:
"@babel/core" "^7.4.0"
"@babel/plugin-syntax-dynamic-import" "^7.2.0"
"@babel/preset-react" "^7.0.0"
"@polkadot/dev" "^0.29.0-beta.1"
"@polkadot/dev" "^0.29.1"
"@types/react" "^16.8.8"
"@types/react-dom" "^16.8.2"
"@types/styled-components" "4.1.8"
@@ -1712,10 +1712,10 @@
webpack-plugin-serve "^0.7.4"
worker-loader "^2.0.0"
"@polkadot/dev@^0.29.0-beta.1":
version "0.29.0-beta.1"
resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.29.0-beta.1.tgz#a87f8cdfb480d3b9fc09c76152df2e0a1ef9b900"
integrity sha512-4niAaZpnQeq7KBDfLxvXr2DcHO50E96JjbOZkJ4yXhuMdAKRKQjpxIKqIQ6OfX3V17yvzk+AwJRwmk960FidXA==
"@polkadot/dev@^0.29.1":
version "0.29.1"
resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.29.1.tgz#919c7b6b8e76dbdb4f59abe5f6d7f812d2b1e99d"
integrity sha512-Z3L2lvMQ/KAkIjP3MTgbjZGmX4WWXAR6S+mhQZ6srdFF+Y8uhmNIrC9QW+KQGGN7o1O8XglitRbQJKtsRkjdlg==
dependencies:
"@babel/cli" "^7.2.3"
"@babel/core" "^7.4.0"
@@ -1761,6 +1761,17 @@
"@types/bs58" "^4.0.0"
bs58 "^4.0.1"
"@polkadot/keyring@^0.76.0-beta.5":
version "0.76.0-beta.5"
resolved "https://registry.yarnpkg.com/@polkadot/keyring/-/keyring-0.76.0-beta.5.tgz#e0b9f7d7269112027db0b0c2d48a0635d90743d1"
integrity sha512-C0X0xcCEBMnC8U3Pt6t1pBlK3WSAhHYrVvw3u/gTE32GviGTbPkkydHgNpp8+PQfduuEg4maedOBmNRK4l6/ow==
dependencies:
"@babel/runtime" "^7.4.2"
"@polkadot/util" "^0.76.0-beta.5"
"@polkadot/util-crypto" "^0.76.0-beta.5"
"@types/bs58" "^4.0.0"
bs58 "^4.0.1"
"@polkadot/ts@^0.1.56":
version "0.1.56"
resolved "https://registry.yarnpkg.com/@polkadot/ts/-/ts-0.1.56.tgz#ffd6e9c95704a7fb90b918193b9dc5c440114b27"
@@ -1795,6 +1806,26 @@
tweetnacl "^1.0.1"
xxhashjs "^0.2.2"
"@polkadot/util-crypto@^0.76.0-beta.5":
version "0.76.0-beta.5"
resolved "https://registry.yarnpkg.com/@polkadot/util-crypto/-/util-crypto-0.76.0-beta.5.tgz#e1217407a65df0a76d100e513020687287b804ef"
integrity sha512-E+68DoVdbeJkN6rYnxuxkV2Yuvz5+Fkti4jlPG3iDGHmFApbvZcDPxU+l4dr+mtYiwDkqYQWZ/N7grYrtMKN1Q==
dependencies:
"@babel/runtime" "^7.4.2"
"@polkadot/util" "^0.76.0-beta.5"
"@polkadot/wasm-crypto" "^0.8.1"
"@types/bip39" "^2.4.2"
"@types/pbkdf2" "^3.0.0"
"@types/secp256k1" "^3.5.0"
"@types/webassembly-js-api" "^0.0.2"
"@types/xxhashjs" "^0.2.1"
bip39 "^2.5.0"
blakejs "^1.1.0"
js-sha3 "^0.8.0"
secp256k1 "^3.6.2"
tweetnacl "^1.0.1"
xxhashjs "^0.2.2"
"@polkadot/util@^0.76.0-beta.3":
version "0.76.0-beta.3"
resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-0.76.0-beta.3.tgz#23d67645a58b9992bff3312371d1dadf831190da"
@@ -1810,11 +1841,31 @@
ip-regex "^4.0.0"
moment "^2.24.0"
"@polkadot/util@^0.76.0-beta.5":
version "0.76.0-beta.5"
resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-0.76.0-beta.5.tgz#9eba25de4deeb942f33b2a2ffd6557aadbbec765"
integrity sha512-o56U1oDjiA/bhZBnoNJJ2TD6+HIcHZIiU0vi/ZCFdWl29uToJwoOfFfPgXnPVe2uuQIcO8s1pKfp5OxnkPmGKg==
dependencies:
"@babel/runtime" "^7.4.2"
"@types/bn.js" "^4.11.4"
"@types/deasync" "^0.1.0"
"@types/ip-regex" "^3.0.0"
bn.js "^4.11.8"
camelcase "^5.2.0"
chalk "^2.4.2"
ip-regex "^4.0.0"
moment "^2.24.0"
"@polkadot/wasm-crypto@^0.8.0-beta.1":
version "0.8.0-beta.1"
resolved "https://registry.yarnpkg.com/@polkadot/wasm-crypto/-/wasm-crypto-0.8.0-beta.1.tgz#601a1b044e111d6fe0431271fcab00b03cdf5447"
integrity sha512-M6draqncbMRCvj61PHVvoFZiv3FfFLizf3dKL7h2wBAAFsflH/cM3bE/ibSoMdaRb8KHCboO4Ot/6hAL0A4xLw==
"@polkadot/wasm-crypto@^0.8.1":
version "0.8.1"
resolved "https://registry.yarnpkg.com/@polkadot/wasm-crypto/-/wasm-crypto-0.8.1.tgz#aa522577c79e0fe0825c64df814f0c0177a66683"
integrity sha512-Z88mi23WUla6EOuE3sSmleaYjabUc7XwhVHyj78GwfmpF0cUo9mZWxJEiT67de6p9Wz70mSZRUQgpg9JUez0BA==
"@types/accepts@*":
version "1.3.5"
resolved "https://registry.yarnpkg.com/@types/accepts/-/accepts-1.3.5.tgz#c34bec115cfc746e04fe5a059df4ce7e7b391575"