Socket
Book a DemoInstallSign in
Socket

betterator

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

betterator

A better sync and async iterator API.

latest
Source
npmnpm
Version
2.0.2
Version published
Weekly downloads
265
24.41%
Maintainers
1
Weekly downloads
 
Created
Source

betterator

version CI gzip size brotli size
A better sync and async iterator API.

Features

  • Intuitive: easy to use hasNext and getNext methods
  • Familiar: lots of other programming languages use the same API
  • Tiny: ~400 bytes minzipped
  • Awesome Name: you have to admit it's pretty rad :sunglasses:

Install

$ npm i betterator

Usage

import { Betterator, AsyncBetterator } from 'betterator'

const slothActivities = [`sleeping`, `eating`, `climbing`]

// Or `new Betterator(slothActivities[Symbol.iterator]())`
const iterator = Betterator.fromIterable(slothActivities)

while (iterator.hasNext()) {
  console.log(iterator.getNext())
}
//=> sleeping
//=> eating
//=> climbing

try {
  iterator.getNext()
} catch (e) {
  console.log(e.message)
}
//=> Doesn't have next

console.log(iterator.getNextOr(() => `being lazy`))
//=> being lazy

const asyncSlothActivities = (async function* () {
  yield* slothActivities
})()

// Or `new AsyncBetterator(slothActivities[Symbol.asyncIterator]())`
const asyncIterator = AsyncBetterator.fromAsyncIterable(asyncSlothActivities)

while (await asyncIterator.hasNext()) {
  console.log(await asyncIterator.getNext())
}
//=> sleeping
//=> eating
//=> climbing

try {
  await asyncIterator.getNext()
} catch (e) {
  console.log(e.message)
}
//=> Doesn't have next

const delay = timeout => new Promise(resolve => setTimeout(resolve, timeout))
console.log(
  await asyncIterator.getNextOr(() => delay(10).then(() => `being lazy`)),
)
//=> being lazy

See the type definitions for more documentation.

Contributing

Stars are always welcome!

For bugs and feature requests, please create an issue.

For pull requests, please read the contributing guidelines.

License

Apache 2.0

This is not an official Google product.

Keywords

iterator

FAQs

Package last updated on 08 Apr 2022

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.