Utilities for converting directory/file structures to plain javascript objects
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Simon Repp c8f1cff1a1
Move repository to codeberg
4 months ago
lib Implement and document regex-based matching for custom handlers 6 months ago
specs Implement and document regex-based matching for custom handlers 6 months ago
.gitignore Add jest coverage report directory to gitignore 6 months ago
CHANGELOG.md Initialize 6 months ago
LICENSE.txt Initialize 6 months ago
README.md Move repository to codeberg 4 months ago
jest.config.js Initialize 6 months ago
package-lock.json Use a compatible version 6 months ago
package.json Use a compatible version 6 months ago

README.md

fs-obj

Utilities for converting directory/file structures to plain javascript objects.

ComVer

Install

Not yet released on npm, meanwhile install it directly from the repository:

npm install git+https://codeberg.org/suchbliss/fs-obj.git

Usage

dir2objSync

const { dir2objSync } = require('fs-obj');

const obj = dir2objSync('my_directory');

// Given my_directory/ contains ...
//   foo.md
//   sub/
//     bar.txt
//     baz.json

console.log(obj);

// ... this is what we get:
// {
//   'foo.md': '... foo.md's raw content ...',
//   'sub': {
//     'bar.txt': '... bar.txt's raw content ...',
//     'baz.json': {
//         ... baz.json's parsed content ...
//     }
//   }
// }

Why?

Often file-based content is already laid out on disk in exactly the way one wants to iterate, traverse and process it in code anyway. fs-obj is your magic Make my directory into an object trick for these cases. Do away with the directory walking and file reading boilerplate and enter a new, happier plane of existence with this revolutionary new tool.

Advanced Usage

const { dir2objSync } = require('fs-obj');

const options = {
  encoding: 'ascii',  // Override the default encoding (utf-8) used for reading in files
  handlers: {
    '.foo': fileContent => {
      // This callback is invoked and passed the file content
      // whenever a file with the .foo extension is read in.
      // You can freely modify the result before passing it on.

      return fileContent;
    }
  }
};

const obj = dir2objSync('my_directory', options);

Note that the trigger for custom handlers ('.foo' in the example above) can be either supplied as a string denoting a file extension (e.g. '.spec.js' or '.rs') or as a string denoting an arbitrary regex applied to the full relative path from the respective base directory passed to dir2objSync (e.g. '/\/my_subdirectory\/.*\.txt/i' or '/^\.gitignore/').

What’s next?

  • Implement the async variant, which is just dir2obj and returns a promise
  • Consider whether arrays or objects are the best default object representation or if this can be customizable
  • Consider if obj2dir is something to pursue as well and how that could look