Treebase
library designed to manage and manipulate tree structures. With a host of powerful utilities and a straightforward API, working with hierarchical data structures has never been easier.
- Easy Manipulation: Add, delete, update, or move nodes with ease.
- Search: Search for nodes based on a specific property.
- Hierarchical Tree: Construct a hierarchical tree structure from a flat list of items.
- Dictionary: Get a key-value dictionary of all items in the tree structure.
yarn add treebase
npm install treebase
import { TreeBase } from "treebase";
const props = {
data: {
// this can also be tree sctructured data
oak: { title: "Oak Item", pid: "root" }, //
apple: { title: "Apple title", pid: "oak" },
banana: { title: "Banan title", pid: "oak" },
cucumber: { title: "Cucumber title", pid: "banana" },
},
// OPTIONS (optional)
options: {
pid: "pid", // key for parent id
children: "children", // key for children
isDir: null, // function to check if item is a directory
defaultRoot: "root", // default root id
},
};
const treebase = new TreeBase(props);
treebase.add({ title: "Watermelon title", pid: "apple" }); // If id is not provided, it will be generated
// tree sctructured data
let treeData = treebase.getTree();
// key value dictionary
let treeDictionary = treebase.getDictionary();
// Get tree of a specific root
let treeData = treebase.getTree("aa");
console.log(treeDictionary, treeData);
// treeDictionary
{
oak: { title: "Oak title", pid: "root" },
apple: { title: "Apple title", pid: "oak" },
banana: { title: "Banan title", pid: "oak" },
cucumber: { title: "Cucumber title", pid: "banana" },
watermelon: { title: "Watermelon title", pid: "apple" },
}
// treeData
[
{
id: "oak",
title: "Oak title",
pid: "root",
children: [
{
id: "apple",
title: "Apple title",
pid: "oak",
children: [
{
id: "watermelon",
title: "Watermelon title",
pid: "apple",
children: [],
},
],
},
{
id: "banana",
title: "Banana title",
pid: "oak",
children: [
{
id: "cucumber",
title: "Cucumber title",
pid: "banana",
children: [],
},
],
},
],
},
];
Constructs a hierarchical tree structure starting from the specified root.
- Parameters:
rootId
: The identifier for the root of the tree (defaults tothis.options.defaultRoot
).keepIndex
: Whether to preserve the order of items based on their index (defaults totrue
).
- Returns: A tree structure where each item has a list of its children.
Adds a new item to the tree structure.
- Parameters:
item
: The item to be added.
- Returns: The newly added item or the existing item if a duplicate is found.
Removes an item from the tree structure.
- Parameters:
id
: The ID of the target item to delete.options
: {moveToRoot?, moveTo?} - IfmoveToRoot
is set totrue
, the item will be moved to the root of the tree. IfmoveTo
is set to the ID of another item, the item will be moved to the specified item's children.
- Returns: Updated dictionary.
Updates properties of a specified item in the tree structure.
- Parameters:
id
: The unique identifier of the item to be updated.payload
: An object containing the properties to be updated.
- Returns: The updated item with the new properties.
Moves a child to a different parent or position within the tree structure.
-
Parameters:
id
: The unique identifier of the child to be moved.options
: {pid?, index?} - Ifpid
is set to the ID of another item, the child will be moved to the specified item's children. Ifindex
is set to a number, the child will be moved to the specified position within the parent's children.
-
Returns: The updated dictionary after the move operation.
Search for an item in the tree structure.
- Parameters:
path
: The path to the property to search for.value
: The value to search for.
- Returns: A list of items that match the search criteria.
Retrieves the direct children of a specified parent item.
- Parameters:
id
: The unique identifier of the parent item.
- Returns: List containing all the direct children of the specified parent item.
Retrieves all descendants (deep children) of a specified parent item.
- Parameters:
pid
: The unique identifier of the parent item.
- Returns: A flattened list containing all descendants of the specified parent item.
Checks if an item has any children.
- Parameters:
id
: target item.
- Returns:
true
if the item has children,false
otherwise.
Get all parents of a child until the root item.
- Parameters:
id
: target child.rootId
: root item id.
- Returns: Array of parent ids.
Checks if an item is a deep parent of another item.
- Parameters:
id
: child item ID.pid
: potential parent item ID.
- Returns:
true
if pid is a parent of id,false
otherwise.
Re-indexes the direct children of a specified parent item.
- Parameters:
pid
: The unique identifier of the parent item.
- Returns: The updated dictionary after reindexing the children.
Update dictionary object from an array of items.
- Parameters:
list
: list items.
- Returns: None.