Skip to content

Commit 5699550

Browse files
committed
Fix formula functions declared with caps
1 parent ece4e78 commit 5699550

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

TODO.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,3 @@
162162
vertical scrolling
163163
- Accidentally typing "RC" while typing another formula can cause everything
164164
to hang
165-
- Formula functions declared with any caps will not be accessible

src/formula-functions.svelte.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ export function evalCode(code, ret = () => {}) {
4242
// Allows user code to show up in the devtools debugger as "user-code.js"
4343
"\n//# sourceURL=user-code.js",
4444
);
45+
// Canonicalize function names, since only lowercased version is used
46+
Object.entries(functions).forEach(([name, f]) => {
47+
functions[name.toLocaleLowerCase()] = f;
48+
});
4549
return ret();
4650
} catch (e) {
4751
return ret(e);

test/sheet-and-formula.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,3 +668,14 @@ test("Negation as a unary operation", async () => {
668668
[Math.pow(3, -2), Math.pow(3, -2), -2, 3 + 4 + 5],
669669
]);
670670
});
671+
672+
test("Custom formula functions with caps in declaration", async () => {
673+
evalCode(
674+
`functions.FACTORIAL = (n) => n == 0 ? 1 : (n * functions.FACTORIAL(n - 1))`,
675+
);
676+
const state = createSheet([["6", "=factorial(RC0)"]]);
677+
await expectSheet(state.currentSheet, [[6, 720]]);
678+
evalCode(`functions.With_underscores = (n) => n * 2`);
679+
state.currentSheet.cells[0][1].formula = "=with_underscores(RC0)";
680+
await expectSheet(state.currentSheet, [[6, 12]]);
681+
});

0 commit comments

Comments
 (0)