CompareExpression.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. "use strict";
  2. /**
  3. * Generic comparison expression that gets used for $eq, $ne, $lt, $lte, $gt, $gte, and $cmp.
  4. * @class CompareExpression
  5. * @namespace mungedb-aggregate.pipeline.expressions
  6. * @module mungedb-aggregate
  7. * @constructor
  8. */
  9. var CompareExpression = module.exports = function CompareExpression(cmpOp) {
  10. if (!(arguments.length === 1 && typeof cmpOp === "string")) throw new Error(klass.name + ": args expected: cmpOp");
  11. this.cmpOp = cmpOp;
  12. base.call(this);
  13. }, klass = CompareExpression, base = require("./FixedArityExpressionT")(CompareExpression, 2), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  14. var Value = require("../Value"),
  15. Expression = require("./Expression");
  16. klass.parse = function parse(jsonExpr, vps, op) {
  17. var expr = new CompareExpression(op),
  18. args = base.parseArguments(jsonExpr, vps);
  19. expr.validateArguments(args);
  20. expr.operands = args;
  21. return expr;
  22. };
  23. /**
  24. * Lookup table for truth value returns
  25. * @param truthValues truth value for -1, 0, 1
  26. * @param reverse reverse comparison operator
  27. * @param name string name
  28. */
  29. var CmpLookup = function CmpLookup(truthValues, reverse, name) { // emulating a struct
  30. if (arguments.length !== 3) throw new Error("args expected: truthValues, reverse, name");
  31. this.truthValues = truthValues;
  32. this.reverse = reverse;
  33. this.name = name;
  34. };
  35. /**
  36. * Enumeration of comparison operators. Any changes to these values require adjustment of
  37. * the lookup table in the implementation.
  38. */
  39. var CmpOp = klass.CmpOp = {
  40. EQ: "$eq",
  41. NE: "$ne",
  42. GT: "$gt",
  43. GTE: "$gte",
  44. LT: "$lt",
  45. LTE: "$lte",
  46. CMP: "$cmp",
  47. };
  48. /**
  49. * a table of cmp type lookups to truth values
  50. * @private
  51. */
  52. var cmpLookupMap = [ //NOTE: converted from this Array to a Dict/Object below using CmpLookup#name as the key
  53. // -1 0 1 reverse name (taking advantage of the fact that our 'enums' are strings below)
  54. new CmpLookup([false, true, false], CmpOp.EQ, CmpOp.EQ),
  55. new CmpLookup([true, false, true], CmpOp.NE, CmpOp.NE),
  56. new CmpLookup([false, false, true], CmpOp.LT, CmpOp.GT),
  57. new CmpLookup([false, true, true], CmpOp.LTE, CmpOp.GTE),
  58. new CmpLookup([true, false, false], CmpOp.GT, CmpOp.LT),
  59. new CmpLookup([true, true, false], CmpOp.GTE, CmpOp.LTE),
  60. // CMP is special. Only name is used.
  61. new CmpLookup([false, false, false], CmpOp.CMP, CmpOp.CMP)
  62. ].reduce(function(r, o) {
  63. r[o.name] = o;
  64. return r;
  65. }, {});
  66. proto.evaluateInternal = function evaluateInternal(vars) {
  67. var left = this.operands[0].evaluateInternal(vars),
  68. right = this.operands[1].evaluateInternal(vars),
  69. cmp = Value.compare(left, right);
  70. // Make cmp one of 1, 0, or -1.
  71. if (cmp === 0) {
  72. //leave as 0
  73. } else if (cmp < 0) {
  74. cmp = -1;
  75. } else if (cmp > 0) {
  76. cmp = 1;
  77. }
  78. if (this.cmpOp === CmpOp.CMP)
  79. return cmp;
  80. var returnValue = cmpLookupMap[this.cmpOp].truthValues[cmp + 1];
  81. return returnValue;
  82. };
  83. proto.getOpName = function getOpName() {
  84. return this.cmpOp;
  85. };
  86. function bindLast(fn, lastArg) { // similar to the boost::bind used in the mongo code
  87. return function() {
  88. return fn.apply(this, Array.prototype.slice.call(arguments).concat([lastArg]));
  89. };
  90. }
  91. Expression.registerExpression("$cmp", bindLast(klass.parse, CmpOp.CMP));
  92. Expression.registerExpression("$eq", bindLast(klass.parse, CmpOp.EQ));
  93. Expression.registerExpression("$gt", bindLast(klass.parse, CmpOp.GT));
  94. Expression.registerExpression("$gte", bindLast(klass.parse, CmpOp.GTE));
  95. Expression.registerExpression("$lt", bindLast(klass.parse, CmpOp.LT));
  96. Expression.registerExpression("$lte", bindLast(klass.parse, CmpOp.LTE));
  97. Expression.registerExpression("$ne", bindLast(klass.parse, CmpOp.NE));