iter wrapper
This commit is contained in:
1 parent
60b522922a
commit
8ff9f29ac0
2 files changed
+44
No files matched your search
@@ -0,0 +1,25 @@
|
||||
import { IteratorWrap } from "./iter"
|
||||
|
||||
|
||||
test('it wraps a simple iter', () => {
|
||||
const wit = new IteratorWrap((function* () {
|
||||
yield '2'
|
||||
return true
|
||||
})());
|
||||
const data = wit.toArray();
|
||||
expect(data).toEqual(['2'])
|
||||
expect(wit.returnValue).toEqual(true)
|
||||
})
|
||||
|
||||
test('it wraps a simple iter while being nested', () => {
|
||||
const wit = new IteratorWrap((function* () {
|
||||
yield '2'
|
||||
return true
|
||||
})());
|
||||
function* makeIter(w: IteratorWrap<string, boolean, any>) {
|
||||
yield* w.iter()
|
||||
}
|
||||
const data = Array.from(makeIter(wit));
|
||||
expect(data).toEqual(['2'])
|
||||
expect(wit.returnValue).toEqual(true)
|
||||
})
|
||||
@@ -0,0 +1,19 @@
|
||||
|
||||
|
||||
export class IteratorWrap<T, TReturn, TNext> {
|
||||
public returnValue: TReturn | null = null;
|
||||
constructor(
|
||||
private it: Generator<T,TReturn,TNext>
|
||||
) { }
|
||||
*iter() {
|
||||
let next = this.it.next()
|
||||
while (!next.done) {
|
||||
yield next.value
|
||||
next = this.it.next()
|
||||
}
|
||||
this.returnValue = next.value
|
||||
}
|
||||
toArray() {
|
||||
return Array.from(this.iter())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user