tr33
v1.1.1
Published
tr33 is a TypeScript library designed to simplify the management of tree structures in your applications. Whether you're dealing with hierarchical data in a frontend framework like React or Vue, or organizing data in a backend service, tr33 provides an in
Readme
tr33
tr33 is a TypeScript library designed to simplify the management of tree structures in your applications. Whether you're dealing with hierarchical data in a frontend framework like React or Vue, or organizing data in a backend service, tr33 provides an intuitive API for creating, manipulating, and traversing tree structures.
Installation You can install tr33 via npm or yarn:
npm install tr33
# or
yarn add tr33
Creating a Tree
To create a tree with tr33, you can use the createTree function. This function takes in either a single RawTreeNode object or an array of RawTreeNode objects and returns a TreeNode or an array of TreeNodes respectively.
import { createTree, RawTreeNode, TreeNode } from 'tr33';
// Define your tree structure
const data: RawTreeNode<number> = {
value: 1,
children: [
{
value: 2,
children: [{ value: 3 }, { value: 4 }],
},
{
value: 5,
},
],
};
// Create a tree
const tree: TreeNode<number> = createTree(data) as TreeNode<number>;
console.log(tree.value()); // Output: { id: '...', value: 1, children: [...] }Manipulating Nodes
You can manipulate nodes in the tree using various methods provided by the TreeNode class.
// Assuming rootNode is a TreeNode instance
// Update the value of a node
rootNode.update(10);
// Add a new child node
rootNode.add({ value: 20 });
// Remove a node by its ID
rootNode.remove('someId');
// Find a node by its ID
const childNode = rootNode.find('someId');Traversing the Tree
tr33 allows you to traverse the tree easily, whether you need to iterate over all nodes or find a specific node.
// Assuming rootNode is a TreeNode instance
// Get an array of child nodes
const children = rootNode.children();
// Check if a node is a leaf node
const isLeaf = rootNode.isLeaf();
// Find all leaf nodes in the tree
const leafNodes = rootNode.query({}, { isLeaf: true });
// Find the parent nodes of a given node
const parentIds = getNodeParents(rootNode, childNode);API Reference
createTree(data: RawTreeNode | RawTreeNode[], options?: { createRootNode: boolean }): TreeNode | TreeNode[]: Creates a tree from raw tree node data.
Methods
.value(): RawTreeNode<J | null | undefined>: Returns the raw representation of the node..id(): string: Returns the ID of the node..update(update: J): TreeNode<NullableVal>: Updates the value of the node..children(): TreeNode<J>[]: Returns an array of child nodes..getChildrenValues(): J[]: Returns an array of values of child nodes..getChildrenIds(): (string | undefined)[]: Returns an array of IDs of child nodes..add(node: RawTreeNode<J>): string: Adds a child node..remove(id: string): void: Removes a child node by ID..find(id: string, acc?: Array<string>): FindResultTuple: Finds a node by its ID.query(query: {}, options: { isLeaf: boolean }, acc?: Array<TreeNode<NullableVal<J>>>): Array<TreeNode<NullableVal<J>>>:Queries the tree for nodes based on a condition..getParentId(): string | undefined: Returns the ID of the parent node..isRoot(): boolean: Checks if the node is a root node..isLeaf(): boolean: Checks if the node is a leaf node..setId(id: string):Sets the ID of the node.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Feel free to open an issue or submit a pull request.
Acknowledgments
Thank you to all contributors who have helped make tr33 better.
