ListOfMatchExpression.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. "use strict";
  2. var MatchExpression = require('./MatchExpression');
  3. /**
  4. * Create a match expression to match a list of
  5. * @class ListOfMatchExpression
  6. * @namespace mungedb-aggregate.pipeline.matcher
  7. * @module mungedb-aggregate
  8. * @constructor
  9. */
  10. var ListOfMatchExpression = module.exports = function ListOfMatchExpression(matchType){
  11. base.call(this);
  12. this._expressions = [];
  13. this._matchType = matchType;
  14. }, klass = ListOfMatchExpression, base = MatchExpression, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  15. proto._expressions = undefined;
  16. /**
  17. *
  18. * Append a new expression to our list
  19. * @method add
  20. * @param Expression
  21. *
  22. */
  23. proto.add = function add( exp ){
  24. // verify(expression)
  25. if(!exp)
  26. throw new Error(exp + " failed verify on ListOfMatchExpression:add");
  27. if(this._expressions) {
  28. this._expressions.push(exp);
  29. } else {
  30. this._expressions = [exp];
  31. }
  32. };
  33. /**
  34. *
  35. * Empty us out
  36. * @method clearAndRelease
  37. *
  38. */
  39. proto.clearAndRelease = function clearAndRelease(){
  40. this._expressions = []; // empty the expressions
  41. };
  42. /**
  43. *
  44. * Get the length of the list
  45. * @method numChildren
  46. * @param
  47. *
  48. */
  49. proto.numChildren = function numChildren(){
  50. return this._expressions.length;
  51. };
  52. /**
  53. *
  54. * Get an item from the expressions
  55. * @method getChild
  56. * @param i index of the child
  57. *
  58. */
  59. proto.getChild = function getChild(i){
  60. return this._expressions[i];
  61. };
  62. /**
  63. *
  64. * Get the expressions
  65. * @method getChildVector
  66. * @param
  67. *
  68. */
  69. proto.getChildVector = function getChildVector(){
  70. return this._expressions;
  71. };
  72. /**
  73. *
  74. * Print the debug info from each expression in the list
  75. * @method _debugList
  76. * @param level
  77. *
  78. */
  79. proto._debugList = function _debugList(debug, level){
  80. for (var i = 0; i < this._expressions.length; i++ )
  81. this._expressions[i].debugString(debug, level + 1);
  82. };
  83. /**
  84. *
  85. * Check if the input list is considered the same as this one
  86. * @method equivalent
  87. * @param other
  88. *
  89. */
  90. proto.equivalent = function equivalent(other){
  91. if (this._matchType != other._matchType)
  92. return false;
  93. var realOther = new ListOfMatchExpression(other);
  94. if (this._expressions.length != realOther._expressions.length)
  95. return false;
  96. // TODO: order doesn't matter
  97. for (var i = 0; i < this._expressions.length; i++ )
  98. if (!this._expressions[i].equivalent(realOther._expressions[i]))
  99. return false;
  100. return true;
  101. };