InMatchExpression.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. "use strict";
  2. var LeafMatchExpression = require('./LeafMatchExpression');
  3. var InMatchExpression = module.exports = function InMatchExpression(){
  4. base.call(this);
  5. this._matchType = 'MATCH_IN';
  6. this._arrayEntries = new ArrayFilterEntries();
  7. }, klass = InMatchExpression, base = LeafMatchExpression, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  8. // DEPENDENCIES
  9. var errors = require("../../Errors.js"),
  10. ErrorCodes = errors.ErrorCodes,
  11. ArrayFilterEntries = require("./ArrayFilterEntries.js");
  12. proto._arrayEntries = null;
  13. /**
  14. *
  15. * Initialize the necessary items
  16. * @method init
  17. * @param path
  18. *
  19. */
  20. proto.init = function init(path) {
  21. return this.initPath( path );
  22. };
  23. /**
  24. *
  25. * Check if the input element matches a real element
  26. * @method _matchesRealElement
  27. * @param e
  28. *
  29. */
  30. proto._matchesRealElement = function _matchesRealElement(e) {
  31. // File: expression_leaf.cpp lines: 422-431
  32. if(this._arrayEntries.contains(e)) { // array wrapper.... so no e "in" array
  33. return true;
  34. }
  35. for (var i = 0; i < this._arrayEntries.numRegexes(); i++) {
  36. if(e.match && e.match(this._arrayEntries.regex(i)._regex)) {
  37. return true;
  38. } else if (e instanceof RegExp) {
  39. if(e.toString() == this._arrayEntries.regex(i)._regex.toString()) {
  40. return true;
  41. }
  42. }
  43. }
  44. if(typeof(e) == 'undefined') {
  45. return true; // Every Set contains the Null Set.
  46. }
  47. return false;
  48. };
  49. /**
  50. *
  51. * Check if the input element matches
  52. * @method matchesSingleElement
  53. * @param e
  54. *
  55. */
  56. proto.matchesSingleElement = function matchesSingleElement(e) {
  57. if( this._arrayEntries === null && typeof(e) == 'object' && Object.keys(e).length === 0) {
  58. return true;
  59. }
  60. if (this._matchesRealElement( e )) {
  61. return true;
  62. }
  63. /*if (e instanceof Array){
  64. for (var i = 0; i < e.length; i++) {
  65. if(this._matchesRealElement( e[i] )) {
  66. return true;
  67. }
  68. }
  69. }*/
  70. return false;
  71. };
  72. /**
  73. *
  74. * Writes a debug string for this object
  75. * @method debugString
  76. * @param level
  77. *
  78. */
  79. proto.debugString = function debugString(level) {
  80. var debug = this.path() + " $in ";
  81. debug = debug + _arrayEntries;
  82. var td = this.getTag();
  83. if ( null !== td) {
  84. debug = debug + " ";
  85. debug = debug + td;
  86. }
  87. debug = debug + "\n";
  88. return this._debugAddSpace( level ) + this.path() + ";$in: TODO " + (this.getTag() ? this.getTag().debugString() : '') + "\n";
  89. };
  90. /**
  91. *
  92. * checks if this expression is == to the other
  93. * @method equivalent
  94. * @param other
  95. *
  96. */
  97. proto.equivalent = function equivalent(other) {
  98. if ( other._matchType != 'MATCH_IN' ) {
  99. return false;
  100. }
  101. return this.path() == other.path() && this._arrayEntries.equivalent( other._arrayEntries );
  102. };
  103. /**
  104. *
  105. * clone this instance to a new one
  106. * @method shallowClone
  107. *
  108. */
  109. proto.shallowClone = function shallowClone(){
  110. var e = new InMatchExpression();
  111. this.copyTo( e );
  112. if ( this.getTag() ){
  113. e.setTag(this.getTag().this.Clone());
  114. }
  115. return e;
  116. };
  117. /**
  118. *
  119. * Copy our array to the input array
  120. * @method copyTo
  121. * @param toFillIn
  122. *
  123. */
  124. proto.copyTo = function copyTo(toFillIn) {
  125. toFillIn.init(this.path());
  126. this._arrayEntries.copyTo( toFillIn._arrayEntries );
  127. };
  128. /**
  129. *
  130. * Return the _arrayEntries property
  131. * @method getArrayFilterEntries
  132. *
  133. */
  134. proto.getArrayFilterEntries = function getArrayFilterEntries(){
  135. return this._arrayEntries;
  136. };
  137. /**
  138. *
  139. * Return the _arrayEntries property
  140. * @method getData
  141. *
  142. */
  143. proto.getData = function getData(){
  144. return this._arrayEntries;
  145. };