Browse Source

Re-Fixes #988: Removed sha3 lib in favor of es6-shim lib. Added test cases

http://source.rd.rcg.local/trac/eagle6/changeset/1328/Eagle6_SVN
Jared Hall 12 years ago
parent
commit
6e4b9be681

+ 0 - 13
lib/pipeline/Value.js

@@ -1,6 +1,4 @@
 var Value = module.exports = Value = (function(){
-	//DEPENDENCIES
-	var SHA3 = require('sha3');
 
 	// CONSTRUCTOR
 	var klass = function Value(){
@@ -133,17 +131,6 @@ var Value = module.exports = Value = (function(){
 
 	};
 
-	//Takes a "value" object and returns a hashcode
-	klass.hashCombine = function hashCombine(val){
-		/*
-		SHA3.update(val)
-		return SHA3.digest('hex');
-		*/
-		var sha3 = new SHA3.SHA3Hash(224);
-		sha3.update(JSON.stringify(val));
-		return sha3.digest('hex');
-	};
-
 //TODO:	klass.hashCombine = ...?
 //TODO:	klass.getWidestNumeric = ...?
 //TODO:	klass.getApproximateSize = ...?

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

@@ -1,17 +1,19 @@
 var AddToSetAccumulator = module.exports = (function(){
+
+	// DEPENDENCIES
+	var Value = require("../Value");
+	require("es6-shim");
+
 	// CONSTRUCTOR
 	/** Create an expression that finds the sum of n operands. **/
 	var klass = module.exports = function AddToSetAccumulator(/* pCtx */){
 		if(arguments.length !== 0) throw new Error("zero args expected");
-		this.set = {};
+		this.set = new Map();
 		//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);
 	}, Accumulator = require("./Accumulator"), base = Accumulator, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
 
-	// DEPENDENCIES
-	var Value = require("../Value");
-
 	// PROTOTYPE MEMBERS
 	proto.getOpName = function getOpName(){
 		return "$addToSet";
@@ -27,19 +29,14 @@ var AddToSetAccumulator = module.exports = (function(){
 		if ('undefined' != typeof rhs) {
 			Value.verifyArray(rhs);
 			for(var i=0; i<rhs.length; i++) {
-				var key = Value.hashCombine(rhs[i]);
-				this.set[key] = rhs[i];
+				this.set.set(rhs[i], rhs[i]); //Sorry about variable names here... just following the rules!
 			}
 		}
 		return undefined;
 	};
 
 	proto.getValue = function getValue() {
-		var arr = [], n = 0;
-		for(var key in this.set){
-			if(this.set.hasOwnProperty(key)) arr[n++] = this.set[key];
-		}
-		return arr;
+		return this.set.values();
 	};
 
 	return klass;

+ 1 - 1
package.json

@@ -24,7 +24,7 @@
 	],
 	"dependencies": {
 		"stream-utils":"*",
-		"sha3":"*"
+		"es6-shim":"*"
 	},
 	"devDependencies": {
 		"mocha": "*",

+ 37 - 8
test/lib/pipeline/accumulators/AddToSetAccumulator.js

@@ -16,6 +16,43 @@ module.exports = {
 
 		"constructor()": {
 
+			"should error if called with args": function testArgsGivenToCtor() {
+				assert.throws(function() {
+					var acc = new AddToSetAccumulator('arg');
+				});
+			},
+
+			"should construct object with set property": function testCtorAssignsSet() {
+				var acc = new AddToSetAccumulator();
+				assert.notEqual(acc.set, null);
+				assert.notEqual(acc.set, undefined);
+			}
+
+		},
+
+		"#evaluate()" : {
+
+			"should error if evaluate is called with no args": function testNoArgs() {
+				assert.throws(function() {
+					var acc = new createAccumulator();
+					acc.evaluate();
+				});
+			},
+
+			"should error if evaluate is called with more than one arg": function testTooManyArgs() {
+				assert.throws(function() {
+					var acc = new createAccumulator();
+					acc.evaluate({}, {});
+				});
+			},
+
+			"should throw an error when given a non-array to evaluate": function testArrayValidity() {
+				assert.throws(function() {
+					var acc = createAccumulator();
+					acc.evaluate({b:5});
+				});
+			}
+
 		},
 
 		"#getValue()": {
@@ -46,14 +83,6 @@ module.exports = {
 				//assert.equal(value[0], 5);
 			},
 
-			"should throw an error when given a non-array to evaluate": function testArrayValidity() {
-				assert.throws(function() {
-					var acc = createAccumulator();
-					acc.evaluate({b:5});
-					var value = acc.getValue();
-				});
-			},
-
 			"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"}]});