ComparisonMatchExpression.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. "use strict";
  2. var LeafMatchExpression = require('./LeafMatchExpression.js');
  3. var Value = require('../Value');
  4. // Autogenerated by cport.py on 2013-09-17 14:37
  5. var ComparisonMatchExpression = module.exports = function ComparisonMatchExpression( type ){
  6. base.call(this);
  7. this._matchType = type;
  8. }, klass = ComparisonMatchExpression, base = LeafMatchExpression, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  9. // File: expression_leaf.h lines: 88-88
  10. proto._rhs = undefined;
  11. /**
  12. *
  13. * Writes a debug string for this object
  14. * @method debugString
  15. * @param level
  16. *
  17. */
  18. proto.debugString = function debugString(level) {
  19. // File: expression_leaf.cpp lines: 135-154
  20. var retStr = this._debugAddSpace( level ) + this.path() + " ";
  21. switch (this._matchType){
  22. case 'LT':
  23. retStr += '$lt';
  24. break;
  25. case 'LTE':
  26. retStr += '$lte';
  27. break;
  28. case 'EQ':
  29. retStr += '==';
  30. break;
  31. case 'GT':
  32. retStr += '$gt';
  33. break;
  34. case 'GTE':
  35. retStr += '$gte';
  36. break;
  37. default:
  38. retStr += "Unknown comparison!";
  39. break;
  40. }
  41. retStr += (this._rhs ? this._rhs.toString() : '?');
  42. if ( this.getTag() ) {
  43. retStr += this.getTag().debugString();
  44. }
  45. return retStr + '\n';
  46. };
  47. /**
  48. *
  49. * checks if this expression is == to the other
  50. * @method equivalent
  51. * @param other
  52. *
  53. */
  54. proto.equivalent = function equivalent(other) {
  55. // File: expression_leaf.cpp lines: 53-61
  56. if (other._matchType != this._matchType) return false;
  57. return this.path() === other.path() && Value.compare(this._rhs,other._rhs) === 0;
  58. };
  59. /**
  60. *
  61. * Return the _rhs property
  62. * @method getData
  63. *
  64. */
  65. proto.getData = function getData(){
  66. // File: expression_leaf.h lines: 85-84
  67. return this._rhs;
  68. };
  69. /**
  70. *
  71. * Return the _rhs property
  72. * @method getRHS
  73. *
  74. */
  75. proto.getRHS = function getRHS(){
  76. // File: expression_leaf.h lines: 79-78
  77. return this._rhs;
  78. };
  79. /**
  80. *
  81. * Initialize the necessary items
  82. * @method init
  83. * @param path
  84. * @param type
  85. *
  86. */
  87. proto.init = function init(path,rhs) {
  88. // File: expression_leaf.cpp lines: 65-87
  89. this._rhs = rhs;
  90. if ( (rhs instanceof Object && Object.keys(rhs).length === 0)) { return {'code':'BAD_VALUE', 'description':'Need a real operand'};}
  91. if ( rhs === undefined ) { return {'code':'BAD_VALUE', 'desc':'Cannot compare to undefined'};}
  92. if (!(this._matchType in {"LT":1, "LTE":1, "EQ":1, "GT":1, "GTE":1})) {
  93. return {'code':'BAD_VALUE', 'description':'Bad match type for ComparisonMatchExpression'};
  94. }
  95. return this.initPath( path );
  96. };
  97. /**
  98. *
  99. * Check if the input element matches
  100. * @method matchesSingleElement
  101. * @param e
  102. *
  103. */
  104. proto.matchesSingleElement = function matchesSingleElement(e){
  105. // File: expression_leaf.cpp lines: 91-132
  106. if( typeof(e) != typeof(this._rhs) ){
  107. if( this._rhs === null) {
  108. if(this._matchType in {'EQ':1, 'LTE':1, 'GTE':1}){
  109. if(e === undefined || e === null || (e instanceof Object && Object.keys(e).length === 0)) {
  110. return true;
  111. }
  112. }
  113. return false;
  114. }
  115. if((e === null || e === undefined) && (this._rhs ===null || this._rhs === undefined)) {
  116. return ["EQ","LTE","GTE"].indexOf(this._matchType) != -1;
  117. }
  118. if (this._rhs.constructor.name in {'MaxKey':1,'MinKey':1} ) {
  119. return this._matchType != "EQ";
  120. }
  121. return false;
  122. }
  123. if( this._rhs instanceof Array) {
  124. if( this._matchType != 'EQ') {
  125. return false;
  126. }
  127. }
  128. var x = Value.compare( e, this._rhs );
  129. switch( this._matchType ) {
  130. case "LT":
  131. return x == -1;
  132. case "LTE":
  133. return x <= 0;
  134. case "EQ":
  135. return x === 0;
  136. case "GT":
  137. return x === 1;
  138. case "GTE":
  139. return x >= 0;
  140. default:
  141. throw new Error("Invalid comparison type evaluated.");
  142. }
  143. return false;
  144. };