DayOfWeekExpression.js 975 B

123456789101112131415161718192021222324252627282930313233
  1. "use strict";
  2. /**
  3. * Get the DayOfWeek from a date.
  4. * @class DayOfWeekExpression
  5. * @namespace mungedb-aggregate.pipeline.expressions
  6. * @module mungedb-aggregate
  7. * @constructor
  8. **/
  9. var DayOfWeekExpression = module.exports = function DayOfWeekExpression(){
  10. if (arguments.length !== 0) throw new Error("zero args expected");
  11. base.call(this);
  12. }, klass = DayOfWeekExpression, base = require("./NaryExpression"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  13. // PROTOTYPE MEMBERS
  14. proto.getOpName = function getOpName(){
  15. return "$dayOfWeek";
  16. };
  17. proto.addOperand = function addOperand(expr) {
  18. this.checkArgLimit(1);
  19. base.prototype.addOperand.call(this, expr);
  20. };
  21. /**
  22. * Takes a date and returns the day of the week as a number between 1 (Sunday) and 7 (Saturday.)
  23. * @method evaluate
  24. **/
  25. proto.evaluate = function evaluate(doc){
  26. this.checkArgCount(1);
  27. var date = this.operands[0].evaluate(doc);
  28. return date.getUTCDay()+1;
  29. };