ReactiveX / IxJS

The Interactive Extensions for JavaScript

Home Page:https://reactivex.io/IxJS/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ExtremaByAsyncIterator doesn't produce the expected result when the extremum is the first item of the source.

millimoose opened this issue · comments

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.

The bug seems to be around here:

let current = next.value;
let resKey = await this._keyFn(current);
done = (next = await it.next()).done;
while (!done) {
let curr = next.value;

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.

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);