InMatchExpression.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 ( this._arrayEntries.regex(i).matchesSingleElement( e ) )
  36. return true;
  37. }
  38. return false;
  39. };
  40. /**
  41. *
  42. * Check if the input element matches
  43. * @method matchesSingleElement
  44. * @param e
  45. *
  46. */
  47. proto.matchesSingleElement = function matchesSingleElement(e) {
  48. if( this._arrayEntries.hasNull() &&
  49. ( e === null ||
  50. e === undefined ||
  51. typeof(e) === 'object' && Object.keys(e).length === 0
  52. ))
  53. {
  54. return true;
  55. }
  56. if (this._matchesRealElement( e )) {
  57. return true;
  58. }
  59. return false;
  60. };
  61. /**
  62. *
  63. * Writes a debug string for this object
  64. * @method debugString
  65. * @param level
  66. *
  67. */
  68. proto.debugString = function debugString(level) {
  69. return this._debugAddSpace( level ) + this.path() + " $in " + this._arrayEntries + (this.getTag() ? this.getTag().debugString() : '') + "\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. };