OrExpression.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. "use strict";
  2. /**
  3. * An $or pipeline expression.
  4. * @class OrExpression
  5. * @extends mungedb-aggregate.pipeline.expressions.VariadicExpressionT
  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. var Value = require("../Value"),
  15. ConstantExpression = require("./ConstantExpression"),
  16. CoerceToBoolExpression = require("./CoerceToBoolExpression"),
  17. Expression = require("./Expression");
  18. proto.evaluateInternal = function evaluateInternal(vars){
  19. var n = this.operands.length;
  20. for (var i = 0; i < n; ++i) {
  21. var value = this.operands[i].evaluateInternal(vars);
  22. if (Value.coerceToBool(value))
  23. return true;
  24. }
  25. return false;
  26. };
  27. proto.optimize = function optimize() {
  28. // optimize the disjunction as much as possible
  29. var expr = base.prototype.optimize.call(this);
  30. // if the result isn't a disjunction, we can't do anything
  31. var orExp = expr instanceof OrExpression ? expr : undefined;
  32. if (!orExp)
  33. return expr;
  34. /*
  35. * Check the last argument on the result; if it's not constant (as
  36. * promised by ExpressionNary::optimize(),) then there's nothing
  37. * we can do.
  38. */
  39. var n = orExp.operands.length;
  40. // ExpressionNary::optimize() generates an ExpressionConstant for {$or:[]}.
  41. if (n <= 0) throw new Error("Assertion failuer");
  42. var lastExpr = orExp.operands[n - 1],
  43. constExpr = lastExpr instanceof ConstantExpression ? lastExpr : undefined;
  44. if (!constExpr)
  45. return expr;
  46. /*
  47. * Evaluate and coerce the last argument to a boolean. If it's true,
  48. * then we can replace this entire expression.
  49. */
  50. var last = Value.coerceToBool(constExpr.evaluateInternal());
  51. if (last)
  52. return ConstantExpression.create(true);
  53. /*
  54. * If we got here, the final operand was false, so we don't need it
  55. * anymore. If there was only one other operand, we don't need the
  56. * conjunction either. Note we still need to keep the promise that
  57. * the result will be a boolean.
  58. */
  59. if (n === 2)
  60. return CoerceToBoolExpression.create(orExp.operands[0]);
  61. /*
  62. * Remove the final "false" value, and return the new expression.
  63. */
  64. orExp.operands.length = n - 1;
  65. return expr;
  66. };
  67. Expression.registerExpression("$or", base.parse);
  68. proto.getOpName = function getOpName() {
  69. return "$or";
  70. };
  71. proto.isAssociativeAndCommutative = function isAssociativeAndCommutative() {
  72. return true;
  73. };