Commit 4d0f43f8 authored by Noah Zinsmeister's avatar Noah Zinsmeister

export format functions

parent 169271b3
......@@ -2,3 +2,4 @@ export * from './types'
export { ChainId, WETH, TradeType } from './constants'
export * from './entities'
export * from './utils'
import invariant from 'tiny-invariant'
import JSBI from 'jsbi'
import _Decimal from 'decimal.js-light'
import _Big, { RoundingMode } from 'big.js'
import toFormat from 'toformat'
import { BigintIsh } from '../types'
import { parseBigintIsh } from './parseInputs'
const Decimal = toFormat(_Decimal)
const Big = toFormat(_Big)
export function formatSignificant(
numerator: JSBI,
denominator: JSBI,
numerator: BigintIsh,
denominator: BigintIsh,
significantDigits: number,
format: object = { groupSeparator: '' },
roundingMode?: number
roundingMode: number = Decimal.ROUND_HALF_UP,
minimumDecimalPlaces: number = 0
): string {
invariant(Number.isInteger(significantDigits), `${significantDigits} is not an integer.`)
invariant(significantDigits > 0, `${significantDigits} isn't positive.`)
const numeratorParsed = parseBigintIsh(numerator)
const denominatorParsed = parseBigintIsh(denominator)
Decimal.set({ precision: significantDigits + 1, rounding: roundingMode ?? Decimal.ROUND_HALF_UP })
const quotient = new Decimal(numerator.toString()).div(denominator.toString()).toSignificantDigits(significantDigits)
return quotient.toFormat(quotient.decimalPlaces(), format)
Decimal.set({ precision: significantDigits + 1, rounding: roundingMode })
const quotient = new Decimal(numeratorParsed.toString())
.div(denominatorParsed.toString())
.toSignificantDigits(significantDigits)
return quotient.toFormat(
minimumDecimalPlaces > quotient.decimalPlaces() ? minimumDecimalPlaces : quotient.decimalPlaces(),
format
)
}
export function formatFixed(
numerator: JSBI,
denominator: JSBI,
numerator: BigintIsh,
denominator: BigintIsh,
decimalPlaces: number,
format: object = { groupSeparator: '' },
roundingMode?: RoundingMode
roundingMode: RoundingMode = RoundingMode.RoundHalfUp
): string {
invariant(Number.isInteger(decimalPlaces), `${decimalPlaces} is not an integer.`)
invariant(decimalPlaces >= 0, `${decimalPlaces} is negative.`)
const numeratorParsed = parseBigintIsh(numerator)
const denominatorParsed = parseBigintIsh(denominator)
Big.DP = decimalPlaces
Big.RM = roundingMode ?? RoundingMode.RoundHalfUp
return new Big(numerator.toString()).div(denominator.toString()).toFormat(decimalPlaces, format)
Big.RM = roundingMode
return new Big(numeratorParsed.toString()).div(denominatorParsed.toString()).toFormat(decimalPlaces, format)
}
export { formatSignificant, formatFixed } from './formatOutputs'
......@@ -62,7 +62,7 @@ describe('entities', () => {
expect(route.output).toEqual(WETH)
})
it('Rate via Route.marketRate', () => {
it('Price via Route.marketRate', () => {
expect(route.midPrice.quote(decimalize(1, route.input.decimals)).toString()).toEqual(
decimalize(1234, route.output.decimals).toString()
)
......@@ -79,6 +79,7 @@ describe('entities', () => {
expect(route.midPrice.formatSignificant(4)).toEqual('1234')
expect(route.midPrice.formatSignificant(5)).toEqual('1234')
expect(route.midPrice.formatSignificant(4, { groupSeparator: ',' })).toEqual('1,234')
expect(route.midPrice.formatSignificant(4, undefined, undefined, 2)).toEqual('1234.00')
expect(route.midPrice.invert().formatSignificant(1)).toEqual('0.0008')
expect(route.midPrice.invert().formatSignificant(2)).toEqual('0.00081')
expect(route.midPrice.invert().formatSignificant(3)).toEqual('0.00081')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment