OrExpression.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. var OrExpression = module.exports = (function(){
  2. // CONSTRUCTOR
  3. /** An $or pipeline expression. @see evaluate **/
  4. var klass = function OrExpression(){
  5. if(arguments.length !== 0) throw new Error("zero args expected");
  6. base.call(this);
  7. }, base = require("./NaryExpression"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  8. // DEPENDENCIES
  9. var Value = require("../Value"),
  10. ConstantExpression = require("./ConstantExpression"),
  11. CoerceToBoolExpression = require("./CoerceToBoolExpression");
  12. // PROTOTYPE MEMBERS
  13. proto.getOpName = function getOpName(){
  14. return "$or";
  15. };
  16. proto.addOperand = function addOperand(expr) {
  17. this.checkArgLimit(1);
  18. base.addOperand(expr);
  19. };
  20. /** 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. **/
  21. proto.evaluate = function evaluate(doc){
  22. for(var i = 0, n = this.operands.length; i < n; ++i){
  23. var value = this.operands[i].evaluate(doc);
  24. if(Value.coerceToBool(value)) return true;
  25. }
  26. return false;
  27. };
  28. proto.optimize = function optimize() {
  29. var pE = base.optimize(); // optimize the disjunction as much as possible
  30. if (!(pE instanceof OrExpression)) return pE; // if the result isn't a disjunction, we can't do anything
  31. var pOr = pE;
  32. // Check the last argument on the result; if it's not const (as promised
  33. // by ExpressionNary::optimize(),) then there's nothing we can do.
  34. if (!pOr.operands.length) throw new Error("OrExpression must have operands!");
  35. var n = pOr.operands.length;
  36. // ExpressionNary::optimize() generates an ExpressionConstant for {$or:[]}.
  37. var pLast = pOr.operands[n - 1];
  38. var pConst = pE;
  39. if (!(pConst instanceof ConstantExpression)) return pE;
  40. // Evaluate and coerce the last argument to a boolean. If it's true, then we can replace this entire expression.
  41. var last = Value.coerceToBool(pLast.evaluate());
  42. if (last) return new ConstantExpression(true);
  43. // If we got here, the final operand was false, so we don't need it anymore.
  44. // 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.
  45. if (n == 2) return new CoerceToBoolExpression(pOr.operands[0]);
  46. // Remove the final "false" value, and return the new expression.
  47. pOr.operands.length = n - 1;
  48. return pE;
  49. };
  50. //TODO: proto.toMatcherBson = ...?
  51. proto.getFactory = function getFactory(){
  52. return klass; // using the ctor rather than a separate .create() method
  53. };
  54. return klass;
  55. })();