InMatchExpression.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. "use strict";
  2. var ArrayFilterEntries = require("./ArrayFilterEntries");
  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 = require("./LeafMatchExpression"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}}); //jshint ignore:line
  8. proto._arrayEntries = null;
  9. /**
  10. *
  11. * Initialize the necessary items
  12. * @method init
  13. * @param path
  14. *
  15. */
  16. proto.init = function init(path) {
  17. return this.initPath( path );
  18. };
  19. /**
  20. *
  21. * Check if the input element matches a real element
  22. * @method _matchesRealElement
  23. * @param e
  24. *
  25. */
  26. proto._matchesRealElement = function _matchesRealElement(e) {
  27. if(this._arrayEntries.contains(e)) { // array wrapper.... so no e "in" array
  28. return true;
  29. }
  30. for (var i = 0; i < this._arrayEntries.numRegexes(); i++) {
  31. if ( this._arrayEntries.regex(i).matchesSingleElement( e ) )
  32. return true;
  33. }
  34. return false;
  35. };
  36. /**
  37. *
  38. * Check if the input element matches
  39. * @method matchesSingleElement
  40. * @param e
  41. *
  42. */
  43. proto.matchesSingleElement = function matchesSingleElement(e) {
  44. if( this._arrayEntries.hasNull() &&
  45. ( e === null ||
  46. e === undefined ||
  47. typeof(e) === "object" && Object.keys(e).length === 0
  48. ))
  49. {
  50. return true;
  51. }
  52. if (this._matchesRealElement( e )) {
  53. return true;
  54. }
  55. return false;
  56. };
  57. /**
  58. *
  59. * Writes a debug string for this object
  60. * @method debugString
  61. * @param level
  62. *
  63. */
  64. proto.debugString = function debugString(level) {
  65. return this._debugAddSpace( level ) +
  66. this.path() + " $in " +
  67. this._arrayEntries +
  68. (this.getTag() ? this.getTag().debugString() : "") +
  69. "\n";
  70. };
  71. /**
  72. *
  73. * checks if this expression is == to the other
  74. * @method equivalent
  75. * @param other
  76. *
  77. */
  78. proto.equivalent = function equivalent(other) {
  79. if (other._matchType !== "MATCH_IN") {
  80. return false;
  81. }
  82. return this.path() === other.path() && this._arrayEntries.equivalent( other._arrayEntries );
  83. };
  84. /**
  85. *
  86. * clone this instance to a new one
  87. * @method shallowClone
  88. *
  89. */
  90. proto.shallowClone = function shallowClone(){
  91. var e = new InMatchExpression();
  92. this.copyTo( e );
  93. if ( this.getTag() ){
  94. e.setTag(this.getTag().Clone());
  95. }
  96. return e;
  97. };
  98. /**
  99. *
  100. * Copy our array to the input array
  101. * @method copyTo
  102. * @param toFillIn
  103. *
  104. */
  105. proto.copyTo = function copyTo(toFillIn) {
  106. toFillIn.init(this.path());
  107. this._arrayEntries.copyTo( toFillIn._arrayEntries );
  108. };
  109. /**
  110. *
  111. * Return the _arrayEntries property
  112. * @method getArrayFilterEntries
  113. *
  114. */
  115. proto.getArrayFilterEntries = function getArrayFilterEntries(){
  116. return this._arrayEntries;
  117. };
  118. /**
  119. *
  120. * Return the _arrayEntries property
  121. * @method getData
  122. *
  123. */
  124. proto.getData = function getData(){
  125. return this._arrayEntries;
  126. };