Browse Source

EAGLESIX-2892 changed loop type, clarified var names, added dassert

Austin Meagher 11 years ago
parent
commit
c2117c8d3c
2 changed files with 25 additions and 21 deletions
  1. 7 7
      lib/pipeline/DepsTracker.js
  2. 18 14
      lib/pipeline/ParsedDeps.js

+ 7 - 7
lib/pipeline/DepsTracker.js

@@ -44,11 +44,11 @@ proto.toProjection = function toProjection() {
 	var last = "",
 		needId = false;
 
-	Object.keys(this.fields).sort().forEach(function (it) {
+	for(var it in this.fields.sort()) {
 		if (it.slice(0,3) == "_id" && (it.length == 3 || it.charAt(3) == ".")) {
 			// _id and subfields are handled specially due in part to SERVER-7502
 			needId = true;
-			return;
+			continue;
 		}
 
 		if (last !== "" && it.slice(0, last.length) === last) {
@@ -57,12 +57,12 @@ proto.toProjection = function toProjection() {
 			// field, the parent wouldn't be fully included. This logic relies
 			// on on set iterators going in lexicographic order so that a string
 			// is always directly before of all fields it prefixes.
-			return;
+			continue;
 		}
 
 		last = it + ".";
 		proj[it] = 1;
-	});
+	}
 
 	if (needId)
 		proj._id = 1;
@@ -88,20 +88,20 @@ proto.toParsedDeps = function toParsedDeps() {
 	}
 
 	var last = "";
-	Object.keys(this.fields).sort().forEach(function (it) {
+	for(var it in this.fields.sort()) {
 		if (last !== "" && it.slice(0, last.length) === last) {
 			// we are including a parent of *it so we don't need to include this
 			// field explicitly. In fact, due to SERVER-6527 if we included this
 			// field, the parent wouldn't be fully included. This logic relies
 			// on on set iterators going in lexicographic order so that a string
 			// is always directly before of all fields it prefixes.
-			return;
+			continue;
 		}
 
 		last = it + ".";
 		// TODO: set nested field to true; i.e. a.b.c = true, not a = true
 		doc[it] = true;
-	});
+	}
 
 	return new ParsedDeps(doc);
 };

+ 18 - 14
lib/pipeline/ParsedDeps.js

@@ -14,6 +14,8 @@ var ParsedDeps = module.exports = function ParsedDeps(fields) {
 	this._fields = fields;
 }, klass = ParsedDeps, base = Object, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
 
+var Value = require("./Value");
+
 /**
  * Extracts fields from the input into a new Document, based on the caller.
  *
@@ -36,12 +38,12 @@ proto.extractFields = function extractFields(input) {
 proto._arrayHelper = function _arrayHelper(array, neededFields) {
 	var values = [];
 
-	array.sort().forEach(function (it) {
+	for (var it in array) {
 		if (it instanceof Array)
 			values.push(_arrayHelper(it, neededFields));
 		else if (it instanceof Object)
 			values.push(proto._documentHelper(it, neededFields));
-	});
+	}
 
 	return values;
 };
@@ -57,25 +59,27 @@ proto._arrayHelper = function _arrayHelper(array, neededFields) {
 proto._documentHelper = function _documentHelper(json, neededFields) {
 	var doc = {};
 
-	Object.keys(json).sort().forEach(function (it) {
-		var jsonElement = json[it],
-			isNeeded = neededFields[it];
+	for (var fieldName in json) {
+		var jsonElement = json[fieldName],
+			isNeeded = neededFields[fieldName];
 
-		if (!isNeeded)
-			return;
+		if (isNeeded === undefined)
+			continue;
 
-		if (typeof(isNeeded) === 'boolean') {
-			doc[it] = jsonElement;
-			return;
+		if (Value.getType(isNeeded) === 'boolean') {
+			doc[fieldName] = jsonElement;
+			continue;
 		}
 
-		if (typeof(isNeeded) === 'object') {
+		if (!isNeeded instanceof Object) throw new Error("dassert failure");
+
+		if (Value.getType(isNeeded) === 'object') {
 			if (jsonElement instanceof Array)
-				doc[it] = proto._arrayHelper(jsonElement, isNeeded);
+				doc[fieldName] = proto._arrayHelper(jsonElement, isNeeded);
 			if (jsonElement instanceof Object)
-				doc[it] = proto._documentHelper(jsonElement, isNeeded);
+				doc[fieldName] = proto._documentHelper(jsonElement, isNeeded);
 		}
-	});
+	}
 
 	return doc;
 };