This library is meant to be used for creating and manipulating tree-node structures.
The API is designed with ease of use in mind, hence the name Treesy (Tree + Easy - get it? 😂 - I'm sorry).
I created it for a side project I'm working on because I couldn't find a library that has a simple API and is "just" for creating tree structures. Oh and it doesn't have any dependencies :)
Warning! The API is not final and is most likely to change!
yarn add treesy
npm install treesy
Or through a CDN
<script src="https://unpkg.com/treesy"></script>
import { Tree } from "treesy";
const tree = new Tree();
tree.add({ name: "bob" });
console.log(tree.toJson());
[
{
"id": "UUID",
"parentId": null,
"data": {
"name": "bob"
},
"children": []
}
]
tree.add("MY_UNIQUE_ID", { name: "bob" });
console.log(tree.toJSON());
[
{
"id": "MY_UNIQUE_ID",
"parentId": null,
"data": {
"name": "bob"
},
"children": []
}
]
Creates the tree.
You can pass an array of JsonNode
's to create a tree with default data. It can be done like this:
const jsonTree = [
{
id: "MY_ID",
data: {
greeting: "Hello",
},
children: [],
},
];
const tree = new Tree(jsonTree);
console.log(tree.toJson()[0].id); // MY_ID
The internal tree itself but exposed.
This is used to add a root node.
Used to search the whole tree for a given node based on the search criteria.
SearchCriteria
- There are 3 possible ways to use the search criteria.
- By specifying a string which will look for a node by its ID.
- By passing an object which will look at the
data
property of the Node. It does asubset
comparison therefore the wholedata
object doesn't have to match exactly, only a subset of the fields. - By a callback if you wanted more control. The callback needs to return a boolean and the first argument of the callback is the
current
node.
The callback can be used like this:
const node = tree.find(
node => node.data.count === 2)
);
SearchOptions
- possible options include:
export type SearchOptions = {
deep?: boolean;
};
If deep
is set to tree, the finder will do a recursive search. If it's false, it'll only look at the direct children of the tree (root nodes). The default value is true
Node
- Used to set the "from" point on the search. If a node is passed, it'll start the search from there and search its children only.
Generates a JSON representation of the tree.
Creates the new node.
Return the ID of the node
Return the parent ID of the node. Can be null if it's a root node or if it's not part of a tree object yet.
Data object of the node. Can be used to store anything such as state.
Node's children.
The tree itself. This can be a quick way to access the methods on the tree object.
Return the direct parent of a given Node.
Same as the add
method on the Tree but this time, you are adding a new node as a child of the current node.
Same as the find
method on the Tree but without the Node
argument.
Changes the data
object of the current Node. This method overrides the current data
object so make sure you use something like Object.assign
if you don't want to lose any data.
This method can be used if you want to move the current node somewhere else in the tree. criteria
works exactly the same as find
but this time, you want to use this to find the target
node - so the node that you want your current
node to be a child of. If you wanted to move it to the root
of the tree, you can pass root
as the criteria
(Note: this is not possible on the find
method).
By default, the moved node is added at the bottom of the children but if you wanted to target a specific index in the children's array, you can pass it as the second argument.
This can be used like this:
const rootNode = tree.add({ greeting: "hello" });
const childNode = rootNode.add({ greeting: "hi" });
childNode.move("root");
Can be used to remove the node completely.
const rootNode = tree.add({ greeting: "hello" });
rootNode.remove();
Generates a JSON representation of the node and it's children.