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

alagator

v1.0.0

Published

Write algorithms that can be re-used for synchronous and asynchronous code

Downloads

14

Readme

alagator

Write algorithms that can be re-used for synchronous and asynchronous code using promises and yield

Build Status Dependency Status NPM version

Installation

npm install alagator

Example

A fully backwards compatible version of @substack's mkdirp but without writing the algorithm out twice:

var path = require('path')
var fs = require('fs')

var Promise = require('promise')
var alagator = require('alagator')

module.exports = mkdirpFactory(true, Promise.denodeify(fs.mkdir), Promise.denodeify(fs.stat))
module.exports.sync = mkdirpFactory(false, fs.mkdirSync, fs.statSync)

module.exports.mkdirp = module.exports.mkdirP = module.exports

function mkdirpFactory(async, mkdir, stat) {
  var rec = alagator(function *(p, mode, made) {
    if (mode === undefined) {
      mode = 0777 & (~process.umask());
    }
    if (!made) made = null;

    if (typeof mode === 'string') mode = parseInt(mode, 8);
    p = path.resolve(p);

    try {
      yield mkdir(p, mode);
      made = made || p;
    }
    catch (err0) {
      switch (err0.code) {
        case 'ENOENT' :
          made = yield rec(path.dirname(p), mode, made);
          yield rec(p, mode, made);
          break;

        // In the case of any other error, just see if there's a dir
        // there already.  If so, then hooray!  If not, then something
        // is borked.
        default:
          var stat;
          try {
              stat = yield stat(p);
          }
          catch (err1) {
              throw err0;
          }
          if (!stat.isDirectory()) throw err0;
          break;
      }
    }

    return made;
  }, async)
  return rec
}

API

alagator(generatorFunction, isAsync)

isAsync defaults to true.

The alagator method takes a generator function, and then either true (for async) or false (for sync). If false is passed, it makes yield act as a pass through, so the method runs fully synchronously. If true is passed, it makes yield await the resolution of a promise (or array of promises) so that the function becomes async (it also uses Promise.nodeify to support both callback and promise based use). The above mkdirp example could be used in any of the following 3 ways:

var mkdirp = require('mkdirp')

mkdirp('/foo/bar', function(err) {
  if (err) throw err
  console.log('/foo/bar exists')
})

//or

mkdirp('/foo/bar')
  .then(function() {
    console.log('/foo/bar exists')
  })
  .done()

//or

mkdirp.sync('/foo/bar')
console.log('/foo/bar exists')

spawn(generatorFunction, isAsync)

Exactly as above, except the function is immediately called with no arguments.

use with other promise libraries

If you want the promise returned from async versions of your algorithms to be of a specific type (other than promise) we've got you covered. Simply pass a wrap function in place of true:

var Q = require('q')

module.exports = mkdirpFactory(Q, Q.denodeify(fs.mkdir), Q.denodeify(fs.stat))
module.exports.sync = mkdirpFactory(false, fs.mkdirSync, fs.statSync)

This will still make use of the same promise library internally, but externally will exclusively use Q.

License

MIT