AndExpression.js 3.0 KB

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