ConcatExpression.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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("./NaryExpression"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  13. // DEPENDENCIES
  14. var Value = require("../Value");
  15. // PROTOTYPE MEMBERS
  16. proto.getOpName = function getOpName(){
  17. return "$concat";
  18. };
  19. proto.getFactory = function getFactory(){
  20. return klass; // using the ctor rather than a separate .create() method
  21. };
  22. /**
  23. * Concats a string of values together.
  24. * @method evaluate
  25. **/
  26. proto.evaluate = function evaluate(doc) {
  27. var result = "";
  28. for (var i = 0, n = this.operands.length; i < n; ++i) {
  29. var val = this.operands[i].evaluate(doc);
  30. if (val === null) return null; // if any operand is null, return null for all
  31. if (typeof(val) != "string") throw new Error("$concat only supports strings, not " + typeof(val) + "; code 16702");
  32. result = result + Value.coerceToString(val);
  33. }
  34. return result;
  35. };