Refactor calculator module helpers
All checks were successful
Deploy Sokko G / deploy (push) Successful in 15s
All checks were successful
Deploy Sokko G / deploy (push) Successful in 15s
This commit is contained in:
parent
e512eef232
commit
61176ba9b7
5 changed files with 211 additions and 170 deletions
96
website/src/features/toolboxes/modules/calculatorUtils.js
Normal file
96
website/src/features/toolboxes/modules/calculatorUtils.js
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
// Rôle : regroupe les helpers purs du calculateur et de son arbre de résultats.
|
||||
export const DISPLAY_NUMBER_FORMATTER = new Intl.NumberFormat("fr-FR", { maximumFractionDigits: 6 });
|
||||
export const BASE_TOKEN = "base";
|
||||
|
||||
export function calculateExpression(expression, baseValue = null) {
|
||||
const normalized = String(expression || "").replaceAll(",", ".").trim();
|
||||
if (!normalized) return null;
|
||||
const withBase = normalized.replace(/\bbase\b/gi, () => Number.isFinite(baseValue) ? `(${formatResult(baseValue)})` : "NaN");
|
||||
if (!/^[\d\s+\-*/().]+$/.test(withBase)) return null;
|
||||
|
||||
try {
|
||||
const value = Function(`"use strict"; return (${withBase})`)();
|
||||
return Number.isFinite(value) ? value : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function formatResult(value) {
|
||||
if (!Number.isFinite(value)) return "";
|
||||
return Number.parseFloat(value.toFixed(6)).toString();
|
||||
}
|
||||
|
||||
export function formatDisplayResult(value) {
|
||||
if (!Number.isFinite(value)) return "";
|
||||
return DISPLAY_NUMBER_FORMATTER.format(Number.parseFloat(value.toFixed(6)));
|
||||
}
|
||||
|
||||
export function getResultLengthClass(value) {
|
||||
const length = formatResult(value).replace(/[^\d]/g, "").length;
|
||||
if (length >= 13) return "is-compact";
|
||||
if (length >= 9) return "is-long";
|
||||
return "";
|
||||
}
|
||||
|
||||
export function getEntryResultLengthClass(value) {
|
||||
const length = formatResult(value).replace(/[^\d]/g, "").length;
|
||||
if (length >= 21) return "is-compact";
|
||||
if (length >= 18) return "is-long";
|
||||
return "";
|
||||
}
|
||||
|
||||
export function getChildren(entries, parentId) {
|
||||
return entries.filter((entry) => (entry.parentId || "") === parentId);
|
||||
}
|
||||
|
||||
export function getDescendantIds(entries, parentId) {
|
||||
const children = getChildren(entries, parentId);
|
||||
return children.flatMap((child) => [child.id, ...getDescendantIds(entries, child.id)]);
|
||||
}
|
||||
|
||||
export function getEntriesInTreeOrder(entries, parentId = "") {
|
||||
return getChildren(entries, parentId).flatMap((entry) => [entry, ...getEntriesInTreeOrder(entries, entry.id)]);
|
||||
}
|
||||
|
||||
export function getEntryFormula(entry) {
|
||||
return String(entry?.formula || "").trim();
|
||||
}
|
||||
|
||||
export function getEditableFormula(entry) {
|
||||
return getEntryFormula(entry) || formatResult(entry.value);
|
||||
}
|
||||
|
||||
export function createStoredFormula(expression, parentValue) {
|
||||
const normalized = String(expression || "").replaceAll(",", ".").trim();
|
||||
if (!Number.isFinite(parentValue)) return normalized;
|
||||
const parentText = formatResult(parentValue);
|
||||
if (/\bbase\b/i.test(normalized)) return normalized;
|
||||
const withBase = normalized.replace(new RegExp(`(^|[^\\d.])${escapeRegExp(parentText)}(?=$|[^\\d.])`, "g"), `$1${BASE_TOKEN}`);
|
||||
if (withBase !== normalized) return withBase;
|
||||
if (/^[+\-*/]/.test(normalized)) return `${BASE_TOKEN}${normalized}`;
|
||||
return normalized;
|
||||
}
|
||||
|
||||
export function evaluateCalculatorEntries(entries) {
|
||||
const byParent = new Map();
|
||||
entries.forEach((entry) => {
|
||||
const parentId = entry.parentId || "";
|
||||
byParent.set(parentId, [...(byParent.get(parentId) || []), entry]);
|
||||
});
|
||||
|
||||
function visit(parentId = "", parentValue = null) {
|
||||
return (byParent.get(parentId) || []).flatMap((entry) => {
|
||||
const formula = getEntryFormula(entry);
|
||||
const calculatedValue = formula ? calculateExpression(formula, parentValue) : entry.value;
|
||||
const nextEntry = { ...entry, value: calculatedValue ?? entry.value };
|
||||
return [nextEntry, ...visit(entry.id, nextEntry.value)];
|
||||
});
|
||||
}
|
||||
|
||||
return visit();
|
||||
}
|
||||
|
||||
function escapeRegExp(value) {
|
||||
return String(value).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue