IfNullExpression.js 1.1 KB

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