CondExpression.js 1008 B

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