IfNullExpression.js 940 B

1234567891011121314151617181920212223242526272829
  1. var IfNullExpression = module.exports = (function(){
  2. // CONSTRUCTOR
  3. /** An $ifNull pipeline expression. @see evaluate **/
  4. var klass = function IfNullExpression(){
  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. // PROTOTYPE MEMBERS
  9. proto.getOpName = function getOpName(){
  10. return "$ifNull";
  11. };
  12. proto.addOperand = function addOperand(expr) {
  13. this.checkArgLimit(2);
  14. base.addOperand(expr);
  15. };
  16. /** Use the $ifNull operator with the following syntax: { $ifNull: [ <expression>, <replacement-if-null> ] } **/
  17. proto.evaluate = function evaluate(doc){
  18. this.checkArgCount(2);
  19. var left = this.operands[0].evaluate(doc);
  20. if(left !== undefined && left !== null) return left;
  21. var right = this.operands[1].evaluate(doc);
  22. return right;
  23. };
  24. return klass;
  25. })();