CondExpression.js 1.1 KB

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