AllElemMatchOp.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. "use strict";
  2. var ElementPath = require("./ElementPath");
  3. var AllElemMatchOp = module.exports = function AllElemMatchOp(){
  4. base.call(this);
  5. this._matchType = "ALL";
  6. this._elementPath = new ElementPath();
  7. this._list = [];
  8. }, klass = AllElemMatchOp, base = require("./MatchExpression"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}}); //jshint ignore:line
  9. // ElementPath _elementPath
  10. proto._elementPath = undefined;
  11. // std::vector< MatchExpression* > _list;
  12. proto._list = undefined;
  13. // StringData _path;
  14. proto._path = undefined;
  15. /**
  16. *
  17. * This method checks the input array and determines if each item inside matches
  18. * @method _allMatch
  19. * @param anArray
  20. *
  21. */
  22. proto._allMatch = function _allMatch(anArray) {
  23. if (this._list.length === 0) return false;
  24. for (var i = 0; i < this._list.length; i++) {
  25. if (!this._list[i].matchesArray(anArray, null)) return false;
  26. }
  27. return true;
  28. };
  29. /**
  30. *
  31. * This method adds a new expression to the internal array of expression
  32. * @method add
  33. * @param expr
  34. *
  35. */
  36. proto.add = function add(expr) {
  37. if (!expr) throw new Error("AllElemMatchOp:add#68 failed to verify expr");
  38. this._list.push(expr);
  39. };
  40. /**
  41. *
  42. * Writes a debug string for this object
  43. * @method debugString
  44. * @param level
  45. *
  46. */
  47. proto.debugString = function debugString(level) {
  48. console.debug(this._debugAddSpace(level) + this._path + " AllElemMatchOp: " + this._path + "\n");
  49. for (var i = 0; i < this._list.length; i++) {
  50. this._list[i].debugString(level +1);
  51. }
  52. };
  53. /**
  54. *
  55. * checks if this expression is == to the other
  56. * @method equivalent
  57. * @param other
  58. *
  59. */
  60. proto.equivalent = function equivalent(other) {
  61. if (this.matchType() !== other.matchType()) {
  62. return false;
  63. }
  64. if (this._path !== other._path) {
  65. return false;
  66. }
  67. if (this._list.length !== other._list.length) {
  68. return false;
  69. }
  70. for (var i = 0; i < this._list.length; i++) {
  71. if (!this._list[i].equivalent(other._list[i])) {
  72. return false;
  73. }
  74. }
  75. return true;
  76. };
  77. /**
  78. *
  79. * gets the specified item from the list
  80. * @method getChild
  81. * @param i
  82. *
  83. */
  84. proto.getChild = function getChild(i) {
  85. return this._list[i];
  86. };
  87. /**
  88. *
  89. * Initialize the necessary items
  90. * @method init
  91. * @param path
  92. *
  93. */
  94. proto.init = function init(path) {
  95. this._path = path;
  96. var s = this._elementPath.init(this._path);
  97. this._elementPath.setTraverseLeafArray(false);
  98. return s;
  99. };
  100. /**
  101. *
  102. * matches checks the input doc against the internal path to see if it is a match
  103. * @method matches
  104. * @param doc
  105. * @param details
  106. *
  107. */
  108. proto.matches = function matches(doc, details) {
  109. var self = this,
  110. checker = function(element) {
  111. if (!(element instanceof Array)) {
  112. return false;
  113. }
  114. //var amIRoot = (element.length === 0);
  115. if (self._allMatch(element)) {
  116. return true;
  117. }
  118. /*
  119. if (!amIRoot && details && details.needRecord() {
  120. details.setElemMatchKey(element);
  121. }
  122. */
  123. return false;
  124. };
  125. return this._elementPath._matches(doc, details, checker);
  126. };
  127. /**
  128. *
  129. * Check if the input element matches
  130. * @method matchesSingleElement
  131. * @param e
  132. *
  133. */
  134. proto.matchesSingleElement = function matchesSingleElement(e) {
  135. if (!(e instanceof Array)) {
  136. return false;
  137. }
  138. return this._allMatch(e);
  139. };
  140. /**
  141. *
  142. * return the length of the internal array
  143. * @method numChildren
  144. * @param
  145. *
  146. */
  147. proto.numChildren = function numChildren() {
  148. return this._list.length;
  149. };
  150. /**
  151. *
  152. * return the internal path
  153. * @method path
  154. * @param
  155. *
  156. */
  157. proto.path = function path() {
  158. return this._path;
  159. };
  160. /**
  161. *
  162. * clone this instance to a new one
  163. * @method shallowClone
  164. * @param
  165. *
  166. */
  167. proto.shallowClone = function shallowClone() {
  168. var e = new AllElemMatchOp();
  169. e.init(this._path);
  170. e._list = this._list.slice(0);
  171. return e;
  172. };