Przeglądaj źródła

REFS DEVOPS-252 Created new class for millisecond, ensured fixedAirty.

David Aebersold 12 lat temu
rodzic
commit
bc176ca135

+ 44 - 0
lib/pipeline/expressions/MillisecondExpression.js

@@ -0,0 +1,44 @@
+"use strict";
+
+/** 
+ * An $millisecond pipeline expression.
+ * @see evaluateInternal
+ * @class MillisecondExpression
+ * @namespace mungedb-aggregate.pipeline.expressions
+ * @module mungedb-aggregate
+ * @constructor
+ **/
+var MillisecondExpression = module.exports = function MillisecondExpression() {
+		this.fixedArity(1);
+		if (arguments.length !== 0) throw new Error("zero args expected");
+		base.call(this);
+}, klass = MillisecondExpression,
+		base = require("./NaryExpression"),
+		proto = klass.prototype = Object.create(base.prototype, {
+				constructor: {
+						value: klass
+				}
+		});
+
+// DEPENDENCIES
+var Expression = require("./Expression");
+
+// PROTOTYPE MEMBERS
+proto.getOpName = function getOpName() {
+		return "$millisecond";
+};
+
+/**
+ * Takes a date and returns the millisecond between 0 and 999, but can be 1000 to account for leap milliseconds.
+ * @method evaluateInternal
+ **/
+proto.evaluateInternal = function evaluateInternal(doc) {
+		this.checkArgCount(1);
+		var date = this.operands[0].evaluateInternal(doc);
+		return date.getUTCMilliseconds(); //TODO: incorrect for last millisecond of leap year, need to fix...
+		// currently leap milliseconds are unsupported in v8
+		// http://code.google.com/p/v8/issues/detail?id=1944
+};
+
+/** Register Expression */
+Expression.registerExpression("$millisecond", MillisecondExpression.parse);

+ 59 - 0
test/lib/pipeline/expressions/MillisecondExpression.js

@@ -0,0 +1,59 @@
+"use strict";
+var assert = require("assert"),
+		MillisecondExpression = require("../../../../lib/pipeline/expressions/MillisecondExpression"),
+		Expression = require("../../../../lib/pipeline/expressions/Expression");
+
+
+module.exports = {
+
+		"MillisecondExpression": {
+
+				"constructor()": {
+
+						"should not throw Error when constructing without args": function testConstructor() {
+								assert.doesNotThrow(function() {
+										new MillisecondExpression();
+								});
+						}
+
+				},
+
+				"#getOpName()": {
+
+						"should return the correct op name; $millisecond": function testOpName() {
+								assert.equal(new MillisecondExpression().getOpName(), "$millisecond");
+						}
+
+				},
+
+				"#getFactory()": {
+
+						"should return the constructor for this class": function factoryIsConstructor() {
+								assert.strictEqual(new MillisecondExpression().getFactory(), undefined);
+						}
+
+				},
+
+				"#evaluate()": {
+
+						"should return the current millisecond in the date; 19 for 2013-02-18 11:24:19 EST": function testStuff() {
+								assert.strictEqual(Expression.parseOperand({
+										$millisecond: "$someDate"
+								}).evaluate({
+										someDate: new Date("2013-02-18T11:24:19.456Z")
+								}), 456);
+						}
+
+						/*
+			"should return the leap millisecond in the date; 60 for June 30, 2012 at 23:59:60 UTC": function testStuff(){
+				assert.strictEqual(Expression.parseOperand({$millisecond:"$someDate"}).evaluate({someDate:new Date("June 30, 2012 at 23:59:60 UTC")}), 60);
+			}
+
+				*/
+				}
+
+		}
+
+};
+
+if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);