mirror of
https://github.com/pezkuwichain/pezkuwi-ui.git
synced 2026-06-14 10:11:03 +00:00
Add ui-util formatting & util (#76)
* Add ui-util formatting & util * Update README
This commit is contained in:
@@ -22,8 +22,8 @@
|
|||||||
"react": "*"
|
"react": "*"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@polkadot/keyring": "^0.34.15",
|
"@polkadot/keyring": "^0.34.19",
|
||||||
"@polkadot/util-crypto": "^0.34.15",
|
"@polkadot/util-crypto": "^0.34.19",
|
||||||
"xmlserializer": "^0.6.1"
|
"xmlserializer": "^0.6.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,9 +16,9 @@
|
|||||||
"store": "^2.0.12"
|
"store": "^2.0.12"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@polkadot/keyring": "^0.34.15",
|
"@polkadot/keyring": "^0.34.19",
|
||||||
"@polkadot/types": "^0.43.6",
|
"@polkadot/types": "^0.43.10",
|
||||||
"@polkadot/util": "^0.34.15"
|
"@polkadot/util": "^0.34.19"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"@polkadot/keyring": "*",
|
"@polkadot/keyring": "*",
|
||||||
|
|||||||
@@ -0,0 +1,201 @@
|
|||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
|
||||||
|
1. Definitions.
|
||||||
|
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
|
the copyright owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
|
||||||
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
|
||||||
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
|
||||||
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
APPENDIX: How to apply the Apache License to your work.
|
||||||
|
|
||||||
|
To apply the Apache License to your work, attach the following
|
||||||
|
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||||
|
replaced with your own identifying information. (Don't include
|
||||||
|
the brackets!) The text should be enclosed in the appropriate
|
||||||
|
comment syntax for the file format. We also recommend that a
|
||||||
|
file or class name and description of purpose be included on the
|
||||||
|
same "printed page" as the copyright notice for easier
|
||||||
|
identification within third-party archives.
|
||||||
|
|
||||||
|
Copyright [yyyy] [name of copyright owner]
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
# @polkadot/ui-util
|
||||||
|
|
||||||
|
Various utility and formatting functions
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
{
|
||||||
|
"name": "@polkadot/ui-util",
|
||||||
|
"version": "0.26.3",
|
||||||
|
"description": "Utility functions (non-React) that aid in display, formats and determination",
|
||||||
|
"main": "index.js",
|
||||||
|
"author": "Jaco Greeff <jacogr@gmail.com>",
|
||||||
|
"maintainers": [
|
||||||
|
"Jaco Greeff <jacogr@gmail.com>"
|
||||||
|
],
|
||||||
|
"license": "Apache-2.0",
|
||||||
|
"dependencies": {
|
||||||
|
"@babel/runtime": "^7.3.1"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@polkadot/types": "^0.43.10"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"@polkadot/types": "*"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,121 @@
|
|||||||
|
// 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' }
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,117 @@
|
|||||||
|
// 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 formatDecimal from './formatDecimal';
|
||||||
|
|
||||||
|
type SiDef = {
|
||||||
|
power: number,
|
||||||
|
text: string,
|
||||||
|
value: string
|
||||||
|
};
|
||||||
|
|
||||||
|
type Defaults = {
|
||||||
|
decimals?: number,
|
||||||
|
unit?: string
|
||||||
|
};
|
||||||
|
|
||||||
|
interface BalanceFormatter {
|
||||||
|
(input?: number | string | BN, withSi?: boolean, decimals?: number): string;
|
||||||
|
findSi (type: string): SiDef;
|
||||||
|
getDefaults (): Defaults;
|
||||||
|
getOptions (decimals?: number): Array<SiDef>;
|
||||||
|
setDefaults (defaults: Defaults): void;
|
||||||
|
}
|
||||||
|
|
||||||
|
const SI_MID = 8;
|
||||||
|
const DEFAULT_DECIMALS = 0;
|
||||||
|
const DEFAULT_UNIT = 'Unit';
|
||||||
|
|
||||||
|
let defaultDecimals = DEFAULT_DECIMALS;
|
||||||
|
let defaultUnit = DEFAULT_UNIT;
|
||||||
|
|
||||||
|
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: 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 <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;
|
||||||
|
|
||||||
|
// 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.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 }: Defaults): void => {
|
||||||
|
defaultDecimals = isUndefined(decimals) ? defaultDecimals : decimals;
|
||||||
|
defaultUnit = isUndefined(unit) ? defaultUnit : unit;
|
||||||
|
|
||||||
|
SI[SI_MID].text = defaultUnit;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default formatBalance;
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
// 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');
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
// 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;
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
// 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');
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -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.
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
// 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());
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// 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 { 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';
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
// 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)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
// 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());
|
||||||
|
}
|
||||||
@@ -1452,11 +1452,6 @@
|
|||||||
universal-user-agent "^2.0.0"
|
universal-user-agent "^2.0.0"
|
||||||
url-template "^2.0.8"
|
url-template "^2.0.8"
|
||||||
|
|
||||||
"@parity/schnorrkel-js@^0.1.2":
|
|
||||||
version "0.1.2"
|
|
||||||
resolved "https://registry.yarnpkg.com/@parity/schnorrkel-js/-/schnorrkel-js-0.1.2.tgz#08c9663299cbe43d292599a1312a761b2426900f"
|
|
||||||
integrity sha512-eQSCY6kb16Lhnlgdz1FbADo1PEI3Xgp0ApGpNydf0yBEQ6AMVo1YN/4zMA3lFjqnurv9NsAy11OJApailUNneQ==
|
|
||||||
|
|
||||||
"@polkadot/dev-react@^0.24.2":
|
"@polkadot/dev-react@^0.24.2":
|
||||||
version "0.24.2"
|
version "0.24.2"
|
||||||
resolved "https://registry.yarnpkg.com/@polkadot/dev-react/-/dev-react-0.24.2.tgz#e8c6c7b031a1933eb93bf133f9ac401944bdefed"
|
resolved "https://registry.yarnpkg.com/@polkadot/dev-react/-/dev-react-0.24.2.tgz#e8c6c7b031a1933eb93bf133f9ac401944bdefed"
|
||||||
@@ -1533,14 +1528,14 @@
|
|||||||
typedoc-plugin-no-inherit "^1.1.6"
|
typedoc-plugin-no-inherit "^1.1.6"
|
||||||
typescript "^3.3.3"
|
typescript "^3.3.3"
|
||||||
|
|
||||||
"@polkadot/keyring@^0.34.15":
|
"@polkadot/keyring@^0.34.19":
|
||||||
version "0.34.15"
|
version "0.34.19"
|
||||||
resolved "https://registry.yarnpkg.com/@polkadot/keyring/-/keyring-0.34.15.tgz#ba8d1293cbfdd70dd9ef7d9568fd9303314cee54"
|
resolved "https://registry.yarnpkg.com/@polkadot/keyring/-/keyring-0.34.19.tgz#86caadfa4fcc0efb799749f73a5486a1a946161f"
|
||||||
integrity sha512-unMuFK5i8w7g1w10gPHqSgHNHMLrrr33eBkIZg5DwKjbHphhBa3vdUpLR72Gsrc3W285y8qZIUhC8E/SUAR7eg==
|
integrity sha512-HLlAeQKqytX0YTHag0u51S1n4BC1nORtrV4s+EsMQ7//DVE6iuksTSRU0eOv80tinPSKkKv0v0aaZ+3y+HpJXw==
|
||||||
dependencies:
|
dependencies:
|
||||||
"@babel/runtime" "^7.3.1"
|
"@babel/runtime" "^7.3.1"
|
||||||
"@polkadot/util" "^0.34.15"
|
"@polkadot/util" "^0.34.19"
|
||||||
"@polkadot/util-crypto" "^0.34.15"
|
"@polkadot/util-crypto" "^0.34.19"
|
||||||
"@types/bs58" "^4.0.0"
|
"@types/bs58" "^4.0.0"
|
||||||
bs58 "^4.0.1"
|
bs58 "^4.0.1"
|
||||||
|
|
||||||
@@ -1549,24 +1544,23 @@
|
|||||||
resolved "https://registry.yarnpkg.com/@polkadot/ts/-/ts-0.1.52.tgz#6fb09e494d244dad4b3a9ca581bfe13beec66565"
|
resolved "https://registry.yarnpkg.com/@polkadot/ts/-/ts-0.1.52.tgz#6fb09e494d244dad4b3a9ca581bfe13beec66565"
|
||||||
integrity sha512-sij1O0x4CY51A394RYD4/aQwDPwIxIeTOpYI4AZgdF/vq5nvF14b4XFq9vAcSnblaIosf0sYKoz1f3dkN3QqLw==
|
integrity sha512-sij1O0x4CY51A394RYD4/aQwDPwIxIeTOpYI4AZgdF/vq5nvF14b4XFq9vAcSnblaIosf0sYKoz1f3dkN3QqLw==
|
||||||
|
|
||||||
"@polkadot/types@^0.43.6":
|
"@polkadot/types@^0.43.10":
|
||||||
version "0.43.6"
|
version "0.43.10"
|
||||||
resolved "https://registry.yarnpkg.com/@polkadot/types/-/types-0.43.6.tgz#0878672be0e4bfd898558af574638c5d1247f282"
|
resolved "https://registry.yarnpkg.com/@polkadot/types/-/types-0.43.10.tgz#eccba24b938a36e193d789e52f21786732db0bc1"
|
||||||
integrity sha512-SJBIcjdDH16zVUqKZ6+wknBA91SgxI8dNobXqDfqOel7KxYXHcXnyHgWgHNzvGMATSoDMZS8zBqs5w455AwthA==
|
integrity sha512-6yUfy2fmGIfTZXT0ff/RXXD9uHt7PjwwKH+LTi/tTPOKNJy7P9ltVVfbaMIi9J9DgBrTLYgJssGUHHvCjmXX2A==
|
||||||
dependencies:
|
dependencies:
|
||||||
"@babel/runtime" "^7.3.1"
|
"@babel/runtime" "^7.3.1"
|
||||||
"@polkadot/keyring" "^0.34.15"
|
"@polkadot/keyring" "^0.34.19"
|
||||||
"@polkadot/util" "^0.34.15"
|
"@polkadot/util" "^0.34.19"
|
||||||
core-js "^2.6.5"
|
core-js "^2.6.5"
|
||||||
|
|
||||||
"@polkadot/util-crypto@^0.34.15":
|
"@polkadot/util-crypto@^0.34.19":
|
||||||
version "0.34.15"
|
version "0.34.19"
|
||||||
resolved "https://registry.yarnpkg.com/@polkadot/util-crypto/-/util-crypto-0.34.15.tgz#528a98d73a744b220e43f1f220140c866a886f39"
|
resolved "https://registry.yarnpkg.com/@polkadot/util-crypto/-/util-crypto-0.34.19.tgz#776fbefe5bee80f32281d6b61c2cb8d5c608f03d"
|
||||||
integrity sha512-UO+8yb8obHA0BxjCdHsnMRxL61SJ/kzQ2gGAOMPf3k0CgrNWlNF+SfBiZhaPkf+nqyRJPS6R3SxC7Pl6PrynNA==
|
integrity sha512-tY5V7GPAt8gHNlpGDV787wnZefvQeIxNbC/eo0liE/WEaCaU4DRbvV3VnbcsRlBvLg4tpuqSqlXYMELoqTNSig==
|
||||||
dependencies:
|
dependencies:
|
||||||
"@babel/runtime" "^7.3.1"
|
"@babel/runtime" "^7.3.1"
|
||||||
"@parity/schnorrkel-js" "^0.1.2"
|
"@polkadot/util" "^0.34.19"
|
||||||
"@polkadot/util" "^0.34.15"
|
|
||||||
"@types/bip39" "^2.4.2"
|
"@types/bip39" "^2.4.2"
|
||||||
"@types/webassembly-js-api" "^0.0.2"
|
"@types/webassembly-js-api" "^0.0.2"
|
||||||
"@types/xxhashjs" "^0.2.0"
|
"@types/xxhashjs" "^0.2.0"
|
||||||
@@ -1576,10 +1570,10 @@
|
|||||||
tweetnacl "^1.0.1"
|
tweetnacl "^1.0.1"
|
||||||
xxhashjs "^0.2.2"
|
xxhashjs "^0.2.2"
|
||||||
|
|
||||||
"@polkadot/util@^0.34.15":
|
"@polkadot/util@^0.34.19":
|
||||||
version "0.34.15"
|
version "0.34.19"
|
||||||
resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-0.34.15.tgz#ea7168748fab68244d915f9e88cb1d5cdad27023"
|
resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-0.34.19.tgz#46e690e2cc732a64e0a1ba859135afc9cad844ab"
|
||||||
integrity sha512-PkmLRZq+XQc27RHTtvaP+V8uWVR5cT1Y6spVKMlEadYreQtesfUNN5Ujaj9zCFuMnmV3Ye89bYqTvaA4cNy99A==
|
integrity sha512-bLWXrkrZr4aNMvpdsElE3G4ZtJFV+1zXciOFM9jTHmVMt1f5KCHO6BdNkLC16RPToGmMDYB0bD+waTadQ6QW6g==
|
||||||
dependencies:
|
dependencies:
|
||||||
"@babel/runtime" "^7.3.1"
|
"@babel/runtime" "^7.3.1"
|
||||||
"@types/bn.js" "^4.11.4"
|
"@types/bn.js" "^4.11.4"
|
||||||
|
|||||||
Reference in New Issue
Block a user