@@ -28,8 +28,8 @@ type MathOperator = "+" | "-" | "*" | "/" | "%" | "^";
2828 * @category Math
2929 */
3030export 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+
5271function 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