Gathering detailed insights and metrics for get-iterator
Gathering detailed insights and metrics for get-iterator
Gathering detailed insights and metrics for get-iterator
Gathering detailed insights and metrics for get-iterator
Get the default iterator or async iterator for an Iterable.
npm install get-iterator
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
2 Stars
33 Commits
3 Forks
1 Watching
2 Branches
4 Contributors
Updated on 14 Dec 2022
TypeScript (100%)
Cumulative downloads
Total Downloads
Last day
5.2%
18,920
Compared to previous day
Last week
9.5%
98,277
Compared to previous week
Last month
-0.7%
395,170
Compared to previous month
Last year
-0%
4,976,858
Compared to previous year
1
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.
1npm install get-iterator
1import { getIterator } from 'get-iterator' 2const input = [1, 2, 3] 3const it = getIterator(input) 4console.log(it.next()) // { done: false, value: 1 } 5console.log(it.next()) // { done: false, value: 2 } 6console.log(it.next()) // { done: false, value: 3 } 7console.log(it.next()) // { done: true, value: undefined }
Regular iterator from iterable:
1import { getIterator } from 'get-iterator' 2 3const input = [1, 2, 3] 4const iterable = { 5 [Symbol.iterator] () { 6 let i = 0 7 return { 8 next () { 9 const value = input[i++] 10 return { done: !value, value } 11 } 12 } 13 } 14} 15 16const it = getIterator(input) 17console.log(it.next()) // { done: false, value: 1 } 18console.log(it.next()) // { done: false, value: 2 } 19console.log(it.next()) // { done: false, value: 3 } 20console.log(it.next()) // { done: true, value: undefined }
Async iterator from iterable:
1import { getIterator } from 'get-iterator' 2 3const input = [1, 2, 3] 4const iterable = { 5 [Symbol.asyncIterator] () { 6 let i = 0 7 return { 8 async next () { 9 const value = await new Promise((resolve, reject) => { 10 setTimeout(() => resolve(input[i++]), 10) 11 }) 12 return { done: !value, value } 13 } 14 } 15 } 16} 17 18const it = getIterator(iterable) 19console.log(await it.next()) // { done: false, value: 1 } 20console.log(await it.next()) // { done: false, value: 2 } 21console.log(await it.next()) // { done: false, value: 3 } 22console.log(await it.next()) // { done: true, value: undefined }
Already an iterator (probably):
1import { getIterator } from 'get-iterator' 2 3const input = [1, 2, 3] 4let i = 0 5const iterator = { 6 next () { 7 const value = input[i++] 8 return { done: !value, value } 9 } 10} 11 12const it = getIterator(iterator) 13console.log(it.next()) // { done: false, value: 1 } 14console.log(it.next()) // { done: false, value: 2 } 15console.log(it.next()) // { done: false, value: 3 } 16console.log(it.next()) // { done: true, value: undefined }
1import { 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.
Name | Type | Description |
---|---|---|
obj | Iterable |Iterator | The object to extract the iterator from (may be an iterator already). |
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. |
Feel free to dive in! Open an issue or submit PRs.
MIT © Alan Shaw
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 15/30 approved changesets -- score normalized to 5
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More