ElemMatchValueMatchExpression.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. "use strict";
  2. var ElemMatchValueMatchExpression = module.exports = function ElemMatchValueMatchExpression(){
  3. base.call(this);
  4. this._matchType = "ELEM_MATCH_VALUE";
  5. this._subs = [];
  6. }, klass = ElemMatchValueMatchExpression, base = require("./ArrayMatchingMatchExpression"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}}); //jshint ignore:line
  7. var errors = require("../errors"),
  8. ErrorCodes = errors.ErrorCodes;
  9. proto._subs = undefined;
  10. /**
  11. *
  12. * Check if the input element matches all items in the array
  13. * @method _arrayElementMatchesAll
  14. * @param element
  15. *
  16. */
  17. proto._arrayElementMatchesAll = function _arrayElementMatchesAll(element){
  18. for (var i = 0; i < this._subs.length; i++ ) {
  19. if (!this._subs[i].matchesSingleElement(element))
  20. return false;
  21. }
  22. return true;
  23. };
  24. /**
  25. *
  26. * push an item onto the internal array
  27. * @method add
  28. * @param sub
  29. *
  30. */
  31. proto.add = function add(sub){
  32. if (!sub) throw new Error(sub + " ElemMatchValueMatchExpression:36");
  33. this._subs.push(sub);
  34. };
  35. /**
  36. *
  37. * Writes a debug string for this object
  38. * @method debugString
  39. * @param level
  40. *
  41. */
  42. proto.debugString = function debugString(level){
  43. var debug = this._debugAddSpace(level) +
  44. this.path() + " $elemMatch (value)" +
  45. (this.getTag() ? this.getTag().debugString() : "") +
  46. "\n";
  47. for (var i = 0; i < this._subs.length; i++) {
  48. debug += this._subs[i].debugString(level + 1);
  49. }
  50. return debug;
  51. };
  52. /**
  53. *
  54. * Get the given child in the internal array
  55. * @method getChild
  56. * @param i
  57. *
  58. */
  59. proto.getChild = function getChild(i){
  60. return this._subs[i];
  61. };
  62. /**
  63. *
  64. * Initialize the necessary items
  65. * @method init
  66. * @param path
  67. * @param sub
  68. *
  69. */
  70. proto.init = function init(path, sub){
  71. this.initPath(path);
  72. if (sub)
  73. this.add(sub);
  74. return {code:ErrorCodes.OK};
  75. };
  76. /**
  77. *
  78. * Check if one of the items in the input array matches everything in the internal array
  79. * @method matchesArray
  80. * @param anArray
  81. * @param details
  82. *
  83. */
  84. proto.matchesArray = function matchesArray(anArray, details){
  85. for (var i in anArray) { //jshint ignore:line
  86. var inner = anArray[i];
  87. if (this._arrayElementMatchesAll(inner)) {
  88. if (details && details.needRecord()) {
  89. details.setElemMatchKey(i);
  90. }
  91. return true;
  92. }
  93. }
  94. return false;
  95. };
  96. /**
  97. *
  98. * Return the number of items in the internal array
  99. * @method numChildren
  100. *
  101. */
  102. proto.numChildren = function numChildren(){
  103. return this._subs.length;
  104. };
  105. /**
  106. *
  107. * clone this instance to a new one
  108. * @method shallowClone
  109. *
  110. */
  111. proto.shallowClone = function shallowClone(){
  112. var element = new ElemMatchValueMatchExpression();
  113. element.init(this.path());
  114. for (var i = 0; i < this._subs.length; ++i) {
  115. element.add(this._subs[i].shallowClone());
  116. }
  117. var td = this.getTag();
  118. if (td) {
  119. element.setTag(td.clone());
  120. }
  121. return element;
  122. };