Browse Source

feature(ticker-usd-format): format USD price with symbol and two decimal places

renovate/lint-staged-8.x
Travis Loncar 7 years ago
parent
commit
dd74961651
No known key found for this signature in database GPG Key ID: E3CCB95C24FCC88C
  1. 4
      app/routes/app/components/components/Nav.js
  2. 5
      app/utils/usd.js
  3. 30
      test/utils/usd.spec.js

4
app/routes/app/components/components/Nav.js

@ -6,7 +6,7 @@ import { MdAccountBalanceWallet } from 'react-icons/lib/md'
import { FaClockO, FaDollar } from 'react-icons/lib/fa'
import CryptoIcon from '../../../../components/CryptoIcon'
import CurrencyIcon from '../../../../components/CurrencyIcon'
import { btc } from '../../../../utils'
import { btc, usd } from '../../../../utils'
import styles from './Nav.scss'
const Nav = ({ ticker, balance, setCurrency, formClicked, currentTicker }) => (
@ -14,7 +14,7 @@ const Nav = ({ ticker, balance, setCurrency, formClicked, currentTicker }) => (
<ul className={styles.info}>
<li className={`${styles.currencies} ${styles.link}`}>
<span
data-hint={currentTicker ? currentTicker.price_usd : null}
data-hint={currentTicker ? usd.formatUsd(currentTicker.price_usd) : null}
className={`${styles.currency} ${ticker.currency === ticker.crypto ? styles.active : ''} hint--bottom`}
onClick={() => setCurrency(ticker.crypto)}
>

5
app/utils/usd.js

@ -1,3 +1,7 @@
export function formatUsd(usd) {
return `$${(+usd).toFixed(2)}`
}
export function usdToBtc(usd, rate) {
if (usd === undefined || usd === null || usd === '') return null
@ -5,5 +9,6 @@ export function usdToBtc(usd, rate) {
}
export default {
formatUsd,
usdToBtc
}

30
test/utils/usd.spec.js

@ -0,0 +1,30 @@
import {
formatUsd,
usdToBtc
} from '../../app/utils/usd'
describe('usd', () => {
describe('formatUsd', () => {
it('should handle a string value', () => {
expect(formatUsd('42.0')).toBe('$42.00')
})
it('should handle a numerical value', () => {
expect(formatUsd(42.0)).toBe('$42.00')
})
it('should round to two decimal places', () => {
expect(formatUsd('42.416')).toBe('$42.42')
})
})
describe('usdToBtc', () => {
it('should convert USD to BTC using rate', () => {
expect(usdToBtc(1, 50)).toBe('0.02000000')
})
it('should round to eight decimal places', () => {
expect(usdToBtc(2, 3)).toBe('0.66666667')
})
})
})
Loading…
Cancel
Save