MultiplyExpression.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. "use strict";
  2. /**
  3. * A $multiply pipeline expression.
  4. * @see evaluate
  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. // PROTOTYPE MEMBERS
  17. proto.getOpName = function getOpName(){
  18. return "$multiply";
  19. };
  20. /**
  21. * Takes an array of one or more numbers and multiples them, returning the resulting product.
  22. * @method evaluate
  23. **/
  24. proto.evaluate = function evaluate(doc){
  25. var product = 1;
  26. for(var i = 0, n = this.operands.length; i < n; ++i){
  27. var value = this.operands[i].evaluate(doc);
  28. if(value instanceof Date) throw new Error("$multiply does not support dates; code 16375");
  29. product *= Value.coerceToDouble(value);
  30. }
  31. if(typeof(product) != "number") throw new Error("$multiply resulted in a non-numeric type; code 16418");
  32. return product;
  33. };
  34. proto.getFactory = function getFactory(){
  35. return klass; // using the ctor rather than a separate .create() method
  36. };