@neo4j/cypher-builder
Version:
A programmatic API for building Cypher queries for Neo4j
103 lines (102 loc) • 3 kB
JavaScript
;
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.MathOp = void 0;
exports.plus = plus;
exports.minus = minus;
exports.multiply = multiply;
exports.divide = divide;
exports.mod = mod;
exports.pow = pow;
const CypherASTNode_1 = require("../../CypherASTNode");
/**
* @group Operators
* @category Math
*/
class MathOp extends CypherASTNode_1.CypherASTNode {
operator;
exprs;
/** @internal */
constructor(operator, exprs) {
super();
this.operator = operator;
this.exprs = exprs;
}
/**
* @internal
*/
getCypher(env) {
const exprs = this.exprs.map((e) => e.getCypher(env));
const operatorStr = ` ${this.operator} `;
return `(${exprs.join(operatorStr)})`;
}
}
exports.MathOp = MathOp;
class UnaryMathOp extends MathOp {
constructor(operator, expr) {
super(operator, [expr]);
}
/**
* @internal
*/
getCypher(env) {
const expr = this.exprs[0];
if (!expr) {
throw new Error("Error in Cypher.minus, expr is not defined");
}
const exprStr = expr.getCypher(env);
return `${this.operator}${exprStr}`;
}
}
function createOp(op, exprs) {
return new MathOp(op, exprs);
}
function plus(...exprs) {
return createOp("+", exprs);
}
/** Minus (-) operator. This operator can be used as a mathematical operator between 2 expressions (3-2) or to negate a single expression (-1)
* @see {@link https://neo4j.com/docs/cypher-manual/current/syntax/operators/#query-operators-mathematical | Cypher Documentation}
* @group Operators
* @category Math
*/
function minus(leftExpr, rightExpr) {
if (rightExpr === undefined) {
return new UnaryMathOp("-", leftExpr);
}
return createOp("-", [leftExpr, rightExpr]);
}
/**
* @see {@link https://neo4j.com/docs/cypher-manual/current/syntax/operators/#query-operators-mathematical | Cypher Documentation}
* @group Operators
* @category Math
*/
function multiply(leftExpr, rightExpr) {
return createOp("*", [leftExpr, rightExpr]);
}
/**
* @see {@link https://neo4j.com/docs/cypher-manual/current/syntax/operators/#query-operators-mathematical | Cypher Documentation}
* @group Operators
* @category Math
*/
function divide(leftExpr, rightExpr) {
return createOp("/", [leftExpr, rightExpr]);
}
/** Modulus (%) operator
* @see {@link https://neo4j.com/docs/cypher-manual/current/syntax/operators/#query-operators-mathematical | Cypher Documentation}
* @group Operators
* @category Math
*/
function mod(leftExpr, rightExpr) {
return createOp("%", [leftExpr, rightExpr]);
}
/** Power (^) operator
* @see {@link https://neo4j.com/docs/cypher-manual/current/syntax/operators/#query-operators-mathematical | Cypher Documentation}
* @group Operators
* @category Math
*/
function pow(leftExpr, rightExpr) {
return createOp("^", [leftExpr, rightExpr]);
}