ConstantExpression.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. var ConstantExpression = module.exports = (function(){
  2. // CONSTRUCTOR
  3. /** Internal expression for constant values **/
  4. var klass = function ConstantExpression(value){
  5. if(arguments.length !== 1) throw new Error("args expected: value");
  6. this.value = value; //TODO: actually make read-only in terms of JS?
  7. base.call(this);
  8. }, base = require("./Expression"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  9. // PROTOTYPE MEMBERS
  10. proto.getOpName = function getOpName(){
  11. return "$const";
  12. };
  13. /**
  14. * Get the constant value represented by this Expression.
  15. *
  16. * @returns the value
  17. **/
  18. proto.getValue = function getValue(){ //TODO: convert this to an instance field rather than a property
  19. return this.value;
  20. };
  21. proto.addDependencies = function addDependencies(deps, path) {
  22. // nothing to do
  23. };
  24. /** Get the constant value represented by this Expression. **/
  25. proto.evaluate = function evaluate(doc){
  26. return this.value;
  27. };
  28. proto.optimize = function optimize() {
  29. return this; // nothing to do
  30. };
  31. proto.toJson = function(isExpressionRequired){
  32. return isExpressionRequired ? {$const: this.value} : this.value;
  33. };
  34. //TODO: proto.addToBsonObj --- may be required for $project to work -- my hope is that we can implement toJson methods all around and use that instead
  35. //TODO: proto.addToBsonArray
  36. return klass;
  37. })();