feat(collections): add new collection APIs, iterator support, and tree serialization utilities
This commit is contained in:
@@ -75,15 +75,15 @@ export class Tree<T> {
|
||||
}
|
||||
|
||||
nextSiblingsIterator(objectArg: T) {
|
||||
return this.symbolTree.nextSiblingsIterator();
|
||||
return this.symbolTree.nextSiblingsIterator(objectArg);
|
||||
}
|
||||
|
||||
ancestorsIterator(objectArg: T) {
|
||||
this.symbolTree.ancestorsIterator();
|
||||
ancestorsIterator(objectArg: T): Iterable<T> {
|
||||
return this.symbolTree.ancestorsIterator(objectArg);
|
||||
}
|
||||
|
||||
treeIterator(rootArg: T, optionsArg: any): Iterable<T> {
|
||||
return this.symbolTree.treeIterator(rootArg);
|
||||
treeIterator(rootArg: T, optionsArg?: any): Iterable<T> {
|
||||
return this.symbolTree.treeIterator(rootArg, optionsArg);
|
||||
}
|
||||
|
||||
index(childArg: T): number {
|
||||
@@ -119,23 +119,48 @@ export class Tree<T> {
|
||||
}
|
||||
|
||||
// ===========================================
|
||||
// Functionionality that extends symbol-tree
|
||||
// Functionality that extends symbol-tree
|
||||
// ===========================================
|
||||
|
||||
/**
|
||||
* returns a branch of the tree as JSON
|
||||
* can be user
|
||||
* returns a branch of the tree as a recursive JSON structure
|
||||
*/
|
||||
toJsonWithHierachy(rootElement: T) {
|
||||
const treeIterable = this.treeIterator(rootElement, {});
|
||||
for (const treeItem of treeIterable) {
|
||||
console.log(treeItem);
|
||||
}
|
||||
toJsonWithHierachy(rootElement: T): ITreeNode<T> {
|
||||
const buildNode = (element: T): ITreeNode<T> => {
|
||||
const children: ITreeNode<T>[] = [];
|
||||
if (this.hasChildren(element)) {
|
||||
const childrenArray = this.childrenToArray(element, {});
|
||||
for (const child of childrenArray) {
|
||||
children.push(buildNode(child));
|
||||
}
|
||||
}
|
||||
return { data: element, children };
|
||||
};
|
||||
return buildNode(rootElement);
|
||||
}
|
||||
|
||||
/**
|
||||
* builds a tree from a JSON with hierachy
|
||||
* @param rootElement
|
||||
* builds a tree from a recursive JSON structure
|
||||
* @param jsonRoot the root node in ITreeNode format
|
||||
* @param reviver optional function to reconstruct T from serialized data
|
||||
*/
|
||||
fromJsonWithHierachy(rootElement: T) {}
|
||||
fromJsonWithHierachy(jsonRoot: ITreeNode<T>, reviver?: (data: any) => T): T {
|
||||
const buildTree = (node: ITreeNode<T>, parentElement?: T): T => {
|
||||
const element = reviver ? reviver(node.data) : node.data;
|
||||
this.initialize(element);
|
||||
if (parentElement) {
|
||||
this.appendChild(parentElement, element);
|
||||
}
|
||||
for (const childNode of node.children) {
|
||||
buildTree(childNode, element);
|
||||
}
|
||||
return element;
|
||||
};
|
||||
return buildTree(jsonRoot);
|
||||
}
|
||||
}
|
||||
|
||||
export interface ITreeNode<T> {
|
||||
data: T;
|
||||
children: ITreeNode<T>[];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user