Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

get-iterator

Package Overview
Dependencies
Maintainers
2
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

get-iterator

Get the default iterator or async iterator for an iterable or async iterable

  • 2.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
84K
decreased by-12.76%
Maintainers
2
Weekly downloads
 
Created
Source

get-iterator

Build Status Dependencies Status JavaScript Style Guide

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
NameTypeDescription
objIterable|IteratorThe object to extract the iterator from (may be an iterator already).
Returns
TypeDescription
IteratorThe 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

Keywords

FAQs

Package last updated on 14 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc