Skip to content

Commit

Permalink
refactor: split out config normalization code
Browse files Browse the repository at this point in the history
Will allow the same logic to be used for loading config from a sheet and loading config from indexDB
  • Loading branch information
mgreminger committed Nov 16, 2024
1 parent 7e86202 commit 7af4efe
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
14 changes: 5 additions & 9 deletions src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
inCellInsertMode, config, unsavedChange, incrementActiveCell,
decrementActiveCell, deleteCell, activeMathField, autosaveNeeded, mathJaxLoaded
} from "./stores";
import { getDefaultBaseUnits, getDefaultFluidConfig, isDefaultConfig } from "./sheet/Sheet";
import { getDefaultBaseUnits, getDefaultFluidConfig, isDefaultConfig,
normalizeConfig } from "./sheet/Sheet";
import type { Statement, SubQueryStatement } from "./parser/types";
import type { SystemDefinition } from "./cells/SystemCell";
import type { FluidFunction } from "./cells/FluidCell";
Expand Down Expand Up @@ -1216,15 +1217,10 @@ Please include a link to this sheet in the email to assist in debugging the prob
$title = sheet.title;
BaseCell.nextId = sheet.nextId;
$sheetId = sheet.sheetId;
// old documents in database will not have the insertedSheets property or a config property
// old documents in database will not have the insertedSheets property
$insertedSheets = sheet.insertedSheets ?? [];
$config = sheet.config ?? getDefaultConfig();
$config.customBaseUnits = $config.customBaseUnits ?? getDefaultBaseUnits(); // customBaseUnits may not exist
$config.simplifySymbolicExpressions = $config.simplifySymbolicExpressions ?? true; // simplifySymboicExpressions may not exist
$config.convertFloatsToFractions = $config.convertFloatsToFractions ?? true; // convertFloatsToFractions may not exist
$config.fluidConfig = $config.fluidConfig ?? getDefaultFluidConfig(); // fluidConfig may not exist
$config.mathCellConfig.showIntermediateResults = $config.mathCellConfig.showIntermediateResults ?? false; // may not exist
$config = normalizeConfig(sheet.config);
$cells = await Promise.all(sheet.cells.map((value) => cellFactory(value, $config)));
if (!$history.map(item => item.hash !== "file" ? getSheetHash(new URL(item.url)) : "").includes(getSheetHash(window.location))) {
Expand Down
12 changes: 12 additions & 0 deletions src/sheet/Sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,16 @@ export function getDefaultBaseUnits(system: BaseUnitSystemNames = "SI"): CustomB
export function isDefaultBaseUnits(baseUnits: CustomBaseUnits, system: BaseUnitSystemNames = "SI"): boolean {
const defaultBaseUnits = baseUnitSystems.get(system);
return Object.entries(defaultBaseUnits).reduce((acum, [key, value]) => acum && value === baseUnits[key], true);
}

export function normalizeConfig(inputConfig: Config | undefined): Config {
const outputConfig = inputConfig ?? getDefaultConfig();

outputConfig.customBaseUnits = outputConfig.customBaseUnits ?? getDefaultBaseUnits(); // customBaseUnits may not exist
outputConfig.simplifySymbolicExpressions = outputConfig.simplifySymbolicExpressions ?? true; // simplifySymboicExpressions may not exist
outputConfig.convertFloatsToFractions = outputConfig.convertFloatsToFractions ?? true; // convertFloatsToFractions may not exist
outputConfig.fluidConfig = outputConfig.fluidConfig ?? getDefaultFluidConfig(); // fluidConfig may not exist
outputConfig.mathCellConfig.showIntermediateResults = outputConfig.mathCellConfig.showIntermediateResults ?? false; // may not exist

return outputConfig;
}

0 comments on commit 7af4efe

Please sign in to comment.