ElemMatchValueMatchExpression.js 2.8 KB

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