ConcatExpression.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. "use strict";
  2. var ConcatExpression = module.exports = (function(){
  3. // CONSTRUCTOR
  4. /**
  5. * Creates an expression that concatenates a set of string operands.
  6. *
  7. * @class ConcatExpression
  8. * @namespace mungedb.aggregate.pipeline.expressions
  9. * @module mungedb-aggregate
  10. * @constructor
  11. **/
  12. var klass = module.exports = function ConcatExpression(){
  13. if(arguments.length !== 0) throw new Error("zero args expected");
  14. base.call(this);
  15. }, base = require("./NaryExpression"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  16. // DEPENDENCIES
  17. var Value = require("../Value");
  18. // PROTOTYPE MEMBERS
  19. proto.getOpName = function getOpName(){
  20. return "$concat";
  21. };
  22. proto.getFactory = function getFactory(){
  23. return klass; // using the ctor rather than a separate .create() method
  24. };
  25. /**
  26. * Concats a string of values together.
  27. * @method evaluate
  28. **/
  29. proto.evaluate = function evaluate(doc) {
  30. var result = "";
  31. for (var i = 0, n = this.operands.length; i < n; ++i) {
  32. var val = this.operands[i].evaluate(doc);
  33. if (val === null)
  34. return null; // if any operand is null, return null for all
  35. if (typeof(val) != "string") throw new Error("$concat only supports strings, not " + typeof(val) + "; code 16702");
  36. result = result + Value.coerceToString(val);
  37. }
  38. return result;
  39. };
  40. return klass;
  41. })();