MultiplyExpression.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. var MultiplyExpression = module.exports = (function(){
  2. // CONSTRUCTOR
  3. /**
  4. * An $Multiply pipeline expression.
  5. *
  6. * @see evaluate
  7. * @class MultiplyExpression
  8. * @namespace munge.pipeline.expressions
  9. * @module munge
  10. * @constructor
  11. **/
  12. var klass = function MultiplyExpression(){
  13. if(arguments.length !== 0) throw new Error("zero args expected");
  14. base.call(this);
  15. }, base = require("./NaryExpression"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  16. // DEPENDENCIES
  17. var Value = require("../Value");
  18. // PROTOTYPE MEMBERS
  19. proto.getOpName = function getOpName(){
  20. return "$multiply";
  21. };
  22. /**
  23. * Takes an array of one or more numbers and multiples them, returning the resulting product.
  24. **/
  25. proto.evaluate = function evaluate(doc){
  26. var product = 1;
  27. for(var i = 0, n = this.operands.length; i < n; ++i){
  28. var value = this.operands[i].evaluate(doc);
  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. proto.getFactory = function getFactory(){
  36. return klass; // using the ctor rather than a separate .create() method
  37. };
  38. return klass;
  39. })();