| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- "use strict";
- /**
- * $cond expression; @see evaluate
- * @class CondExpression
- * @namespace mungedb-aggregate.pipeline.expressions
- * @module mungedb-aggregate
- * @constructor
- **/
- var CondExpression = module.exports = function CondExpression(vars) {
- this.nargs = 3;
- this.pCond = this.evaluateInternal(vars);
- this.idx = this.pCond.coerceToBool() ? 1 : 2;
- if (arguments.length !== 3) throw new Error("three args expected");
- base.call(this);
- }, klass = CondExpression,
- base = require("./NaryExpression"),
- proto = klass.prototype = Object.create(base.prototype, {
- constructor: {
- value: klass
- }
- });
- // DEPENDENCIES
- var Value = require("../Value"),
- Expression = require("./Expression");
- // PROTOTYPE MEMBERS
- proto.getOpName = function getOpName() {
- return "$cond";
- };
- klass.parse = function parse(expr, vps) {
- this.checkArgLimit(3);
- // if not an object, return;
- if (typeof(expr) !== Object)
- return Expression.parse(expr, vps);
- // verify
- if (Expression.parseOperand(expr) !== "$cond")
- throw new Error("Invalid expression");
- var ret = new CondExpression();
- var ex = Expression.parseObject(expr);
- var args = Expression.parseOperand(expr, vps);
- if (args[0] !== "if")
- throw new Error("Missing 'if' parameter to $cond");
- if (args[1] !== "then")
- throw new Error("Missing 'then' parameter to $cond");
- if (args[2] !== "else")
- throw new Error("Missing 'else' parameter to $cond");
- return ret;
- };
- /**
- * Use the $cond operator with the following syntax: { $cond: [ <boolean-expression>, <true-case>, <false-case> ] }
- * @method evaluate
- **/
- proto.evaluateInternal = function evaluateInternal(vars) {
- var pCond1 = this.operands[0].evaluateInternal(vars);
- this.idx = 0;
- if (pCond1.coerceToBool()) {
- this.idx = 1;
- } else {
- this.idx = 2;
- }
- return this.operands[this.idx].evaluateInternal(vars);
- };
- /** Register Expression */
- Expression.registerExpression("$cond", klass.parse);
|