Skip to content

Commit

Permalink
useGetSubValueByPath cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
OAGr committed Jan 19, 2024
1 parent 8361d0c commit 29c5ae3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
22 changes: 10 additions & 12 deletions packages/components/src/components/SquiggleViewer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ export function pathToShortName(path: SqValuePath): string {
if (path.isRoot()) {
return topLevelName(path);
} else {
const lastPathItem = path.items[path.items.length - 1];
return lastPathItem.toDisplayString();
return path.lastItem().toDisplayString();
}
}

Expand All @@ -50,17 +49,19 @@ export function useGetSubvalueByPath() {
subValuePath: SqValuePath
): SqValue | undefined => {
const { context } = topValue;
if (!context) {
return;
}

if (!subValuePath.contains(context.path)) {
if (!context || !subValuePath.contains(context.path)) {
return;
}

let currentValue = topValue;
for (let i = 0; i < subValuePath.items.length; i++) {
const pathItem = subValuePath.items[i];
const subValuePaths = subValuePath.itemsAsValuePaths({
includeRoot: false,
});

for (const subValuePath of subValuePaths) {
const pathItem = subValuePath.lastItem();

let nextValue: SqValue | undefined;

if (currentValue.tag === "Array" && pathItem.value.type === "number") {
Expand Down Expand Up @@ -89,10 +90,7 @@ export function useGetSubvalueByPath() {
} else if (pathItem.type === "calculator") {
// The previous path item is the one that is the parent of the calculator result.
// This is the one that we use in the ViewerContext to store information about the calculator.
const calculatorPath = new SqValuePath({
root: subValuePath.root,
items: subValuePath.items.slice(0, i),
});
const calculatorPath = subValuePath;
const calculatorState = itemStore.getCalculator(calculatorPath);
const result = calculatorState?.calculatorResult;
if (!result?.ok) {
Expand Down
4 changes: 4 additions & 0 deletions packages/squiggle-lang/src/public/SqValuePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ export class SqValuePath {
this.items = props.items;
}

lastItem() {
return this.items[this.items.length - 1];
}

extend(item: SqPathItem) {
return new SqValuePath({
root: this.root,
Expand Down

0 comments on commit 29c5ae3

Please sign in to comment.