1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-12-26 00:48:35 +00:00
trezor-wallet/src/utils/formatUtils.js

55 lines
1.4 KiB
JavaScript
Raw Normal View History

2017-12-13 11:01:37 +00:00
/* @flow */
// TODO: chagne currency units
2018-09-12 08:54:03 +00:00
const currencyUnitsConstant: string = 'mbtc2';
2017-12-13 11:01:37 +00:00
2018-09-21 08:46:50 +00:00
export const formatAmount = (n: number, coinInfo: any, currencyUnits: string = currencyUnitsConstant): string => {
2018-07-30 10:52:13 +00:00
const amount = (n / 1e8);
2017-12-13 11:01:37 +00:00
if (coinInfo.isBitcoin && currencyUnits === 'mbtc' && amount <= 0.1 && n !== 0) {
2018-07-30 10:52:13 +00:00
const s = (n / 1e5).toString();
2017-12-13 11:01:37 +00:00
return `${s} mBTC`;
}
2018-07-30 10:52:13 +00:00
const s = amount.toString();
2017-12-13 11:01:37 +00:00
return `${s} ${coinInfo.shortcut}`;
2018-07-30 10:52:13 +00:00
};
2017-12-13 11:01:37 +00:00
export const formatTime = (n: number): string => {
2018-07-30 10:52:13 +00:00
const hours = Math.floor(n / 60);
const minutes = n % 60;
2017-12-13 11:01:37 +00:00
if (!n) return 'No time estimate';
let res = '';
2018-09-05 12:54:24 +00:00
if (hours !== 0) {
2018-07-30 10:52:13 +00:00
res += `${hours} hour`;
2017-12-13 11:01:37 +00:00
if (hours > 1) {
res += 's';
}
res += ' ';
}
2018-09-06 15:04:28 +00:00
if (minutes !== 0) {
2018-07-30 10:52:13 +00:00
res += `${minutes} minutes`;
2017-12-13 11:01:37 +00:00
}
return res;
2018-07-30 10:52:13 +00:00
};
2017-12-13 11:01:37 +00:00
2018-07-30 10:52:13 +00:00
export const btckb2satoshib = (n: number): number => Math.round(n * 1e5);
2017-12-13 11:01:37 +00:00
export const stringToHex = (str: string): string => {
let result: string = '';
2018-04-16 21:19:50 +00:00
let hex: string;
2017-12-13 11:01:37 +00:00
for (let i = 0; i < str.length; i++) {
hex = str.charCodeAt(i).toString(16);
2018-07-30 10:52:13 +00:00
result += (`000${hex}`).slice(-4);
2017-12-13 11:01:37 +00:00
}
return result;
2018-07-30 10:52:13 +00:00
};
2017-12-13 11:01:37 +00:00
export const hexToString = (hex: string): string => {
2018-07-30 10:52:13 +00:00
let str = '';
for (let i = 0; i < hex.length; i += 2) {
const v = parseInt(hex.substr(i, 2), 16);
2017-12-13 11:01:37 +00:00
if (v) str += String.fromCharCode(v);
}
return str;
2018-07-30 10:52:13 +00:00
};