AndExpression.js 2.9 KB

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