CondExpression.js 967 B

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