MultiplyExpression.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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(klass.name + ": no args expected");
  12. base.call(this);
  13. }, klass = MultiplyExpression, base = require("./VariadicExpressionT")(MultiplyExpression), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  14. var Value = require("../Value"),
  15. Expression = require("./Expression");
  16. proto.evaluateInternal = function evaluateInternal(vars) {
  17. var product = 1; //NOTE: DEVIATION FROM MONGO: no need to track narrowest so just use one var
  18. var n = this.operands.length;
  19. for (var i = 0; i < n; ++i) {
  20. var val = this.operands[i].evaluateInternal(vars);
  21. if (typeof val === "number") {
  22. product *= Value.coerceToDouble(val);
  23. } else if (val === undefined || val === null) {
  24. return null;
  25. } else {
  26. throw new Error("$multiply only supports numeric types, not " +
  27. Value.getType(val) + "; uasserted code 16555");
  28. }
  29. }
  30. if (typeof product === "number")
  31. return product;
  32. throw new Error("$multiply resulted in a non-numeric type; massert code 16418");
  33. };
  34. Expression.registerExpression("$multiply", base.parse);
  35. proto.getOpName = function getOpName(){
  36. return "$multiply";
  37. };
  38. proto.isAssociativeAndCommutative = function isAssociativeAndCommutative() {
  39. return true;
  40. };