node-directory-tree icon indicating copy to clipboard operation
node-directory-tree copied to clipboard

"Attributes" not working as expected

Open clse opened this issue 5 years ago • 4 comments

Current default output includes type, size, and extension. It is impossible to remove these attributes, though they are cluttering the JSON since they are unnecessary for my purposes.

    const output = dirTree(
      mypath,
      {attributes: ["extension", "type", "size"]}
    );

By adding those three attributes to the call, it does the following:

  • toggles off extension from files
  • toggles off type from files
  • toggles on type for directories
  • toggles on size for directories
  • keeps size on files

This is very inconsistent behavior so I'm unsure how to proceed.

Thanks in advance.

clse avatar Mar 31 '20 16:03 clse

Indeed, there are a few attrs that are always present - https://github.com/mihneadb/node-directory-tree/blob/master/lib/directory-tree.js#L76-L78 and https://github.com/mihneadb/node-directory-tree/blob/master/lib/directory-tree.js#L102-L103.

Should be easy to fix. Probably extension, type and size can become optional as well. What are your thoughts?

mihneadb avatar Apr 02 '20 21:04 mihneadb

Thanks for the quick response, @mihneadb , appreciate it! In my opinion, only "name", "path" and "children" are necessary fields that should be toggled by default. If you think other attributes need to be on by default, I would argue it would be preferable for those attributes to be consistent across both files and directories (all nodes get "extension", all nodes get "size", etc) so they could be easily disabled within the attributes option rather than produce unexpected behavior (turned off for files, turned on for directories). The last option would be to have fileAttributes and directoryAttributes separate objects in options. To summarize, three courses of action would solve the issue:

  1. Option 1: Default attributes only "name", "path" and "children"
  2. Option 2: Any number of default attributes, but same default attributes for files and directories
  3. Option 3: Separate attribute options into "fileAttributes" and "directoryAttributes".

My vote would be for Option 1. Leaner is better. What do you think?

clse avatar Apr 06 '20 21:04 clse

Sorry about the extremely slow response this time. Indeed, option 1 sounds best. PR is welcome if you want to contribute!

mihneadb avatar Jun 23 '20 17:06 mihneadb

Hi. It is good to have the option to exclude default attributes! I am looking for it too.

ApayRus avatar Feb 03 '21 03:02 ApayRus