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

starx

v0.1.8

Published

Generator executor for ECMAScript 6

Downloads

46

Readme

Motivation

starx allows you to use ECMAScript 6 generators to perform flow control. (The 'star' in the name is inspired from the declaration of a generator function, i.e. function*() {...} while 'x' stands for 'executor'.)

Basically, no more callback hells with cumbersome exception handling...

request(url1, function(err, res1) {
  if (err) return print(err)
  request(url2, function(err, res2) {
    if (err) return print(err)
    request(url3, function(err, res3) {
      if (err) return print(err)
      print(size([res1, res2, res3]) + " bytes")
    })
  })
})

With starx, you can write:

starx(function*() {
  try {
    var res1 = yield request(url1)
    var res2 = yield request(url2)
    var res3 = yield request(url3)
    print(size([res1, res2, res3]) + " bytes")
  } catch (err) {
    print(err)
  }
})()

How it works

Invoking starx() on a generator returns an executor. You can create multiple executors for the same generator instance.

This executor, when invoked, keeps calling generator.next() until the generator is exhausted. Each call to generator.next() returns the next value yielded (or returned) from inside the generator. For starx to work, this value must be "yieldable", i.e. one of the followings:

  • A functions whose only argument is a callback accepting (err, val) (see yieldable functions)
  • A promise (anything with a then(callback, errback))
  • A value (primitive, object, null)
  • Another executor created by starx
  • A generator or iterator
  • An array of the aboves (nesting okay)

Yieldable functions

If a function takes more than one argument, you can use starx.yieldable(fn) to convert it to a compliant form. For example:

If fn is function(arg1, arg2, cb) {...}, invoking starx.yieldable(fn) returns a new function with the following form:

var newFn = function(arg1, arg2) { 
    fn(arg1, arg2, fakeCb)
    return function(cb) {...}
}

Invoking newFn(arg1, arg2) returns a function accepting a callback, which as mentioned, is compliant with starx and thus can be yielded, like so:

starx(function*() {
    yield newFn(arg1, arg2)
})()

Most NodeJS functions (built-in and libraries) can be converted to a yieldable function using this approach.

Examples

Read file

starx = require('starx')
readFile = starx.yieldable(require('fs').readFile)

var generator = function*() {
  var content = yield readFile(__filename, 'utf8')
  console.log(content)
}
var executor = starx(generator)
executor()

Or simply:

starx(function*() {
  var content = yield readFile(__filename, 'utf8')
  console.log(content)
})()

Serial download

starx = require('starx')
request = starx.yieldable(require('request'))

starx(function*() {
  var res1 = yield request("https://www.google.com/")
  var res2 = yield request("https://www.bing.com/")
  var res3 = yield request("https://www.yahoo.com/")
  console.log(size([res1, res2, res3]), "bytes")
})()

function size(responses) {
  return responses.reduce(function(a, c) {
    return a + c.body.length
  }, 0)
}

Parallel download

starx(function*() {
  var r1 = request("https://www.google.com/")
  var r2 = request("https://www.bing.com/")
  var r3 = request("https://www.yahoo.com/")
  var res = yield [r1, r2, r3]
  console.log(size(res), "bytes")
})()

A DRYer version

starx(function*() {
  var res = yield [urls].map(function(url) {
    return request(url)
  })
  console.log(size(res), "bytes")
})()

You might think we could have written:

yield [urls].map(request)

But that wouldn't work. The reason is map invoke request with not just the element, but also its index and the original array. Because yieldable passes through all arguments by default, request would end up being invoked with those 3 arguments while it actually expects the second argument to be a callback.

To make this work, either explicitly invoke request with url as the previous example or provide a second argument to yieldable: argCount. If argCount is true, yieldable limits the number of arguments passed through to be fn.length-1. If argCount is a number, yieldable limits the number of arguments to be argCount-1.

We can revise the previous example as follows:

request = starx.yieldable(require('request'), true /* argCount */)
starx(function*() {
  var res = yield [urls].map(request)
  console.log(size(res), "bytes")
})()

Install

NPM

npm install starx

Bower

bower install starx
  • Node >= 0.11.6 (run with --harmony or --harmony-generators)
  • Chrome >= 28 (turn on experimental flag)
  • Firefox >= 27
  • Or use Google Traceur