ArrayFilterEntries.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. "use strict";
  2. var Value = require('../Value'),
  3. ErrorCodes = require('../../Errors').ErrorCodes;
  4. var ArrayFilterEntries = module.exports = function ArrayFilterEntries(){
  5. this._hasNull = false;
  6. this._hasEmptyArray = false;
  7. this._equalities = [];
  8. this._regexes = [];
  9. }, klass = ArrayFilterEntries, base = Object, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  10. proto._equalities = undefined;
  11. proto._hasEmptyArray = undefined;
  12. proto._hasNull = undefined;
  13. proto._regexes = undefined;
  14. /**
  15. *
  16. * Push the input expression onto the _equalities array
  17. * @method addEquality
  18. * @param e
  19. *
  20. */
  21. proto.addEquality = function addEquality(e) {
  22. if( e instanceof RegExp ) {
  23. return {'code': ErrorCodes.BAD_VALUE, 'desc':'ArrayFilterEntries equality cannot be a regex'};
  24. }
  25. if (e === undefined) {
  26. return {'code': ErrorCodes.BAD_VALUE, 'desc':'ArrayFilterEntries equality cannot be undefined'};
  27. }
  28. if( e === null ) {
  29. this._hasNull = true;
  30. }
  31. if (e instanceof Array && e.length === 0) {
  32. this._hasEmptyArray = true;
  33. }
  34. this._equalities.push( e );
  35. return {'code':ErrorCodes.OK};
  36. };
  37. /**
  38. *
  39. * Push the input regex onto the _regexes array
  40. * @method addRegex
  41. * @param expr
  42. *
  43. */
  44. proto.addRegex = function addRegex(expr) {
  45. this._regexes.push( expr );
  46. return {'code':ErrorCodes.OK};
  47. };
  48. /**
  49. *
  50. * Check if the input element is contained inside of _equalities
  51. * @method contains
  52. * @param elem
  53. *
  54. */
  55. proto.contains = function contains(elem) {
  56. for (var i = 0; i < this._equalities.length; i++) {
  57. if(typeof(elem) == typeof(this._equalities[i])){
  58. if(Value.compare(elem, this._equalities[i]) === 0) {
  59. return true;
  60. }
  61. }
  62. }
  63. return false;
  64. };
  65. /**
  66. *
  67. * Copy our internal fields to the input
  68. * @method copyTo
  69. * @param toFillIn
  70. *
  71. */
  72. proto.copyTo = function copyTo(toFillIn) {
  73. // File: expression_leaf.cpp lines: 407-412
  74. toFillIn._hasNull = this._hasNull;
  75. toFillIn._hasEmptyArray = this._hasEmptyArray;
  76. toFillIn._equalities = this._equalities.slice(0); // Copy array
  77. toFillIn._regexes = this._regexes.slice(0); // Copy array
  78. for (var i = 0; i < this._regexes.length; i++){
  79. toFillIn._regexes.push(this._regexes[i].shallowClone());
  80. }
  81. };
  82. /**
  83. *
  84. * Return the _equalities property
  85. * @method equalities
  86. *
  87. */
  88. proto.equalities = function equalities(){
  89. return this._equalities;
  90. };
  91. /**
  92. *
  93. * checks if this expression is == to the other
  94. * @method equivalent
  95. * @param other
  96. *
  97. */
  98. proto.equivalent = function equivalent(other) {
  99. if (this._hasNull != other._hasNull) {return false;}
  100. if (this.size() != other.size()) {return false;}
  101. for (var i = 0; i < this._regexes.length; i++) {
  102. if ( !this._regexes[i].equivalent( other._regexes[i] ) ) {
  103. return false;
  104. }
  105. }
  106. return Value.compare(this._equalities, other._equalities);
  107. };
  108. /**
  109. *
  110. * Return the _hasEmptyArray property
  111. * @method hasEmptyArray
  112. *
  113. */
  114. proto.hasEmptyArray = function hasEmptyArray(){
  115. return this._hasEmptyArray;
  116. };
  117. /**
  118. *
  119. * Return the _hasNull property
  120. * @method hasNull
  121. *
  122. */
  123. proto.hasNull = function hasNull(){
  124. return this._hasNull;
  125. };
  126. /**
  127. *
  128. * Return the length of the _regexes property
  129. * @method numRegexes
  130. *
  131. */
  132. proto.numRegexes = function numRegexes(){
  133. return this._regexes.length;
  134. };
  135. /**
  136. *
  137. * Return the regex at the given index
  138. * @method regex
  139. * @param idx
  140. *
  141. */
  142. proto.regex = function regex(idx) {
  143. return this._regexes[idx];
  144. };
  145. /**
  146. *
  147. * Return whether we have a single item and it is null
  148. * @method singleNull
  149. *
  150. */
  151. proto.singleNull = function singleNull(){
  152. return this.size() == 1 && this._hasNull;
  153. };
  154. /**
  155. *
  156. * Return the length of both _regexes and _equalities
  157. * @method size
  158. *
  159. */
  160. proto.size = function size(){
  161. return this._equalities.length + this._regexes.length;
  162. };
  163. proto.debugString = function debugString(){
  164. var debug = "[ ";
  165. for (var i = 0; i < this._equalities.length; i++){
  166. debug += this._equalities[i].toString() + " ";
  167. }
  168. for (var j = 0; j < this._regexes.length; j++){
  169. debug += this._regexes[j].shortDebugString() + " ";
  170. }
  171. debug += "]";
  172. return debug;
  173. };