MultiplyExpression.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. "use strict";
  2. /**
  3. * A $multiply pipeline expression.
  4. * @see evaluateInternal
  5. * @class MultiplyExpression
  6. * @namespace mungedb-aggregate.pipeline.expressions
  7. * @module mungedb-aggregate
  8. * @constructor
  9. **/
  10. var MultiplyExpression = module.exports = function MultiplyExpression(){
  11. if (arguments.length !== 0) throw new Error("Zero args expected");
  12. base.call(this);
  13. }, klass = MultiplyExpression, base = require("./NaryExpression"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  14. // DEPENDENCIES
  15. var Value = require("../Value"),
  16. Expression = require("./Expression");
  17. // PROTOTYPE MEMBERS
  18. proto.getOpName = function getOpName(){
  19. return "$multiply";
  20. };
  21. /**
  22. * Takes an array of one or more numbers and multiples them, returning the resulting product.
  23. * @method evaluateInternal
  24. **/
  25. proto.evaluateInternal = function evaluateInternal(vars){
  26. var product = 1;
  27. for(var i = 0, n = this.operands.length; i < n; ++i){
  28. var value = this.operands[i].evaluateInternal(vars);
  29. if(value instanceof Date) throw new Error("$multiply does not support dates; code 16375");
  30. product *= Value.coerceToDouble(value);
  31. }
  32. if(typeof(product) != "number") throw new Error("$multiply resulted in a non-numeric type; code 16418");
  33. return product;
  34. };
  35. /** Register Expression */
  36. Expression.registerExpression("$multiply", base.parse(MultiplyExpression));