MultiplyExpression.js 1.4 KB

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