1

Closed

Bug in aggregate function if array is empty

description

There is a bug in the aggregate function in jsinq-enumerable.js . If you pass to the function a seed argument the function shouldn't throw with an empty array. It should return the seed.
 
My proposed fix:
aggregate: function() {
var enumerator = this.getEnumerator();
 
var running;
var func;
var resultSelector = identity;
if (arguments.length >= 2) {
    running = arguments[0];
    func = arguments[1];
    if (arguments.length >= 3) {
        resultSelector = arguments[2];
    }
} else {
    if (!enumerator.moveNext()) {
        throw new InvalidOperationException();
    }                       
 
    func = arguments[0];        
    running = enumerator.current();
}
while (enumerator.moveNext()) {
    running = func(running, enumerator.current());
} 
return resultSelector(running);
},
Closed Mar 27, 2010 at 3:35 PM by kaijaeger
Fixed in 1.0

comments

kaijaeger wrote Mar 27, 2010 at 3:35 PM

Somehow this totally slipped past me. Mea culpa! It is fixed in the 1.0 release.

kaijaeger wrote Mar 27, 2010 at 3:35 PM

Oh, and thanks a lot for reporting/suggesting a fix! Much appreciated!