Skip to content

Commit

Permalink
Merge pull request #26 from tfso/bugfix/iteration-of-several-operators
Browse files Browse the repository at this point in the history
bugfix/Iteration of several operators
  • Loading branch information
lostfields authored Oct 25, 2024
2 parents 5c9e352 + aa26000 commit 7a5d5b9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
32 changes: 9 additions & 23 deletions src/linq/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,11 @@ export default abstract class Base<TEntity extends Entity> 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 {
Expand All @@ -139,12 +135,9 @@ export default abstract class Base<TEntity extends Entity> implements IEnumerabl
}
}

let iterator = iterate(this.items instanceof Repository ? this.items[Symbol.asyncIterator](this) : this.items[Symbol.asyncIterator](), this.operators),
result: IteratorResult<TEntity>
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<TEntity> {
Expand All @@ -161,15 +154,11 @@ export default abstract class Base<TEntity extends Entity> 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 {
Expand All @@ -193,12 +182,9 @@ export default abstract class Base<TEntity extends Entity> implements IEnumerabl
}
}

let iterator = iterate(this.items[Symbol.iterator](), this.operators),
result: IteratorResult<TEntity>

while (!(result = iterator.next()).done) {
yield result.value
}
let iterator = iterate(this.items[Symbol.iterator](), this.operators)

yield * iterator
}

[Symbol.iterator](): IterableIterator<TEntity> {
Expand Down
18 changes: 18 additions & 0 deletions src/test/enumerable/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit 7a5d5b9

Please sign in to comment.