AnyElementTrueExpression.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. "use strict";
  2. /**
  3. * Create an expression that returns true exists in any element.
  4. * @class AnyElementTrueExpression
  5. * @namespace mungedb-aggregate.pipeline.expressions
  6. * @module mungedb-aggregate
  7. * @constructor
  8. **/
  9. var AnyElementTrueExpression = module.exports = function AnyElementTrueExpression(){
  10. this.nargs = (1);
  11. base.call(this);
  12. }, klass = AnyElementTrueExpression, NaryExpression = require("./NaryExpression"), base = NaryExpression, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  13. // DEPENDENCIES
  14. var Value = require("../Value"),
  15. Expression = require("./Expression");
  16. // PROTOTYPE MEMBERS
  17. proto.getOpName = function getOpName(){
  18. return "$anyElementTrue";
  19. };
  20. /**
  21. * Takes an array of one or more numbers and returns true if any.
  22. * @method @evaluateInternal
  23. **/
  24. proto.evaluateInternal = function evaluateInternal(vars) {
  25. var arr = this.operands[0].evaluateInternal(vars);
  26. if (!(arr instanceof Array)) {
  27. throw new Error("uassert 17041: $anyElementTrue's " +
  28. "argument must be an array, but is " +
  29. typeof arr);
  30. }
  31. for (var i=0, n=arr.length; i<n; ++i) {
  32. if (Value.coerceToBool(arr[i]))
  33. return true;
  34. }
  35. return false;
  36. };
  37. /** Register Expression */
  38. Expression.registerExpression("$anyElementTrue",base.parse(AnyElementTrueExpression));