CondExpression.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. "use strict";
  2. /**
  3. * $cond expression; @see evaluate
  4. * @class AndExpression
  5. * @namespace mungedb-aggregate.pipeline.expressions
  6. * @module mungedb-aggregate
  7. * @constructor
  8. **/
  9. var CondExpression = module.exports = function CondExpression(){
  10. if (arguments.length !== 0) throw new Error("zero args expected");
  11. base.call(this);
  12. }, klass = CondExpression, base = require("./NaryExpression"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  13. // DEPENDENCIES
  14. var Value = require("../Value");
  15. // PROTOTYPE MEMBERS
  16. proto.getOpName = function getOpName(){
  17. return "$cond";
  18. };
  19. proto.addOperand = function addOperand(expr) {
  20. this.checkArgLimit(3);
  21. base.prototype.addOperand.call(this, expr);
  22. };
  23. /**
  24. * Use the $cond operator with the following syntax: { $cond: [ <boolean-expression>, <true-case>, <false-case> ] }
  25. * @method evaluate
  26. **/
  27. proto.evaluate = function evaluate(doc){
  28. this.checkArgCount(3);
  29. var pCond = this.operands[0].evaluate(doc),
  30. idx = Value.coerceToBool(pCond) ? 1 : 2;
  31. return this.operands[idx].evaluate(doc);
  32. };