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

dir-mapper

v1.0.0

Published

Recursive directory mapper. Supports callback, streaming and promise based APIs. Will map at all files in a directory and sub-directories.

Downloads

10

Readme

Asynchronous Directory Mapper

Asyncronously recursively maps out a directory structure. Contains tools for organizing and filtering files too.

Example

This

	root/
		css/
			fonts.css
			style.css
		images/
			bg.png
			me.jpg
		scripts/
			jquery.js
		index.html
		readme.txt

Will become

	[ {file: '/path/to/root/css'},
		{file: 'path/to/root/css/fonts.css'},
		{file: 'path/to/root/css/style.css'},
		{file: 'path/to/root/images'},
		{file: 'path/to/root/images/bg.png'}, 
		{file: 'path/to/root/images/me.jpg'},
		{file: 'path/to/root/index.html'},
		{file: 'path/to/root/scripts'},
		{file: 'path/to/root/scripts/jquery.js'},
		{file: 'path/to/root/readme.txt'}
	]

Install

	npm install dir-mapper

API

Directory Mapper supports both a callback based api and an event driven api.

Callback

	var DirMapper = require('dir-mapper'),
			root = new DirMapper();

	root.walk('path/to/root', function(error, results) {
		if (error) throw error;
		// results.files is an array of objects, as demonstrated above
	});

Event

	var root = new (require('dir-mapper'));

	root.on('error', function(err) {
		// we have an error
	});

	root.on('end', function(results) {
		// do stuff
	});

	root.walk('path/to/root');

Methods

DirMapper([dir ], [recurse], [cb])

  • dir: string, directory to walk, if provided.

  • recurse: boolean, to map all sub-directories or not. Defaults to true (Note: this parameter does not make a difference at this time, Directory Mapper will map all sub directories.)

  • cb: a callback function to call once all directories have been walked, otherwise may listen for the end event.

      var DirMapper = require('dir-mapper');
      new DirMapper('path/to/root', function(err, results) {
      	// do stuff
      });

DirMapper.prototype.walk(dir, [recurse], [cb])

Same as the constructor method except dir is required.

	var DirMapper = require('dir-mapper'),
		root = new DirMapper();

	root.walk('path/to/root');

DirMapper.prototype.sort(order, [field])

Since Directory Mapper maps a directory structure asynchronously there is no garuanteed ordered. This method will organize the resulting list in ascending or descending order.

  • order: number, 1 for ascending and -1 for descending.

  • field: the field to order the array of objects by, defaults to 'file'.

      var DirMapper = require('dir-mapper'),
      	root = new DirMapper();
    
      root.walk('path/to/root', function(err, results) {
      	results.sort(1);
      	// results.files will be in the same order as the example above. 
      });

DirMapper.prototype.flag(field, [fn])

This method, when provided both the field and fn arguments, acts just like the Array.prototype.map method. If only provided the field parameter then this method acts like Array.prototype.filter, returning an array of file objects where each object contains a 'true' value for the field.

  • field: string, the field to filter by or add to each file object.
  • fn: function to mark each file object with, receives three arguments fn(el, i, arr)
    • el: current array element.

    • i: the current key of the array.

    • arr: the files array itself.

      var DirMapper = require('dir-mapper'); var root = new DirMapper();

      root.walk('path/to/root', function(err, results) { // find which files are in the images directory results.flag('images', function(el) { // true if its in the images directory // it is not necessary to return a boolean, may return a string return //images/.test(el.file); }); var images = results.flag('images'); // sort the new images list in descending order images.sort(-1); });

Testing

First run

	npm install --dev

Then

	npm test

TODO:

  • refractor
  • Add the ability to either recurse or not
  • Add synchronous version?
  • Since there is an event driven api maybe convert to a stream??
  • more tests, tests are not comprehensive
  • comment code.