Skip to content

Commit c65f51d

Browse files
authored
Merge pull request #596 from neo4j/594-minus-with-a-single-variable
Add support for minus operator as a unary operator
2 parents 2d16c5f + c5a35bb commit c65f51d

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-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: 26 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,25 @@ 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 expr = this.exprs[0];
62+
if (!expr) {
63+
throw new Error("Error in Cypher.minus, expr is not defined");
64+
}
65+
const exprStr = expr.getCypher(env);
66+
67+
return `${this.operator}${exprStr}`;
68+
}
69+
}
70+
5271
function createOp(op: MathOperator, exprs: Expr[]): MathOp {
5372
return new MathOp(op, exprs);
5473
}
@@ -65,12 +84,15 @@ export function plus(...exprs: Expr[]): MathOp {
6584
return createOp("+", exprs);
6685
}
6786

68-
/**
87+
/** Minus (-) operator. This operator can be used as a mathematical operator between 2 expressions (3-2) or to negate a single expression (-1)
6988
* @see {@link https://neo4j.com/docs/cypher-manual/current/syntax/operators/#query-operators-mathematical | Cypher Documentation}
7089
* @group Operators
7190
* @category Math
7291
*/
73-
export function minus(leftExpr: Expr, rightExpr: Expr): MathOp {
92+
export function minus(leftExpr: Expr, rightExpr?: Expr): MathOp {
93+
if (rightExpr === undefined) {
94+
return new UnaryMathOp("-", leftExpr);
95+
}
7496
return createOp("-", [leftExpr, rightExpr]);
7597
}
7698

0 commit comments

Comments
 (0)