mihneadb / node-directory-tree

Convert a directory tree to a JS object.
MIT License
523 stars 106 forks source link

Get folder only results. #62

Closed shijianjian closed 5 years ago

shijianjian commented 5 years ago

Hi,

Thank you for the pretty simple plugin.

In my application, I care about the folder structure only. I therefore do not want to include the final files. Is there a easy way to select?

For example, I am now getting:

-- Folder A
    -- Folder B
        -- FileA.doc
        ...
    -- Folder C
        -- FileA.doc
        ...

While, what I want is:

-- Folder A
    -- Folder B
    -- Folder C

I wish to filter the files out.

mihneadb commented 5 years ago

Hi, you should be able to write a recursive function that only keeps items with children.

cope commented 5 years ago

@shijianjian:

const _ = require('lodash');
const dirTree = require("directory-tree");
const removeFiles = (children) => {
    return _.filter(children, child => {
        if (child.children) child.children = removeFiles(child.children);
        return child.type !== "file"
    });
};
const tree = dirTree("some_path");
tree.children = removeFiles(tree.children);