OrExpression.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. var OrExpression = module.exports = (function(){
  2. // CONSTRUCTOR
  3. /**
  4. * An $or pipeline expression.
  5. *
  6. * @see evaluate
  7. * @class OrExpression
  8. * @namespace munge.pipeline.expressions
  9. * @module munge
  10. * @constructor
  11. **/
  12. var klass = function OrExpression(){
  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. ConstantExpression = require("./ConstantExpression"),
  19. CoerceToBoolExpression = require("./CoerceToBoolExpression");
  20. // PROTOTYPE MEMBERS
  21. proto.getOpName = function getOpName(){
  22. return "$or";
  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. **/
  27. proto.evaluate = function evaluate(doc){
  28. for(var i = 0, n = this.operands.length; i < n; ++i){
  29. var value = this.operands[i].evaluate(doc);
  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(pLast.evaluate());
  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. proto.getFactory = function getFactory(){
  56. return klass; // using the ctor rather than a separate .create() method
  57. };
  58. return klass;
  59. })();