InMatchExpression.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. if(this._arrayEntries.contains(e)) { // array wrapper.... so no e "in" array
  32. return true;
  33. }
  34. for (var i = 0; i < this._arrayEntries.numRegexes(); i++) {
  35. if(e.match && e.match(this._arrayEntries.regex(i)._regex)) {
  36. return true;
  37. } else if (e instanceof RegExp) {
  38. if(e.toString() === this._arrayEntries.regex(i)._regex.toString()) {
  39. return true;
  40. }
  41. }
  42. }
  43. if(typeof(e) === 'undefined') {
  44. return true; // Every Set contains the Null Set.
  45. }
  46. return false;
  47. };
  48. /**
  49. *
  50. * Check if the input element matches
  51. * @method matchesSingleElement
  52. * @param e
  53. *
  54. */
  55. proto.matchesSingleElement = function matchesSingleElement(e) {
  56. if( this._arrayEntries === null && typeof(e) == 'object' && Object.keys(e).length === 0) {
  57. return true;
  58. }
  59. if (this._matchesRealElement( e )) {
  60. return true;
  61. }
  62. /*if (e instanceof Array){
  63. for (var i = 0; i < e.length; i++) {
  64. if(this._matchesRealElement( e[i] )) {
  65. return true;
  66. }
  67. }
  68. }*/
  69. return false;
  70. };
  71. /**
  72. *
  73. * Writes a debug string for this object
  74. * @method debugString
  75. * @param level
  76. *
  77. */
  78. proto.debugString = function debugString(level) {
  79. return this._debugAddSpace( level ) + this.path() + " $in " + this._arrayEntries + (this.getTag() ? this.getTag().debugString() : '') + "\n";
  80. };
  81. /**
  82. *
  83. * checks if this expression is == to the other
  84. * @method equivalent
  85. * @param other
  86. *
  87. */
  88. proto.equivalent = function equivalent(other) {
  89. if ( other._matchType != 'MATCH_IN' ) {
  90. return false;
  91. }
  92. return this.path() === other.path() && this._arrayEntries.equivalent( other._arrayEntries );
  93. };
  94. /**
  95. *
  96. * clone this instance to a new one
  97. * @method shallowClone
  98. *
  99. */
  100. proto.shallowClone = function shallowClone(){
  101. var e = new InMatchExpression();
  102. this.copyTo( e );
  103. if ( this.getTag() ){
  104. e.setTag(this.getTag().Clone());
  105. }
  106. return e;
  107. };
  108. /**
  109. *
  110. * Copy our array to the input array
  111. * @method copyTo
  112. * @param toFillIn
  113. *
  114. */
  115. proto.copyTo = function copyTo(toFillIn) {
  116. toFillIn.init(this.path());
  117. this._arrayEntries.copyTo( toFillIn._arrayEntries );
  118. };
  119. /**
  120. *
  121. * Return the _arrayEntries property
  122. * @method getArrayFilterEntries
  123. *
  124. */
  125. proto.getArrayFilterEntries = function getArrayFilterEntries(){
  126. return this._arrayEntries;
  127. };
  128. /**
  129. *
  130. * Return the _arrayEntries property
  131. * @method getData
  132. *
  133. */
  134. proto.getData = function getData(){
  135. return this._arrayEntries;
  136. };