39 lines
1.4 KiB
JavaScript
39 lines
1.4 KiB
JavaScript
// Rôle : normalise et compacte les données du calculateur.
|
|
import { uid } from "./shared.js";
|
|
|
|
function normalizeCalculatorValue(value) {
|
|
const parsed = Number(value);
|
|
return Number.isFinite(parsed) ? parsed : 0;
|
|
}
|
|
|
|
function normalizeCalculatorFormula(value) {
|
|
return String(value || "").replaceAll(",", ".").trim();
|
|
}
|
|
|
|
export function normalizeCalculatorData(data) {
|
|
const entries = (data?.entries || [])
|
|
.map((entry) => ({
|
|
id: entry?.id || uid("calc"),
|
|
parentId: String(entry?.parentId || ""),
|
|
label: String(entry?.label || "").trim(),
|
|
value: normalizeCalculatorValue(entry?.value),
|
|
formula: normalizeCalculatorFormula(entry?.formula)
|
|
}));
|
|
const entryIds = new Set(entries.map((entry) => entry.id));
|
|
return {
|
|
scrollResults: data?.scrollResults === true,
|
|
entries: entries.map((entry) => entryIds.has(entry.parentId) ? entry : { ...entry, parentId: "" })
|
|
};
|
|
}
|
|
|
|
export function compactCalculatorDataForStorage(value) {
|
|
const normalized = normalizeCalculatorData(value);
|
|
const entries = normalized.entries.map((entry) => {
|
|
const compact = { id: entry.id, label: entry.label, value: entry.value };
|
|
if (entry.parentId) compact.parentId = entry.parentId;
|
|
if (entry.formula) compact.formula = entry.formula;
|
|
return compact;
|
|
});
|
|
if (!entries.length && !normalized.scrollResults) return null;
|
|
return normalized.scrollResults ? { entries, scrollResults: true } : { entries };
|
|
}
|