summaryrefslogtreecommitdiff
path: root/apps/web/src/utils/math.ts
blob: 8043871f7827fe590458afde25d483284b1800d6 (plain) (blame)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
export function clamp({
	value,
	min,
	max,
}: {
	value: number;
	min: number;
	max: number;
}): number {
	return Math.max(min, Math.min(max, value));
}

export function clampRound({
	value,
	min,
	max,
}: {
	value: number;
	min: number;
	max: number;
}): number {
	return Math.round(clamp({ value, min, max }));
}

export function getFractionDigitsForStep({ step }: { step: number }): number {
	const normalizedStep = step.toString().toLowerCase();
	if (normalizedStep.includes("e-")) {
		return Number(normalizedStep.split("e-")[1] ?? 0);
	}
	const [, fractionalPart = ""] = normalizedStep.split(".");
	return fractionalPart.length;
}

export function snapToStep({
	value,
	step,
}: {
	value: number;
	step: number;
}): number {
	if (step <= 0) return value;
	const snappedValue = Math.round(value / step) * step;
	return Number(
		snappedValue.toFixed(getFractionDigitsForStep({ step })),
	);
}

export function isNearlyEqual({
	leftValue,
	rightValue,
	epsilon = 0.0001,
}: {
	leftValue: number;
	rightValue: number;
	epsilon?: number;
}): boolean {
	return Math.abs(leftValue - rightValue) <= epsilon;
}

export function formatNumberForDisplay({
	value,
	fractionDigits,
	minFractionDigits = 0,
	maxFractionDigits = 6,
}: {
	value: number;
	fractionDigits?: number;
	minFractionDigits?: number;
	maxFractionDigits?: number;
}): string {
	const resolvedMaxFractionDigits = Math.max(
		0,
		fractionDigits ?? maxFractionDigits,
	);
	const resolvedMinFractionDigits = Math.min(
		Math.max(0, fractionDigits ?? minFractionDigits),
		resolvedMaxFractionDigits,
	);
	const fixedValue = value.toFixed(resolvedMaxFractionDigits);

	if (resolvedMaxFractionDigits === 0) {
		return Number(fixedValue) === 0 ? "0" : fixedValue;
	}

	const [integerPart, fractionPart = ""] = fixedValue.split(".");
	const normalizedIntegerPart = Number(fixedValue) === 0 ? "0" : integerPart;
	let trimmedFractionPart = fractionPart;

	while (
		trimmedFractionPart.length > resolvedMinFractionDigits &&
		trimmedFractionPart.endsWith("0")
	) {
		trimmedFractionPart = trimmedFractionPart.slice(0, -1);
	}

	return trimmedFractionPart
		? `${normalizedIntegerPart}.${trimmedFractionPart}`
		: normalizedIntegerPart;
}

export function evaluateMathExpression({
	input,
}: {
	input: string;
}): number | null {
	const sanitized = input.trim();
	if (!/^[\d.\s+\-*/()]+$/.test(sanitized)) return null;
	try {
		const result = new Function(`return (${sanitized})`)();
		if (typeof result !== "number" || !Number.isFinite(result)) return null;
		return result;
	} catch {
		return null;
	}
}