file-paths-to-tree
TypeScript icon, indicating that this package has built-in type declarations

3.0.0 • Public • Published

file-paths-to-tree

A basic NPM module which converts list of file paths to a tree structure and also allows you to print it neatly.

Written in TypeScript ❤️

Usage

import { filePathsToTree, treeToString, printFilePathsAsTree } from 'file-paths-to-tree';

filePathsToTree(['foo/bar/baz', 'foo/bar/fuz']);

// [
//   {
//     "name": "foo",
//     "path": undefined,
//     "data": undefined,
//     "parent": undefined,
//     "children": [
//       {
//         "name": "bar",
//         "path": "foo/bar",
//         "data": undefined,
//         "parent": <ref to foo>,
//         "children": [
//           {
//             "name": "baz",
//             "path": "foo/bar/baz",
//             "data": undefined,
//             "children": [],
//             "parent": <ref to foo/bar>,
//           },
//           {
//             "name": "fuz",
//             "path": "foo/bar/fuz",
//             "data": undefined,
//             "children": [],
//             "parent": <ref to foo/bar>,
//           }
//         ]
//       }
//     ]
//   }
// ]

treeToString(filePathsToTree(['foo/bar/baz', 'foo/bar/fuz']));

// foo
// └── bar
//     ├── baz
//     └── fuz

printFilePathsAsTree(['foo/bar/baz', 'foo/bar/fuz']);

// prints the above to console

You can also attach own data to each node using a callback:

filePathsToTree<number>(['foo/bar/baz', 'foo/bar/fuz'], node => 10);

// [
//   {
//     "name": "foo",
//     "path": undefined,
//     "data": 10,
//     "parent": undefined,
//     "children": [
//       {
//         "name": "bar",
//         "path": "foo/bar",
//         "data": 10,
//         "parent": <ref to foo>,
//         "children": [
//           {
//             "name": "baz",
//             "path": "foo/bar/baz",
//             "data": 10,
//             "children": [],
//             "parent": <ref to foo/bar>,
//           },
//           {
//             "name": "fuz",
//             "path": "foo/bar/fuz",
//             "data": 10,
//             "children": [],
//             "parent": <ref to foo/bar>,
//           }
//         ]
//       }
//     ]
//   }
// ]

Readme

Keywords

none

Package Sidebar

Install

npm i file-paths-to-tree

Weekly Downloads

17

Version

3.0.0

License

MIT

Unpacked Size

1.73 MB

Total Files

182

Last publish

Collaborators

  • alexgorbatchev