@@ -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,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+
5267function 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