New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

dir-obj

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dir-obj

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

  • 2.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4
decreased by-33.33%
Maintainers
1
Weekly downloads
 
Created
Source

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')`
});

Keywords

FAQs

Package last updated on 04 Apr 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc