AndExpression.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. "use strict";
  2. /**
  3. * Create an expression that finds the conjunction of n operands. The
  4. * conjunction uses short-circuit logic; the expressions are evaluated in the
  5. * order they were added to the conjunction, and the evaluation stops and
  6. * returns false on the first operand that evaluates to false.
  7. *
  8. * @class AndExpression
  9. * @namespace mungedb-aggregate.pipeline.expressions
  10. * @module mungedb-aggregate
  11. * @constructor
  12. **/
  13. var AndExpression = module.exports = function AndExpression() {
  14. // if (arguments.length !== 0) throw new Error("zero args expected");
  15. base.call(this);
  16. }, klass = AndExpression, base = require("./VariadicExpressionT")(AndExpression), proto = klass.prototype = Object.create(base.prototype, {constructor: {value: klass}});
  17. // DEPENDENCIES
  18. var Value = require("../Value"),
  19. ConstantExpression = require("./ConstantExpression"),
  20. CoerceToBoolExpression = require("./CoerceToBoolExpression"),
  21. Expression = require("./Expression");
  22. // PROTOTYPE MEMBERS
  23. klass.opName = "$and";
  24. proto.getOpName = function getOpName() {
  25. return klass.opName;
  26. };
  27. /**
  28. * Takes an array one or more values and returns true if all of the values in the array are true. Otherwise $and returns false.
  29. * @method evaluate
  30. **/
  31. proto.evaluateInternal = function evaluateInternal(vars) {
  32. for (var i = 0, n = this.operands.length; i < n; ++i) {
  33. var value = this.operands[i].evaluateInternal(vars);
  34. if (!Value.coerceToBool()) return false;
  35. }
  36. return true;
  37. };
  38. proto.optimize = function optimize() {
  39. var expr = base.prototype.optimize.call(this); //optimize the conjunction as much as possible
  40. // if the result isn't a conjunction, we can't do anything
  41. if (!(expr instanceof AndExpression)) return expr;
  42. var andExpr = expr;
  43. // Check the last argument on the result; if it's not constant (as promised by ExpressionNary::optimize(),) then there's nothing we can do.
  44. var n = andExpr.operands.length;
  45. // ExpressionNary::optimize() generates an ExpressionConstant for {$and:[]}.
  46. if (!n) throw new Error("requires operands!");
  47. var lastExpr = andExpr.operands[n - 1];
  48. if (!(lastExpr instanceof ConstantExpression)) return expr;
  49. // Evaluate and coerce the last argument to a boolean. If it's false, then we can replace this entire expression.
  50. var last = Value.coerceToBool(lastExpr.evaluate());
  51. if (!last) return new ConstantExpression(false);
  52. // If we got here, the final operand was true, so we don't need it anymore.
  53. // If there was only one other operand, we don't need the conjunction either.
  54. // Note we still need to keep the promise that the result will be a boolean.
  55. if (n == 2) return new CoerceToBoolExpression(andExpr.operands[0]);
  56. //Remove the final "true" value, and return the new expression.
  57. //CW TODO: Note that because of any implicit conversions, we may need to apply an implicit boolean conversion.
  58. andExpr.operands.length = n - 1; //truncate the array
  59. return expr;
  60. };
  61. /** Register Expression */
  62. Expression.registerExpression(klass.opName, base.parse);
  63. //TODO: proto.toMatcherBson