YearExpression.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. var YearExpression = module.exports = (function(){
  2. // CONSTRUCTOR
  3. /**
  4. * A $year pipeline expression.
  5. *
  6. * @see evaluate
  7. * @class YearExpression
  8. * @namespace mungedb.aggregate.pipeline.expressions
  9. * @module mungedb-aggregate
  10. * @constructor
  11. **/
  12. var klass = function YearExpression(){
  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. DayOfYearExpression = require("./DayOfYearExpression");
  19. // PROTOTYPE MEMBERS
  20. proto.getOpName = function getOpName(){
  21. return "$year";
  22. };
  23. proto.addOperand = function addOperand(expr) {
  24. this.checkArgLimit(1);
  25. base.prototype.addOperand.call(this, expr);
  26. };
  27. /**
  28. * Takes a date and returns the full year.
  29. * @method evaluate
  30. **/
  31. proto.evaluate = function evaluate(doc) {
  32. this.checkArgCount(1);
  33. var date = this.operands[0].evaluate(doc);
  34. return date.getUTCFullYear();
  35. };
  36. return klass;
  37. })();