SubtractExpression.js 1.1 KB

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