ExtremaByAsyncIterator doesn't produce the expected result when the extremum is the first item of the source.
millimoose opened this issue · comments
David Vallner commented
IxJS version:
2.5.3
Code to reproduce:
import * as aix from 'ix/asynciterable';
async function *xx() {
yield {x: 3};
yield {x: 2};
yield {x: 1};
}
async function main() {
console.log('max xx', await aix.toArray(aix.maxBy(
xx(),
({x}) => x
)));
}
main().catch(console.error);
Expected behavior:
The logged output is the array `[{x: 3}]
Actual behavior:
The output is the empty array []
.
Additional information:
The example code produces the expected output if yield {x: 3}
is anything but the first yielded value.
David Vallner commented
The bug seems to be around here:
IxJS/src/asynciterable/_extremaby.ts
Lines 39 to 43 in 7b393b4
The value of current
is never actually used anywhere, this should probably be:
let curr = next.value;
result.push(curr);
with the let curr
declaration below just reassigning this variable instead.
David Vallner commented
Bug remains in 3.0.0, an updated reproduction would be:
import * as aix from 'ix/asynciterable';
import * as aixop from 'ix/asynciterable/operators';
async function *xx(): any {
yield {x: 3};
yield {x: 2};
yield {x: 1};
}
async function main() {
const max = aix.from(xx()).pipe(aixop.maxBy((it: {x: number}) => it.x))
console.warn(await aix.toArray(max))
}
main().catch(console.error);