@kettek/filepaths-to-tree
v0.2.2
Published
POSIX/win32 path strings to POJO tree
Downloads
1
Maintainers
Readme
filepaths-to-tree
This module provides functions for creating and manipulating a plain object tree built from POSIX and win32 style paths. There are no classes, only functions that manipulate a POJO representing a tree structure.
Functions
Typedefs
Make(paths, cb) ⇒ Object
Returns: Object - The resulting tree.
| Param | Type | Description | | --- | --- | --- | | paths | Array.<string> | An array of POSIX or win32 style paths to build the tree object. | | cb | MakeCallback | A optional callback to assign to the end node of the path. |
Example
// returns { a: { b: { c: 'a/b/c', d: 'a\\b\\d', e: 'a\\b/e' } }, b: { c: { a: 'b/c\\a' } } }
Make(['a/b/c', 'a\\b\\d', 'a\\b/e', 'b/c\\a'], p => p)
Insert(root, path, value) ⇒ Object
Returns: Object - The root tree object.
| Param | Type | Description | | --- | --- | --- | | root | Object | The root tree object to grow to fit the path. | | path | string | The POSIX or win32 path to create as a branch. | | value | any | The value to set the end of the branch to. |
Example
// returns { my: { mixed: { path: 'something' } } }
Insert({}, '/my\\mixed/path', 'something')
Remove(root, path) ⇒ Object
Returns: Object - The root tree object.
| Param | Type | Description | | --- | --- | --- | | root | Object | The root tree object to grow to fit the path. | | path | string | The POSIX or win32 path to remove from the tree. |
Example
// returns { a: { '1': 'a/1', '2': 'a/2' }, b: { '1': 'b\\1' } }
let t = Make(['a/1', 'a/2', 'b\\1'])
// returns { a: { '2': 'a/2' }, b: { '1': 'b\\1' } }
Remove(t, 'a/1')
// returns { a: { '2': 'a/2' } }
Remove(t, 'b/1')
Find(root, path) ⇒ Object
Returns: Object - The tree branch, end value, or undefined.
| Param | Type | Description | | --- | --- | --- | | root | Object | The root tree object to grow to fit the path. | | path | string | The POSIX or win32 path to remove from the tree. |
SplitPath(path) ⇒ Array.<string>
Returns: Array.<string> - An array of paths separated by / or \.
| Param | Type | Description | | --- | --- | --- | | path | string | The POSIX or win32 path to remove from the tree. |
MakeCallback ⇒ any
Returns: any - The value to assign to the given entry.
| Param | Type | Description | | --- | --- | --- | | fullpath | string | The full path of the given entry. |
© 2022 Ketchetwahmeegwun T. Southall