Skip to content

Commit 5ee6005

Browse files
committed
Add negation as a unary operation
1 parent 2f518bb commit 5ee6005

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/formula.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ class UnaryOperation extends Expression {
138138
static operations = {
139139
"!": (x) => !x,
140140
"~": (x) => ~x,
141+
"-": (x) => -x,
141142
};
142143

143144
operator;
@@ -435,7 +436,7 @@ const unary = forwardDeclaration();
435436
unary.become(
436437
lex(
437438
alt(
438-
seq(alt(lex("!"), lex("~")), unary).map(
439+
seq(alt(...Object.keys(UnaryOperation.operations).map(lex)), unary).map(
439440
(args) => new UnaryOperation(...args),
440441
),
441442
value,

test/sheet-and-formula.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,3 +655,14 @@ test("Single-quoted strings", async () => {
655655
["test", "te'st\n\n", "te\\'st"],
656656
]);
657657
});
658+
659+
test("Negation as a unary operation", async () => {
660+
const state = createSheet([
661+
["-5.0", "=-3.2", "=-(6)", "=---3"],
662+
["=3**-2", "=3**-(--2)", "=-AVERAGE(1, 2, 3)", "=---SUM(-3, -4, -5)"],
663+
]);
664+
await expectSheet(state.currentSheet, [
665+
[-5.0, -3.2, -6, -3],
666+
[Math.pow(3, -2), Math.pow(3, -2), -2, 3 + 4 + 5],
667+
]);
668+
});

0 commit comments

Comments
 (0)