ListOfMatchExpression.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.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}}); //jshint ignore:line
  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(level){
  80. var s = "";
  81. for (var i = 0; i < this._expressions.length; i++) {
  82. s += this._expressions[i].debugString(level + 1);
  83. }
  84. return s;
  85. };
  86. /**
  87. *
  88. * Check if the input list is considered the same as this one
  89. * @method equivalent
  90. * @param other
  91. *
  92. */
  93. proto.equivalent = function equivalent(other){
  94. if (this._matchType !== other._matchType)
  95. return false;
  96. var realOther = other; //NOTE: the JS does not need to static_cast like the C++ does
  97. if (this._expressions.length !== realOther._expressions.length)
  98. return false;
  99. // TODO: order doesn't matter
  100. for (var i = 0; i < this._expressions.length; i++ )
  101. if (!this._expressions[i].equivalent(realOther._expressions[i]))
  102. return false;
  103. return true;
  104. };