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-obj

v2.0.1

Published

Recusively load files from a directory and return an object with keys from the directory and file names

Downloads

20

Readme

dir-obj

Create an object from a directory tree.

dir-obj creates an object with keys for each directory (by name). All files within a directory that can be required (.js or .json) are added to the directory object, using the base filename as the key and require('file.js') as the value.

This is useful for things like loading test fixture data.

API

You can also customize how dir-obj processes the directory tree

const fixtures = dirObj.readDirectory(path.join(__dirname, 'fixtures'), {
  // Use the `filter` option to filter all directories and files
  // Return `true` to include the directory or file (call `file.isDirectory` to check)
  // Return `false` to exclude the file or directory from the result
  // The default is to only allow `.js` and `.json` files
  filter: file => (file.isDirectory || file.isRequirable) && file.name !== 'index.js',
  // This function takes a File object and returns the value to be added to the object
  // You can mutate `File.key` to change the object key used to reference the value
  // You can return `undefined` to not include the file in the result
  // The default `fileTransform` just calls `require(file.fullpath)`
  // Example: Change whitespace in a filename to `_` then set the value using `require`
  fileTransform: file => {
    file.key = file.basename.replace(/\s/g, '_');
    return require(file.fullpath);
  },
  // This function takes a File (which is a directory) and a value, which is the recursively
  // calculated value of that directory
  // Example: Do not include any directories that have no child keys
  dirTransform: (file, value) => Object.getOwnPropertyNames(value).length > 0 ? value : undefined
});

Example

Using the default options:

/*
directory structure:

fixtures
  ok
    success.json
  responses
    success.json
*/

const path = require('path');
const dirObj = require('dir-obj');
const fixtures = dirObj.readDirectory(path.combine(__dirname, '/fixtures'));

describe('test', function () {
  it('should work', function () {
    nock(/api/).get('/ok').reply(200, fixtures.ok.success);

    request('/')
      .expect(200, fixtures.ok.response.success);
  });
});

Loading SQL files:

// Get an object where the value of each key is a string loaded from each SQL file

const fixtures = dirObj.readDirectory(path.resolve('../../models/sql'), {
  filter: file => file.isDirectory || file.ext === '.sql'.
  fileTransform: file => file.readSync() // helper function equivilent to `fs.readFileSync(file.fullpath, 'utf8')`
});