diff --git a/__tests__/utils.test.ts b/__tests__/utils.test.ts index c7ad7469..637a76d8 100644 --- a/__tests__/utils.test.ts +++ b/__tests__/utils.test.ts @@ -1,5 +1,7 @@ -import { displayAmount, displayNumber } from "../src/app/utils"; +import { displayNumber } from "../src/app/utils"; +// the separators are set to "." and " " for testing purposes +// inside jest.setup.js describe("displayAmount", () => { it("sends error message if noDigits is less than 4", () => { let digits = 3; @@ -8,23 +10,23 @@ describe("displayAmount", () => { ]; inputs.forEach(([input, expected]) => { - expect(displayAmount(input, digits)).toBe(expected); + expect(displayNumber(input, digits)).toBe(expected); }); digits = 0; inputs.forEach(([input, expected]) => { - expect(displayAmount(input, digits)).toBe(expected); + expect(displayNumber(input, digits)).toBe(expected); }); digits = -3; inputs.forEach(([input, expected]) => { - expect(displayAmount(input, digits)).toBe(expected); + expect(displayNumber(input, digits)).toBe(expected); }); digits = -10; inputs.forEach(([input, expected]) => { - expect(displayAmount(input, digits)).toBe(expected); + expect(displayNumber(input, digits)).toBe(expected); }); }); - it("displays amounts in 4 digits with thousands_separator", () => { + it("displays amounts in 4 digits", () => { const digits = 4; const inputs: [number, string][] = [ [0, "0"], @@ -54,47 +56,11 @@ describe("displayAmount", () => { ]; inputs.forEach(([input, expected]) => { - expect(displayAmount(input, digits)).toBe(expected); + expect(displayNumber(input, digits)).toBe(expected); }); }); - it("displays amounts in 4 digits without thousands_separator", () => { - const digits = 4; - const inputs: [number, string][] = [ - [0, "0"], - [0.1, "0.1"], - [0.101, "0.1"], - [0.12, "0.12"], - [0.123, "0.12"], - [0.1234, "0.12"], - [0.123456, "0.12"], - [0.1234567, "0.12"], - [0.12345678, "0.12"], - [0.123456789, "0.12"], - [1.101, "1.1"], - [1.1234567, "1.12"], - [12.1234567, "12.1"], - [123.1234567, "123"], - [1234.1234567, "1234"], - [1, "1"], - [12, "12"], - [123, "123"], - [1234, "1234"], - [1356, "1356"], - [34567, "34K"], - [123456, "123K"], - [1234567, "1.2M"], - [12345678, "12M"], - [123456789, "123M"], - [1234567890, "1.2B"], - ]; - - inputs.forEach(([input, expected]) => { - expect(displayAmount(input, digits, -1, ".", "")).toBe(expected); - }); - }); - - it("displays amounts in 6 digits with thousands_separator", () => { + it("displays amounts in 6 digits", () => { const digits = 6; const inputs: [number, string][] = [ [0, "0"], @@ -127,11 +93,11 @@ describe("displayAmount", () => { ]; inputs.forEach(([input, expected]) => { - expect(displayAmount(input, digits, -1, ".", " ")).toBe(expected); + expect(displayNumber(input, digits, -1)).toBe(expected); }); }); - it("displays amounts in 10 digits with thousands_separator", () => { + it("displays amounts in 10 digits", () => { const digits = 10; const inputs: [number, string][] = [ [0, "0"], @@ -165,11 +131,11 @@ describe("displayAmount", () => { ]; inputs.forEach(([input, expected]) => { - expect(displayAmount(input, digits, -1, ".", " ")).toBe(expected); + expect(displayNumber(input, digits, -1)).toBe(expected); }); }); - it("displays amounts in 6 digits with thousands_separator and fixed decimals = 3", () => { + it("displays amounts in 6 digits with fixed decimals = 3", () => { const digits = 6; const inputs: [number, string][] = [ [0, "0.000"], @@ -203,11 +169,11 @@ describe("displayAmount", () => { ]; inputs.forEach(([input, expected]) => { - expect(displayAmount(input, digits, 3, ".", " ")).toBe(expected); + expect(displayNumber(input, digits, 3)).toBe(expected); }); }); - it("displays amounts in 10 digits with thousands_separator and fixed decimals = 3", () => { + it("displays amounts in 10 digits with fixed decimals = 3", () => { const digits = 10; const inputs: [number, string][] = [ [0, "0.000"], @@ -241,39 +207,7 @@ describe("displayAmount", () => { ]; inputs.forEach(([input, expected]) => { - expect(displayAmount(input, digits, 3, ".", " ")).toBe(expected); - }); - }); -}); - -describe("displayNumber", () => { - it("formats all numbers to fixed decimal places", () => { - const inputs = [ - 1002, 1248, 99.95, 88, 77.7, 1000000.1, 0.03, 0.000004, 0.1, - ]; - const expected = [ - "1K", - "1.25K", - "99.95000", - "88.00000", - "77.70000", - "1M", - "0.03000", - "0.00000", - "0.10000", - ]; - - inputs.forEach((input, index) => { - expect(displayNumber(input, 5)).toBe(expected[index]); - }); - }); - - it("rounds decimals correctly", () => { - const inputs = [0.233, 0.235]; - const expected = ["0.23", "0.24"]; - - inputs.forEach((input, index) => { - expect(displayNumber(input, 2)).toBe(expected[index]); + expect(displayNumber(input, digits, 3)).toBe(expected); }); }); }); diff --git a/src/app/components/OrderBook.tsx b/src/app/components/OrderBook.tsx index 0de976e4..b5a9efea 100644 --- a/src/app/components/OrderBook.tsx +++ b/src/app/components/OrderBook.tsx @@ -5,6 +5,8 @@ import * as utils from "../utils"; import { OrderBookRowProps, orderBookSlice } from "../redux/orderBookSlice"; import { useAppDispatch, useAppSelector } from "../hooks"; +const N_DIGITS = 8; + function OrderBookRow(props: OrderBookRowProps) { const { barColor, orderCount, price, size, total, maxTotal } = props; if ( @@ -16,9 +18,9 @@ function OrderBookRow(props: OrderBookRowProps) { typeof maxTotal !== "undefined" ) { const charactersToDisplay = 6; - const priceString = utils.displayAmount(price, charactersToDisplay); - const sizeString = utils.displayAmount(size, charactersToDisplay); - const totalString = utils.displayAmount(total, charactersToDisplay); + const priceString = utils.displayNumber(price, charactersToDisplay); + const sizeString = utils.displayNumber(size, charactersToDisplay); + const totalString = utils.displayNumber(total, charactersToDisplay); const barWidth = `${(total / maxTotal) * 100}%`; const barStyle = { @@ -70,10 +72,12 @@ function CurrentPriceRow() { if (orderBook.spreadPercent !== null && orderBook.spread !== null) { const spread = utils.displayPositiveNumber( orderBook.spread, + N_DIGITS, priceMaxDecimals ); const spreadPercent = utils.displayPositiveNumber( orderBook.spreadPercent, + N_DIGITS, 2 ); diff --git a/src/app/components/PriceChart.tsx b/src/app/components/PriceChart.tsx index d0aa93ca..ad195645 100644 --- a/src/app/components/PriceChart.tsx +++ b/src/app/components/PriceChart.tsx @@ -9,7 +9,7 @@ import { initializeLegend, } from "../redux/priceChartSlice"; import { useAppDispatch, useAppSelector } from "../hooks"; -import { displayAmount } from "../utils"; +import { displayNumber } from "../utils"; import * as tailwindConfig from "../../../tailwind.config"; interface PriceChartProps { @@ -37,29 +37,29 @@ function PriceChartCanvas(props: PriceChartProps) { const noDigits = 8; const fixedDecimals = 6; - const volume = displayAmount(props.volume || 0, noDigits, 2); - const percChange = displayAmount(props.percChange || 0, noDigits, 2); - const change = displayAmount(props.change || 0, noDigits, 2); + const volume = displayNumber(props.volume || 0, noDigits, 2); + const percChange = displayNumber(props.percChange || 0, noDigits, 2); + const change = displayNumber(props.change || 0, noDigits, 2); - const candleOpen = displayAmount( + const candleOpen = displayNumber( candlePrice?.open || 0, noDigits, fixedDecimals ); - const candleHigh = displayAmount( + const candleHigh = displayNumber( candlePrice?.high || 0, noDigits, fixedDecimals ); - const candleLow = displayAmount( + const candleLow = displayNumber( candlePrice?.low || 0, noDigits, fixedDecimals ); - const candleClose = displayAmount( + const candleClose = displayNumber( candlePrice?.close || 0, noDigits, fixedDecimals diff --git a/src/app/components/PriceInfo.tsx b/src/app/components/PriceInfo.tsx index 49880f7e..50649ef2 100644 --- a/src/app/components/PriceInfo.tsx +++ b/src/app/components/PriceInfo.tsx @@ -1,16 +1,16 @@ import React from "react"; import { useAppSelector } from "../hooks"; -import { displayAmount } from "../utils"; +import { displayNumber } from "../utils"; export function PriceInfo() { const priceInfo = useAppSelector((state) => state.priceInfo); const noDigits = 4; const fixedDecimals = 3; - const lastPrice = displayAmount(priceInfo.lastPrice, noDigits, fixedDecimals); - const change = displayAmount(priceInfo.change24h, noDigits, fixedDecimals); - const high = displayAmount(priceInfo.high24h, noDigits, fixedDecimals); - const low = displayAmount(priceInfo.low24h, noDigits, fixedDecimals); - const volume = displayAmount(priceInfo.value24h, noDigits, fixedDecimals); + const lastPrice = displayNumber(priceInfo.lastPrice, noDigits, fixedDecimals); + const change = displayNumber(priceInfo.change24h, noDigits, fixedDecimals); + const high = displayNumber(priceInfo.high24h, noDigits, fixedDecimals); + const low = displayNumber(priceInfo.low24h, noDigits, fixedDecimals); + const volume = displayNumber(priceInfo.value24h, noDigits, fixedDecimals); const isNegativeOrZero = priceInfo.isNegativeOrZero; const basePair = "XRD"; diff --git a/src/app/components/order_input/AmountInput.tsx b/src/app/components/order_input/AmountInput.tsx index 6b61ea1d..839431bc 100644 --- a/src/app/components/order_input/AmountInput.tsx +++ b/src/app/components/order_input/AmountInput.tsx @@ -10,6 +10,7 @@ import { selectValidationByAddress, } from "redux/orderInputSlice"; import { BottomRightErrorLabel } from "components/BottomRightErrorLabel"; +import { getLocaleSeparators } from "utils"; export const enum PayReceive { PAY = "YOU PAY:", @@ -43,14 +44,8 @@ export function AmountInput(props: TokenInputFiledProps) { onChange, } = props; - // TODO: after https://github.com/DeXter-on-Radix/website/pull/159 - // read this from the state instead - const getDecimalSeparator = () => { - const numberWithDecimal = 1.1; - return numberWithDecimal.toLocaleString().substring(1, 2); - }; - - const placeholder = `0${getDecimalSeparator()}0`; + const { decimalSeparator } = getLocaleSeparators(); + const placeholder = `0${decimalSeparator}0`; return (