CondExpression.js 1.2 KB

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