ArrayMatchingMatchExpression.js 2.5 KB

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