ComparisonMatchExpression.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. "use strict";
  2. var LeafMatchExpression = require("./LeafMatchExpression.js");
  3. var Value = require("../Value");
  4. /**
  5. * ComparisonMatchExpression
  6. * @class ComparisonMatchExpression
  7. * @namespace mungedb-aggregate.pipeline.matcher
  8. * @module mungedb-aggregate
  9. * @constructor
  10. */
  11. var ComparisonMatchExpression = module.exports = function ComparisonMatchExpression(type){
  12. base.call(this);
  13. this._matchType = type;
  14. }, klass = ComparisonMatchExpression, base = LeafMatchExpression, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  15. proto._rhs = undefined;
  16. /**
  17. *
  18. * Writes a debug string for this object
  19. * @method debugString
  20. * @param level
  21. *
  22. */
  23. proto.debugString = function debugString(level) {
  24. var retStr = this._debugAddSpace(level) + this.path() + " ";
  25. switch (this._matchType) {
  26. case "LT":
  27. retStr += "$lt";
  28. break;
  29. case "LTE":
  30. retStr += "$lte";
  31. break;
  32. case "EQ":
  33. retStr += "==";
  34. break;
  35. case "GT":
  36. retStr += "$gt";
  37. break;
  38. case "GTE":
  39. retStr += "$gte";
  40. break;
  41. default:
  42. retStr += "Unknown comparison!";
  43. break;
  44. }
  45. retStr += (this._rhs ? this._rhs.toString() : "?");
  46. if (this.getTag()) {
  47. retStr += this.getTag().debugString();
  48. }
  49. return retStr + "\n";
  50. };
  51. /**
  52. *
  53. * checks if this expression is == to the other
  54. * @method equivalent
  55. * @param other
  56. *
  57. */
  58. proto.equivalent = function equivalent(other) {
  59. if (other._matchType != this._matchType) return false;
  60. return this.path() === other.path() && Value.compare(this._rhs,other._rhs) === 0;
  61. };
  62. /**
  63. *
  64. * Return the _rhs property
  65. * @method getData
  66. *
  67. */
  68. proto.getData = function getData() {
  69. return this._rhs;
  70. };
  71. /**
  72. *
  73. * Return the _rhs property
  74. * @method getRHS
  75. *
  76. */
  77. proto.getRHS = function getRHS() {
  78. return this._rhs;
  79. };
  80. /**
  81. *
  82. * Initialize the necessary items
  83. * @method init
  84. * @param path
  85. * @param type
  86. *
  87. */
  88. proto.init = function init(path,rhs) {
  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. if (Value.canonicalize(e) !== Value.canonicalize(this._rhs)) {
  106. if (Value.canonicalize(e) + Value.canonicalize(this._rhs) === 5) {
  107. return ["EQ","LTE","GTE"].indexOf(this._matchType) != -1;
  108. }
  109. if (["MaxKey","MinKey"].indexOf(Value.getType(this._rhs)) != -1) {
  110. return this._matchType !== "EQ";
  111. }
  112. return false;
  113. }
  114. var x = Value.compare(e, this._rhs);
  115. switch(this._matchType) {
  116. case "LT":
  117. return x < 0;
  118. case "LTE":
  119. return x <= 0;
  120. case "EQ":
  121. return x === 0;
  122. case "GT":
  123. return x > 0;
  124. case "GTE":
  125. return x >= 0;
  126. default:
  127. throw new Error("Invalid comparison type evaluated.");
  128. }
  129. };