浏览代码

Refs #2335, #2262: rm `es6-shim`; fix `$addToSet`

Kyle P Davis 12 年之前
父节点
当前提交
ee36685d48
共有 3 个文件被更改,包括 25 次插入17 次删除
  1. 8 10
      lib/pipeline/accumulators/AddToSetAccumulator.js
  2. 0 1
      package.json
  3. 17 6
      test/lib/pipeline/accumulators/AddToSetAccumulator.js

+ 8 - 10
lib/pipeline/accumulators/AddToSetAccumulator.js

@@ -1,9 +1,5 @@
 "use strict";
 var AddToSetAccumulator = module.exports = (function(){
-
-	// DEPENDENCIES
-	require("es6-shim");
-
 	// CONSTRUCTOR
 	/** 
 	 * Create an expression that finds the sum of n operands.
@@ -14,7 +10,7 @@ var AddToSetAccumulator = module.exports = (function(){
 	**/
 	var klass = module.exports = function AddToSetAccumulator(/* pCtx */){
 		if(arguments.length !== 0) throw new Error("zero args expected");
-		this.set = new Map();
+		this.set = {};
 		//this.itr = undefined; /* Shoudln't need an iterator for the set */
 		//this.pCtx = undefined; /* Not using the context object currently as it is related to sharding */
 		base.call(this);
@@ -32,14 +28,16 @@ var AddToSetAccumulator = module.exports = (function(){
 	proto.evaluate = function evaluate(doc) {
 		if(arguments.length !== 1) throw new Error("One and only one arg expected");
 		var rhs = this.operands[0].evaluate(doc);
-		if ('undefined' != typeof rhs) {
-			this.set.set(rhs, rhs); //Sorry about variable names here... just following the rules!
-		}
-		return undefined;
+		if (rhs === undefined) return;
+		this.set[JSON.stringify(rhs)] = rhs;
 	};
 
 	proto.getValue = function getValue() {
-		return this.set.values();
+		var setValues = [];
+		for (var setKey in this.set) {
+			setValues.push(this.set[setKey]);
+		}
+		return setValues;
 	};
 
 	return klass;

+ 0 - 1
package.json

@@ -22,7 +22,6 @@
     "alteration"
   ],
   "dependencies": {
-    "es6-shim": "*",
     "sift": "*",
     "async":"*"
   },

+ 17 - 6
test/lib/pipeline/accumulators/AddToSetAccumulator.js

@@ -7,7 +7,7 @@ var assert = require("assert"),
 
 var createAccumulator = function createAccumulator() {
 	var myAccumulator = new AddToSetAccumulator();
-	myAccumulator.addOperand(new FieldPathExpression("b") );
+	myAccumulator.addOperand(new FieldPathExpression("b"));
 	return myAccumulator;
 };
 
@@ -21,7 +21,7 @@ module.exports = {
 
 			"should error if called with args": function testArgsGivenToCtor() {
 				assert.throws(function() {
-					var acc = new AddToSetAccumulator('arg');
+					new AddToSetAccumulator('arg');
 				});
 			},
 
@@ -65,7 +65,7 @@ module.exports = {
 				acc.evaluate({b:5});
 				acc.evaluate({b:5});
 				var value = acc.getValue();
-				assert.deepEqual(value, [5]);
+				assert.deepEqual(JSON.stringify(value), JSON.stringify([5]));
 			},
 
 			"should produce value that is an array of multiple elements": function testMultipleItems() {
@@ -73,14 +73,14 @@ module.exports = {
 				acc.evaluate({b:5});
 				acc.evaluate({b:{key: "value"}});
 				var value = acc.getValue();
-				assert.deepEqual(value, [5, {key: "value"}]);
+				assert.deepEqual(JSON.stringify(value), JSON.stringify([5, {key: "value"}]));
 			},
 
 			"should return array with one element that is an object containing a key/value pair": function testKeyValue() {
 				var acc = createAccumulator();
 				acc.evaluate({b:{key: "value"}});
 				var value = acc.getValue();
-				assert.deepEqual(value, [{key: "value"}]);
+				assert.deepEqual(JSON.stringify(value), JSON.stringify([{key: "value"}]));
 			},
 
 			"should ignore undefined values": function testKeyValue() {
@@ -88,11 +88,22 @@ module.exports = {
 				acc.evaluate({b:{key: "value"}});
 				acc.evaluate({a:5});
 				var value = acc.getValue();
-				assert.deepEqual(value, [{key: "value"}]);
+				assert.deepEqual(JSON.stringify(value), JSON.stringify([{key: "value"}]));
+			},
+
+			"should coalesce different instances of equivalent objects": function testGetValue_() {
+				var acc = createAccumulator();
+				acc.evaluate({b:{key: "value"}});
+				acc.evaluate({b:{key: "value"}});
+				var value = acc.getValue();
+				assert.deepEqual(JSON.stringify(value), JSON.stringify([{key: "value"}]));
 			}
 
 		}
+
 	}
+
 };
 
+
 if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);