OrExpression.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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("./VariadicExpressionT")(OrExpression), 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. klass.opName = "$or";
  21. proto.getOpName = function getOpName(){
  22. return klass.opName;
  23. };
  24. proto.isAssociativeAndCommutative = function isAssociativeAndCommutative() { return true; }
  25. /**
  26. * 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.
  27. * @method evaluateInternal
  28. **/
  29. proto.evaluateInternal = function evaluateInternal(vars){
  30. for(var i = 0, n = this.operands.length; i < n; ++i){
  31. var value = this.operands[i].evaluateInternal(vars);
  32. if (Value.coerceToBool(value)) return true;
  33. }
  34. return false;
  35. };
  36. proto.optimize = function optimize() {
  37. var pE = base.prototype.optimize.call(this); // optimize the disjunction as much as possible
  38. if (!(pE instanceof OrExpression)) return pE; // if the result isn't a disjunction, we can't do anything
  39. var pOr = pE;
  40. // Check the last argument on the result; if it's not const (as promised
  41. // by ExpressionNary::optimize(),) then there's nothing we can do.
  42. var n = pOr.operands.length;
  43. // ExpressionNary::optimize() generates an ExpressionConstant for {$or:[]}.
  44. if (!n) throw new Error("OrExpression must have operands!");
  45. var pLast = pOr.operands[n - 1];
  46. if (!(pLast instanceof ConstantExpression)) return pE;
  47. // Evaluate and coerce the last argument to a boolean. If it's true, then we can replace this entire expression.
  48. var last = Value.coerceToBool();
  49. if (last) return new ConstantExpression(true);
  50. // If we got here, the final operand was false, so we don't need it anymore.
  51. // 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.
  52. if (n == 2) return new CoerceToBoolExpression(pOr.operands[0]);
  53. // Remove the final "false" value, and return the new expression.
  54. pOr.operands.length = n - 1;
  55. return pE;
  56. };
  57. /** Register Expression */
  58. Expression.registerExpression(klass.opName, base.parse);