Browse Source

Merge branch 'develop'

* develop:
  EAGLESIX-2127: fix bug in aggregate when ctx is omitted and inputs is a doc src
  Refs #EAGLESIX-1628: update package.json to new version 0.0.3+2014-Q3-3.
  EAGLESIX-1628 update package.json to use release branch for version v0.0.2+2014-Q3-2.
  EAGLESIX-1581: Add context to the aggregate function.
  Refs #EAGLESIX-1365: update package.json dependencies to #develop.
  EAGLESIX-1365 update package.json to use release branch for version v0.0.1+2014-Q3-1.
  Refs #EAGLESIX-1365: update package.json to new version 0.0.1+2014-Q3-1.
  Refs #EAGLESIX-1365: update package.json to new version 0.0.2+2014-Q3-2.
  EAGLESIX-1365 update package.json to use release branch for version v0.0.1+2014-Q3-1.
  EAGLESIX-482 pinned jscoverage
  LP-250 sync async dep version with other packages while cleaning up npm deps
  Refs #EAGLESIX-452: update package.json to new version 0.7.1+2014.04.17.
  EAGLESIX-452 update package.json to use release branch for version v0.7.0+2014.03.14.
  Refs #DEVOPS-362: update package.json to new version 0.7.0+2014.03.14.

Conflicts:
	package.json
Charles Ezell 11 years ago
parent
commit
d70ef2bdb9
3 changed files with 49 additions and 22 deletions
  1. 12 9
      lib/index.js
  2. 4 4
      package.json
  3. 33 9
      test/lib/aggregate.js

+ 12 - 9
lib/index.js

@@ -11,18 +11,21 @@
  * @method aggregate
  * @namespace mungedb
  * @module mungedb-aggregate
- * @param pipeline  {Array}  The list of pipeline document sources in JSON format
- * @param [inputs]  {Array}  Optional inputs to pass through the `docSrcs` pipeline
- * @param [callback]               {Function}                                 Optional callback if using async extensions, called when done
+ * @param pipeline  {Array}   The list of pipeline document sources in JSON format
+ * @param [ctx]     {Object}  Optional context object to pass through to pipeline
+ * @param [inputs]  {Array}   Optional inputs to pass through the `docSrcs` pipeline
+ * @param [callback]             {Function}                                 Optional callback if using async extensions, called when done
  * @param   callback.err           {Error}                                    The Error if one occurred
  * @param   callback.docs          {Array}                                    The resulting documents
  **/
-exports = module.exports = function aggregate(pipeline, inputs, callback) {	// function-style interface; i.e., return the utility function directly as the require
-	var ctx = {}, //not used yet
-		pipelineInst = exports.pipeline.Pipeline.parseCommand({
+exports = module.exports = function aggregate(pipeline, ctx, inputs, callback) {	// function-style interface; i.e., return the utility function directly as the require
+	var DocumentSource = exports.pipeline.documentSources.DocumentSource;
+	if (ctx instanceof Array || ctx instanceof DocumentSource) callback = inputs, inputs = ctx, ctx = {};
+	var pipelineInst = exports.pipeline.Pipeline.parseCommand({
 			pipeline: pipeline
 		}, ctx),
-		aggregator = function aggregator(inputs, callback) {
+		aggregator = function aggregator(ctx, inputs, callback) {
+			if (ctx instanceof Array || ctx instanceof DocumentSource) callback = inputs, inputs = ctx, ctx = {};
 			if (!callback) callback = exports.SYNC_CALLBACK;
 			if (!inputs) return callback("arg `inputs` is required");
 
@@ -35,7 +38,7 @@ exports = module.exports = function aggregate(pipeline, inputs, callback) {	// f
 
 			// use or build input src
 			var src;
-			if(inputs instanceof exports.pipeline.documentSources.DocumentSource){
+			if(inputs instanceof DocumentSource){
 				src = inputs;
 			}else{
 				try{
@@ -54,7 +57,7 @@ exports = module.exports = function aggregate(pipeline, inputs, callback) {	// f
 			pipelineInst = null; // unset so that subsequent calls can rebuild the pipeline
 			return results;
 		};
-	if(inputs) return aggregator(inputs, callback);
+	if(inputs) return aggregator(ctx, inputs, callback);
 	return aggregator;
 };
 

+ 4 - 4
package.json

@@ -1,6 +1,6 @@
 {
   "name": "mungedb-aggregate",
-  "version": "0.6.9+2014.03.07",
+  "version": "0.0.3+2014-Q3-3",
   "description": "A JavaScript data aggregation pipeline based on the MongoDB aggregation framework.",
   "author": "Rivera Group <support@riverainc.com>",
   "contributors": [
@@ -15,7 +15,7 @@
     "test": "npm_scripts/test/test.sh"
   },
   "repository": {
-    "url": "git+https://stash.rd.rcg.local/scm/eagle6/mungedb-aggregate.git#master"
+    "url": "git+https://stash.rd.rcg.local/scm/eagle6/mungedb-aggregate.git#develop"
   },
   "keywords": [
     "manipulation",
@@ -23,12 +23,12 @@
   ],
   "dependencies": {
     "sift": "*",
-	"async":"git+https://github.com/louischatriot/async"
+	"async": "*"
   },
   "devDependencies": {
     "mocha": "*",
     "jshint": "*",
-    "jscoverage": "*",
+    "jscoverage": "0.3.8",
     "jscheckstyle": "*"
   },
   "license": "AGPL",

+ 33 - 9
test/lib/aggregate.js

@@ -10,8 +10,17 @@ function testAggregate(opts){
 	var results = aggregate(opts.pipeline, opts.inputs);
 	assert.equal(JSON.stringify(results), JSON.stringify(opts.expected));
 
+	// SYNC: test one-off usage with context
+	results = aggregate(opts.pipeline, {hi: "there"}, opts.inputs);
+	assert.equal(JSON.stringify(results), JSON.stringify(opts.expected));
+
+	// SYNC: test use with context
+	var aggregator = aggregate(opts.pipeline, {hi: "there"});
+	results = aggregator(opts.inputs);
+	assert.equal(JSON.stringify(results), JSON.stringify(opts.expected));
+
 	// SYNC: test reusable aggregator functionality
-	var aggregator = aggregate(opts.pipeline);
+	aggregator = aggregate(opts.pipeline);
 	results = aggregator(opts.inputs);
 	assert.equal(JSON.stringify(results), JSON.stringify(opts.expected));
 
@@ -24,19 +33,34 @@ function testAggregate(opts){
 		assert.ifError(err);
 		assert.equal(JSON.stringify(results), JSON.stringify(opts.expected));
 
-		// ASYNC: test reusable aggregator functionality
-		var aggregator = aggregate(opts.pipeline);
-		aggregator(opts.inputs, function(err, results){
+		// ASYNC: test one-off usage with context
+		aggregate(opts.pipeline, {hi: "there"}, opts.inputs, function(err, results){
 			assert.ifError(err);
 			assert.equal(JSON.stringify(results), JSON.stringify(opts.expected));
 
-			// ASYNC: test that it is actually reusable
-			aggregator(opts.inputs, function(err, results){
+			// ASYNC: test reusable aggregator functionality with context
+			var aggregator = aggregate(opts.pipeline);
+			aggregator({hi: "there"}, opts.inputs, function(err, results){
 				assert.ifError(err);
-				assert.equal(JSON.stringify(results), JSON.stringify(opts.expected), "Reuse of aggregator should yield the same results!");
+				assert.equal(JSON.stringify(results), JSON.stringify(opts.expected));
+
+				// ASYNC: test reusable aggregator functionality
+				var aggregator = aggregate(opts.pipeline);
+				aggregator(opts.inputs, function(err, results){
+					assert.ifError(err);
+					assert.equal(JSON.stringify(results), JSON.stringify(opts.expected));
+
+					// ASYNC: test that it is actually reusable
+					aggregator(opts.inputs, function(err, results){
+						assert.ifError(err);
+						assert.equal(JSON.stringify(results), JSON.stringify(opts.expected), "Reuse of aggregator should yield the same results!");
+
+						// success!
+						return opts.next();
+					});
+
+				});
 
-				// success!
-				return opts.next();
 			});
 
 		});