OrExpression.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. "use strict";
  2. /**
  3. * An $or pipeline expression.
  4. * @see evaluateInternal
  5. * @class OrExpression
  6. * @namespace mungedb-aggregate.pipeline.expressions
  7. * @module mungedb-aggregate
  8. * @constructor
  9. **/
  10. var OrExpression = module.exports = function OrExpression(){
  11. if (arguments.length !== 0) throw new Error("zero args expected");
  12. base.call(this);
  13. }, klass = OrExpression, base = require("./NaryExpression"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  14. // DEPENDENCIES
  15. var Value = require("../Value"),
  16. ConstantExpression = require("./ConstantExpression"),
  17. CoerceToBoolExpression = require("./CoerceToBoolExpression"),
  18. Expression = require("./Expression");
  19. // PROTOTYPE MEMBERS
  20. proto.getOpName = function getOpName(){
  21. return "$or";
  22. };
  23. /**
  24. * Takes an array of one or more values and returns true if any of the values in the array are true. Otherwise $or returns false.
  25. * @method evaluateInternal
  26. **/
  27. proto.evaluateInternal = function evaluateInternal(vars){
  28. for(var i = 0, n = this.operands.length; i < n; ++i){
  29. var value = this.operands[i].evaluateInternal(vars);
  30. if (Value.coerceToBool(value)) return true;
  31. }
  32. return false;
  33. };
  34. proto.optimize = function optimize() {
  35. var pE = base.prototype.optimize.call(this); // optimize the disjunction as much as possible
  36. if (!(pE instanceof OrExpression)) return pE; // if the result isn't a disjunction, we can't do anything
  37. var pOr = pE;
  38. // Check the last argument on the result; if it's not const (as promised
  39. // by ExpressionNary::optimize(),) then there's nothing we can do.
  40. var n = pOr.operands.length;
  41. // ExpressionNary::optimize() generates an ExpressionConstant for {$or:[]}.
  42. if (!n) throw new Error("OrExpression must have operands!");
  43. var pLast = pOr.operands[n - 1];
  44. if (!(pLast instanceof ConstantExpression)) return pE;
  45. // Evaluate and coerce the last argument to a boolean. If it's true, then we can replace this entire expression.
  46. var last = Value.coerceToBool();
  47. if (last) return new ConstantExpression(true);
  48. // If we got here, the final operand was false, so we don't need it anymore.
  49. // If there was only one other operand, we don't need the conjunction either. Note we still need to keep the promise that the result will be a boolean.
  50. if (n == 2) return new CoerceToBoolExpression(pOr.operands[0]);
  51. // Remove the final "false" value, and return the new expression.
  52. pOr.operands.length = n - 1;
  53. return pE;
  54. };
  55. /** Register Expression */
  56. Expression.registerExpression("$or", base.parse(OrExpression));