|
|
@@ -9,24 +9,32 @@
|
|
|
* @constructor
|
|
|
**/
|
|
|
var WeekExpression = module.exports = function WeekExpression() {
|
|
|
- this.nargs = 1;
|
|
|
- base.call(this);
|
|
|
-}, klass = WeekExpression,
|
|
|
- base = require("./NaryExpression"),
|
|
|
- proto = klass.prototype = Object.create(base.prototype, {
|
|
|
- constructor: {
|
|
|
- value: klass
|
|
|
- }
|
|
|
- });
|
|
|
+ //this.nargs = 1;
|
|
|
+ //base.call(this);
|
|
|
+}, klass = WeekExpression, base = require("./FixedArityExpressionT")(klass, 1), proto = klass.prototype = Object.create(base.prototype, {constructor: {value: klass}});
|
|
|
|
|
|
// DEPENDENCIES
|
|
|
var Value = require("../Value"),
|
|
|
DayOfYearExpression = require("./DayOfYearExpression"),
|
|
|
Expression = require("./Expression");
|
|
|
|
|
|
+// STATIC MEMBERS
|
|
|
+klass.extract = function extract(date) {
|
|
|
+ //note: copied from evaluateInternal
|
|
|
+ dayOfWeek = date.getUTCDay(),
|
|
|
+ dayOfYear = DayOfYearExpression.getDateDayOfYear(date),
|
|
|
+ prevSundayDayOfYear = dayOfYear - dayOfWeek, // may be negative
|
|
|
+ nextSundayDayOfYear = prevSundayDayOfYear + 7; // must be positive
|
|
|
+ // Return the zero based index of the week of the next sunday, equal to the one based index of the week of the previous sunday, which is to be returned.
|
|
|
+
|
|
|
+ return (nextSundayDayOfYear / 7) | 0; // also, the `| 0` here truncates this so that we return an integer
|
|
|
+};
|
|
|
+
|
|
|
+klass.opName = "$week";
|
|
|
+
|
|
|
// PROTOTYPE MEMBERS
|
|
|
proto.getOpName = function getOpName() {
|
|
|
- return "$week";
|
|
|
+ return klass.opName;
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
@@ -37,14 +45,11 @@ proto.getOpName = function getOpName() {
|
|
|
* @method evaluateInternal
|
|
|
**/
|
|
|
proto.evaluateInternal = function evaluateInternal(vars) {
|
|
|
- var date = this.operands[0].evaluateInternal(vars),
|
|
|
- dayOfWeek = date.getUTCDay(),
|
|
|
- dayOfYear = DayOfYearExpression.getDateDayOfYear(date),
|
|
|
- prevSundayDayOfYear = dayOfYear - dayOfWeek, // may be negative
|
|
|
- nextSundayDayOfYear = prevSundayDayOfYear + 7; // must be positive
|
|
|
- // Return the zero based index of the week of the next sunday, equal to the one based index of the week of the previous sunday, which is to be returned.
|
|
|
- return (nextSundayDayOfYear / 7) | 0; // also, the `| 0` here truncates this so that we return an integer
|
|
|
+ var date = this.operands[0].evaluateInternal(vars);
|
|
|
+
|
|
|
+ //NOTE: DEVIATION FROM MONGO: need to return a Value object. Our Value class only consists of static helpers at the moment. We need a value instance to be consistent.
|
|
|
+ return klass.extract(date);
|
|
|
};
|
|
|
|
|
|
/** Register Expression */
|
|
|
-Expression.registerExpression("$week", base.parse(WeekExpression));
|
|
|
+Expression.registerExpression(klass.opName, base.parse(klass));
|