From aa26000b3ee42addc7a6418ae9c689d4a7e3aaa1 Mon Sep 17 00:00:00 2001 From: lostfields Date: Thu, 24 Oct 2024 15:31:31 +0200 Subject: [PATCH] fix: iteration of several operators --- src/linq/base.ts | 32 +++++++++----------------------- src/test/enumerable/record.ts | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/src/linq/base.ts b/src/linq/base.ts index 032a129..e9a62da 100644 --- a/src/linq/base.ts +++ b/src/linq/base.ts @@ -107,15 +107,11 @@ export default abstract class Base implements IEnumerabl if(operator) { if('evaluate' in operator) { - switch(idx) { - case 0: generator = items; break - default: generator = iterate(items, operators, idx - 1) - } - + generator = iterate(items, operators, idx - 1) evaluate = operator.evaluate() } else { - generator = operator.asyncIterator(items) + generator = iterate(operator.asyncIterator(items), operators, idx - 1) } } else { @@ -139,12 +135,9 @@ export default abstract class Base implements IEnumerabl } } - let iterator = iterate(this.items instanceof Repository ? this.items[Symbol.asyncIterator](this) : this.items[Symbol.asyncIterator](), this.operators), - result: IteratorResult + let iterator = iterate(this.items instanceof Repository ? this.items[Symbol.asyncIterator](this) : this.items[Symbol.asyncIterator](), this.operators) - while (!(result = await Promise.resolve(iterator.next())).done) { - yield result.value - } + yield * iterator } protected * getIterator(): IterableIterator { @@ -161,15 +154,11 @@ export default abstract class Base implements IEnumerabl if(operator) { if('evaluate' in operator) { - switch(idx) { - case 0: generator = items; break - default: generator = iterate(items, operators, idx - 1) - } - + generator = iterate(items, operators, idx - 1) evaluate = operator.evaluate() } else { - generator = operator.iterator(items) + generator = iterate(operator.iterator(items), operators, idx - 1) } } else { @@ -193,12 +182,9 @@ export default abstract class Base implements IEnumerabl } } - let iterator = iterate(this.items[Symbol.iterator](), this.operators), - result: IteratorResult - - while (!(result = iterator.next()).done) { - yield result.value - } + let iterator = iterate(this.items[Symbol.iterator](), this.operators) + + yield * iterator } [Symbol.iterator](): IterableIterator { diff --git a/src/test/enumerable/record.ts b/src/test/enumerable/record.ts index ff0189b..630f3e2 100644 --- a/src/test/enumerable/record.ts +++ b/src/test/enumerable/record.ts @@ -110,6 +110,15 @@ describe('When using enumerable for record type', () => { chai.expect(ar).to.deep.equal([1990, 1999, 2004, 2009, 2019]) }) + it('should async iterate using where and orderBy', async () => { + let ar = [] + + for await(let car of new jsEnumerable.Enumerable(asyncIterator()).where(it => it.year >= 2004).orderBy('year')) + ar.push(car.year) + + chai.expect(ar).to.deep.equal([2004, 2009, 2019]) + }) + it('should async iterate using select with property keys', async () => { let car = await new jsEnumerable.Enumerable(asyncIterator()).select('make', 'year').firstAsync() @@ -395,6 +404,15 @@ describe('When using enumerable for record type', () => { chai.expect(ar).to.deep.equal([1990, 1999, 2004, 2009, 2019]) }) + it('should iterate using where and orderBy', async () => { + let ar = [] + + for(let car of new jsEnumerable.Enumerable(iterator()).where(it => it.year >= 2004).orderBy('year')) + ar.push(car.year) + + chai.expect(ar).to.deep.equal([2004, 2009, 2019]) + }) + it('should iterate using select with property keys', () => { let car = new jsEnumerable.Enumerable(iterator()).select('make', 'year').first()