CondExpression.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. "use strict";
  2. /**
  3. * $cond expression; @see evaluate
  4. * @class CondExpression
  5. * @namespace mungedb-aggregate.pipeline.expressions
  6. * @module mungedb-aggregate
  7. * @constructor
  8. **/
  9. var CondExpression = module.exports = function CondExpression(vars) {
  10. if (arguments.length !== 0) throw new Error("zero args expected");
  11. base.call(this);
  12. }, klass = CondExpression,
  13. base = require("./FixedArityExpression")(klass, 3),
  14. proto = klass.prototype = Object.create(base.prototype, {
  15. constructor: {
  16. value: klass
  17. }
  18. });
  19. // DEPENDENCIES
  20. var Value = require("../Value"),
  21. Expression = require("./Expression");
  22. // PROTOTYPE MEMBERS
  23. klass.opName = "$cond";
  24. proto.getOpName = function getOpName() {
  25. return klass.opName;
  26. };
  27. /**
  28. *
  29. * @param expr - I expect this to be the RHS of $cond:{...} or $cond:[,,,]
  30. * @param vps
  31. * @returns {*}
  32. */
  33. klass.parse = function parse(expr, vps) {
  34. // There may only be one argument - an array of 3 items, or a hash containing 3 keys.
  35. //this.checkArgLimit(3);
  36. // if not an object, return;
  37. // todo I don't understand why we'd do this. shouldn't expr be {}, [], or wrong?
  38. if (typeof(expr) !== Object)
  39. return Expression.parse(expr, vps);
  40. // ...or expr could be the entirety of $cond:{...} or $cond:[,,,].
  41. if(!(klass.opName in expr)) {
  42. throw new Error("Invalid expression. Expected to see '"+klass.opName+"'");
  43. }
  44. var ret = new CondExpression();
  45. // If this is an Object and not an array, verify all the bits are specified.
  46. // If this is an Object that is an array, verify there are three bits.
  47. // (My issue here is that we got to this parse function when we parsed the $cond:{...} item, and we're calling
  48. // parseOperand (again) without altering the input.)
  49. // var args = Expression.parseOperand(expr, vps);
  50. var args = expr[getOpName()];
  51. if (typeof args !== 'object') throw new Error("this should not happen");
  52. if (args instanceof Array) {
  53. // it's the array form. Convert it to the object form.
  54. if (args.length !== 3) throw new Error("$cond requires exactly three arguments");
  55. args = {if: args[0], then: args[1], else: args[2]};
  56. }
  57. // One way or the other, args is now in object form.
  58. Object.keys(args).forEach(function(arg) {
  59. if (arg === 'if') {
  60. ret.operands[0] = Expression.parseOperand(args['if'], vps);
  61. }
  62. else if (arg === 'then') {
  63. ret.operands[1] = Expression.parseOperand(args['then'], vps);
  64. }
  65. else if (arg === 'else') {
  66. ret.operands[2] = Expression.parseOperand(args['else'], vps);
  67. }
  68. else {
  69. throw new Error("Unrecognized parameter to $cond: '" + arg + "'; code 17083");
  70. }
  71. });
  72. if (!ret.operands[0]) throw new Error("Missing 'if' parameter to $cond");
  73. if (!ret.operands[1]) throw new Error("Missing 'then' parameter to $cond");
  74. if (!ret.operands[2]) throw new Error("Missing 'else' parameter to $cond");
  75. return ret;
  76. };
  77. /**
  78. * Use the $cond operator with the following syntax:
  79. * { $cond: { if: <boolean-expression>, then: <true-case>, else: <false-case-> } }
  80. * -or-
  81. * { $cond: [ <boolean-expression>, <true-case>, <false-case> ] }
  82. * @method evaluate
  83. **/
  84. proto.evaluateInternal = function evaluateInternal(vars) {
  85. var pCond1 = this.operands[0].evaluateInternal(vars);
  86. this.idx = 0;
  87. if (pCond1.coerceToBool()) {
  88. this.idx = 1;
  89. } else {
  90. this.idx = 2;
  91. }
  92. return this.operands[this.idx].evaluateInternal(vars);
  93. };
  94. /** Register Expression */
  95. Expression.registerExpression(klass.opName, klass.parse);