Cater for negative values in number formats (#415)
* Cater for negative values in number formats * Handle edge-case where -text is provided
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/util",
|
||||
"version": "0.92.0-beta.3",
|
||||
"version": "0.92.0",
|
||||
"description": "A collection of useful utilities for @polkadot",
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
|
||||
@@ -68,6 +68,20 @@ describe('formatBalance', () => {
|
||||
).toEqual('123.456 Unit');
|
||||
});
|
||||
|
||||
it('formats -123,456,789,000 (decimals=15)', () => {
|
||||
expect(
|
||||
formatBalance(new BN('-123456789000'), true, 15)
|
||||
).toEqual('-123.456µ Unit');
|
||||
});
|
||||
|
||||
describe('calcSi', () => {
|
||||
it('exposes calcSi on formatBalance', () => {
|
||||
expect(
|
||||
formatBalance.calcSi('12345').value
|
||||
).toEqual('k');
|
||||
});
|
||||
});
|
||||
|
||||
describe('findSi', () => {
|
||||
it('finds the SI value', () => {
|
||||
expect(
|
||||
|
||||
@@ -31,12 +31,20 @@ 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();
|
||||
let text = (input || '').toString();
|
||||
|
||||
if (text.length === 0 || text === '0') {
|
||||
return '0';
|
||||
}
|
||||
|
||||
// strip the negative sign so we can work with clean groupings, re-add this in the
|
||||
// end when we return the result (from here on we work with positive numbers)
|
||||
const isNegative = text[0] === '-';
|
||||
|
||||
if (isNegative) {
|
||||
text = text.substr(1);
|
||||
}
|
||||
|
||||
assert(/^\d+$/.test(text), `Non-integer input value '${text}' supplied to balanceFormat`);
|
||||
|
||||
// NOTE We start at midpoint (8) minus 1 - this means that values display as
|
||||
@@ -54,7 +62,7 @@ function _formatBalance (input?: number | string | BN, withSi: boolean = true, d
|
||||
)
|
||||
: '';
|
||||
|
||||
return `${formatDecimal(prefix || '0')}.${postfix}${units}`;
|
||||
return `${isNegative ? '-' : ''}${formatDecimal(prefix || '0')}.${postfix}${units}`;
|
||||
}
|
||||
|
||||
const formatBalance = _formatBalance as BalanceFormatter;
|
||||
|
||||
@@ -16,4 +16,12 @@ describe('formatDecimal', () => {
|
||||
it('returns input for non-decimal', () => {
|
||||
expect(formatDecimal('test')).toEqual('test');
|
||||
});
|
||||
|
||||
it('returns non-sensical negative text', () => {
|
||||
expect(formatDecimal('-test')).toEqual('-test');
|
||||
});
|
||||
|
||||
it('formats negative numbers', () => {
|
||||
expect(formatDecimal('-123456')).toEqual('-123,456');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -5,9 +5,14 @@
|
||||
const NUMBER_REGEX = new RegExp('(\\d+?)(?=(\\d{3})+(?!\\d)|$)', 'g');
|
||||
|
||||
export default function formatDecimal (value: string): string {
|
||||
const matched = value.match(NUMBER_REGEX);
|
||||
// We can do this by adjusting the regx, however for the sake of clarity
|
||||
// we rather strip and re-add the negative sign in the output
|
||||
const isNegative = value[0] === '-';
|
||||
const matched = isNegative
|
||||
? value.substr(1).match(NUMBER_REGEX)
|
||||
: value.match(NUMBER_REGEX);
|
||||
|
||||
return matched
|
||||
? matched.join(',')
|
||||
? `${isNegative ? '-' : ''}${matched.join(',')}`
|
||||
: value;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
// 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 formatNumber from './formatNumber';
|
||||
|
||||
describe('formatNumber', () => {
|
||||
it('formats empty', () => {
|
||||
expect(
|
||||
formatNumber()
|
||||
).toEqual('0');
|
||||
});
|
||||
|
||||
it('formats negative numbers', () => {
|
||||
expect(
|
||||
formatNumber(-123456)
|
||||
).toEqual('-123,456');
|
||||
});
|
||||
|
||||
it('formats BN numbers', () => {
|
||||
expect(
|
||||
formatNumber(new BN(12345))
|
||||
).toEqual('12,345');
|
||||
});
|
||||
|
||||
it('formats Compact<BN>', () => {
|
||||
expect(
|
||||
formatNumber({ toBn: () => new BN(12345) } as Compact)
|
||||
).toEqual('12,345');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user