Преглед изворни кода

REFS DEVOPS-266 Added new class for v2.6 compatibility

David Aebersold пре 12 година
родитељ
комит
013b8b68d1

+ 43 - 0
lib/pipeline/expressions/SizeExpression.js

@@ -0,0 +1,43 @@
+"use strict";
+
+/** 
+ * A $size pipeline expression.
+ * @see evaluateInternal
+ * @class SizeExpression
+ * @namespace mungedb-aggregate.pipeline.expressions
+ * @module mungedb-aggregate
+ * @constructor
+ **/
+var SizeExpression = module.exports = function SizeExpression() {
+		this.fixedAirty(1);
+		if (arguments.length !== 1) throw new Error("one arg expected");
+		base.call(this);
+}, klass = SizeExpression,
+		base = require("./NaryExpression"),
+		proto = klass.prototype = Object.create(base.prototype, {
+				constructor: {
+						value: klass
+				}
+		});
+
+// DEPENDENCIES
+var Value = require("../Value"),
+		Expression = require("./Expression");
+
+// PROTOTYPE MEMBERS
+proto.getOpName = function getOpName() {
+		return "$size";
+};
+
+/** 
+ * Takes an array and return the size.
+ **/
+proto.evaluateInternal = function evaluateInternal(doc) {
+		this.checkArgCount(1);
+		var array = this.operands[0].evaluateInternal(doc);
+		if (array instanceof Date ) throw new Error("$size does not support dates; code 16376");
+		return array.length;
+};
+
+/** Register Expression */
+Expression.registerExpression("$size", SizeExpression.parse);

+ 46 - 0
test/lib/pipeline/expressions/SizeExpression.js

@@ -0,0 +1,46 @@
+"use strict";
+var assert = require("assert"),
+		SizeExpression = require("../../../../lib/pipeline/expressions/SizeExpression"),
+		Expression = require("../../../../lib/pipeline/expressions/Expression");
+
+
+module.exports = {
+
+		"SizeExpression": {
+
+				"constructor()": {
+
+						"should throw Error when constructing without args": function testConstructor() {
+								assert.throws(function() {
+										new SizeExpression();
+								});
+						}
+
+				},
+
+				"#getOpName()": {
+
+						"should return the correct op name; $size": function testOpName() {
+								assert.equal(new SizeExpression("test").getOpName(), "$size");
+						}
+
+				},
+
+				"#evaluateInternal()": {
+
+						// New test not working
+						"should return the size": function testSize() {
+								assert.strictEqual(Expression.parseOperand({
+										$size: ["$a"]
+								}).evaluateInternal({
+										a: [{a:1},{b:2}],
+										b: [{c:3}]
+								}), 4);
+						}
+				}
+
+		}
+
+};
+
+if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);