Przeglądaj źródła

Refs #990. FirstAccumulator is ported, but still need to port test cases.

http://source.rd.rcg.local/trac/eagle6/changeset/1303/Eagle6_SVN
Spencer Rathbun 13 lat temu
rodzic
commit
e6bb5d0db7
1 zmienionych plików z 38 dodań i 0 usunięć
  1. 38 0
      lib/pipeline/accumulators/FirstAccumulator.js

+ 38 - 0
lib/pipeline/accumulators/FirstAccumulator.js

@@ -0,0 +1,38 @@
+var Accumulator = module.exports = (function(){
+	// CONSTRUCTOR
+	/** 
+	* A base class for all pipeline accumulators. Uses NaryExpression as a base class.
+	*
+	**/
+	var klass = function Accumulator(){
+		if(arguments.length !== 0) throw new Error("zero args expected");
+		base.call(this);
+	}, base = require("./AccumulatorSingleValue"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
+
+	// PROTOTYPE MEMBERS
+	proto.getOpName = function getOpName(){
+		return "$first";
+	};
+
+	proto.getFactory = function getFactory(){
+		return klass;	// using the ctor rather than a separate .create() method
+	};
+
+	/** 
+	* Takes a document and returns the first value in the document
+	*
+	* @param {Object} doc the document source
+	* @return the first value
+	**/
+	proto.evaluate = function evaluate(doc){
+		if (this.operands.length == 1) throw new Error("this should never happen");
+
+		/* only remember the first value seen */
+		if (!base.prototype.pValue.get.call(this))
+			this.pValue = this.operands[0].evaluate(doc);
+
+		return this.pValue;
+	};
+
+	return klass;
+})();