1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Move shorten balance to util. Add as property of generateBalance object output.

This commit is contained in:
Kevin Serrano 2016-07-11 16:31:12 -07:00
parent 37f21cf18f
commit 6a61591dad
3 changed files with 28 additions and 23 deletions

View File

@ -14,8 +14,10 @@ function EthBalanceComponent () {
EthBalanceComponent.prototype.render = function () { EthBalanceComponent.prototype.render = function () {
var state = this.props var state = this.props
var style = state.style var style = state.style
var value = formatBalance(state.value)
var maxWidth = state.maxWidth const value = formatBalance(state.value)
var width = state.width
return ( return (
h('.ether-balance', { h('.ether-balance', {
@ -24,7 +26,7 @@ EthBalanceComponent.prototype.render = function () {
h('.ether-balance-amount', { h('.ether-balance-amount', {
style: { style: {
display: 'inline', display: 'inline',
maxWidth: maxWidth, width: width,
}, },
}, this.renderBalance(value, state)), }, this.renderBalance(value, state)),
]) ])
@ -34,11 +36,12 @@ EthBalanceComponent.prototype.render = function () {
EthBalanceComponent.prototype.renderBalance = function (value, state) { EthBalanceComponent.prototype.renderBalance = function (value, state) {
if (value === 'None') return value if (value === 'None') return value
var balanceObj = generateBalanceObject(value) var balanceObj = generateBalanceObject(value)
var balance
var balance = balanceObj.balance
if (state.shorten) { if (state.shorten) {
balance = shortenBalance(balance) balance = balanceObj.shortBalance
} else {
balance = balanceObj.balance
} }
var label = balanceObj.label var label = balanceObj.label
@ -59,6 +62,7 @@ EthBalanceComponent.prototype.renderBalance = function (value, state) {
h('div', { h('div', {
style: { style: {
width: '100%', width: '100%',
textAlign: 'right',
}, },
}, balance), }, balance),
h('div', { h('div', {
@ -71,17 +75,3 @@ EthBalanceComponent.prototype.renderBalance = function (value, state) {
]) ])
) )
} }
function shortenBalance (balance) {
var truncatedValue
var convertedBalance = parseFloat(balance)
if (convertedBalance > 1000000) {
truncatedValue = (balance / 1000000).toFixed(1)
return `${truncatedValue}m`
} else if (convertedBalance > 1000) {
truncatedValue = (balance / 1000).toFixed(1)
return `${truncatedValue}k`
} else {
return balance
}
}

View File

@ -73,7 +73,7 @@ TransactionListItem.prototype.render = function () {
isTx ? h(EtherBalance, { isTx ? h(EtherBalance, {
value: txParams.value, value: txParams.value,
maxWidth: '55px', width: '55px',
shorten: true, shorten: true,
}) : h('.flex-column'), }) : h('.flex-column'),
]) ])

View File

@ -85,7 +85,6 @@ function parseBalance (balance) {
const trailingZeros = /0+$/ const trailingZeros = /0+$/
beforeDecimal = weiString.length > 18 ? weiString.slice(0, weiString.length - 18) : '0' beforeDecimal = weiString.length > 18 ? weiString.slice(0, weiString.length - 18) : '0'
// We don't use weiToEth here because we need to maintain decimal precision.
afterDecimal = ('000000000000000000' + wei).slice(-18).replace(trailingZeros, '') afterDecimal = ('000000000000000000' + wei).slice(-18).replace(trailingZeros, '')
if (afterDecimal === '') { afterDecimal = '0' } if (afterDecimal === '') { afterDecimal = '0' }
return [beforeDecimal, afterDecimal] return [beforeDecimal, afterDecimal]
@ -115,15 +114,31 @@ function formatBalance (balance, decimalsToKeep) {
return formatted return formatted
} }
function generateBalanceObject (formattedBalance) { function generateBalanceObject (formattedBalance) {
var balance = formattedBalance.split(' ')[0] var balance = formattedBalance.split(' ')[0]
var label = formattedBalance.split(' ')[1] var label = formattedBalance.split(' ')[1]
var beforeDecimal = balance.split('.')[0] var beforeDecimal = balance.split('.')[0]
var afterDecimal = balance.split('.')[1] var afterDecimal = balance.split('.')[1]
var shortBalance = shortenBalance(balance)
if (beforeDecimal === '0' && afterDecimal.substr(0, 5) === '00000') { balance = '< 0.00001' } if (beforeDecimal === '0' && afterDecimal.substr(0, 5) === '00000') { balance = '< 0.00001' }
return { balance, label } return { balance, label, shortBalance }
}
function shortenBalance (balance) {
var truncatedValue
var convertedBalance = parseFloat(balance)
if (convertedBalance > 1000000) {
truncatedValue = (balance / 1000000).toFixed(1)
return `>${truncatedValue}m`
} else if (convertedBalance > 1000) {
truncatedValue = (balance / 1000).toFixed(1)
return `>${truncatedValue}k`
} else {
return balance
}
} }
function dataSize (data) { function dataSize (data) {