npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

iterable.flow

v0.1.0

Published

Base classes for creating (Async) Iterable/Iterator/Generator in flow.

Downloads

3

Readme

Iterable.flow

travis package downloads styled with prettier

At the moment of writing flow does not have a proper support for Iterable/Iterator/Generator, it does not allow [Symbol.iterator] in class definitions and insteard requires use of @@iterator which on the other hand breaks babel.

This libary provides a workaround in form of Iterable.Sync / Iterable.Async classes that do have [Symbol.iterator] / [Symbol.asyncIterator] method which delegate to iterator / asyncIterator allowing you to define valid iterators that both flow and babel understand and that also works natively in JS engines with Symbol.iterator / Symbol.asyncIterator support in them.

Usage

Import

Rest of the the document & provided code examples assumes that library is installed (with yarn or npm) and imported as follows:

import Iterable from "iterable.flow"

Sync Iterator

In order to define sync iterator you'd neet to implement next method and iterator method that most likely returns this.

class RangeIterator extends Iterable.Sync<number> {
  value:number
  max:number
  static new(from:number=0, to:number=Infinity)
  constructor(from:number, to:number) {
    super()
    this.value = from
    this.max = to
  }
  iterator() {
    return this
  }
  next() {
    const {value, max} = this
    if (value <= max) {
      this.value ++
      return {done:false, value}
    } else {
      return {done:true}
    }
  }
}

console.log([...Range.new(0, 3)]) //=> [0, 1, 2, 3]

Sync Iterable

In order to define sync iteratable you just need to implemnet iterator method:

class Range extends Iterable.Sync<number> {
  from:number
  to:number
  constructor(from:number, to:number) {
    super()
    this.from = from
    this.to = to
  }
  static new(from:number=0, to:number=Infinity) {
    return new Range(from, to)
  }
  *iterator():Iterable.Iterator<number> {
    let n = this.from
    while (n <= this.to) {
      yield n++
    }
  }
}

console.log([...Range.new(0, 4)]) // [0, 1, 2, 3, 4]

Note: You can also return manually defined Iterator (as illustrated in previous example) from iterator method.

Async Iterable

class Beat extends Iterable.Async<number> {
  tempo:number
  time:number
  static new(tempo:number, time:number = 0) {
    this.tempo = tempo
    this.time = time
  }
  constructor(tempo:number, time:number) {
    super()
    this.tempo = tempo
    this.time = time
  }
  asyncIterator() {
    return this
  }
  next() {
    return new Promise(resolve => {
      setTimeout(() => {
        resolve({ done:false, value: (this.time += this.tempo) })
      }, this.tempo)
    })
  }
}


async function main() {
  for await (let time of Beat.new(100)) {
    // ...
  }
}

Install

npm install iterable.flow