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

degenerate

v0.0.2

Published

Desugared generator constructor

Downloads

5

Readme

degenerate

Build Status

Browser support

Library lets you create ES6 generators with in the (dis)comfort of ES5 JS environment (like < [email protected]) and without syntax sugar. This magic happens at the cost of the syntax transformations at the generator instantiation. You can use this with task.js, co, suspend or any other Generator-based flow control based library without waiting on your favorite JS environment to enable generators for you!

API

Given a generators support in your environment you should be able to write code like this:

var range = function*(from, to, step) {
  step = step || 1
  var n = from

  while (n <= to) {
    yield n
    n ++
  }

  return n
}

var digits = range(0, 9)
var step
while (step = digits.next(), !step.done) {
  console.log(step.value)
}

Unfortunately above code will throw a syntax error in engines that don't have generators support.

With a degenerate you can create equivalent range generator by typeing this:

var Generator = require("degenerate")
var range = new Generator(function(from, to, step) {
  step = step || 1
  var n = from

  while (n <= to) {
    yield(n)
    n ++
  }

  return n
})

var digits = range(0, 9)
var step
while (step = digits.next(), !step.done) {
  console.log(step.value)
}

Notice the difference ? Just loose a * and replace yield n statement with yield(n) call expression.

You can also use delegeting yields:

var concat = new Generator(function(xs, ys) {
  yield* xs
  yield* ys
})

var zs = concat(range(1, 3), range(4, 7))
while (step = zs.next(), !step.done) {
  console.log(step.value)
}

Limitations

yield isn't a function

Don't try to outsmart this tool, even though yield may feel like a first class function, it is not and any attempts to use it as such will break the code.

strict mode is strict about yield

If you try to define function that refers to yield in strict mode, you'll get a syntax error :( To overcome this limitation all you need to do is write this.yield instead:

"use strict";

var range = Generator(function(from, to) {
  while (from <= to)
    this.yield(from++)
})

var concat = new Generator(function(xs, ys) {
  this.yield* xs
  this.yield* ys
})

Enclosed variables are lost

This is the most unfortunate limitation of all! You can not enclose any scope variables, all that generator body will have has access to is a top scope or whatever has being passed into as this pseudo variable or as arguments.

What's the trick ?

Generator function parses source of the given function, transforms AST to make function expression a generator, replace yield(x), this.yield(x) with yield x. Transforms new AST via facebook/regenerator (Thank them for making it possible) and finally generates a generator from the results.

Install

npm install degenerate