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

noms

v0.0.0

Published

easily make yummy noms for other streams

Downloads

4,424,943

Readme

noms

create super easy readable-streams filled with yummy data to nom on, inspired by from2 (and a test based on one from there).

npm install noms
var noms = require('noms');

Create a quick readable stream

nom([options], read, [before]);

options is optional and passed to readable stream just like in from2 or through2

read is the main read function, it is similar to the original node streams but size is optional and a callback is passed. It will NOT be called again until the callback is called.

before is called right before the first call to read in order to do setup, it is passed a callback and read will not be called until the callback is called.

like through2 and from2 noms also features

nom.obj([options], read, [before]);

which is shorthand for creating an object stream and like from2 noms has

noms.ctor(read, [before]);

which returns a constructor function for use if you're creating a large number of copies of the stream.

example (based on one from from2):

function fromString(string) {
  return noms(function(size, next) {
    // if there's no more content
    // left in the string, close the stream.
    if (string.length <= 0) {
      return this.push(null);
    }

    // Pull in a new chunk of text,
    // removing it from the string.
    var chunk = string.slice(0, size);
    string = string.slice(size);

    // Emit "chunk" from the stream.
    next(null, chunk);
  })
}

you can use this.push(foo) and next(null, foo) interchangeably, just remember to call next at the end.

function fromString(string) {
  return noms(function(size, next) {
    // if there's no more content
    // left in the string, close the stream.
    if (string.length <= 0) {
      return next(null, null);
    }

    // Pull in a new chunk of text,
    // removing it from the string.
    var chunk = string.slice(0, size);
    string = string.slice(size);

    // Emit "chunk" from the stream.
    this.push(chunk);
    // finish up
    next();
  })
}

If you don't care about size you can omit it

function fromString(sentence) {
  var strings = sentence.trim().split(/\s+/);
  var i = -1;
  var len = strings.length;
  return noms(function(next) {
    // if there's no more content
    // left in the string, close the stream.
    if (++i < len) {
      return this.push(strings[i]);
    } else {
     return this.push(null);
    }
    next();
}

You don't have to worry about the response from this.push, as noms will call the function again after you call next until the cache is full.

var fs = require('fs');
var path = require('path');
function getFiles(dir) {
  var stack = [path.resolve(dir)];
  return noms(function(next) {
    if (!stack.length) {
      //we are done
      return next(null, null);
    }
    var self = this;
    var current = stack.pop();
    fs.readdir(current, function (err, paths) {
      if (err) {
        return next(err);
      }
      if (!paths.length) {
        // this directory is empty
        return next();
      }
     var todo = paths.length;
     paths.forEach(function (file) {
        var fullPath = path.join(current, file);
        fs.stat(fullPath, function (err, stats) {
          todo--;
          if (err) {
            return next(err);
          }
          if (stats.isFile()) {
            //found a file
            // emit it as data
            self.push(fullPath);
          } else if (stats.isDirectory()) {
            // found another directory
            // put it into the stack
            // is depth first, switch this to
            // a shift to make it breadth first
            stack.push(fullPath);
          }
          if (!todo) {
            // we've done all the files
            // would be a lot simpler if I used promises
            // would that help or hurt the example?
            next();
          }
        });
      });
    });

}