mirror of
https://github.com/trezor/trezor-wallet
synced 2024-11-13 20:08:56 +00:00
Test format utils, remove unused code
This commit is contained in:
parent
921ca49535
commit
366e5f4d10
@ -1,32 +1,24 @@
|
||||
import * as utils from '../formatUtils';
|
||||
|
||||
describe('format utils', () => {
|
||||
// TODO: check this weird function
|
||||
it('formatAmount', () => {
|
||||
expect(utils.formatAmount(0, { isBitcoin: false, shortcut: 'mbtc' }, 'mbtc')).toBe('0 mbtc');
|
||||
expect(utils.formatAmount(1000000, { isBitcoin: true }, 'mbtc')).toBe('10 mBTC');
|
||||
expect(utils.formatAmount(0.5, { isBitcoin: true }, 'mbtc')).toBe('0.000005 mBTC');
|
||||
expect(utils.formatAmount(1, { isBitcoin: false, shortcut: 'eth' }, null)).toBe('1e-8 eth');
|
||||
expect(utils.formatAmount(99999, { isBitcoin: false, shortcut: 'tau' }, null)).toBe('0.00099999 tau');
|
||||
it('to decimal amount', () => {
|
||||
expect(utils.toDecimalAmount(0, 1)).toBe('0');
|
||||
expect(utils.toDecimalAmount(1, 1)).toBe('0.1');
|
||||
expect(utils.toDecimalAmount(1000, 1)).toBe('100');
|
||||
expect(utils.toDecimalAmount(1000, 2)).toBe('10');
|
||||
expect(utils.toDecimalAmount(1000, 3)).toBe('1');
|
||||
expect(utils.toDecimalAmount('1', 'a')).toBe('NaN');
|
||||
expect(utils.toDecimalAmount('a', 'a')).toBe('0');
|
||||
expect(utils.toDecimalAmount('a', '1')).toBe('0');
|
||||
});
|
||||
|
||||
it('btckb2satoshib', () => {
|
||||
expect(utils.btckb2satoshib(0)).toBe(0);
|
||||
expect(utils.btckb2satoshib(1)).toBe(100000);
|
||||
expect(utils.btckb2satoshib(2)).toBe(200000);
|
||||
expect(utils.btckb2satoshib(100)).toBe(10000000);
|
||||
expect(utils.btckb2satoshib(999)).toBe(99900000);
|
||||
});
|
||||
|
||||
it('string to hex', () => {
|
||||
expect(utils.stringToHex('test')).toBe('0074006500730074');
|
||||
expect(utils.stringToHex('0001')).toBe('0030003000300031');
|
||||
expect(utils.stringToHex('test99999')).toBe('007400650073007400390039003900390039');
|
||||
});
|
||||
|
||||
it('hex to string', () => {
|
||||
expect(utils.hexToString('0074006500730074')).toBe('test');
|
||||
expect(utils.hexToString('0030003000300031')).toBe('0001');
|
||||
expect(utils.hexToString('007400650073007400390039003900390039')).toBe('test99999');
|
||||
it('from decimal amount', () => {
|
||||
expect(utils.fromDecimalAmount(0, 1)).toBe('0');
|
||||
expect(utils.fromDecimalAmount(10, 1)).toBe('100');
|
||||
expect(utils.fromDecimalAmount(10, 2)).toBe('1000');
|
||||
expect(utils.fromDecimalAmount(10, 3)).toBe('10000');
|
||||
expect(utils.fromDecimalAmount('1', 'a')).toBe('NaN');
|
||||
expect(utils.fromDecimalAmount('a', 'a')).toBe('0');
|
||||
expect(utils.fromDecimalAmount('a', '1')).toBe('0');
|
||||
});
|
||||
});
|
||||
|
@ -2,39 +2,6 @@
|
||||
|
||||
import BigNumber from 'bignumber.js';
|
||||
|
||||
const currencyUnitsConstant: string = 'mbtc2';
|
||||
|
||||
export const formatAmount = (n: number, coinInfo: any, currencyUnits: string = currencyUnitsConstant): string => {
|
||||
const amount = (n / 1e8);
|
||||
if (coinInfo.isBitcoin && currencyUnits === 'mbtc' && amount <= 0.1 && n !== 0) {
|
||||
const s = (n / 1e5).toString();
|
||||
return `${s} mBTC`;
|
||||
}
|
||||
const s = amount.toString();
|
||||
return `${s} ${coinInfo.shortcut}`;
|
||||
};
|
||||
|
||||
export const btckb2satoshib = (n: number): number => Math.round(n * 1e5);
|
||||
|
||||
export const stringToHex = (str: string): string => {
|
||||
let result: string = '';
|
||||
let hex: string;
|
||||
for (let i = 0; i < str.length; i++) {
|
||||
hex = str.charCodeAt(i).toString(16);
|
||||
result += (`000${hex}`).slice(-4);
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
export const hexToString = (hex: string): string => {
|
||||
let str = '';
|
||||
for (let i = 0; i < hex.length; i += 2) {
|
||||
const v = parseInt(hex.substr(i, 2), 16);
|
||||
if (v) str += String.fromCharCode(v);
|
||||
}
|
||||
return str;
|
||||
};
|
||||
|
||||
export const toDecimalAmount = (amount: string | number, decimals: number): string => {
|
||||
try {
|
||||
const bAmount = new BigNumber(amount);
|
||||
|
Loading…
Reference in New Issue
Block a user