Skip to content

Commit e3a3447

Browse files
committed
Add support for minus operator as a unary operator
1 parent 3a0a0f0 commit e3a3447

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

.changeset/calm-moles-yell.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@neo4j/cypher-builder": patch
3+
---
4+
5+
Support for a single value in `Cypher.minus` operator:
6+
7+
```js
8+
Cypher.minus(var1, var2); // var1 - var2
9+
Cypher.minus(var1); // -var1
10+
```

src/expressions/operations/math.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ describe("math operators", () => {
6464
expect(cypher).toMatchInlineSnapshot(`"(10 - 3)"`);
6565
});
6666

67+
test("minus with a single parameter", () => {
68+
const subtract = Cypher.minus(new Cypher.Literal(10));
69+
const { cypher } = new TestClause(subtract).build();
70+
expect(cypher).toMatchInlineSnapshot(`"-10"`);
71+
});
72+
73+
test("minus with a single expression", () => {
74+
const subtract = Cypher.minus(Cypher.multiply(new Cypher.Literal(10), new Cypher.Literal(2)));
75+
const { cypher } = new TestClause(subtract).build();
76+
expect(cypher).toMatchInlineSnapshot(`"-(10 * 2)"`);
77+
});
78+
6779
test("divide", () => {
6880
const divide = Cypher.divide(new Cypher.Literal(10), new Cypher.Literal(3));
6981
const { cypher } = new TestClause(divide).build();

src/expressions/operations/math.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ type MathOperator = "+" | "-" | "*" | "/" | "%" | "^";
2828
* @category Math
2929
*/
3030
export class MathOp extends CypherASTNode {
31-
private readonly operator: MathOperator;
32-
private readonly exprs: Expr[];
31+
protected readonly operator: MathOperator;
32+
protected readonly exprs: Expr[];
3333

3434
/** @internal */
3535
constructor(operator: MathOperator, exprs: Expr[]) {
@@ -49,6 +49,21 @@ export class MathOp extends CypherASTNode {
4949
}
5050
}
5151

52+
class UnaryMathOp extends MathOp {
53+
constructor(operator: MathOperator, expr: Expr) {
54+
super(operator, [expr]);
55+
}
56+
57+
/**
58+
* @internal
59+
*/
60+
public getCypher(env: CypherEnvironment): string {
61+
const exprStr = this.exprs[0].getCypher(env);
62+
63+
return `${this.operator}${exprStr}`;
64+
}
65+
}
66+
5267
function createOp(op: MathOperator, exprs: Expr[]): MathOp {
5368
return new MathOp(op, exprs);
5469
}
@@ -65,12 +80,15 @@ export function plus(...exprs: Expr[]): MathOp {
6580
return createOp("+", exprs);
6681
}
6782

68-
/**
83+
/** Minus (-) operator. This operator can be used as a mathematical operator between 2 expressions (3-2) or to negate a single expression (-1)
6984
* @see {@link https://neo4j.com/docs/cypher-manual/current/syntax/operators/#query-operators-mathematical | Cypher Documentation}
7085
* @group Operators
7186
* @category Math
7287
*/
73-
export function minus(leftExpr: Expr, rightExpr: Expr): MathOp {
88+
export function minus(leftExpr: Expr, rightExpr?: Expr): MathOp {
89+
if (rightExpr === undefined) {
90+
return new UnaryMathOp("-", leftExpr);
91+
}
7492
return createOp("-", [leftExpr, rightExpr]);
7593
}
7694

0 commit comments

Comments
 (0)