get-iterator
v2.0.1
Published
Get the default iterator or async iterator for an iterable or async iterable
Downloads
380,556
Readme
get-iterator
Get the default iterator or async iterator for an iterable or async iterable
Reduce the boilerplate of extracting the iterator from an object when you don't know if the object is an (async) iterable or already an (async) iterator.
Install
npm install get-iterator
Usage
import { getIterator } from 'get-iterator'
const input = [1, 2, 3]
const it = getIterator(input)
console.log(it.next()) // { done: false, value: 1 }
console.log(it.next()) // { done: false, value: 2 }
console.log(it.next()) // { done: false, value: 3 }
console.log(it.next()) // { done: true, value: undefined }
Examples
Regular iterator from iterable:
import { getIterator } from 'get-iterator'
const input = [1, 2, 3]
const iterable = {
[Symbol.iterator] () {
let i = 0
return {
next () {
const value = input[i++]
return { done: !value, value }
}
}
}
}
const it = getIterator(input)
console.log(it.next()) // { done: false, value: 1 }
console.log(it.next()) // { done: false, value: 2 }
console.log(it.next()) // { done: false, value: 3 }
console.log(it.next()) // { done: true, value: undefined }
Async iterator from iterable:
import { getIterator } from 'get-iterator'
const input = [1, 2, 3]
const iterable = {
[Symbol.asyncIterator] () {
let i = 0
return {
async next () {
const value = await new Promise((resolve, reject) => {
setTimeout(() => resolve(input[i++]), 10)
})
return { done: !value, value }
}
}
}
}
const it = getIterator(iterable)
console.log(await it.next()) // { done: false, value: 1 }
console.log(await it.next()) // { done: false, value: 2 }
console.log(await it.next()) // { done: false, value: 3 }
console.log(await it.next()) // { done: true, value: undefined }
Already an iterator (probably):
import { getIterator } from 'get-iterator'
const input = [1, 2, 3]
let i = 0
const iterator = {
next () {
const value = input[i++]
return { done: !value, value }
}
}
const it = getIterator(iterator)
console.log(it.next()) // { done: false, value: 1 }
console.log(it.next()) // { done: false, value: 2 }
console.log(it.next()) // { done: false, value: 3 }
console.log(it.next()) // { done: true, value: undefined }
API
import { getIterator } from 'get-iterator'
getIterator(obj)
Get the default iterator or async iterator for an Iterable. If obj
is already an iterator (i.e. has a next
function) return it, since it's probably already an iterator.
This function will throw if obj
is not an iterable or iterator.
Parameters
| Name | Type | Description |
|------|------|-------------|
| obj | Iterable
|Iterator
| The object to extract the iterator from (may be an iterator already). |
Returns
| Type | Description |
|------|-------------|
| Iterator
| The result of calling obj[Symbol.iterator]()
or obj[Symbol.asyncIterator]()
or simply the passed obj
if it is already an iterator. |
Contribute
Feel free to dive in! Open an issue or submit PRs.
License
MIT © Alan Shaw