Skip to content

Commit

Permalink
chore: update formatting function.
Browse files Browse the repository at this point in the history
Signed-off-by: Eugene Panteleymonchuk <[email protected]>
  • Loading branch information
panteleymonchuk committed Jan 31, 2024
1 parent e37e3a2 commit 63b7dea
Showing 1 changed file with 26 additions and 37 deletions.
63 changes: 26 additions & 37 deletions api/src/routes/stardust/transactionhistory/download/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export const getTransactionHistoryRecords = (
timestamp: lastOutputTime,
dateFormatted: moment(lastOutputTime * 1000).format("YYYY-MM-DD HH:mm:ss"),
balanceChange,
balanceChangeFormatted: (isSpent ? "-" : "+") + formatAmount(Math.abs(balanceChange), tokenInfo, false, 2, true),
balanceChangeFormatted: formatAmount(Math.abs(balanceChange), tokenInfo, false),
outputs,
});
}
Expand Down Expand Up @@ -189,56 +189,45 @@ export const calculateBalanceChange = (outputs: OutputWithDetails[]) => {
/**
* Formats a numeric value into a string using token information and specified formatting rules.
*
* @param {number} value - The value to format.
* @param {INodeInfoBaseToken} tokenInfo - Information about the token, including units and decimals.
* @param {boolean} [formatFull=false] - If true, formats the entire number. Otherwise, uses decimalPlaces.
* @param {number} [decimalPlaces=2] - Number of decimal places in the formatted output.
* @param {boolean} [trailingDecimals] - Determines inclusion of trailing zeros in decimals.
* @returns {string} The formatted amount with the token unit.
* @param value - The value to format.
* @param tokenInfo - Information about the token, including units and decimals.
* @param formatFull - If true, formats the entire number. Otherwise, uses decimalPlaces.
* @param isSpent

Check warning on line 195 in api/src/routes/stardust/transactionhistory/download/post.ts

View workflow job for this annotation

GitHub Actions / lint-check (api)

Missing JSDoc @param "isSpent" description
* @returns The formatted amount with the token unit.
*/
export function formatAmount(
value: number,
tokenInfo: INodeInfoBaseToken,
formatFull: boolean = false,
decimalPlaces: number = 2,
trailingDecimals?: boolean,
): string {
export function formatAmount(value: number, tokenInfo: INodeInfoBaseToken, formatFull: boolean = false, isSpent: boolean = false): string {
const isSpentSymbol = isSpent ? "-" : "+";

if (formatFull) {
return `${value} ${tokenInfo.subunit ?? tokenInfo.unit}`;
return `${isSpentSymbol}${value} ${tokenInfo.subunit ?? tokenInfo.unit}`;
}

const baseTokenValue = value / Math.pow(10, tokenInfo.decimals);
const formattedAmount = toFixedNoRound(baseTokenValue, decimalPlaces, trailingDecimals);
const formattedAmount = cropNumber(baseTokenValue);

return `${formattedAmount} ${tokenInfo.unit}`;
return `${isSpentSymbol}${formattedAmount} ${tokenInfo.unit}`;
}

/**
* Format amount to two decimal places without rounding off.
* @param value The raw amount to format.
* @param precision The decimal places to show.
* @param trailingDecimals Whether to show trailing decimals.
* @returns The formatted amount.
* Crops the fractional part of a number to 6 digits.
* @param value The value to crop.
* @param decimalPlaces - The number of decimal places to include in the formatted output.
* @returns The cropped value.
*/
function toFixedNoRound(value: number, precision: number = 2, trailingDecimals?: boolean): string {
const defaultDecimals = "0".repeat(precision);
const valueString = `${value}`;
const [integer, fraction = defaultDecimals] = valueString.split(".");

if (fraction === defaultDecimals && !trailingDecimals) {
return valueString;
}
function cropNumber(value: number, decimalPlaces: number = 6): string {
const valueAsString = value.toString();

if (!precision) {
return integer;
if (!valueAsString.includes(".")) {
return valueAsString;
}

const truncatedFraction = fraction.slice(0, precision);
const [integerPart, rawFractionalPart] = valueAsString.split(".");
let fractionalPart = rawFractionalPart;

// avoid 0.00 case
if (!Number(truncatedFraction)) {
return `${integer}.${fraction}`;
if (fractionalPart.length > decimalPlaces) {
fractionalPart = fractionalPart.slice(0, 6);
}
fractionalPart = fractionalPart.replace(/0+$/, "");

return `${integer}.${truncatedFraction}`;
return fractionalPart ? `${integerPart}.${fractionalPart}` : integerPart;
}

0 comments on commit 63b7dea

Please sign in to comment.