ArrayMatchingMatchExpression.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. "use strict";
  2. var MatchExpression = require('./MatchExpression');
  3. var ArrayMatchingMatchExpression = module.exports = function ArrayMatchingMatchExpression(matchType){
  4. base.call(this);
  5. this._matchType = matchType;
  6. this._elementPath = new ElementPath();
  7. }, klass = ArrayMatchingMatchExpression, base = MatchExpression, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  8. // DEPENDENCIES
  9. var errors = require("../../Errors.js"),
  10. ErrorCodes = errors.ErrorCodes,
  11. ElementPath = require('./ElementPath.js');
  12. proto._path = undefined;
  13. /**
  14. *
  15. * Initialize the input path as our element path
  16. * @method initPath
  17. * @param path
  18. *
  19. */
  20. proto.initPath = function initPath(path){
  21. this._path = path;
  22. var status = this._elementPath.init(this._path);
  23. this._elementPath.setTraverseLeafArray(false);
  24. return status;
  25. };
  26. /**
  27. * Deviation from mongo:
  28. * matches checks the input doc against the internal path to see if it is a match
  29. * @method matches
  30. * @param doc
  31. * @param details
  32. *
  33. */
  34. proto.matches = function matches(doc, details){
  35. var self = this,
  36. checker = function(element) {
  37. // we got the whole path, now check it
  38. if (!(element instanceof Array))
  39. return false;
  40. //var amIRoot = (element.length === 0);
  41. if (!self.matchesArray(element, details))
  42. return false;
  43. /*
  44. if (!amIRoot && details && details.needRecord() {
  45. details.setElemMatchKey(element);
  46. }
  47. */
  48. return true;
  49. };
  50. return this._elementPath._matches(doc, details, checker);
  51. };
  52. /**
  53. *
  54. * Check if the input element matches
  55. * @method matchesSingleElement
  56. * @param
  57. *
  58. */
  59. proto.matchesSingleElement = function matchesSingleElement(element){
  60. if (!(element instanceof Array)){
  61. return false;
  62. }
  63. return this.matchesArray(element, null);
  64. };
  65. /**
  66. *
  67. * Check if the input element is equivalent to us
  68. * @method equivalent
  69. * @param
  70. *
  71. */
  72. proto.equivalent = function equivalent(other){
  73. if ( this._matchType != other._matchType)
  74. return false;
  75. var realOther = new ArrayMatchingMatchExpression(other);
  76. if (this._path != realOther._path)
  77. return false;
  78. if (this.numChildren() != realOther.numChildren())
  79. return false;
  80. for (var i = 0; i < this.numChildren(); i++)
  81. if (! (this.getChild(i).equivalent(realOther.getChild(i)) ) )
  82. return false;
  83. return true;
  84. };
  85. /**
  86. *
  87. * return the internal path
  88. * @method path
  89. * @param
  90. *
  91. */
  92. proto.path = function path(){
  93. return this._path;
  94. };
  95. /**
  96. *
  97. * Check if the input array matches
  98. * @method path
  99. * @param anArray
  100. * @param details
  101. *
  102. */
  103. proto.matchesArray = function matchesArray(anArray, details){
  104. throw new Error("not implemented");
  105. };