MultiplyExpression.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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("./VariadicExpressionT")(klass), 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. klass.opName = "$multiply";
  19. proto.getOpName = function getOpName(){
  20. return klass.opName;
  21. };
  22. /**
  23. * Takes an array of one or more numbers and multiples them, returning the resulting product.
  24. * @method evaluateInternal
  25. **/
  26. proto.evaluateInternal = function evaluateInternal(vars){
  27. var product = 1;
  28. for(var i = 0, n = this.operands.length; i < n; ++i){
  29. var value = this.operands[i].evaluateInternal(vars);
  30. if(value instanceof Date) throw new Error("$multiply does not support dates; code 16375");
  31. product *= Value.coerceToDouble(value);
  32. }
  33. if(typeof(product) != "number") throw new Error("$multiply resulted in a non-numeric type; code 16418");
  34. return product;
  35. };
  36. /** Register Expression */
  37. Expression.registerExpression(klass.opName, base.parse(klass));