DayOfWeekExpression.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. "use strict";
  2. var DayOfWeekExpression = module.exports = (function(){
  3. // CONSTRUCTOR
  4. /**
  5. * Get the DayOfWeek from a date.
  6. *
  7. * @class DayOfWeekExpression
  8. * @namespace mungedb.aggregate.pipeline.expressions
  9. * @module mungedb-aggregate
  10. * @constructor
  11. **/
  12. var klass = function DayOfWeekExpression(){
  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. // PROTOTYPE MEMBERS
  17. proto.getOpName = function getOpName(){
  18. return "$dayOfWeek";
  19. };
  20. proto.addOperand = function addOperand(expr) {
  21. this.checkArgLimit(1);
  22. base.prototype.addOperand.call(this, expr);
  23. };
  24. /**
  25. * Takes a date and returns the day of the week as a number between 1 (Sunday) and 7 (Saturday.)
  26. * @method evaluate
  27. **/
  28. proto.evaluate = function evaluate(doc){
  29. this.checkArgCount(1);
  30. var date = this.operands[0].evaluate(doc);
  31. return date.getDay() + 1;
  32. };
  33. return klass;
  34. })();