SubtractExpression.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. var SubtractExpression = module.exports = (function(){
  2. // CONSTRUCTOR
  3. /**
  4. * A $subtract pipeline expression. @see evaluate
  5. **/
  6. var klass = function SubtractExpression(){
  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. // DEPENDENCIES
  11. var Value = require("../Value");
  12. // PROTOTYPE MEMBERS
  13. proto.getOpName = function getOpName(){
  14. return "$subtract";
  15. };
  16. proto.addOperand = function addOperand(expr) {
  17. this.checkArgLimit(2);
  18. base.prototype.addOperand.call(this, expr);
  19. };
  20. /**
  21. * Takes an array that contains a pair of numbers and subtracts the second from the first, returning their difference.
  22. **/
  23. proto.evaluate = function evaluate(doc) {
  24. this.checkArgCount(2);
  25. var left = this.operands[0].evaluate(doc),
  26. right = this.operands[1].evaluate(doc);
  27. if(left instanceof Date || right instanceof Date) throw new Error("$subtract does not support dates; code 16376");
  28. return left - right;
  29. };
  30. return klass;
  31. })();