0.0.1 • Published 1 year ago

@duskim/dir2json v0.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

dir-to-json-2

Description

Recursively and asynchronously traverses a directory. Keys are set to directory names. see json example.

Usage

const { dirToJson } = require("./dir-to-json");

(async function () {
  const config = {
    dir: "F://js",
    whitelist: [".js", "*"],
    writeJsonToFile: "./out.json", //optional
    omitEmptyDirectories: true, //optional
    omitObjectsWithMaximumDepthOf: 2, //optional
  };

  <!-- JSON -->
  console.log(JSON.stringify(await dirToJson(config), null, 2));
})();

Configuration

  • dir: directory path to search
  • whitelist: file types to look for inside directories. Set to '*' to include all files
  • (optional) writeJsonToFile: output path for json file
  • (optional) omitEmptyDirectories: omit nested empty objects
const j = {
  a: { b: { c: { d: { e: { f: { g: {} } } } } } },
  b: { c: { js: ["js"], s: "string" } },
};
const actual = await omitNestedEmpty(j, { omitEmptyDirectories: true });
const expected = { b: { c: { js: ["js"], s: "string" } } };
assert.deepEqual(expected, actual);

-> true
  • (optional) omitObjectsWithMaximumDepthOf: omit nested objects with a depth > 'omitObjectsWithMaximumDepthOf',
const j = {
  a: { a: { a: { a: { a: { a: { a: {} } } } } }, b: ["p", "p"] },
};
const actual = await omitMaximumDepth(j, {
  omitObjectsWithMaximumDepthOf: 2,
});
const expected = {
  a: {
    a: {},
    b: ["p", "p"],
  },
};
assert.deepEqual(expected, actual);
-> true

JSON Example

{
  "*": [
    "test\\test.js"
  ],
  "test\\test2": {
    "*": [
      "test\\test2\\main.js"
    ]
  },
  "test\\test3": {
    "*": [
      "test\\test3\\package.json"
    ]
  },
  "test\\test4": {
    "*": [
      "test\\test4\\README.md"
    ]
  }
}