IfNullExpression.js 971 B

123456789101112131415161718192021222324252627282930313233
  1. var IfNullExpression = module.exports = (function(){
  2. // CONSTRUCTOR
  3. /**
  4. * An $ifNull pipeline expression. @see evaluate
  5. **/
  6. var klass = function IfNullExpression(){
  7. if(arguments.length !== 0) throw new Error("zero args expected");
  8. base.call(this);
  9. }, base = require("./NaryExpression"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  10. // PROTOTYPE MEMBERS
  11. proto.getOpName = function getOpName(){
  12. return "$ifNull";
  13. };
  14. proto.addOperand = function addOperand(expr) {
  15. this.checkArgLimit(2);
  16. base.prototype.addOperand.call(this, expr);
  17. };
  18. /**
  19. * Use the $ifNull operator with the following syntax: { $ifNull: [ <expression>, <replacement-if-null> ] }
  20. **/
  21. proto.evaluate = function evaluate(doc){
  22. this.checkArgCount(2);
  23. var left = this.operands[0].evaluate(doc);
  24. if(left !== undefined && left !== null) return left;
  25. var right = this.operands[1].evaluate(doc);
  26. return right;
  27. };
  28. return klass;
  29. })();