OrExpression.js 2.5 KB

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