Skip to content

Commit 253674c

Browse files
committed
make use of getValue/setValue where applicable
1 parent 90adcc9 commit 253674c

File tree

4 files changed

+21
-34
lines changed

4 files changed

+21
-34
lines changed

dashi/src/lib/actions/helpers/applyStateChangeRequests.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type { ContribPoint } from "@/lib/types/model/extension";
1111
import type { ContributionState } from "@/lib";
1212
import { updateArray } from "@/lib/utils/updateArray";
1313
import { isContainerState } from "@/lib/actions/helpers/isContainerState";
14+
import { getValue, setValue } from "@/lib/utils/objPath";
1415

1516
export function applyStateChangeRequests(
1617
stateChangeRequests: StateChangeRequest[],
@@ -106,12 +107,10 @@ export function applyComponentStateChange(
106107
): ComponentState {
107108
if (component.id === stateChange.id) {
108109
const property = stateChange.property;
109-
const valueOld = (component as unknown as Record<string, unknown>)[
110-
property
111-
];
110+
const valueOld = getValue(component, property);
112111
const valueNew = stateChange.value;
113112
if (valueOld !== valueNew) {
114-
return { ...component, [property]: valueNew };
113+
return setValue(component, property, valueNew);
115114
}
116115
} else if (isContainerState(component)) {
117116
const containerOld: ContainerState = component;
@@ -135,17 +134,13 @@ export function applyComponentStateChange(
135134
}
136135

137136
// we export for testing only
138-
export function applyStateChanges<S extends object>(
139-
state: S | undefined,
137+
export function applyStateChanges<S extends object | undefined>(
138+
state: S,
140139
stateChanges: StateChange[],
141-
): S | undefined {
140+
): S {
142141
stateChanges.forEach((stateChange) => {
143-
if (
144-
!state ||
145-
(state as unknown as Record<string, unknown>)[stateChange.property] !==
146-
stateChange.value
147-
) {
148-
state = { ...state, [stateChange.property]: stateChange.value } as S;
142+
if (!state || getValue(state, stateChange.property) !== stateChange.value) {
143+
state = setValue(state, stateChange.property, stateChange.value) as S;
149144
}
150145
});
151146
return state;

dashi/src/lib/actions/helpers/getInputValues.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ export function getInputValueFromComponent(
4848
componentState: ComponentState,
4949
): unknown {
5050
if (componentState.id === input.id && input.property) {
51-
return (componentState as unknown as Record<string, unknown>)[
52-
input.property
53-
];
51+
return getValue(componentState, input.property);
5452
} else if (isContainerState(componentState)) {
5553
for (let i = 0; i < componentState.components.length; i++) {
5654
const item = componentState.components[i];
@@ -73,16 +71,7 @@ export function getInputValueFromState(
7371
inputValue = inputValue[input.id];
7472
}
7573
if (isSubscriptable(inputValue)) {
76-
const property = input.property;
77-
// TODO: The Input.property should be normalized to be a path
78-
// and have type string[].
79-
// See also interface Input in types/model/channel.ts
80-
if (property.includes(".")) {
81-
const path = property.split(".");
82-
inputValue = getValue(inputValue, path);
83-
} else {
84-
inputValue = inputValue[property];
85-
}
74+
inputValue = getValue(inputValue, input.property);
8675
}
8776
return inputValue;
8877
}

dashi/src/lib/utils/mapObject.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { describe, it, expect } from "vitest";
22

3-
import { getValue, setValue, toObjPath } from "./objPath";
43
import { mapObject } from "@/lib/utils/mapObject";
54

65
describe("Test that mapObject()", () => {

dashi/src/lib/utils/objPath.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ export type ObjPath = (string | number)[];
44

55
type Obj = Record<string | number, unknown>;
66

7-
export function getValue(obj: object, path: ObjPath): unknown {
7+
export function getValue(
8+
obj: object | undefined,
9+
path: ObjPath | string,
10+
): unknown {
11+
path = toObjPath(path);
812
let value: unknown = obj;
913
for (const key of path) {
1014
if (isObject(value)) {
@@ -16,19 +20,19 @@ export function getValue(obj: object, path: ObjPath): unknown {
1620
return value;
1721
}
1822

19-
export function setValue<S extends object>(
23+
export function setValue<S extends object | undefined>(
2024
obj: S,
2125
path: ObjPath | string,
2226
value: unknown,
23-
): S | undefined {
27+
): S {
2428
return _setValue(obj, toObjPath(path), value);
2529
}
2630

27-
export function _setValue<S extends object>(
28-
obj: S | undefined,
31+
export function _setValue<S extends object | undefined>(
32+
obj: S,
2933
path: ObjPath,
3034
value: unknown,
31-
): S | undefined {
35+
): S {
3236
if (path.length === 1) {
3337
const key = path[0];
3438
if (isObject(obj)) {
@@ -66,7 +70,7 @@ export function toObjPath(
6670
): ObjPath {
6771
if (Array.isArray(property)) {
6872
return property as ObjPath;
69-
} else if (property === "" || !property) {
73+
} else if (!property || property === "") {
7074
return [];
7175
} else if (typeof property === "number") {
7276
return [property];

0 commit comments

Comments
 (0)