Skip to content

Commit 0a007a3

Browse files
committed
fix: unit error
1 parent 94e4c1a commit 0a007a3

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/lib/helpers/unit.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,18 @@ export function createValueUnitPair<T = string>(initialValue = 0, units: Unit<T>
4444

4545
unit.subscribe((newValue, prev) => {
4646
value.update((v) => {
47-
const unitInBase = v * units.find((u) => u.name === prev).value;
48-
return unitInBase / units.find((u) => u.name === newValue).value;
47+
const prevUnit = units.find((u) => u.name === prev);
48+
const newUnit = units.find((u) => u.name === newValue);
49+
50+
// If either unit is not found, just return the current value
51+
if (!prevUnit || !newUnit) {
52+
return v;
53+
}
54+
55+
const unitInBase = v * prevUnit.value;
56+
return unitInBase / newUnit.value;
4957
});
5058
});
51-
5259
return {
5360
value,
5461
unit,

0 commit comments

Comments
 (0)