All files / packages/app/hooks use-decimals.ts

100% Statements 11/11
100% Branches 0/0
100% Functions 6/6
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31              2991x   112x 2879x 2879x   48986x       13836x           112x 112x 17301x   2745x      
import React from "react";
 
import {
	debtAmountSchemaDecimal,
	partSchemaDecimal,
} from "~app/utils/validation";
 
const getDecimalsPower = (decimalDigits: number) => 10 ** decimalDigits;
 
export const useDecimals = () => {
	const decimalsPower = getDecimalsPower(debtAmountSchemaDecimal);
	return {
		fromSubunitToUnit: React.useCallback(
			(input: number) => input / decimalsPower,
			[decimalsPower],
		),
		fromUnitToSubunit: React.useCallback(
			(input: number) => Math.round(input * decimalsPower),
			[decimalsPower],
		),
	};
};
 
const partsDecimalsPower = getDecimalsPower(partSchemaDecimal);
export const useRoundParts = () =>
	React.useCallback(
		(input: number) =>
			Math.round(input * partsDecimalsPower) / partsDecimalsPower,
		[],
	);