0.2.2 • Published 2 years ago

@kettek/filepaths-to-tree v0.2.2

Weekly downloads
-
License
MPL-2.0
Repository
-
Last release
2 years ago

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.

ParamTypeDescription
pathsArray.<string>An array of POSIX or win32 style paths to build the tree object.
cbMakeCallbackA 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.

ParamTypeDescription
rootObjectThe root tree object to grow to fit the path.
pathstringThe POSIX or win32 path to create as a branch.
valueanyThe 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.

ParamTypeDescription
rootObjectThe root tree object to grow to fit the path.
pathstringThe 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.

ParamTypeDescription
rootObjectThe root tree object to grow to fit the path.
pathstringThe POSIX or win32 path to remove from the tree.

SplitPath(path) ⇒ Array.<string>

Returns: Array.<string> - An array of paths separated by / or \.

ParamTypeDescription
pathstringThe POSIX or win32 path to remove from the tree.

MakeCallback ⇒ any

Returns: any - The value to assign to the given entry.

ParamTypeDescription
fullpathstringThe full path of the given entry.

© 2022 Ketchetwahmeegwun T. Southall