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
- Make(paths, cb) ⇒
Object
Make creates a new tree object from an array of POSIX or win32 style paths.
- Insert(root, path, value) ⇒
Object
Insert inserts the given path into the root object and assigns the value to the end node.
- Remove(root, path) ⇒
Object
Remove removes the given path from the root object. Also removes any branches that are made empty during this process.
- Find(root, path) ⇒
Object
Find returns the tree branch, end node value, or undefined from the given path in the tree.
- SplitPath(path) ⇒
Array.<string>
SplitPath returns an array of strings representing a path's structure split by forward or back slashes.
Typedefs
- MakeCallback ⇒
any
Make(paths, cb) ⇒ Object
Make creates a new tree object from an array of POSIX or win32 style paths.
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
Insert inserts the given path into the root object and assigns the value to the end node.
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
Remove removes the given path from the root object. Also removes any branches that are made empty during this process.
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
Find returns the tree branch, end node value, or undefined from the given path in the tree.
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>
SplitPath returns an array of strings representing a path's structure split by forward or back slashes.
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