리펙토링: 코드 최적화
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
export const ACTIVE_DISTRIBUTION_COLOR = "#f97316";
|
||||
|
||||
function colorLatex(latex: string, color: string) {
|
||||
return `\\textcolor{${color}}{${latex}}`;
|
||||
}
|
||||
|
||||
interface FormatParenthesisExpressionLatexParams<T> {
|
||||
multiplier: number;
|
||||
terms: T[];
|
||||
operators: ("+" | "-")[];
|
||||
formatTermLatex: (term: T) => string;
|
||||
activeTermIndex?: number;
|
||||
activeColor?: string;
|
||||
}
|
||||
|
||||
export function formatParenthesisExpressionLatex<T>({
|
||||
multiplier,
|
||||
terms,
|
||||
operators,
|
||||
formatTermLatex,
|
||||
activeTermIndex,
|
||||
activeColor = ACTIVE_DISTRIBUTION_COLOR,
|
||||
}: FormatParenthesisExpressionLatexParams<T>) {
|
||||
const hasActiveTerm = activeTermIndex !== undefined;
|
||||
const maybeColor = (latex: string, active: boolean) =>
|
||||
hasActiveTerm && active ? colorLatex(latex, activeColor) : latex;
|
||||
const termLatex = terms
|
||||
.map((term, index) => {
|
||||
const displayTerm = maybeColor(
|
||||
formatTermLatex(term),
|
||||
index === activeTermIndex,
|
||||
);
|
||||
|
||||
if (index === 0) return displayTerm;
|
||||
|
||||
return `${operators[index - 1]} ${displayTerm}`;
|
||||
})
|
||||
.join(" ");
|
||||
|
||||
if (hasActiveTerm) {
|
||||
return `${colorLatex(`${multiplier}`, activeColor)}${colorLatex(
|
||||
"(",
|
||||
activeColor,
|
||||
)}${termLatex}${colorLatex(")", activeColor)}`;
|
||||
}
|
||||
|
||||
return `${multiplier}\\left(${termLatex}\\right)`;
|
||||
}
|
||||
Reference in New Issue
Block a user