ConcatExpression.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. "use strict";
  2. /**
  3. * Creates an expression that concatenates a set of string operands.
  4. * @class ConcatExpression
  5. * @namespace mungedb-aggregate.pipeline.expressions
  6. * @module mungedb-aggregate
  7. * @constructor
  8. */
  9. var ConcatExpression = module.exports = function ConcatExpression(){
  10. if (arguments.length !== 0) throw new Error("zero args expected");
  11. base.call(this);
  12. }, klass = ConcatExpression, base = require("./VariadicExpressionT")(ConcatExpression), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  13. var Value = require("../Value"),
  14. Expression = require("./Expression");
  15. proto.evaluateInternal = function evaluateInternal(vars) {
  16. var n = this.operands.length;
  17. var result = "";
  18. for (var i = 0; i < n; ++i) {
  19. var val = this.operands[i].evaluateInternal(vars);
  20. if (val === undefined || val === null)
  21. return null;
  22. if (typeof val !== "string")
  23. throw new Error(this.getOpName() + " only supports strings, not " +
  24. Value.getType(val) + "; uassert code 16702");
  25. result += val;
  26. }
  27. return result;
  28. };
  29. Expression.registerExpression("$concat", base.parse);
  30. proto.getOpName = function getOpName(){
  31. return "$concat";
  32. };